Basic Impulse Resolution (with restitution)
Basic Impulse Resolution (with restitution)¶
function resolveCollision(a, b, normal, restitution):
relativeVelocity = b.velocity - a.velocity
velAlongNormal = dot(relativeVelocity, normal)
if velAlongNormal > 0:
return // already separating, no response needed
invMassA = 1 / a.mass
invMassB = 1 / b.mass
j = -(1 + restitution) * velAlongNormal / (invMassA + invMassB)
impulse = scale(normal, j)
a.velocity -= scale(impulse, invMassA)
b.velocity += scale(impulse, invMassB)
Impulse resolution models collisions using conservation of momentum. The formula computes the impulse magnitude j needed so that, after applying it, the objects separate at a rate scaled by the restitution coefficient e:
- e = 1: perfectly elastic (bounces back with full energy, like a superball)
- e = 0: perfectly inelastic (objects stick together, no bounce)
The 1/mass terms (inverse mass) let heavier objects resist velocity changes more — a common trick is to treat static/immovable objects as having "infinite mass" (inverse mass of 0), so all the impulse goes into the movable object. The check velAlongNormal > 0 guards against double-resolving a collision where objects are already moving apart.