Reference · Engine

The rigid body and its integrator

Cars carry a rigid body at +0x30; the car's position, velocity, rotation, angular velocity and its force and torque sums are the body's fields (see the car object). integrate (0x8006c504) steps it. Its callers are car_update for cars under full physics, 0x80040494 for cars at state 1, the track's moving volumes (0x8006b754) and the flying wheels (0x8007c894, see flying wheels). After each step the caller runs orthonormalize (0x80025be4) on the rotation.

Layout (from the body's start)

offsettypemeaning
+0x58s64[3][3]inverse inertia, body axes
+0xb0s32mass
+0xb4s32inverse mass
+0xb8s32gravity's strength, 386 (in/s²)
+0xbcVECTORgravity's direction, (0, 0, -1) on the tracks seen
+0xdcVECTORposition
+0xecVECTORmomentum
+0xfcVECTORvelocity
+0x10cs32speed
+0x110MATRIXrotation, body to world (the translation part is not used here)
+0x130s64[3]angular momentum
+0x148s64[3][3]inverse inertia, world axes
+0x1a0VECTORangular velocity, radians a second
+0x1b0s32its length
+0x1b4s32[3]force summed over the step
+0x1c8s64[3]torque summed over the step
+0x1e0u8asleep: not integrated (the car clears it when a pedal is pressed)

One step of dt seconds

  1. Asleep: nothing.
  2. Gravity direction × (strength × mass) joins the force sum.
  3. Momentum += F dt; velocity = momentum × inverse mass; speed is its length (the 64-bit square root shared with vec_length). Above 0x900000 (2304 in/s, about 131 mph) momentum and velocity are scaled down to it.
  4. Position += v dt; the force sum is cleared.
  5. Angular momentum += (torque × dt) >> 12 in 64 bits.
  6. World inverse inertia = (R · I⁻¹) · Rᵀ, through 0x800273ec (M · A) and 0x80026884 (A · M): each term a 64-bit entry times a 4.12 short, shifted down 12.
  7. Angular velocity = (I⁻¹_world · L), each product of two 64-bit values shifted down 20, summed, then down 8. Above 4π (fx(0x4000, 0x3244)) both it and the angular momentum are scaled down to it.
  8. Rotation += ([ω dt]ₓ · R) >> 8, entry by entry in 16 bits, where [ω dt]ₓ is the skew matrix of (ω << 8) dt >> 12 in 64 bits.
  9. The torque sum is cleared.

The rotation then drifts off orthonormal; orthonormalize fixes it by classical Gram-Schmidt on the columns, each normalised with the inline (a << 12) / length divide and kept to 16 bits.

Holding an axis (0x80071bc0) and damping spin (0x8003d5cc)

align(body, axis, dir) (axis 0, 1, anything else 2; dir by value) keeps only the angular momentum about dir: its dot with dir (each product of dir << 8 and the 64-bit momentum, shifted down 20) times dir again. Then, if the axis's column is within 60 degrees of dir (dot at least ½), that column becomes dir and the other two lose their components along it; otherwise the rotation is left. Air control uses it to hold the car level about the axis it last turned.

damp_spin(car) scales the angular momentum by 1 - fx(8, 25.0) = 3896/4096 each call.

Unknown

  • What 0x8006b754 and 0x8007c894 integrate.
  • Where mass, inertia and gravity are set up (the car loader?).