2个移动物体的碰撞算法
这是一个 2d 游戏的数学问题。
Given 2 object ( 2 car, 2 tank, 2...), for each object i know:
1. X, Y
2. Actual Speed
3. Degree of movement (or radiant)
如何计算 2 个物体碰撞的“效果”
Ps:
我用这个简单的公式“移动”物体,每次“游戏循环”的时间点:(
ychange = Math.Sin(radiant) * ((carspeed / xVar));
xchange = Math.Cos(radiant) * ((carspeed / xVar));
其中 xVar 是一个“乘法器”,可以使我的车速度更慢 对于碰撞的“第一个”对象
,如果我在该公式前面添加“减号”(-),我可以模拟第一个对象的良好“弹跳”..但不能模拟第二个对象。
所以我的问题是关于计算第二个对象的良好(不完美!)且更现实的“影响效果”的方法。
感谢您的帮助
this is a Math problem for a 2d game.
Given 2 object ( 2 car, 2 tank, 2...), for each object i know:
1. X, Y
2. Actual Speed
3. Degree of movement (or radiant)
How to calculate the "effect" of a collision for the 2 object
Ps:
I "move" the object with this simple formula each tick of my "game loop":
ychange = Math.Sin(radiant) * ((carspeed / xVar));
xchange = Math.Cos(radiant) * ((carspeed / xVar));
(where xVar is a "multiplicator" to make more slow my car on the screen)
And for the "first" object which collide, if i add a "minus" (-) in front of that formula, i can simulate a good "bounce" of the first object.. but not for the second.
So my question regard a way to calculate a good (not perfect ! ) and more realistic "impact effect" for the second object.
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
几个月前,我实现了一款斯诺克游戏。我使用以下代码使两个球发生碰撞。请注意,它们不是边界框。
该方法根据每个物体的质量、速度和角度计算结果速度和角度。
它对我的游戏来说就像一种魅力。
这段代码是用纯 C 语言编写的。
我存储运动数据的方式和你的一样。变量b1和b2是球。
我相信代码本身并不难理解,但其物理原理却并非微不足道。
希望有帮助!
Few months ago I implemented a Snooker game. I used the following code to make two balls collide. Note that they are not bounding boxes.
This method computes the resulting speed and angles based on the mass, speed and angle of each objects.
It worked like a charm at my game.
This code was written in pure C.
The way I store the movement data is just the same as yours. The variables b1 and b2 are balls.
I believe the code itself isn't hard to understand, but the physic isn't trivial.
Hope it helps!
最简单的方法是让它们交换速度。如果它们相同,这是有道理的。
如果您想做更多,您可以为参数选择一个随机值,该值将确定它们是否有掠过碰撞(几乎没有接触)或完全碰撞,或介于两者之间。您还可以模拟两个物体的不同质量(当摩托车撞上货运卡车时,卡车不会发生太大变化)。最后,您可以进行仅部分弹性的碰撞;也就是说,它们不会像橡皮球那样弹跳,而是会嘎吱作响并损失一些能量。这些需要一些数学知识和对物理学的理解。
The simplest way is to have them swap velocities. This makes sense if they are identical.
If you want to do more, you can choose a random value for a parameter that will determine whether they have a glancing collision (barely touching) or a full impact, or something in between. You can also simulate different masses of the two objects (when a motorcycle hits a freight truck, not much happens to the truck). Finally, you can have a collision that is only partly elastic; that is, they do not bounce like rubber balls, they crunch and lose some energy. These will require some math, and an understanding of the physics.
以下是物理学的解释:
您有四个未知数:碰撞后每个球的 x 和 y 速度。
所以你需要四个方程。
x 和 y 动量守恒给出 2。妈妈=mv
动能守恒给出了三分之一; Ke = 1/2 * mv^2(之前和之后)
此外,相对论原理可以用来假设一个球最初是静止的,比如 uB.x = uB.y = 0
并且脉冲必须沿着击球时每个球中心之间的连接线。因此,如果您假设球 B 最初处于静止状态,那么现在它将沿着该线的方向移动,因此您可以表示 vB.y = f + g * vB.x,从而将未知数减少到 3。
三个未知数,三个方程。耶!
如果碰撞不是弹性的,那么这取决于您想要模拟的精确程度。像 Ke_after = 0.99 * Ke_before 这样的东西,然后将其放入数学中。
我将其作为练习留给读者推导完整的方程。
Here is an explanation of the physics:
You have four unknows: The x and y velocities of each ball after the collision.
So you need four equations.
Conservation of momentum in x and y gives two. Mom = mv
Conservation of kinetic energy gives a third; Ke = 1/2 * mv^2 (before & after)
Further to this, the principle of relativity can be used to assume one ball is initially stationary, say uB.x = uB.y = 0
And the impulse must happen along the line that joins between the centres of each ball, at the time of impact. So if you assumed ball B was initially at rest, now it will be moving in the direction of that line, so you can express vB.y = f + g * vB.x, thus reducing the number of unknowns to 3.
Three unknowns, three equations. Yay!
If the collision is not elastic, then it depends how accurately you want to simulate. Something like Ke_after = 0.99 * Ke_before, and then throw that into the math.
I leave it as an exercise to the reader to derive the full equations.
嗯……我已经很久没有做过游戏开发碰撞了,所以请不要轻易接受我的建议。您是否尝试过“翻转”运动程度或简单地将一个 Pi 添加到
radiant
?Hmmm...it's been a long time since I've done game development collisions so take my advice lightly. Have you tried "flipping" the degree of movement or simply adding one Pi to
radiant
?