On this page
Engine, gearbox, wheels and springs
A vehicle in Refractor 2 is a tree of objects. The root has the physics node that moves; engines, springs, wings and floaters are children whose own physics nodes do not integrate themselves but push the root. This page follows the push of a land vehicle from the throttle to the ground.
Try it#
The bench below runs the page's formulas 30 times a second. Push the throttle, tilt the slope, hold a gear: the torque curve's marker, the gear bars and the slip curve follow. Switch the arithmetic to Heroes and the curve is read with Heroes' float stores (float precision); the difference is in the last bits, so the picture does not move, which is the point.
The engine#
The Engine object (Game\Objects\Vehicle\Engine.cpp) comes in two classes. EngineTemplate::createObject (bf2_w32ded.exe!0x00609360) builds the classic class (constructor 0x0062a270) for every engine type without bit 0x40 and the NewCar class (constructor 0x0062b880) for the others.
| Type bit | Meaning in the update |
|---|---|
0x01 | a thrust engine: propeller or rotor force on the root (planes, helicopters, boats) |
0x04 | the engine speed is scaled per side by the steering (getCurrentDifferentialRPM) |
0x06 | both bits: a tracked vehicle |
0x08 | a water engine |
0x10 | throttle forced to 1 (rockets, torpedoes) |
0x20 | a rotor: the angle-of-attack block runs (helicopters) |
0x40 | the NewCar class |
0x80 | c_ETNewCar2 (0xc2): the template's own gear ratios and torque curve, an rpm band, reverse selection |
Engine speed#
The engine's "rpm" is a unitless speed. One tick of Engine::update (bf2_w32ded.exe!0x0062a420, client BF2.exe!0x00680200; ours ClassicEngine::update in crates/bf2-physics/src/vehicle_engine.rs):
// l4 = the throttle axis' rotation / its maximum; fb = last tick's feedback from the wheels
rpm = k * ((l4 - fb) - rpm * 0.5) + rpm; // k = phy-engine-inertia, 0.05 (tanks: 0.07)
rpm = clamp(rpm, -1.0, 1.2); // 0x3f99999a above, -1 below
if rpm.abs() < 0.01 { rpm = 0.0; } // 0x3c23d70aWith no load the speed settles where (l4 - fb) = rpm / 2: full throttle would settle at 2, so the clamp at 1.2 is what a free engine reaches. The load comes from the wheels. For each wheel in contact Engine::feedbackLoop (0x0062ac00; ours feedback_core_p in vehicle_friction.rs) takes a sample:
v = (the wheel's friction force . the forward row) x wheel ratio / current torque
fb = (n x fb + v) x 0.99 / (n + 1) the running mean of the tick's samples, n the count so farA mean under 0.001 counts as 0. An engine type with bit 0x04 keeps the largest sample instead of the mean (the smallest when it runs backwards). The engine's next update reads fb and starts the mean again. So a wheel that pushes hard against the ground in a high gear, where the wheel ratio is large and the torque small, holds the engine's speed down; a spinning or lifted wheel lets it rise.
For a tracked engine the feedback is scaled by clamp(1 - speed / s, 0, 1), and s is a process-wide static (0x00892c70): differential x 0.75 of the first tank engine ever updated in the process. Every later tank uses that value, whatever its own differential is. The port keeps the static (EngineStatics::tank_speed_scale).
Torque#
getCurrentTorque (0x0062a360 -> EngineTemplate 0x0060a3d0; ours classic_torque):
torque_now = torque_curve(min(|rpm|, 1.0)) * template.torqueThe torque curve and the gear-ratio curve are 101-point tables over [0, 1] (EngineCurve, crates/bf2-gameplay/src/vehicle/curve.rs). A template sets a few control points; the table between them is filled by linear interpolation (0x004fb450), and a read blends the two entries around x x 100 (0x006090e0). Heroes reads the same table with two extra float stores (float precision).
Gears#
After the speed step (update_p):
if rpm > gear_up && clutch == 0.0 && gear < number_of_gears { gear += 1; } // gearUp, default 0.7
if rpm < gear_down && gear > 1 { gear -= 1; } // gearDown, default 0.3The clutch counts down by dt / gearChangeTime a tick. The classic class has five fixed ratios, [4.0, 2.0, 1.5, 1.25, 1.0] (0x00834274; classic_gear_ratio); only c_ETNewCar2 reads the template's setGearRatios.
The wheel ratio#
What a wheel gets from the engine (getWheelRatio 0x0062a330, client BF2.exe!0x00683a80; ours classic_wheel_ratio and wheel_ratio):
wheel_ratio = f32(differential * 3.5) / gear_ratio(gear)A low gear has a high ratio (4.0), so the wheel ratio is small: the wheels turn slowly for the same engine speed and get more torque.
The wheel#
A wheel is a Spring object with a PhysicsSpring node (0x0062d9d0; ours SpringNode in crates/bf2-physics/src/vehicle.rs), and its contact with the ground is a response (ResponsePhysics_Basic::applyWheelContacts 0x00699fc0; ours crates/bf2-physics/src/vehicle_friction.rs).
The spring#
PhysicsSpring::updatePhysics (0x0062dc40, BF2.exe!0x00683d20; ours spring_force_p): with d the wheel's displacement from its rest place under the body and v the displacement's change over the tick,
k = gravity * (-1.0 / 9.82) * strength; // NEG_INV_G: -0.101833, the float 0xbdd08dd2; gravity is negative
force = -(d * k + v * damping); // per axis, applied to the root at the wheel's offsetThe factor gravity x (-1 / 9.82) scales the springs with the gravity: with the engine's default gravity of -14.73 (physics) it is 1.5, so k = 1.5 x strength, and under another gravity the springs are stiffer or softer by the same share: a vehicle sits at the same height. The bench below stands a box on four such springs. Raise the strength and the body sits higher and swings faster; take the damping away and it never settles; tilt the slope and the downhill springs carry more.
The wheel's speed#
0x00698a40 (ours wheel_speed_step): one step of the wheel's own rotation speed s under a drive torque and a resistance,
s1 = s + torque / inertia * dt;
s2 = sign(s1) * max(|s1| - resist / inertia * dt, 0.0);The resistance can stop a wheel but never turn it round. Battlefield 2 and 2142 multiply by the reciprocal of the inertia; Heroes divides, and stores three intermediate results as floats (BFHeroes.exe@bfh126!0x009588d6).
Slip and friction#
computeFriction (0x00698870) turns the slip between the wheel and the ground into a force, through one curve (0x006987e0; ours slip_curve_p):
// x the slip, peak where grip is best, tail the grip that is left when sliding
if |x| <= peak { sign(x) * |x| / peak }
else { sign(x) * max(tail, (1 - tail) / (peak - 1) * (|x| - 1) + tail) }The force is scaled by friction x max(n.y, 0)^4, with n the ground's normal: on a slope the grip falls with the fourth power of the normal's height: at 30 degrees 56 % of it is left, at 50 degrees 17 %.
What differs per game#
Engine::updatePhysics (BF2142.exe!0x0064df20) has no per-gear boost tail and no boost force: a sprint scales the thrust by the seat's sprintFactor or the wheels' drive torque. An engine with noPowerWhenUpsideDown gives no thrust when it points down, and an engine whose player control object is empty gives no force. computeFriction takes a brake argument that scales the lateral grip by friLatBrakeMod. The spring has a settle byte.
The statements are the earlier games' with Heroes' float stores: the curve read (0x007d0993), the wheel step (0x009588d6), the torque (0x007d0ead), the wheel ratio (0x007d0e6d). The audit of the shipped values (jeeps, tanks, planes: armour, engine torque, differential, gears) reads equal in all 54 rows.
How it is tested#
- Bit for bit against the original's functions:
crates/bf2-physics/tests/vehicle_oracle.rs,wheel_terrain_oracle.rs,wheel_speed_step_bfh126.rs, in both precision modes. - Against the original server's own drive: the vehicle differential replays captured input (a slalom of 500 ticks, a ram of 1,227) through our server and compares every tick (test rigs).
- Statement by statement:
crates/bf2-gameplay/src/vehicle/gearbox_update_bf2_tests.rs.
Notes: notes/subsystems/gameplay-vehicles.md ("Engine"), notes/subsystems/physics-dynamics.md (section 6c, "Vehicle component nodes"). To change a vehicle's values see add a vehicle.
More of a vehicle: boats and aircraft (floats, wings, rotors), seats and cameras, spawners and wrecks.
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-gameplay | the behaviour of gameplay objects: weapons, projectiles, soldiers, vehicles |
Files this page names: crates/bf2-physics/src/vehicle_engine.rs, crates/bf2-gameplay/src/vehicle/curve.rs, crates/bf2-physics/src/vehicle.rs, crates/bf2-physics/src/vehicle_friction.rs, crates/bf2-physics/tests/vehicle_oracle.rs, crates/bf2-gameplay/src/vehicle/gearbox_update_bf2_tests.rs.
docs/dev/vehicles/drivetrain.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.