What must a deterministic physics step guarantee for input-recorded replays such as time-trial ghosts, and how far should a team take determinism?
answer
- same inputs in, same bits out
- fixed length, fixed counts, fixed order
- hidden state: caches, sleep flags, seeds
- scope: one machine, one platform, all
- a checksum every step
basics
~20 sSame start state and inputs must give bit-identical state: a fixed step length, fixed substep and iteration counts, a stable processing order, no clock reads or unseeded randomness, and all hidden state restored. The scope is the lead's decision.
solid answer
~50 sA replay stored as inputs works only if re-simulating reproduces every bit. Inside the step that means a length that never comes from measured frame time; substep and iteration counts that never adapt to the frame budget; bodies, pairs, contacts and constraints processed in an order that does not depend on memory addresses, hash iteration or thread timing; parallel work split and combined in a fixed order; randomness from a seeded generator kept in the state; and a start snapshot that includes hidden state such as warm-start impulses, contact caches and sleep flags. The lead decides the scope: the same build on one platform is cheap and covers ghosts, while cross-platform also needs controlled floating-point behaviour or fixed point. The alternative, recording state snapshots, needs no determinism but costs storage. Hash the state every step so divergence is caught where it starts.
go deeper
Recall that an input-recorded replay needs the same start state and inputs to produce exactly the same physics state, which a fixed step length alone does not guarantee.
Explain the step's requirements: fixed length and counts, a stable processing order, no clock reads or unseeded randomness, and hidden state restored.
Diagnose a diverging replay with per-step hashes, then trace it to an unordered container, an adaptive count or missing hidden state such as warm-start impulses.
Own the scope decision: which features need determinism on one machine, one platform or all, what each costs, and when snapshots are the cheaper honest answer.
## Two ways to record a replay | Approach | What is stored | Needs a deterministic step | Trade-off | |---|---|---|---| | Input recording | start state, then inputs per step | yes, bit for bit | tiny files; any divergence ruins the replay | | State snapshots | body states at intervals, interpolated | no | far larger files; playback is not a simulation | A time-trial ghost stored as inputs costs a few bytes per step, but only if running the step again from the same start reproduces every bit. **Determinism** here means exactly that: the same initial state and the same inputs give a bit-identical state after every step. Snapshots need none of it, at a much higher storage and bandwidth cost. ## What the step itself must guarantee 1. **A fixed step length**, never measured frame time. How the loop accumulates time into whole steps is the game loop's concern; the physics only needs the same `dt` every step. 2. **Fixed substep and solver iteration counts.** Adapting either to the remaining frame budget makes results depend on machine load. 3. **A stable processing order** for bodies, broad-phase pairs, contacts and constraints. An iterative solver's result depends on the order it visits constraints, so an order derived from memory addresses, pointer-keyed hash tables or allocation history breaks the replay. 4. **Deterministic parallelism:** partition work the same way every run, by island or index range, and combine per-thread results by index in a fixed order rather than in completion order. 5. **No outside inputs:** no wall-clock reads and no unseeded randomness; the random generator's state is part of the simulation state. 6. **Inputs applied at a defined step**, not at whichever rendered frame they arrived on. ## Hidden state that breaks replays - **Warm-start impulses** and the **contact cache**: the solver's starting guess comes from the previous step. - **Sleep flags and timers**: a body asleep in one run and awake in the other takes a different path through the step. - **Creation order and ids**: if processing order follows ids, ids must be assigned identically. - **Broad-phase structure**: a tree built incrementally can report pairs in an order that depends on its insertion history, so sort pairs by a stable key before solving. So a replay must start from a level load or from a snapshot of the **full** simulation state, not just positions and velocities. ## Choosing the scope | Scope | Typical use | Extra cost | |---|---|---| | Same build, same machine | debugging, local ghosts | ordering discipline only | | Same build, other machines of one platform | shared ghosts, replays checked by a server | also avoid code paths chosen by CPU features and approximate hardware instructions | | Across platforms and compilers | cross-platform ghosts | also controlled floating-point behaviour or fixed point; slower and harder to keep | The floating-point sources of divergence and fixed-point arithmetic are covered with floating-point robustness, and middleware determinism settings with physics middleware tuning. The step's part is to hand those measures a fixed, ordered computation to act on. ## Catching divergence - Hash the simulation state every step, or every few steps, and store the hashes in the replay. - On playback compare hashes: the first mismatch names the step where divergence began, which is far easier to debug than a ghost that ends up inside a wall a lap later. - Run recorded replays in automated tests and fail on any mismatch, because determinism regresses silently when someone adds an unordered container. ## A policy a lead can enforce - Decide the scope per feature: ghosts may need one platform; a cross-platform mode would need all of them. - Ban address-keyed unordered containers, clock reads and global random state inside simulation code. - Tag replays with the build that recorded them and refuse a mismatched build rather than playing it wrongly. - If the scope a feature needs is too expensive, choose snapshots for that feature instead of half-delivering determinism.
- Why does a replay that restores only positions and velocities diverge?The step reads more than that: warm-start impulses from the previous step, the contact cache, sleep timers and flags, the random generator's state and the order bodies were created in. Starting from positions and velocities alone gives the solver a different initial guess, so the first step differs in the last bits and the difference grows. Snapshot the full simulation state, or start replays from a fresh level load.
- Does multithreading rule out a deterministic physics step?No, but it constrains it. Work must be partitioned the same way every run, by island or index range rather than by whichever thread is free; each partition processes its items in a fixed order; and results are written and combined by index, never in completion order. Work stealing for scheduling is fine as long as it cannot change what is computed or the order it is combined in.
- A fixed timestep alone makes the simulation deterministic.
- Iterating bodies from a pointer-keyed hash set is fine; the contents are the same.
- A deterministic step is automatically identical across platforms.
- A replay can start from positions and velocities only.
- Multithreaded physics can never be deterministic.