Skip to content
Physical AI
All experiments

Collision and contact: soft vs hard

Two ways a simulator can stop objects passing through each other: a stiff spring–damper that allows small penetration (penalty, soft contact), or an instantaneous velocity change plus position correction (impulse, hard contact). Also shows why stiff contact needs small timesteps.

Loading simulation…

What it demonstrates

Two ways a simulator can stop objects passing through each other: a stiff spring–damper that allows small penetration (penalty, soft contact), or an instantaneous velocity change plus position correction (impulse, hard contact). Also shows why stiff contact needs small timesteps.

How it works

Two frictionless discs move under gravity inside a box. Every step the simulator checks circle–floor, circle–wall and circle–circle overlap (narrow-phase collision detection; with two bodies no broad phase is needed).

Penalty model: an overlap δ produces a normal force F = max(0, k·δ + c·δ'), with c = 2ζ√(k·m_eff). The force is added like any other force and integrated with semi-implicit Euler. A resting disc sinks to δ = m·g/k. If ω·Δt = √(k/m)·Δt exceeds about 2, the explicit integration of the contact spring becomes unstable and the simulation diverges.

Impulse model: bodies are integrated freely, then any overlap is removed by moving the bodies apart (split by inverse mass), and the approaching normal velocity is reversed and scaled by e. The applied impulse divided by Δt is reported as an average force. Four sequential passes let simultaneous contacts settle.

Adjustable parameters

  • Contact model: penalty or impulse
  • Stiffness k (10^2.5–10^6 N/m), damping ratio ζ (0–1) — penalty
  • Restitution e (0–1) — impulse
  • Physics rate (250 Hz, 1 kHz, 4 kHz)
  • Mass of the large disc (0.2–5 kg)

Known limitations

  • Frictionless contact. The discs slide and never spin. Real engines add tangential friction and rolling.
  • The impulse model resolves contacts one at a time. Engines such as MuJoCo instead solve all contacts together as an optimisation problem.
  • Discs only. Real collision detection handles boxes, capsules and meshes, and uses a broad phase to avoid testing every pair.

Equations

δ=ra+rb−∥pb−pa∥,n=pb−pa∥pb−pa∥\delta = r_a + r_b - \lVert p_b - p_a \rVert, \qquad n = \frac{p_b - p_a}{\lVert p_b - p_a \rVert}
Fn=max⁡(0, k δ+c δ˙),c=2ζk meffF_n = \max\big(0,\ k\,\delta + c\,\dot\delta\big), \quad c = 2\zeta\sqrt{k\,m_{eff}}
j=−(1+e) (vb−va)⋅n1/ma+1/mb,va−=jman, vb+=jmbnj = \frac{-(1+e)\,(v_b - v_a)\cdot n}{1/m_a + 1/m_b}, \qquad v_a \mathrel{-}= \frac{j}{m_a} n,\ v_b \mathrel{+}= \frac{j}{m_b} n
penalty stability (undamped, semi-implicit Euler): k/meff  Δt<2\text{penalty stability (undamped, semi-implicit Euler): } \sqrt{k/m_{eff}}\;\Delta t < 2

Core update rule

// Penalty (soft): a force from the overlap
const depth = a.r + b.r - dist;
if (depth > 0) {
  const vn = dot(sub(b.v, a.v), n);            // separating speed
  const F = Math.max(0, k * depth + c * -vn);  // spring + damper, never pulls
  fA -= F * n;  fB += F * n;
}

// Impulse (hard): fix positions, then velocities
if (depth > 0) {
  a.p -= n * depth * wa / (wa + wb);  b.p += n * depth * wb / (wa + wb);
  const vn = dot(sub(b.v, a.v), n);
  if (vn < 0) {
    const j = -(1 + e) * vn / (wa + wb);
    a.v -= j * wa * n;  b.v += j * wb * n;
  }
}

Explained in Chapter 2: Physics Engines