问题描述
假设有3个球同时碰撞。我发现解决冲突的顺序会影响最终结果,这当然是没有道理的。
为解释和保持简单,请考虑3个球1D,所有相同质量,弹性碰撞。顶部的数字是速度,箭头是方向。假设它们当前正在彼此接触,即在碰撞中
-> 2-> 1<- -3
OOO
ABC
这表明球A从
现在,如果我们首先解决与B的碰撞A,然后解决与C的碰撞B,但使用新的速度B,如果我们改为解决B与C的冲突,然后解决A与B的冲突(使用B的新速度),则应该得到相同的结果。
第一种情况:A与B一起,然后是B与C
A与B给出
-> 1-> 2
OO
AB
并且B与C一起给出(但使用上述B的新速度2,而不是原始速度1)
<-3-> 2
OO
BC
因此最终结果为
-> 1<- -3 ----> 2
OOO
ABC
第二种情况:B与C,然后是A与B
B与C给出
<-3 ---> 1
OO
BC
A与B(但使用B中3的新速度,而不是原始1)
<-3 -> 2
OO
AB
因此最终结果为
<-3-> 2 ----> 1
OOO
ABC
您可以看到最终状态有所不同。
我在做什么错?更重要的是,处理此问题的正确方法是什么?
对于模拟许多球以及与墙壁碰撞的情况,这种情况很有可能。
(例如,球撞到墙壁并同时被另一个球击中,将产生与上述相同的问题,顺序将产生不同的结果)。
发生任何错误,但您的碰撞响应尚未完成。
在第一个A B中仍在碰撞,在第二个B和C中仍在碰撞。
因此,您应该解决这些冲突。
如果发生冲突,您将得到相同的答案 A<-3,B-> 1,C-> 2
尽管在模拟中您可以同时碰撞对象,但实际上它们永远不会碰撞。两次碰撞之间总是会有一点时间(可能很少)。
因此,为简单起见,物理引擎成对解决了碰撞。但是他们应该这样做,直到所有对都分开为止。这需要多次迭代。如果您尝试模拟诸如配置之类的堆积体,则迭代次数可能会非常高。
您可以检查
有关减少迭代的更多信息。
Suppose there are 3 balls colliding at the same time. I find that the order in which I resolve collisions makes a difference in the final result, which ofcourse makes no sense.
To explain and keep things simple, consider 3 balls in 1D, all same mass, elastic collision. The numbers at the top are the speeds and the arrows is the direction. Assume they are currently all touching each others, i.e. in collision
-->2 -->1 <---3
O O O
A B C
This shows ball A hitting ball B from the back and ball B and C are colliding face on.
Now if we resolve collision A with B first, followed by resolving collision B with C, but using the new speed of B, this should give the same result if we instead have resolved collision of B with C, followed by resolving A with B (using the new speed of B).
But it does not.
first case: A with B, followed by B with C
A with B gives
-->1 -->2
O O
A B
and B with C gives (but using new B speed of 2 above, not the original speed of 1)
<--3 -->2
O O
B C
Hence the final result is
-->1 <--3 ---->2
O O O
A B C
second case: B with C, followed by A with B
B with C gives
<--3 --->1
O O
B C
A with B (but using new speed of B of 3 above, not original 1)
<--3 -->2
O O
A B
Hence final result is
<--3 -->2 ---->1
O O O
A B C
You can see the final state is different.
What Am I doing wrong? and more importantly, what is the correct method to handle this?
For simulation with many balls and also collision with walls, this case is very possible.(for example, ball hitting a wall and being hit by another ball at the same time, would give same problem as above, the order gives different results).
Currently I use a loop to iterate over all objects and resolve collisions between each 2 at a time. Hence the order I use is arbitrary (order is just the index of the ball in an array).
You are not doing anything wrong but your collision response is not yet finished.In the first one A B are still colliding, and in the second one B and C are still colliding.So you should resolve those collisions.
And if you collide you will get the same answer A <--3 , B-->1, C-->2
Although in your simulation you objects can collide at the same time, in reality they will never. There will be always a little time(may be very very little) between collisions.So for simplicity physics engines resolve collision by pairs. But they should do it until all pairs are seperated. And this requires more than one iterations. And the numbers of iterations may become very high if you are trying to simulate stacking bodies like configuration.
you can check
how to sort objects for Guendelman shock propagation?
for further information to reduce iterations.
这篇关于如何处理同时发生的碰撞,以便处理顺序无关紧要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!