five-on-it / packages/sim/src/substeps
packages/sim/src/substeps
How many physics steps a tick takes: one, unless two moving bodies are closing on each other faster than a single step's contacts can hold them apart.
Rapier hands the solver a contact's points once two colliders are within CONTACT_PREDICTION (10 cm, world.ts), and soft CCD turns what a car will hit within the step into a contact before it gets there: against the level (a wall, a kerb, a lamp) one step a tick is solid at any speed. Between two bodies that both move it is not: two cars closing faster than 10 cm a step meet with a contact predicted from afar, of which Rapier keeps a single point; its blow at a corner spins a car into the other, and they sank up to half a metre into each other. So a tick is cut into as many steps as it takes for the fastest pair closing on each other (a car and another car or trailer, a car and a person on foot) to close at most those 10 cm a step, MAX_PHYSICS_SUBSTEPS at most: the contacts are found again every step and every point of them goes to the solver.
What counts is how fast the gap between the two shrinks: their velocities along the line between their nearest points (the contact normal), with what their spins may add. Cars queueing at a light, scraping past each other, a chaser pressed against the player's car and pushing it along, a crowd strolling by a parked car: none of them closes fast, and the tick takes one step and costs what it always did. A head-on, a rear-end, a T-bone or a car running into a person takes as many as its closing speed asks for, until the blow is over.
The pairs are found by sweeping the bodies sorted along x (each widened by how far it can reach within the tick), and a pair's boxes on the ground are compared before its hulls are, so a crowded room costs a few dozen box tests a tick and a hull query for each pair that may meet within the tick, not every car against every body; the bodies are kept between ticks and filled anew.
Deterministic: decided from the world's state alone, on the server and in every replay of the client's prediction alike. The client, whose distant twins are proxies, may take one step where the server takes four for a crash across the town; the player's own car then integrates its gravity over one step instead of four, a millimetre a tick, which the next snapshot takes back.