# game physics: contact constraint and relative velocity

For two rigidbodies (2D boxes), A and B, I have been colliding A with B and finding the collision normal pointing towards A i.e. the direction that would separate A from B. When it comes to calculating the relative velocity for the contact constraint, does it matter which body has the edge that the collision normal is perpendicular in terms of which velocity to subtract from which?

If I understood the quesion, NO.

Here follows a simple resolvecollision , from How to Create a Custom 2D Physics Engine: The Basics and Impulse Resolution , as you can see you put yourself in object A frame of reference , then subtract collision impulse from A and add it to B.

void ResolveCollision( Object A, Object B )
{
// Calculate relative velocity
Vec2 rv = B.velocity - A.velocity

// Calculate relative velocity in terms of the normal direction
float velAlongNormal = DotProduct( rv, normal )

// Do not resolve if velocities are separating
if(velAlongNormal > 0)
return;

// Calculate restitution
float e = min( A.restitution, B.restitution)

// Calculate impulse scalar
float j = -(1 + e) * velAlongNormal
j /= 1 / A.mass + 1 / B.mass

// Apply impulse
Vec2 impulse = j * normal
A.velocity -= 1 / A.mass * impulse
B.velocity += 1 / B.mass * impulse
}


Be carefull not to re-execute resolvecollision(B,A) as you has already execuitetd resolvecollision(A,B), even if :

  // Do not resolve if velocities are separating
if(velAlongNormal > 0)
return;


may prevent this.