Vehicle physics, tyres, damage & AI
Shift Engine's vehicle model lives in libs/drive (namespace drive) — a standalone, deterministic, double-precision C++20 library with zero graphics or OS dependencies. Every car runs as a 6-DOF rigid body in a right-handed Y-up frame (+X right, +Y up, +Z forward; wheels 0=LF, 1=RF, 2=LR, 3=RR) stepped at a fixed 120 Hz outer rate split into 3 × 360 Hz internal substeps.
Vehicle::step() 120 Hz Fixed Outer Step -> 3 x 360 Hz Substeps (double precision, +X right, +Y up, +Z forward): Pre-substep inputs: - shape_input(): 31-sample Catmull-Rom speed-sensitive steering curve (bypassed in 900-deg Separate Wheel linear mode). - update_gearbox(): automatic / manual / manual+clutch state machine. - Ground queries: drive::MeshGround using 4-wide BVH4 (128 B nodes) and 8-wide BVH8 (256 B nodes) with x86-64 AVX2+FMA+BMI1 (bvh8_avx2.asm) and ARM64 NEON (bvh8_neon.S) kernels. Substep Stage 1 — Suspension & Ground Contact: - Ackermann front steering; 5-pass raycast rejecting near-vertical faces (dot(n, up) < 0.17) and steps > 0.30 m. - 9 procedural SurfaceTypes.xml Bumpiness noise types (HeightGrid, Simplex, Ridges, RidgesAndBumps, Simplex4/Power/Abs, Simplex3Multiplied, SinWave) with 25 ms low-pass height filter and analytical normal gradients. - Soft-ground Springiness: slew-clamped deflection smoothed by 3-sample moving-average ring buffer. - Static corner preloads, asymmetric bump/rebound dampers, progressive bumpstops damped by closing speed along ground normal, anti-roll bars, airborne gravity scaling (1.05 per lifted tyre, 1.15 all-off). Substep Stage 2 — Rev Limiter, Brakes, ABS & STM: - 40 ms rev-limiter throttle cut without opening turbo BOV. - Per-car brake calibration (6 fixed-point longitudinal load-transfer iterations at 50/80/200 mph + 10-sample Catmull-Rom decel table). - Stability Management (STM): asymmetric outside-wheel braking (+amt outside, -0.5*amt inside), capped near -0.9 slip when ABS is off. - ABS / No-ABS slip ring buffer (up to 8 substeps), steering-dependent release thresholds, smooth 40 ms pressure recovery. Substep Stage 3 — Combined-Slip Tyres & Linearized Spin: - Two-load blended friction curves (light ~10.2 kgf to heavy ~1019.7 kgf, clamped 5000 kgf). - Normalized combined slip: sigma = (omega*r - vx)/v_min, alpha = atan2(vy, v_min)*180/pi, sx = sigma/sig_pk, sy = alpha/alpha_norm, s = max(hypot(sx, sy), 1e-4). - Fx = mu_x(s*sig_pk) * (sx/s) * N * kx, Fy = -mu_y(s*alpha_pk) * (sy/s) * N * ky. - Finite-difference longitudinal slope dFx/domega folded into J_eff[i] = J_wheel + reflected*weight + dt*r*slope. Substep Stage 4 — Coupled 4x4 Implicit Driveline, Clutch, TCS & LSD: - 2-stage TCS (15 ms attack / 45 ms recovery torque cut + launch wheel-speed trim). - 15 Hz turbo/supercharger boost pressure filter. - Coupled 4x4 active-set linear solve (solve_4x4) across front, rear, and center LSDs simultaneously with clutch capacity solve. Substep Stage 5 — Body Forces, Yaw Shaping & 3D Aerodynamics: - Re-evaluate tyre forces at omega_new + slew-clamped VelDepFriction surface drag. - FrictionTorqueMod yaw-torque shaping (steering mode, drivetrain, off-road weight, mass, cos_yaw, 8..15 m/s blend). - Per-surface LinearDrag/AngularDrag + 3D quadratic drag, axle lateral drag, pitch AoA front/rear downforce, and drafting. Substep Stage 6 — Integration, Body Contacts & Voxel Damage: - Semi-implicit Euler linear velocity + gyroscopic angular velocity (tl - wl x (I*wl)). - Body collision spheres (restitution + speed-blended 10..40 mph friction + push-out) + GimbalLock rollover guard (cos0=0.87, cos1=0.82). - Quaternion pose integration + 10x10x10 (32 KB) trilinear VoxelDamageGrid with exact algebraic rewind inverse and 21 mechanical channels.
Tyres, coupled 4×4 driveline & damage#
Two-Load Combined-Slip Tyre Model & Coupled 4x4 Implicit Driveline / Voxel Damage: Tyre Model: - Blends light-load (~10.2 kgf) and heavy-load (~1019.7 kgf, clamped 5000 kgf) friction curves linearly by corner load N/9.80665. - Evaluates combined normalized slip s = max(hypot(sx, sy), 1e-4) where sx = sigma / sig_pk and sy = alpha / alpha_norm. - Off-road surfaces shift alpha_norm toward off_road_peak_sa by surf.off_roadness. - Forces: Fx = mu_x(s * sig_pk) * (sx / s) * N * kx; Fy = -mu_y(s * alpha_pk) * (sy / s) * N * ky. - Linearized implicit wheel spin inertia: J_eff[i] = wheel_inertia + reflected * weight[i] + dt * r[i] * dFx/domega. Coupled 4x4 Driveline & LSD Active-Set Solver: - Engine (15 Hz turbo/supercharger boost, 40 ms rev limiter) -> Clutch (locked vs. slipping at ClutchMaxTorque) -> Center LSD (AWD, rolling-radius matched r_front/r_rear) -> Front LSD (LF/RF) & Rear LSD (LR/RR). - Solves all 4 wheel angular velocities (omega0..omega3) simultaneously in a 4x4 linear system (solve_4x4) with active-set saturation transitions at +-RelVelClamp (up to 4 inner active-set iterations x 2 outer clutch passes). Voxel Damage Grid & Mechanical Channels: - 10x10x10 car-space grid (1,000 cells x 8 corner floats = 32,000 bytes, lazily allocated on first impact), Schlick impact falloff (max 0.95), exact algebraic rewind inverse (rewind_corner_damage), glass break threshold 0.005. - 7 car-local 3D mechanical falloff boxes map impacts into 21 rewind-serialized channels (9 vehicle-wide + 4 wheels x 3).
Subsystem breakdown#
Suspension & procedural surfaces
Preloaded raycast corners with 5-pass wall/curb filtering, asymmetric bump/rebound dampers, closing-speed bumpstops, anti-roll bars, 9 procedural SurfaceTypes.xml Bumpiness noise modes, and 3-tap soft-ground Springiness.
Two-load combined-slip tyres
Blends light (~10.2 kgf) and heavy (~1019.7 kgf) friction curves by corner load, evaluates normalized combined slip s = hypot(sx, sy) with off-road peak slip shift, and folds dFx/dω into implicit wheel inertia J_eff.
Coupled 4×4 implicit LSD & boost
Solves all 4 wheel speeds simultaneously in solve_4x4() with active-set lockup across front, rear, and radius-matched AWD center LSDs — eliminating multi-diff oscillation — alongside a 15 Hz turbo/SC boost filter and 40 ms rev cut.
Brakes, ABS, TCS, STM & yaw shaping
6-pass per-car brake calibration, steering-dependent ABS with 40 ms recovery, 2-stage TCS, asymmetric outside-wheel STM, Catmull-Rom speed-sensitive steering, and FrictionTorqueMod yaw-torque shaping.
SIMD BVH8 collision & 3D aero
MeshGround packs 4-wide BVH4 (128 B) and 8-wide BVH8 (256 B) SoA trees with hand-written AVX2+FMA+BMI1 (bvh8_avx2.asm) and ARM64 NEON (bvh8_neon.S) kernels, body spheres + GimbalLock, and pitch-AoA 3D downforce/drafting.
10×10×10 voxel damage & rewind
Lazily allocated 32 KB trilinear voxel deformation grid with Schlick falloff and an exact algebraic rewind inverse, plus 7 car-local falloff boxes driving 21 rewind-serialized mechanical damage channels.
AI drivers, wheels & UDP telemetry#
| Module | Header | Key capabilities |
|---|---|---|
| AI speed & steering planner | drive/ai_driver.h | Data-oriented SoA O(1) updates; 40-bin AiCapacity fixed-point tables from each car's actual physics; 20-bin empirical AiYawGain; 3D curvature/bank/grade friction-circle braking planner; live AI Rubberbanding toggle & strength/catchup/slowdown (0%–150%). |
| Separate USB wheels & FFB | drive/wheel_ffb.h | Multi-USB wheel base + separate pedals + H-shifter (1–7+R) + analog handbrake, 270°–1080° linear mode, physics-driven DirectInput FFB (inertia, friction, damper, aligning torque, road/engine rumble split), and ffb-trace.csv recorder. |
| Forza UDP Telemetry Out | drive/telemetry_udp.h | Streams FM7 Data Out Dash (311 B) or FH4/5 Horizon (324 B) little-endian UDP packets (default 127.0.0.1:5300) for SimHub & motion rigs, with 0.5 s pause auto-muting (IsRaceOn = 0). |
| Rewind & replays | drive/rewind.h, drive/replay.h | Rolling ring-buffer snapshots preserving pose, wheel spin/compression, boost/clutch, all 21 damage channels, and voxel impact history. |