Java 弹跳球游戏 - 滚球场景
我正在用 Java 为 Android 手机编写一个弹跳球游戏。除了碰撞和恢复系数的小问题之外,一切似乎都工作正常。
当球与表面碰撞时,会计算该表面的法线矢量(nx 和 ny),并且球的矢量方向(dx 和 dy)反映在该法线矢量中。
目前我使用 0.9 作为恢复系数,并将 dx 和 dy 乘以该值,这显然太简单了,因为它不能正确模拟滚动。
是否有一个简单的公式可以更准确地计算新的 dx 和 dy,这样,如果球在碰撞时几乎平行于斜坡行进,那么它比垂直碰撞时损失的速度要少。
很抱歉没有发布我的任何代码,我离开了我的计算机,因此如果这有助于任何人的理解,可以稍后发布。
感谢您提前提供的任何帮助。
I am writing a bouncing ball game in Java for Android phones. Everything seems to work fine apart from a small problem with collisions and the coefficient of restitution.
When a ball collides with a surface, the vector normal of this surface is calculated (nx and ny), and the vector direction of the ball (dx and dy) is reflected in this normal vector.
At the moment I am then using 0.9 as the coefficient of restitution and so multiplying both dx and dy by this value, which is obviously far too simplistic, as it does not properly simulate rolling.
Is there a simple formula to calculate the new dx and dy more accurately, so that if the ball is travelling almost parallel to a slope when it collides it loses less speed than if it collides perpendicularly.
Apologies for not posting any of my code, I'm away from my computer so can post it later if this would help anyone's understanding.
Thanks for any help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恢复系数与碰撞物体的相对弹性有关。这是解释球和表面在撞击时都会变形这一事实的简单方法。一些变形能量会损失(例如产生的热量、声波等),但大部分会返回到“推”另一个物体。
如果恢复系数等于 1.0,则不会损失任何能量。将其视为“捏造因素”,这样您就不必解决动态弹性问题。
滚动是另一回事。
我认为恢复系数应该只应用于速度的法向分量。如果您想更接近真实的物理现象,则必须将摩擦和滑动应用于切向分量。
除了 x、y 方向的位移之外,您还需要另一个方程。您还需要另一个来计算围绕球质心的 z 轴扭矩。
牛顿运动方程将 x 和 y 方向上的力以及球绕 z 轴的力矩相加。
Coefficient of restitution has to do with the relative elasticity of the colliding objects. It's a simplisitic way to account for the fact that the ball and the surface both deform on impact. Some of the energy of deformation is lost (e.g. generated heat, sound waves, etc.), but most of it goes back into "pushing" the other body away.
None of the energy is lost if the coefficient of restitution equals 1.0. Think of it as a "fudge factor" so you don't have to do a dynamic elasticity problem.
Rolling is another matter.
I think the coefficient of restitution should only be applied to the normal component of the velocity. Friction and sliding would have to apply to the tangential component if you want to get closer to the real physics.
You'll also need another equation beyond just displacements in the x, y directions. You'll need another for the torque about the z-axis about the centroid of the ball.
Newton's equations of motion would sum forces in the x and y directions and the moments about the z-axis for the ball.
我可能是错的,但解决这个问题的方法是计算球的角度,即 artan(dy/dx)。
有了这个相对于原点的角度,你就可以乘以这个角度的余弦。如果完全平行,速度将保持不变,如果完全垂直,速度将为零。
您必须考虑到这一点,以确保您希望球弹跳的最小和最大速度。
希望有帮助!
I might be wrong but an solution to this would be to calculate the angle of the ball, meaning artan(dy/dx).
Having this angle in reference to your origin you could then multiply by the cosine of this angle. Were if it would be completely parallel the speed would remain unchanged and if was completely perpendicular the speed would be zero.
You would have to account for this to make sure there is a min and max speed you want the balls to bounce.
Hope it helps!
最好的选择可能是参考工程或物理教科书。我在 Google 上快速搜索了“恢复倾斜影响系数",它生成了这个 Scribd 文档,其中包含一些有用的信息:http://www.scribd.com/doc/28272448/46/Coefficient-of-restitution
Your best bet is probably to reference an engineering or physics textbook. I did a quick Google search for "coefficient of restitution oblique impact" and it yielded this Scribd document which has a bit of useful information: http://www.scribd.com/doc/28272448/46/Coefficient-of-restitution