On this page
Physics: nodes, the tick and the pair pass
Refractor 2's physics is small and exact: one integration a tick, no substeps, one pass of collision and response. What makes it hard to port is not its size but that every float of it decides where a vehicle is a second later. This page is the map; the wheels and springs are on their own page, the queries on collision queries.
The parts#
| Class | Role |
|---|---|
PhysicsSystem | gravity, wind, the height at which the air has no density. Defaults: gravity -14.73, wind 0, 1000 m. No script of the games sets them. |
PhysicsNode | one body's state: transform, velocities, mass, drag, the accumulators of force and torque, the sleep counter |
PhysicsNodeManager | the list of nodes, updated in insertion order (bf2_w32ded.exe!0x006b39a0) |
ResponsePhysics | a body's collision response: rigid (class 0xc42d), soldier (0xc42e), static (0xc430) |
ResponsePhysicsManager | the passes of collision detection and response (updateAllObjects 0x006b51d0) |
CollisionManager | owns the collision mesh templates; it is not the collision loop |
Gravity is -14.73, not -9.81: things fall half again as fast as on earth.
The order in a tick#
Game::simulateTick (0x0044a840), once per 30 Hz tick:
- the objects' update (an engine's update is one of them);
- the players' node stage (the frame and the tick);
PhysicsNodeManager::update: every other node integrates;ResponsePhysicsManager::update: collision detection and response, once;- the players' response stage;
- the post-frame updates: each node's own, then the responses store the transforms as "previous" for the next tick's sweeps.
There are no substeps: one integration a tick with the tick's length. The broadphase and the soldier's queries sweep along the step.
A node's step#
What PhysicsNode::update does with a tick (verified bit for bit in both precision modes):
- Forces to velocity. The accumulators hold what the tick's forces added: engines, springs, wings, floats, explosions, gravity, drag. The linear step adds them over the mass; the angular step adds
(torque + friction torque) x dtthrough the inverse inertia. - The inertia is a box's. The inverse inertia comes from the full extents of the body's bounding box and its mass. A template's collision box is therefore also what makes a vehicle easy or hard to turn.
- Drag acts on the linear and the angular velocity; it sees no wind. The angular drag uses its row-1 coefficient for row 2 as well: an oddity of the original that the port keeps and a test would catch if somebody "fixed" it.
- Sleeping. A node has a sleep counter; a node at rest stops integrating until it is woken. Sleeping nodes are still visited by the manager: the update itself handles the sleep.
Vehicle parts do not integrate themselves: a spring, a wing, an engine or a float is a node whose update puts a force on the vehicle's root node (engine, gearbox, wheels).
The rigid pair pass#
For each body of the rigid list (alive, collidable, awake), checkMeshObjectVsObjects (0x006b46d0, identical byte for byte in the client BF2.exe!0x006ffc30):
- Stamp the body's response with the frame's number: a pair is handled once, when the first of the two is "self".
- Broadphase (
0x006b40d0): the objects within a sphere atpos - v dt / 2with the radiusmin(|v dt|, 50) + the body's own. - Filters: a deleted object, another response class, a stamp of this frame, or a shared collision group skips the candidate.
- Sphere test: the two radii plus a quarter tick of each body's speed.
- Box tests: the smaller body's sphere against the larger body's box; then, when both meshes have more than 10 vertices, box against box.
- The radius ratio
k = r1 / r2decides the order:
k | Collide |
|---|---|
| below 0.25 (self is small) | self against the candidate |
| 0.25 to 4 | the candidate against self, then self against the candidate |
| above 4 (self is large) | the candidate against self; then the other way too when both are mobile |
For bodies of similar size both ways run, in this order; the port keeps the order because the second call meets the state the first left.
collide (checkObjectVsObject 0x0069b680) makes ray queries against the other body's collision mesh, with a collision type per pair (collision queries); the contacts become impulses (impulseOn 0x0069abc0, solveImpulse 0x0069aee0) and friction (addFriction 0x00699fc0). Against the terrain a body tests its points against the heightmap (checkVsTerrain 0x00698e40), where a wheel's spring has a branch of its own.
Mass#
The pass has mass-ratio rules besides the radius ratio: for very different masses a test of the objects' collision type 4 decides whether the response is also run in the other direction.
How it is held#
| Rig | What it compares |
|---|---|
| node oracle | a node's full state and transform after every tick, ours against the original's function: 400 cases at 53 bits, 200 at 24 (float precision) |
| pair oracle, multi-body trajectories | two and more bodies through the pair pass, on terrain and water |
| the vehicle replay | recorded drives through our server against the original server's, every node, every tick (test rigs) |
Notes: notes/subsystems/physics.md, physics-dynamics.md, physics-response.md. Code: crates/bf2-physics (node.rs, pass.rs, pair.rs, response.rs, vehicle*.rs), crates/bf2-world (the candidates' query).
What pushes a body: boats and aircraft for floats, wings and engines; projectiles for the point node.
How it is tested#
- The original computes the expected value.
crates/bf2-physics/tests/friction_oracle.rscalls the original's wheel friction and spring solve once per case, for the server and the client executable and in both floating-point precision modes, and the port must produce the same hash of the wheel, the spring node and the root node. That is the strongest proof on this page: it holds the formulas and their rounding. - Statement tests in the source files hold what the listing says per game: the collision response's impulse limit and its lever arm about the turned centre of mass in 2142 (
response.rs), a node's sleep and wake rules (node.rs), the order in which a vehicle's parts are walked (vehicle_body.rs). - Pairs of bodies (
crates/bf2-world/src/vehicle_pairs.rs): which candidates a vehicle's pass takes and in whose turn, one case checked against a traced run of the original. - In play: the vehicle replay drives recorded inputs through the port and compares every vehicle's node state with the original server's capture, tick by tick (how a port is made).
In the code#
The crates this page names, each with its line of docs/release/LAYOUT.md (the list is gathered by the wiki's build from the page itself):
| Crate | What it is |
|---|---|
crates/bf2-physics | physics: collision queries, materials, rigid-body integration |
crates/bf2-world | the runtime object world: objects from templates, object manager, hierarchy, tick order |
Files this page names: crates/bf2-physics/tests/friction_oracle.rs, crates/bf2-world/src/vehicle_pairs.rs.
docs/dev/engine/physics.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.