I'm developing a 3D physics engine (I know, I should use one of the many good existing physics libraries, however my collision detection is optimised for objects with highly ordered highly complex substructure which seems to be unusual and slow in the engines I've tried).
Collision detection is working really nicely, as are bouncing type behaviours but contact behaviours are not behaving nicely at all. Initially I had the problem that an object would spin up out of control if it had prolonged contact with a surface; I found that the reason for this was that an object was falling under gravity into collision, then getting a "free ride" upwards while it was pushed out of collision; giving the object free energy. If a significant portion of this energy made it into rotational degrees of freedom then considerable energy could be imparted before eventually the object was flung outwards. My solution to this was to impart an energy penalty when pushing out of collision equal to:
EnergyPenalty=mass * gravity dotted with the push-out-of-collision vector (i.e the E=m*g*h it has gained for free)
This improved matters considerably, but now objects "get stuck" on their corners just before the shape relaxes into its resting position; presumably because too much energy seems to be being taken away.
A loop round the engine works as following
-Collisions detected
-Pushed out of collision (energy penalty if pushed up against gravity)
-Collisional forces applied if object are still approaching (may not be the case if push energy penalty slowed to 0)
-Gravity applied
-Contact forces applied if object are still approaching (as collision but with elasticity = 0)
-Move and rotate objects
I've taken friction out of the equation for the time being
I’ve found that while in contact with the surface the contact forces correctly bring the two objects at their collision points to zero relative velocity (taking into account both translation and rotation) but that that balance of translational movement balancing rotation is instantaneous. I.e. over a complete physics step the rotational component changes to be no longer in perfect balance with the translational. So a very small push-out-of-collision is required even when in ‘contact mode’ with a very small energy penalty to boot. However this energy penalty seems too large; freezing the object in position.
Which brings me to my question; what is the correct way to deal with the “free energy” imparted by a push-out-of-collision in a collision resolution step? (Even if the answer is; what you're doing is the correct way to deal with it i'll know this is a debugging issue not an algorithmic issue)
(E=m*g*h)
from the object's kinetic energy? Although that might produce a passable damping visual effect, I don't think it is valid physics. Does your engine otherwise track potential energy at all? If not, I would rethink the science you are trying to apply to this problem. \$\endgroup\$