Calculating the resultant velocity vector after a 2D collision

733 views Asked by At

Currently I have a mini physics game which uses Separating Axis Theorem for collision detecting and response, however I came to a standstill when I discovered that there wasn't much documentation on what happens to an object's velocity after it collides with another shape using SAT collision detection.

Here are two diagrams of what I'm talking about.

enter image description here

enter image description here

Can anyone point me into a right direction please?

All information that can be given after the collision is the minimum penetration vector.

EDIT: Found some code online that's very related to this but I don't understand it:

void CBody::ProcessCollision(CBody& xBody, const Vector& N, float t)
{
    Vector D = m_xDisplacement - xBody.m_xDisplacement;

    float n  = D * N;

    Vector Dn = N * n;
    Vector Dt = D - Dn;

    if (n > 0.0f) Dn = Vector(0, 0);

    float dt  = Dt * Dt;
    float CoF = s_fFriction;

    if (dt < s_fGlue*s_fGlue) CoF = 1.01f;

    D = -(1.0f + s_fRestitution) * Dn - (CoF) * Dt;

    float m0 =       GetInvMass();
    float m1 = xBody.GetInvMass();
    float m  = m0 + m1;
    float r0 = m0 / m;
    float r1 = m1 / m;

          m_xDisplacement += D * r0;
    xBody.m_xDisplacement -= D * r1;
}
1

There are 1 answers

2
Roland Smith On

Collision detection and how to handle collisions are two separate things.

What happens after the collision depends on what kind of physics you're trying to achieve.

E.g. in your top picture let's assume that the red piece is an unmovable wall and only the blue piece is moving. The movement vector of the blue piece is already split up in orthogonal parts parallel and perpendicular to the surface of the red wall. A simple way to handle collisions in this case would be to simply flip the component of the velocity vector perpendicular to the wall. So the vector [-4, 10] would become [4,10].