I'm currently programming a 2D rigid body dynamics engine. While I have realistic collision detection and response between polygons, I have trouble in the simulation once I implement gravity. When these rigid bodies are influenced by gravity, they never fall to complete rest (e.g. they always retain some kinetic energy and keep bouncing around). Additionally, when several bodies are on top of each other, my separating algorithm fails and the bodies interpenetrate a little. I need to figure out a way to implement resting contact forces. Any advice would be appreciated.