Collision detection in ruby


Posted by: Mhmd on 08/21/2015 | Add Revision

I'm making a simple pong game in ruby.

I did the collision detection this way:

def collides_with(player)
      ((@x + @image.width >= player.x) && (@x <= player.x + player.image.width)) && ((@y + @image.height >= player.y) && (@y <= player.y + player.image.height))

If this is true I multiply the ball's y coordinate by -1 (to change its direction):

@ball.vy *= -1

This is working as expected, except in one case when the ball reaches the left (or right) side of the paddle while it's moving. this will cause the ball to move into the inside of the paddle,which seems strange.

What should I do to fix this?

Visit Website