Search code examples
collisiongame-physics

Bouncing a ball off a surface


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!


Solution

  • 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.