Contents · Physics Engines
Part 0 · Orientation
Part 1 · Mathematical Foundations
Part 2 · Equations of Motion
Part 3 · Time Integration
Part 4 · Collision Detection
Part 5 · Contact & Constraints
Part 6 · Constraint Solvers
Part 7 · Position-Based Dynamics
Part 8 · GPU & Parallelism
Part 9 · Differentiable Simulation
- Differentiable Simulation
Part 9 · Differentiable Simulation
Differentiable Simulation
A simulator is just a function applied over and over. Each step maps the current state to the next, , where holds the parameters you might want to tune: controls, masses, stiffnesses. A rollout of steps is the composition . Differentiable simulation propagates gradients through that composition, by autodiff or by analytic derivatives, so you can optimize with gradient descent instead of black-box search.12
The chain rule through a rollout
By the chain rule, the Jacobian of the whole rollout is the product of the per-step Jacobians. Take semi-implicit Euler on a free particle under constant acceleration , with state :
Differentiating one step with respect to the state (the acceleration is constant, so it drops out) gives a single block:
At that is
Composing steps multiplies copies of . For , multiply it out entry by entry:
The pattern holds for any :
The off-diagonal is exactly the elapsed time, which reads as: nudging the initial velocity shifts the final position by . In smooth free flight the gradient is exact, cheap, and stable no matter how many steps you chain. This is the case differentiable simulators are built for.
Where contact breaks it
Contact is where the clean product falls apart. At a bounce the velocity flips,
with restitution . Around the instant of contact, is not differentiable: the state jumps, and the sensitivity of "did it hit?" to the inputs is a step, not a slope. Push contact through a stiff spring instead and the derivative exists but is enormous and short-lived, so a single step can miss it entirely.
The consequence is measured, not just aesthetic. Naively differentiating through stiff or discontinuous contact yields gradients that are biased or high variance, and Suh et al. show first-order (differentiable-simulator) gradient estimators can be worse than the zeroth-order estimators that never differentiate the simulator at all.3 The usual fixes smooth the contact: replace the hard switch with an analytic softening, or average the gradient over randomized perturbations so the expectation is differentiable even where a sample is not.
The demo below is the smallest version of this. A projectile is launched at a fixed ; the only knob is the launch speed . A wall sits between the launcher and the target. If the ball clears the wall it flies on to a range ; if not, it stops dead at the wall. The loss is the squared miss distance to the target. Watch the loss curve as you drag .
- landing x
- 6.00 m
- loss (x − 10)²
- 16.00
- clears wall
- no
The step in the right-hand curve is where the contact mode flips from blocked to clearing. On the flat plateau the gradient dloss/dv₀ is zero; at the step it is undefined. Neither one points toward the target, which is why contact makes differentiable simulation hard.
On the flat plateau the gradient is zero: a little more speed changes nothing because the ball still hits the wall in the same place. At the cliff the gradient is undefined. Only inside the smooth basin, once contact is out of the way, does actually point at the target. That narrow regime, exact where motion is smooth and blind where contact switches, is the whole story of why differentiable simulation is powerful in free flight and hard the moment things touch.
Footnotes
-
Hu et al., DiffTaichi: Differentiable Programming for Physical Simulation (ICLR 2020). ↩
-
Freeman et al., Brax: A Differentiable Physics Engine for Large Scale Rigid Body Simulation (2021). ↩
-
Suh, Simchowitz, Zhang & Tedrake, Do Differentiable Simulators Give Better Policy Gradients? (ICML 2022). ↩