Step 1 of 5 · Reading · ~4 min
Read
Production
Production Physics
You now own every stage of a physics step: integration, bounding-box tests, contact geometry, impulses and position correction. Production physics is those same stages run in a fixed order over many bodies, with the budget and the bugs that come with scale. After this lesson your program will step a small world of circles under gravity, bouncing off each other and off a floor.
The step, in order
An engine step is a pipeline, and the order is part of the behaviour:
1. integrate every dynamic body (forces -> velocity -> position)
2. find overlapping pairs (broad phase, then exact test)
3. resolve each contact (impulse, then position correction)
4. resolve contacts with the world (static geometry such as the floor)
Integrating first means contacts are detected at the positions the bodies have just moved to. Resolving contacts one after another means later contacts see the velocities and positions earlier ones produced. That is the sequential impulse idea you met in the constraints lesson, with a single pass.
What one contact does
For each overlapping pair, in this exercise, with A the lower-numbered body and n pointing from A to B:
- the test is
|d|^2 <= (rA + rB)^2withd = pB - pA; the normal isd / |d|and the depth isrA + rB - |d|(coincident centres: normal(1, 0), depthrA + rB); - the impulse uses the restitution
e = min(eA, eB)and inverse masses (0 for a static body), and is applied only if the bodies are approaching; - then the position correction pushes the bodies apart by a fraction of the overlap beyond a small tolerance.
The floor is a static body with infinite mass and the normal pointing up. A circle touches it when its lowest point is at or below the floor line, and the depth is how far below.
Worked trace: a ball with radius 0.5, mass 1, e = 0.5 and velocity (0, -3) at height 0.6, gravity -10, dt = 0.1, floor at 0 with e = 1 (so the contact uses min(0.5, 1) = 0.5).
step 1: v = -3 - 1 = -4, y = 0.6 - 0.4 = 0.2. lowest point -0.3: depth 0.3
impulse: v = +2. correction: (0.3 - 0.01) * 0.8 = 0.232 -> y = 0.432
step 2: v = 2 - 1 = 1, y = 0.532. lowest point 0.032: no contact
step 3: v = 0, y = 0.532. the ball has reached the top of its bounce
Notice that the ball is sunk into the floor for one step and only the correction restores it. If you skipped the correction the ball would be lower after every bounce.
What production engines add
- A broad phase so the pair loop is not quadratic (the broad-phase lesson), and sleeping so resting bodies cost nothing.
- Several solver iterations and sub-steps so stacks stay stable. One pass, as here, is enough for a toy and not for a tower of boxes.
- Continuous collision detection for fast bodies, the last lesson.
- A time budget. At 60 frames per second a frame is 16.7 ms, and physics usually gets 4 to 8 ms of it; going over drops frames. This is why engines cap iterations and sleep aggressively.
- Determinism when replays or lockstep multiplayer need it: fixed step, fixed iteration order, and care with floating point.
- GPU physics pays off for thousands of independent elements such as particles and cloth. For a few hundred rigid bodies that gameplay reads back every frame, the transfer cost makes it slower than the CPU.
For a shipping game, use Box2D, Bullet, PhysX or Rapier: they cover hundreds of edge cases (stable stacking, joint limits, CCD, tuning) that take years to get right. You built this to understand them.
Common mistakes
- Resolving contacts before integrating, so every contact is one step stale.
- Counting a contact but skipping the position correction, or the reverse.
- Applying the impulse to a pair that is already separating.
- Visiting pairs in a different order from the numbering. The result depends on the order, so keep
i < jin index order. - Giving a static body gravity or letting it move.
Your exercise
The starter reads the world, steps it and prints the result. step already calls the three stages in order; you write integrate, collide_pairs and collide_floor. The most likely mistake is the order of integration and collision.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…