Skip to content
Lesson 11 of 14

Step 1 of 5 · Reading · ~3 min

Read

Resources & Rendering

Physics Integration

A physics engine's inner loop is tiny: given where a body is, how fast it moves and what is pushing on it, work out where it is one step later. Choosing how to do that tiny update decides whether a spring settles or flies apart. After this lesson your program will simulate bodies with both of the integrators engines use or avoid, and measure the energy that reveals which is which.

State, rates and steps

A body has a position x and a velocity v. Velocity is the rate of change of position, and acceleration a (force over mass) is the rate of change of velocity. The computer cannot follow a continuous change, so it takes fixed steps of length dt and assumes the rates hold during each step. Two ways of ordering the update look almost identical.

Explicit (forward) Euler advances both quantities from the state at the start of the step:

new_x = x + v * dt
new_v = v + a * dt        (a evaluated at the old position)

Semi-implicit (symplectic) Euler updates the velocity first and then moves the position with the NEW velocity:

v = v + a * dt
x = x + v * dt

It costs the same and needs the same inputs; only the order differs. The acceleration is evaluated once, at the position the step started from, in both variants.

What the difference looks like

Drop a body from y = 100 with gravity -10 and a step of 1. The exact answer is y = 100 - 5 t^2:

after step   exact   explicit   semi-implicit
    1          95       100          90
    2          80        90          70
    3          55        70          40

Both are off, by the same amount in opposite directions, so for free fall neither wins. The real difference shows up with something that oscillates.

Energy: why games use semi-implicit

Take a unit mass on a spring that pulls toward the origin with acceleration -k * position. The physical system never gains or loses energy; its total, 0.5*v^2 + 0.5*k*x^2, is constant, and the body swings forever with the same amplitude.

  • Explicit Euler multiplies that energy by 1 + k*dt^2 on every step. The amplitude grows without bound. With k = 1 and dt = 0.1 that is 1% per step, and after 100 steps the body has 2.7 times the energy it started with. A pendulum simulated this way swings higher and higher until it flies off.
  • Semi-implicit Euler does not conserve energy exactly, but the error does not accumulate: the computed energy wobbles inside a small band around the true value for ever, provided k*dt^2 is small enough (below 4). It is symplectic: it preserves a nearby energy-like quantity exactly. That is why Box2D, Bullet and nearly every game engine use it.

Fixed steps

All of this assumes dt is constant. If it changes from frame to frame, the integration error changes, replays diverge, and the stability limit k*dt^2 < 4 can be crossed by a slow frame. Engines therefore run physics at a fixed step (the accumulator from the game-loop lesson) and render in between.

Higher-order options

Verlet keeps the previous position instead of a velocity and is popular for cloth and ropes. RK4 samples the acceleration four times per step; it is far more accurate but costs four times as much and does not conserve energy better in the long run. Implicit Euler is unconditionally stable but needs an equation solve. Most games use semi-implicit Euler plus a constraint solver for contacts.

Common mistakes

  • Using the updated position to compute the acceleration inside the same step.
  • Updating position first with semi-implicit Euler (that is explicit Euler).
  • Updating velocity first with explicit Euler (that is semi-implicit Euler).
  • Reading the old velocity after it has been overwritten.
  • Printing -0.0000; a tiny negative number prints as 0.0000.

Your exercise

Implement step_body and energy as described in the exercise. The starter parses the commands, steps every body and prints. The most likely mistake is mixing up which value each variant reads.

Up nextScripting & GameplayProduction

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…