DEV_LOGS / PHYSICS_ENGINE_V0.4
C# UNITY

Collision Resolution:
Impulse Method

By Aryan S.Oct 24, 20254 min read
A B

FIG 1.0: IMPULSE VECTORS

Earlier versions of my physics engine used a penalty-based method for collisions. While simple to implement, it resulted in "spongy" interactions. To fix this, I refactored the solver to use Impulse Resolution.

The Math

The magnitude of the impulse scalar $j$ is calculated using the coefficient of restitution $e$ (bounciness) and the relative velocity along the normal.

j = -(1 + e) ċ (vrel ċ n) mA-1 + mB-1

Implementation

Here is the simplified C# implementation used in the solver loop.

PhysicsSolver.cs
public void ResolveCollision(Body a, Body b, Contact contact) {
    // 1. Calculate relative velocity
    Vector2 relativeVel = b.Velocity - a.Velocity;

    // 2. Calculate velocity along normal
    float velAlongNormal = Vector2.Dot(relativeVel, contact.Normal);

    if (velAlongNormal > 0) return;

    // 3. Calculate impulse scalar
    float j = -(1 + e) * velAlongNormal;
    j /= a.InvMass + b.InvMass;

    // 4. Apply impulse
    Vector2 impulse = j * contact.Normal;
    a.Velocity -= a.InvMass * impulse;
}