Stepping Motion Forward: Euler, Semi-Implicit and Verlet
When there's no formula for the motion, a game steps it forward a little at a time: nudge the position by the velocity, nudge the velocity by the acceleration. Doing those two nudges in the wrong order makes a spring gain energy every step until it explodes; updating the velocity first keeps it bounded forever, as long as the time step stays below .
What the symbols mean
- Position at step n
Where the object is after n steps.
- Velocity at step n
How fast it's moving after n steps.
- Time step
How much time one step covers.
- Acceleration
Force divided by mass, worked out from the current position (for a spring, a = −ω²x).
What the symbols mean
- New velocity
Updated first, then used right away to move the position.
What the symbols mean
- Previous position
Verlet remembers where the object was last step instead of storing a velocity.
What the symbols mean
- Energy
E = ½v² + ½ω²x², the spring's energy per unit mass.
Explanation
Numerical integration is simply moving a simulation forward one small time step at a time: from where things are and how fast they're going right now, work out where they'll be a moment later. It's called integration because that's the job it replaces: position is the integral of velocity and velocity is the integral of acceleration, and when there's no formula for the answer you add up small steps instead. Every recipe below has the same two pieces: (the position) gets nudged by velocity times , and (the velocity) gets nudged by acceleration times . The methods differ only in which velocity and acceleration they use, which in code is the order of two lines. It sounds like a detail. Bear with it, because it's the difference between a spring that bounces forever and one that explodes.
Explicit Euler. The most natural recipe is the one from Euler's Method: use the current values for everything.
const a = accel(x); // explicit Euler: both updates use the old values
x += dt * v;
v += dt * a;
Why explicit Euler gains energy. Test it on a spring with no damping, . The true motion keeps its energy forever. One explicit step gives and . Square and add (writing for ):
The cross terms cancel, and what's left factors: . So every step multiplies the energy by , a number bigger than no matter how small the step. There's a nice picture of why. Plot velocity against position (scaled so the true motion is a circle). The true motion goes around the circle; an Euler step moves along the circle's tangent, and a tangent line always leads outside the circle. Every step lands on a slightly bigger circle, and the orbit spirals out. There's an animation further down that shows it.
Semi-implicit Euler doesn't. Now swap the two lines: update the velocity first, then move with the new velocity.
v += dt * accel(x); // semi-implicit Euler: velocity first...
x += dt * v; // ...then position, using the new velocity
Same cost, same two lines. To see what changed, write one step as a matrix acting on , the way Linear Transformations does:
The determinant says how a step scales areas in the plane. Explicit Euler's is : it inflates. Semi-implicit Euler's is exactly: it preserves area, so a cloud of starting states can't puff up, and the orbit can't spiral. (Methods with this property are called symplectic, Greek for "woven together", a name from the geometry of position and momentum.) In fact it keeps the quantity exactly constant, so the true energy wobbles slightly within a band but never drifts.
The stability limit. Area preserving isn't the whole story. The step matrix's eigenvalues (see Eigenvectors) solve , since the trace is and the determinant is . Their product is . While they're complex, both of size , and the motion just goes around. Once they turn real, one of them below , and every step flips the sign and grows. So semi-implicit Euler is stable exactly when
A stiff spring (big ) needs a small step.
Verlet. There's a third way that doesn't store velocity at all. Write the Taylor series (see Taylor Series) one step forward and one step back:
Add the two and every odd term cancels: . Solve for the new position and you have Verlet's formula in the box. The error per step is tiny, of size , and on a spring, started from the same first two positions, it produces exactly the same positions as semi-implicit Euler (with the same stability limit). It's popular for cloth, ropes and ragdolls, because constraints like "these two points stay 1 cm apart" can be enforced by simply moving positions, and the velocity, implied by , follows along.
Why games use a fixed time step. Frame times vary: ms one frame, ms when something loads. Two reasons not to feed that straight into the physics. First, stability: is a condition on , so one long frame can push a stiff spring past it and the simulation explodes. Second, determinism: the same inputs should give the same results on every machine, for replays and networked games. So physics runs on a fixed step, as many steps as the frame needs, and the leftover time carries into the next frame:
acc += frameTime; // real time owed to the physics
while (acc >= DT) { // DT is fixed, say 1/120 s
step(DT);
acc -= DT;
}
A common mistake. It's tempting to think a smaller step fixes explicit Euler. It slows the growth but never stops it. For a spring with at steps per second, each step multiplies the energy by , which compounds to about times the energy every second. At steps per second it's still about times per second. On a spring, explicit Euler is unstable at every step size; the fix is to change the method, not the step.
A common mistake. The difference between explicit and semi-implicit Euler is just the order of two lines, so it's easy to switch from one to the other by accident while refactoring. If a spring that used to settle starts slowly growing, check the order first.
The same story holds for the smoothing of Smoothing That Doesn't Depend on Frame Rate: explicit Euler on multiplies the gap by each step, which flips sign and grows once . Whenever the exact solution is known, as for the springs of SmoothDamp and Second-Order Follow, stepping it exactly beats any of these methods.
See it
Worked examples
A spring with (so ) starts at at rest. Take two steps of with explicit Euler, then two with semi-implicit Euler, and track the energy .
- 1
Starting energy: . The true spring keeps exactly this forever.
A stiff spring in a physics engine has , stepped with semi-implicit Euler. Is it stable at steps per second? What about a hitch where one frame takes s? How many fixed steps of s cover that hitch?
- 1
Find the frequency: rad/s.
Practice
Type your answer and check it. Solve them all to complete the lesson.
A spring with is at moving at . Take one explicit Euler step with . Enter the new position and velocity, in that order.
A spring with is at moving at . Take one semi-implicit Euler step with . Enter the new position and velocity, in that order.
An undamped spring with is simulated with explicit Euler at . After steps ( seconds), by what factor has its energy grown?
A kg crate hangs on a spring with N/m in a game, simulated with semi-implicit Euler. What is the largest time step (in seconds) that keeps it stable?
A rope point is simulated with Verlet, , under a spring force , with . Its last two positions were and . Find .
A game runs its physics on a fixed step of s with an accumulator that starts empty. Three frames take s, s and s. How many physics steps run in total over the three frames?
A UI value is smoothed by with per second, stepped with explicit Euler at s. Each step multiplies the gap by the same number. What is that number?
Which statement about explicit Euler on an undamped spring is true?
The hardest one here. Get it, and you've got this lesson.
A spring with rad/s is stepped with semi-implicit Euler at s. The numerical motion is still a steady oscillation, but at the wrong speed. What is its period in seconds? (The true period is s.)
Where it's used
Most real-time physics engines step rigid bodies with semi-implicit Euler on a fixed time step, often 1/60 or 1/120 s, with the accumulator loop above. Cloth, hair, ropes and ragdolls are usually position-based Verlet: points remember their last position, and constraints are satisfied by nudging positions directly. Molecular dynamics and planetary simulations use velocity Verlet and other symplectic methods for the same reason games do: over millions of steps, an orbit that slowly gains energy is worse than one that's slightly off but bounded. And "the physics exploded" bug reports are, more often than not, a stiff spring meeting a long frame.