Ball bounce off the surface

I am currently in the middle of writing a game like Breakout, and I was wondering how I can properly bounce the ball off the surface.

I went in a naive way to rotate speed 90 degrees, which was:

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

Which (unsurprisingly) doesn't work so well. If I know the position and brightness of the ball, as well as the point where the ball hits (but bounces instead), how can I bounce off this point?

Limitations:

  • I use integer math (no FP anywhere)
  • All my surfaces are simple flat surfaces (vertical, horizontal or block)
  • I only want to bounce at an angle of 90 degrees.
  • All collisions are purely elastic (this is a breakthrough - there is no need for friction, etc.)

I do not need any language specific code. If anyone could give a little mathematical formula on how to do it right, that would work well for me.

Thanks!

+4
source share
7 answers

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

For horizontal / vertical surfaces, the normal is easy to calculate. For more complex surfaces, this may depend on the equation of the surface, etc.

In addition, this suggests that the energy of the ball does not change. If you take into account friction / heat loss / rotation of the ball, etc., this can get complicated.

+4
source

Assuming that you will only bounce off vertical or horizontal surfaces, you can simply negate the speed 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].

+5
source

I would try [vx, vy] → [vx, -vy] on horizontal walls and [vx, vy] → [-vx, vy] on vertical walls.

+5
source

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

exit_angle = 180 - impact_angle.

+3
source

Assuming that no energy is lost in the collision, a ball moving at a speed of (vx, vy) will move at a speed of (-vx, vy) after bouncing off a vertical surface and (vx, -vy) after bouncing off a horizontal surface.

In the general case (bouncing off a plane with an arbitrary normal vector, still not accepting any energy loss), see this article in the Wikipedia section in the "Calculation" section: http://en.wikipedia.org/wiki/Specular_reflection

+3
source

You need to know the surface as well as the speed of the ball. For example, bouncing off a line parallel to the x axis [vx, vy] will become [vx, -vy]. If the line is parallel to the y axis, then [vx, vy] will become [-vx, vy]. This is more difficult if the line is not parallel to any of the axes, but you are looking for a simple reflection of the velocity along the direction of the surface ((1, 0) and (0, 1) for the x, y axes).

+1
source

90-degree reflections from rectangles aligned on the axis is a question of changing the sign of the speed X / Y. In addition, it requires a point product and a little vector bending, but this math is still very safe - it can easily be performed as a fixed point, if necessary.

0
source

Source: https://habr.com/ru/post/1309921/


All Articles