I have static objects and movable objects. The collisions are detected using the separating-axis-theorem.
For example, in this situation I have two static objects (in red):
and a movable object between the two:
My algorithm is able to compute the collision between two of these objects, and it also spits out a perfect resolution vector (meaning a minimum-displacement-vector) to the collision.
So for example, when I check the collision between the green rectangle and the right red rectangle, the algorithm spits out a vector that tells me how I need to move the green rectangle in order to resolve the collision:
Notice that I just quickly drew this in MSPaint, so in that picture it could actually be that the minimum-translation-vector pushes the green rectangle out on the top, but I'm going to assume here that pushing it out to the left/right is actually shorter.
The general way of approaching this would be to only resolve the collision of one collision per frame, instead of all at once. But in my case, this would result in flip-flopping:
First, the solver detects two collisions but only resolves the collision between the right rectangle and the green rectangle:
Then, in the next frame, it detects only one collision which is between the left red rectangle and the green rectangle, and resolves it:
As you can see, this doesn't actually resolve the collision (for example by pushing the green rectangle out to the top), and instead just flip flops between the two states infinitely.
How can I solve this?
Answer
Depending on exactly what you are trying to achieve (high physical accuracy or just a close-enough real-time simulation), you could try using speculative contacts.
Here are the details: http://www.wildbunny.co.uk/blog/2011/03/25/speculative-contacts-an-continuous-collision-engine-approach-part-1/
He describes in that article what you need to know to implement it, and it's very simple compared to other approaches (such as sphere casting and then sorting collision resolutions by time of impact).
If you need/want more, you can purchase his source code for (IIRC) $7.
Here is a video of my implementation in 3D: http://www.youtube.com/watch?v=JvT2H1RmOas
Notice how stable the simulation is with just a single iteration. You could easily use multiple iterations per frame to resolve multiple collisions to a stable state, which would be more accurate.
No comments:
Post a Comment