物理:确定碰撞后 2 个球体的速度
我正在使用 游戏程序员的物理,用于开发一个简单的基于物理的游戏。
我需要计算弹性碰撞后两个球体的最终速度。本书第 6 章中的示例假设第二个球体是静止的,因此一些方程被简化为 0。我需要在两个物体都运动时进行数学计算。
我尝试将书中的示例转换为代码,并弄清楚第二个球体的作用线和正常的 V2p 和 V2n 会发生什么。我的代码可以工作,但有时速度会突然加快并失控。显然我的数学有问题。
这是我正在使用的。代码是用Java编写的,“s1”和“s2”是球体。
double e = 1d;
// distance of sphere centers
double dX = s2.getCenterX() - s1.getCenterX();
double dY = s2.getCenterY() - s1.getCenterY();
double tangent = dY / dX;
double angle = Math.atan(tangent);
// v1 line of action
double v1p = s1.getVelocityX() * Math.cos(angle) + s1.getVelocityY() * Math.sin(angle);
// v1 normal
double v1n = -s1.getVelocityX() * Math.sin(angle) + s1.getVelocityY() * Math.cos(angle);
// v2 line of action
double v2p = s2.getVelocityX() * Math.cos(angle) + s2.getVelocityY() * Math.sin(angle);
// v2 normal
double v2n = -s2.getVelocityX() * Math.sin(angle) + s2.getVelocityY() * Math.cos(angle);
double v1massScale = (s1.getMass() - (e * s2.getMass())) / (s1.getMass() + s2.getMass());
double v2massScale = ((1 + e) * s1.getMass()) / (s1.getMass() + s2.getMass());
// compute post-collision velocities
double v1pPrime = v1massScale * v1p + v2massScale * v2p;
double v2pPrime = v2massScale * v1p + v1massScale * v2p;
// rotate back to normal
double v1xPrime = v1pPrime * Math.cos(angle) - v1n * Math.sin(angle);
double v1yPrime = v1pPrime * Math.sin(angle) + v1n * Math.cos(angle);
double v2xPrime = v2pPrime * Math.cos(angle) - v2n * Math.sin(angle);
double v2yPrime = v2pPrime * Math.sin(angle) + v2n * Math.cos(angle);
I'm using Physics for Games Programmers to develop a simple physics-based game.
I need to compute the resulting velocities for two spheres after an elastic collision. The book example for this in Chapter 6 assumes that the 2nd sphere is stationary, and so some of the equations are simplified to 0. I need the math to work when both bodies are in motion.
I've tried to convert the book's example to code, and puzzle out what should happen for the second sphere's line of action and normal- V2p and V2n. My code sort of works, but occasionally the velocities suddenly speed up and bounce out of control. Clearly there's something wrong with my math.
Here's what I'm using. The code is in Java, "s1" and "s2" are the spheres.
double e = 1d;
// distance of sphere centers
double dX = s2.getCenterX() - s1.getCenterX();
double dY = s2.getCenterY() - s1.getCenterY();
double tangent = dY / dX;
double angle = Math.atan(tangent);
// v1 line of action
double v1p = s1.getVelocityX() * Math.cos(angle) + s1.getVelocityY() * Math.sin(angle);
// v1 normal
double v1n = -s1.getVelocityX() * Math.sin(angle) + s1.getVelocityY() * Math.cos(angle);
// v2 line of action
double v2p = s2.getVelocityX() * Math.cos(angle) + s2.getVelocityY() * Math.sin(angle);
// v2 normal
double v2n = -s2.getVelocityX() * Math.sin(angle) + s2.getVelocityY() * Math.cos(angle);
double v1massScale = (s1.getMass() - (e * s2.getMass())) / (s1.getMass() + s2.getMass());
double v2massScale = ((1 + e) * s1.getMass()) / (s1.getMass() + s2.getMass());
// compute post-collision velocities
double v1pPrime = v1massScale * v1p + v2massScale * v2p;
double v2pPrime = v2massScale * v1p + v1massScale * v2p;
// rotate back to normal
double v1xPrime = v1pPrime * Math.cos(angle) - v1n * Math.sin(angle);
double v1yPrime = v1pPrime * Math.sin(angle) + v1n * Math.cos(angle);
double v2xPrime = v2pPrime * Math.cos(angle) - v2n * Math.sin(angle);
double v2yPrime = v2pPrime * Math.sin(angle) + v2n * Math.cos(angle);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可能是
Math.atan(y/x)
。您需要处理一些特殊情况,通常需要使用 Math.atan2(y,x) 来代替。请参阅 Math.atan() 和 Math.atan2() 文档。
It might be the
Math.atan(y/x)
. You need to handle some special cases, and you usually want to useMath.atan2(y,x)
instead.See the Math.atan() and Math.atan2() docs.
如果您不太了解物理或数学,那么这是一个非常困难的问题。
您需要了解牛顿运动定律。这些是微分方程组,因此您需要知道如何求解它们。
如果您假设某些因素对于您感兴趣的行为来说并不重要,那么问题会更容易:刚性球体与可变形球体、摩擦力和其他因素。
答案很大程度上取决于您想做什么。
快速浏览一下您发布的代码表明您所能期望的最好的结果就是您有一个正确但不是最佳的实现。您可能无法充分理解物理和数学,无法自行解决该问题。
我会用你选择的语言谷歌搜索物理引擎,并使用其他人已经编码的理想化。
It's a surprisingly difficult problem if you don't know much about physics or math.
You need to know about Newton's laws of motion. These are couple differential equations, so you'll need to know how to solve those.
The problem is easier if you assume certain things to be unimportant for the behavior you're interested in: rigid versus deformable spheres, friction, and other factors.
The answer depends a lot on what you'd like to do.
A quick glance at the code you posted suggests that the best you can hope for is that you have a correct but less than optimal implementation. It's likely that you fail to understand the physics and mathematics sufficiently well to solve it on your own.
I'd Google for a physics engine in the language of your choice and use an idealization that someone else has already coded.