从表面弹起球 [英] Bouncing a ball off a surface

查看:75
本文介绍了从表面弹起球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写Breakout之类的游戏,我想知道如何才能正确地将球从表面弹起。

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.

天真地将速度旋转90度的方法是:

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?

约束:


  • 我正在使用整数数学(任何地方都没有FP

  • 我所有的表面都是简单的
    平坦表面(垂直,
    水平或块状)

  • 我只想以90度角弹起

  • 所有碰撞都是纯弹性的(这是突破-无需摩擦等)

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

谢谢!

推荐答案

您需要在以下位置计算法向矢量联络点。沿法线的速度分量将切换方向,而垂直于法线的速度分量将保持不变。

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.

这篇关于从表面弹起球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆