Forces and Motion
Simulating motion
Three ways to step time; only two survive
Every game that moves a character, every planet in a space simulation, every cloth that drapes, is really just stepped forward a tiny slice of time at once. Get the stepping subtly wrong and orbits spiral into the void, characters phase through walls, and stacks of boxes explode. This is where physics becomes code.
We ended the force lessons with a rule: find the acceleration from the forces, then push velocity and position forward one small timestep. Doing that stepping is . A computer cannot glide along the true smooth path; it can only take finite hops. How you take each hop is a real choice, and the choices behave startlingly differently.
Your orbit simulation slowly spirals outward and the planet escapes, which is wrong. What is the most effective fix?
Play first: one orbit, three ways to step time
The same body orbits the same central mass in all three panels. The only difference is the integration scheme. Press Run, then drag dt upward. Watch the energy trace under each: only the red one, explicit Euler, climbs away and sends its orbit spiralling out. The other two hold.
total energy over time
Unrolling the loop: where the energy leaks in
Start from the obvious loop. At each step, read the acceleration from the current position, then update velocity and position with the values you have. Written the most natural way, that is .
def explicit_euler(pos, vel, dt):
a = accel(pos) # acceleration from the CURRENT position
new_pos = pos + vel * dt # move using the OLD velocity
new_vel = vel + a * dt # then update the velocity
return new_pos, new_velThe flaw is hiding in plain sight. It moves the position using the old velocity, the velocity from before gravity had its say this step. For a curving path that old velocity points a hair too far outward, so every step nudges the body slightly farther out than it should be. Those nudges accumulate as a steady gain in total energy, and the orbit winds outward. The total energy should be constant; the method does not know that.
The one-line fix: use the velocity you just computed
Now move a single line. Update the velocity first, then move the position using that brand-new velocity. This is , the workhorse of nearly every real-time physics engine.
def semi_implicit_euler(pos, vel, dt):
a = accel(pos) # acceleration from the current position
new_vel = vel + a * dt # update the velocity FIRST
new_pos = pos + new_vel * dt # move using the NEW velocity
return new_pos, new_velDerived by swapping two lines
Verlet: remember the past, run time backward
We can do better still. advances the position using the current acceleration, then updates the velocity with the average of the old and new accelerations. That symmetry makes it , which is why its energy trace is the flattest of the three.
def velocity_verlet(pos, vel, dt):
a = accel(pos)
new_pos = pos + vel * dt + 0.5 * a * dt * dt # move with current accel
a_new = accel(new_pos) # accel at the new spot
new_vel = vel + 0.5 * (a + a_new) * dt # average the two accels
return new_pos, new_velAnd the digital catch: floating point
This is the fusion the whole module was building toward. Motion is a = F/m; simulating it is a loop; and the character of that loop, energy that leaks, holds, or barely drifts, comes down to which line you write first. Physics and code are the same object here.
Lock it in
- Simulating motion is numerical integration: read , then step velocity and position forward by a small dt.
- Explicit Euler updates both from old values and steadily adds energy, so orbits and springs spiral outward.
- Semi-implicit (symplectic) Euler updates velocity first and moves with the new velocity; its energy stays bounded, which is why engines use it.
- Velocity Verlet averages old and new acceleration, is time-reversible, and drifts least of all.
- The fix for a spiralling sim is update order, not just a smaller timestep.
Check yourself
Run plain explicit Euler on a stable circular orbit with a largish timestep. Over time the orbit tends to:
What is the single code change that turns explicit Euler into semi-implicit Euler?
Why do real-time physics engines use semi-implicit Euler instead of plain explicit Euler?
Think about what happens to energy over thousands of steps. Try to state it, then check.
Match each integration method to its defining property.
energy grows; orbits spiral outward
energy bounded; stable and cheap
time-reversible; averages old and new accel
Primary source
Glenn Fiedler, Integration Basics (Gaffer On Games)Fiedler walks through explicit Euler, semi-implicit Euler, and higher-order schemes from a game developer's seat, with exactly this spring-and-orbit lens.[1] Pair it with the semi-implicit Euler reference for why the energy stays bounded.[2]
Sources