Bouncing a ball off a surface

2020-04-10 00:47发布

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!

7条回答
对你真心纯属浪费
2楼-- · 2020-04-10 01:24

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.

查看更多
登录 后发表回答