# I need help with 2D collision response (of stacking rotating polygons, with friction and gravity, for a game)

Hi I am looking for suggestions on how to write a collision response for game programming purpose (so not a scientific simulation). I am dealing with 2D polygons that are rotating, and I want them to be able to stack. I also want friction and gravity. I have a detection mechanism that returns the separating axis, how long the polygons are overlapping, and up to 2 points of contact. For the response, I am currently using an impulse-based response, which main idea is:

1. find the separating axis, length of overlap, and the point of contact (if there are two, pick a random point between to simulate averaged force. i believe there are better ways than this)
2. separate the object (modifying their positions, taking into account of their masses. i do not separate them completely though, to keep track that they are colliding to reduce jitter)
3. calculate normal force based on the coefficient of restitution as if there is no friction.
4. calculate friction, as if there is no normal force. I also assume that the direction of the friction is the same throughout the collision.
5. apply the two forces (which result in a rather inaccurate result, since each force is calculated as if the other is not present. for non-rotating bodies though, this method is exact)

I am aware that this method requires the coefficient of friction to be sufficiently small due to the assumption that the direction of friction stays the same in a collision.

Also, the result is visually satisfying if gravity is not present. However, when there is gravity, objects on ground jitter and drift (even with zero coefficient of restitution)! It also happens for stacking objects. Larger coefficient of restitution and gravity increase the jittering.

I hope you can help me with this. Some things i would like to know more about is how to handle collision with two point of contacts (how to end up having an object sitting still on the ground?), how to reduce, and prevent if possible, jitter and drift (do people use the most accurate method possible, or is there a trick to overcome this?), and how to handle multiple objects collision (for example, in the case of stacking objects, how do I check collisions between all of them and keep them all stable at every frame so they don't jitter?). A total reformulation of my algorithm is also welcomed, as long as it works. Another thing to note is that I am not making a Physics game, so I only need a visually satisfying response (though a realistic response is preferable, if it is not performance-heavy). But surely jittering and drifting objects on flat ground are not at all acceptable. In addition, I am a Physics student, so feel free to talk about impulse and whatever needed.

Finally, I'm sorry for the long post. I tried to be as concise as I can. Thank you for reading it!

EDIT

It seems what I didn't manage to come up all this time is to separate resting contact as a class of its own and how to solve them. Currently reading the paper suggested by Jedediah. More suggestions on the topic are welcome :)

CASE CLOSED

After reading various papers referenced in the paper, successfully implemented simultaneous impulse method (referring to the original paper by Erin Catto, [Catt05]). Thanks maaaan!! The paper is wonderful. The current system is visibly much better than the previous.

Still haven't separated resting contact as a class of its own though, which brings me to my next question. Love you all! Haha (sorry, I'm just so happy thanks to you).

-

Google "resting contact" -- physics engines typically handle it as an explicit state, rather than allowing objects to collide continuously, which causes jitter. Check many sources. There are a variety of approaches and none of them are easy, unfortunately.

This paper serves as a good overview of all aspects of physics simulation, including resting contact.

-
On chapter 3 and seems it is just the right reference I need! Gonna read through it as soon as possible. Thanks so much! – Register Sole Apr 24 '12 at 20:37

Take a look at SAT. Separating axis theorem. Also this is nice website http://www.metanetsoftware.com/technique/tutorialA.html Doesn't seem to explain everything that you have asked, but a good part of it.

-
Thanks for the info! But I already used SAT for my collision detection. In fact, I learned much from this website for my detection. The problem is that the website, which deals with non-rotating bodies, does not talk about jittering objects. This is probably because non-rotating bodies will not jitter a lot. In my case, when rotation is disabled, jitters are only visible when objects are stacked (which the website also does not deal with), even though very very large gravity is applied. – Register Sole Dec 26 '11 at 10:15