Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Collision Detection: Sprites getting "stuck"
- - By wanderinweezard Date 2013-10-10 17:34
Hi everyone,

I've tried working through this on my own - first by reading Gosu's examples (CptnRuby and Tutorial), second by looking up articles on Gamedev.net and third by searching the Gosu forum.  I like to look at examples, but implement the concept on my own without copy-pasting the examples.

I'm stuck on implementing a bounding-box collision example.  I've just got a very simple example - I've drawn a sprite in the middle of a window (immovable) and drawn another sprite (the player) that is moveable.  Following the example here (Gamedev.net) - I think I've written it correctly for Ruby.

What happens is I move the "player" sprite and once I make contact with the non-moving sprite, the player sprite will get stuck.  I thought I had my code checking for the collision before actually doing the move, but I'm obviously wrong.

So a couple questions:
1) I have an image that I've drawn using draw_rot, when I ask that image what it's x, y, width, and height values are... is that from the center of the image?

2) Can you help me work through why my sprite is getting stuck?  I'm not understanding everything that is happening in my game loop obviously.

Any guidance would be muchly appreciated.

Here is my collision checking routine (defined in my game class):

def check_collisions(player, object)
    left1 = player.x
    left2 = object.x
    right1 = player.x + player.width
    right2 = object.x + object.width
    top1 = player.y
    top2 = object.y
    bottom1 = player.y + player.height
    bottom2 = object.y + object.height

    if bottom1 < top2
      false
      return
    end

    if top1 > bottom2
      false
      return
    end

    if right1 < left2
      false
      return
    end
    if left1 > right2
      false
      return
    end

    true
  end
end


And here is the update routine for the Game class:

  def update
    move_x = move_y = 0

    move_x += 3 if button_down? Gosu::KbRight
    move_x -= 3 if button_down? Gosu::KbLeft
    move_y += 3 if button_down? Gosu::KbDown
    move_y -= 3 if button_down? Gosu::KbUp

    if check_collisions(@player, @pot_image)
      move_x = move_y = 0     
    end
    @player.move(move_x, move_y)
  end


And finally a Google Drive link to a screenshot showing the problem and my full Ruby source:
Google Drive
Parent - - By SPK Date 2013-10-10 19:05 Edited 2013-10-10 19:11
Your code doesn't make any sense, because you always return nil.

Either remove the redundant return (well, not in your case) or move the true/false next to return.

Example:
  if top1 > bottom2
      false
      return
    end

becomes

  if top1 > bottom2
      return false
    end

and so on.
Parent - By wanderinweezard Date 2013-10-11 00:33
Thanks for your feedback.  My inexperience with Ruby is showing through.  I've made that tweaks like you suggested, but I still encounter the same result with my sprite getting 'stuck' once the collision happens.

Do you see anything else I am missing?


    if bottom1 < top2
      return false
    end

    if top1 > bottom2
      return false
    end

    if right1 < left2
      return false
    end
    if left1 > right2
      return false
    end

    return true
Parent - - By jlnr (dev) Date 2013-10-11 04:35
First, an image does not have an x or y; rather, your objects have x and y methods and you are drawing an image relative to that. Because of how draw_rot works, x and y are at the centre of the image of each object.

Your collision does not work because you do not check for collisions before moving - you do not pass move_x and move_y into check_collision, which are the new values, but instead check_collision uses the current position values.
Parent - By wanderinweezard Date 2013-10-11 09:18
Thank you for your help - it makes sense now what was happening.
Up Topic Gosu / Gosu Exchange / Collision Detection: Sprites getting "stuck"

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill