views:

295

answers:

7

Hi all, I'm currently in the middle of writing a game like Breakout, and I was wondering how I could properly bounce a ball off a surface.

I went with the naive way of rotating the velocity by 90 degrees, which was:

[vx, vy] -> [-vy, vx]

Which (unsurprisingly) didn't work so well. If I know the position and veocity of the ball, as well as the point the ball would hit (but is going to instead bounce off of) how can I bounce it off that point?

Constraints:

  • I'm using integer math (No FP anywhere)
  • All my surfaces are simple flat surfaces (Vertical, horizontal, or a block)
  • I only want to bounce off in a 90 degree angle
  • All collisions are purely elastic (This is breakout -- No need to friction, etc)

I don't need any language specific code. If anyone could provide a small, mathematical formula on how to properly do this that would work fine for me.

Thanks!

+5  A: 

Assuming you are only going to be bouncing off of either vertical or horizontal surfaces, you can just negate the velocity in the X or Y directions, respectively.

So, if you have [vx, vy], and it bounces off a vertical wall, you will have [-vx, vy].

If you have [vx, vy], and it bounces off a horizontal wall, you will have [vx, -vy].

retracile
+5  A: 

I'd try [vx, vy] -> [vx, -vy] on horizontal walls and [vx, vy] -> [-vx, vy] on vertical walls.

Kaniu
+2  A: 

You are reflecting the vector around a line perpendicular to the surface at the point of impact. in 2D:

exit_angle = 180 - impact_angle.

drawnonward
+1  A: 

You need to know the surface as well as the velocity of the ball. For instance bouncing off a line parallel to the x axis [vx, vy] would become [vx, -vy]. If the line is parallel to the y axis then [vx, vy] would become [-vx, vy]. It's more complicated if the line is not parallel to either axis, but you're looking for a simple reflection of velocity along the directionality of the surface ( (1, 0) and (0 , 1) for the x, y axes).

patros
+3  A: 

You need to compute the normal vector at the point of contact. The component of the velocity along the normal will switch direction while the component of velocity perpendicular to the normal will remain the same.

For horizontal/vertical surfaces the normal is easy to calculate. For more complicated surfaces, it might depend on the equation of the surface etc.

Also, this assumes that the energy of the ball does not change. If you take friction/heat loss/rotation of ball etc into account it might get complicated.

Moron
Thank you. This solved my problem. I was trying to created a unified bounce function that would work on anything, so I could call it on the point of contact for any surface (block, wall, paddle).
Sagekilla
A: 

90 degree reflections from axis aligned boxes is jst a matter of reversing X/Y velocity signs appropriately. Beyond that, it takes a dot product and a little vector twiddling, but that math is still very int safe - could easily be done as fixed point if need be.

Michael Dorgan
While this is true and precisely answers the OP's question, this is incorrect as far as what the OP actually wants. Picture a ball moving perpendicularly to the wall; you wouldn't want that ball to "bounce" and start moving parallel to the wall.
dash-tom-bang
+2  A: 

Assuming no energy is lost in the collision, a ball travelling with speed (vx, vy) will travel with speed (-vx, vy) after bouncing off a vertical surface and (vx, -vy) after bouncing off a horizontal surface.

For the general case (bouncing off a plane with an arbitrary normal vector, still assuming no energy losses though) see this wikipedia article under the Calculation section: http://en.wikipedia.org/wiki/Specular_reflection

Oskar Alexanderson