Skip to content
Lesson 3 of 14

Step 1 of 5 · Reading · ~3 min

Read

Math & Integration

Numerical Integration

A rigid body is a few numbers: where it is, how fast it moves, and how heavy it is. Integration is the rule that turns those numbers into the same body one step later. After this lesson your program will advance a body through time under gravity, drag and an applied force, one fixed step at a time.

State and Newton

A body carries a position p, a velocity v and a mass m. Forces change velocity and velocity changes position:

a = F / m          Newton's second law: acceleration from net force
dv = a * dt        the velocity changes by acceleration times the step
dp = v * dt        the position changes by velocity times the step

Computers cannot integrate continuously, so the engine takes the current slope and walks one step of length dt along it. This is numerical integration, and the only real question is the order of the updates.

Semi-implicit Euler: velocity first

1. work out the net force F from the CURRENT state
2. a = F / m
3. v = v + a * dt
4. p = p + v * dt        <- uses the NEW velocity

Step 4 uses the velocity from step 3. That one choice is what separates the stable integrator used by Box2D and most game engines (semi-implicit, or symplectic, Euler) from plain explicit Euler, which moves the position with the old velocity. The next lesson shows why that matters over thousands of steps; here you just have to get the order right.

Worked trace, ball thrown at v = (2, 3) from the origin with a = (0, -10) and dt = 0.5:

step   v after        p after
 1     (2, -2)        (1, -1)
 2     (2, -7)        (2, -4.5)

The exact answer at t = 1 is y = 3 - 5 = -2, and the engine says -4.5. The error is large because dt is large; it shrinks in proportion to dt, which is why engines use small fixed steps.

Forces accumulate

Several things push on a body at once, and their effects simply add. Compute each as a force and sum them before you divide by the mass:

  • Gravity is a force proportional to mass, m * g. Dividing by m later cancels it, so a heavy and a light ball fall identically. If you forget the factor m, heavy bodies fall slower than light ones, which is wrong.
  • Linear drag opposes the velocity: -c * v. As the body speeds up the drag grows until it balances the push, and the body stops accelerating. That speed is the terminal velocity.
  • Applied forces (thrust, wind, a spring) are whatever the game supplies.

Evaluate drag from the velocity at the START of the step, as part of step 1. Using the already-updated velocity folds the step into itself and changes the numbers.

Inverse mass and immovable bodies

Engines store 1/m, the inverse mass, instead of m. Acceleration becomes F * (1/m), there is no division in the hot loop, and an immovable body (a floor, a wall) is just 1/m = 0: no force can accelerate it. In this exercise a mass of 0 means an immovable body whose state never changes. Treat it as a special case before dividing, never as a division by zero.

Common mistakes

  • Updating the position before the velocity (explicit Euler). The first step of a body at rest then leaves the position unchanged.
  • Forgetting that gravity is m * g and applying g as a force. Heavy bodies then fall slowly.
  • Reading the velocity after you already updated it when you compute drag.
  • Dividing by a zero mass and printing nan or inf.

Your exercise

The starter reads a script, keeps the world settings and prints the state after each step; you write step, which advances the body once. The most likely mistake is the order of the velocity and position updates.

Up nextSymplectic vs Explicit EulerMath & Integration

Discussion

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

Sign in to post a comment or reply.

Loading…