for( all contacts in contact list ){ if( contact.penetration > 0 ) { if( GetClosingVelocity( contact.pBody1, contact.pBody2 ) > 0 ) { ResolveContact( contact ); } }}
But I cant seem to get the GetClosingVelocity() function to work for all situations, and often objects get stuck in one another, I believe due to collisions being resolved and then reversed indefinatly. The function should return a positive value if the objects are closing in on each other, and a negative value when they're separting, both in relation to the contact normal. Where the normal is always given in relation to body A.
Situation 1
-----------
Involves 2 bodies that are deeply penetrating each other, where A is going left and B is going right. These are separating as one is going left and one right, and the contact normal is relative to A, so would be ( 1, 0 )
VelocityA = ( -10, 0 );
VelocityB = ( 11, 0 );
ClosingVelocity = Dot( VelocityA - VelocityB, ContactNormal );
= Dot( ( -21, 0 ), ( 1, 0 ) );
= ( -21 )
So this produces the correct result of them not closing in on each other as the result is negative, and so no collision needs to be resloved
Situation 2
-----------
Involves 2 bodies that are deeply penetrating each other, where A is going up and B is going down. These are separating as one is going up and one down, and the contact normal is relative to A, so would be ( 0, -1 )
VelocityA = ( 0, 10 );
VelocityB = ( 0, -11 );
ClosingVelocity = Dot( VelocityA - VelocityB, ContactNormal );
= Dot( ( 0, -21 ), ( 0, -1 ) );
= ( 21 )
So this produces the incorrect result of them closing in on each other as the result is positive, when they're actually separating, and so a collision will to be resloved that shouldnt. and the impulse will flip flop each frame
[/source]