Suppose there are three balls colliding at the same time. I find that the order in which I resolve the collisions matters in the final result, which of course does not make sense.
To explain and keep things simple, consider 3 balls in 1D, all the same masses, elastic collision. The numbers at the top are the speeds, and the arrows are the direction. Suppose now they all touch each other, i.e. in a collision
-->2 -->1 <---3 OOO ABC
This shows ball A hitting ball B from behind, and ball B and C collide face to face.
Now, if we first allow the collision of A with B, and then allow the collision of B with C, but using the new speed B, this should give the same result if instead we allowed the collision of B with C, followed by the resolution of A with B ( using the new speed B).
But this is not so.
first case: A with B followed by B with C
A with B gives
-->1 -->2 OOAB
and B with C gives (but using the new speed B 2 is higher, not the initial speed 1)
<--3 -->2 OOBC
Therefore, the final result
-->1 <--3 ---->2 OOO ABC
second case: B with C followed by A with B
B with C gives
<--3 --->1 OOBC
A with B (but using the new speed B of 3 higher than the original 1)
<--3 -->2 OOAB
Therefore, the final result
<--3 -->2 ---->1 OOO ABC
You can see that the final state is different.
What am I doing wrong? and more importantly, what is the right method for this?
For simulations with a large number of balls, as well as collisions with walls, this case is very possible. (for example, a ball hitting a wall and simultaneously being hit by another ball will give the same problem as above, the order gives different results).
I am currently using a loop to iterate over all objects and resolve conflicts between every 2 at a time. Therefore, the order I use is arbitrary (the order is just the index of the ball in the array).