BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Physics: nodes, the tick and the pair pass

BF22142Heroes

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.

One tick, no substeps nodes integrate forces and torques -> velocity -> place collision and response once a tick, over the rigid list post frame previous transforms; AI, portals The rigid pair pass, for one body against its candidates (0x006b46d0) broadphase: objects within the body's sweep (centre pos - v dt / 2, radius the step plus its own) skip: deleted, another response class, already handled this frame, a shared collision group sphere test: the two radii plus a quarter step of each speed box test: the smaller body's sphere against the larger one's box; then box against box the radius ratio k = r1 / r2 decides who collides against whom: k < 0.25: self against the candidate 0.25 .. 4: the candidate first, then self k > 4: the candidate against self collide: ray queries against the other's mesh (0x0069b680); contacts are solved as impulses (0x0069aee0) gravity -14.73 by default, never set by a script of the games; sleeping nodes are still updated: the update handles sleep bf2_w32ded.exe!0x006b39a0 (PhysicsNodeManager::update), 0x006b51d0 (updateAllObjects), 0x006b3930 (postFrameUpdate)
A tick's physics. The rigid pair pass is where two bodies meet.

The parts#

ClassRole
PhysicsSystemgravity, 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.
PhysicsNodeone body's state: transform, velocities, mass, drag, the accumulators of force and torque, the sleep counter
PhysicsNodeManagerthe list of nodes, updated in insertion order (bf2_w32ded.exe!0x006b39a0)
ResponsePhysicsa body's collision response: rigid (class 0xc42d), soldier (0xc42e), static (0xc430)
ResponsePhysicsManagerthe passes of collision detection and response (updateAllObjects 0x006b51d0)
CollisionManagerowns 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:

  1. the objects' update (an engine's update is one of them);
  2. the players' node stage (the frame and the tick);
  3. PhysicsNodeManager::update: every other node integrates;
  4. ResponsePhysicsManager::update: collision detection and response, once;
  5. the players' response stage;
  6. 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 dt through 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):

  1. Stamp the body's response with the frame's number: a pair is handled once, when the first of the two is "self".
  2. Broadphase (0x006b40d0): the objects within a sphere at pos - v dt / 2 with the radius min(|v dt|, 50) + the body's own.
  3. Filters: a deleted object, another response class, a stamp of this frame, or a shared collision group skips the candidate.
  4. Sphere test: the two radii plus a quarter tick of each body's speed.
  5. 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.
  6. The radius ratio k = r1 / r2 decides the order:
kCollide
below 0.25 (self is small)self against the candidate
0.25 to 4the 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#

RigWhat it compares
node oraclea 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 trajectoriestwo and more bodies through the pair pass, on terrain and water
the vehicle replayrecorded 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.rs calls 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):

CrateWhat it is
crates/bf2-physicsphysics: collision queries, materials, rigid-body integration
crates/bf2-worldthe 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.

Source: docs/dev/engine/physics.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.