开发者

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]
开发者_C百科

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!


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


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


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.


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

exit_angle = 180 - impact_angle.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜