Rollover Detection
Reference for `gameplay_statisticModules_watchRollover`, a statistic submodule that detects vehicle rollovers (flipping upside-down and back upright).
Reference for gameplay_statisticModules_watchRollover, a statistic submodule that detects vehicle rollovers (flipping upside-down and back upright).
Module Exports (M)
| Function | Signature | Description |
|---|---|---|
workload | (vehObj, vehId, dtSim) | Per-frame detection function called by the statistic scheduler |
Hooks (Event Handlers)
| Hook | Description |
|---|---|
onExtensionLoaded | No-op placeholder |
onVehicleResetted | Cancels detection if tracked vehicle resets |
onVehicleSwitched | Cancels detection on vehicle switch |
Internals
Detection Algorithm
The module tracks the vehicle's up-vector Z component through a state machine:
- Upright - Vehicle's up-vector Z > 0.7 (roughly right-side-up). Records the upright timestamp
- Roof - Vehicle's up-vector Z < -0.7 (upside-down) AND moving more sideways than forward (
|dir·vel| < |right·vel|). Enters triggered state - Completion - When upright again after being on the roof, checks that roof was within 4s and upright was within 8s. If both pass → rollover counted
- Timeout - If conditions aren't met within timeouts, the attempt resets
Constants
| Constant | Value | Description |
|---|---|---|
THRESHOLD | 0.7 | Up-vector Z threshold for upright/roof detection |
REFRESH | 1 | Minimum seconds between state timestamp updates |
TIMEOUT | 4 | Seconds timeout for roof-to-upright transition |
M.onExtensionLoaded | () | - |
M.onVehicleResetted | (vid) | - |
M.onVehicleSwitched | (oldid, newid, player) | - |
M.workload | (v , vid , dtSim) | - |
Metric Recorded
On successful rollover detection:
gameplay_statistic.metricAdd("vehicle/rollover", 1)How It Works
- The
workloadfunction is registered withgameplay_statistic.addSchedule()during module load - Each frame (round-robin), computes the vehicle's up, direction, velocity, and right vectors
- Tracks transitions between upright → roof → upright within time windows
- The sideways velocity check prevents false positives from driving on steep slopes
- Vehicle resets and switches cancel any in-progress detection
Usage Example
-- This module is auto-loaded by gameplay_statistic
-- To check rollover count:
local rollData = gameplay_statistic.metricGet("vehicle/rollover", true)
if rollData then
log("I", "", "Rollovers: " .. rollData.value)
endSee Also
- gameplay/statisticModules/watchJturn - J-Turn Detection - Related reference
- Gameplay Systems Guide - Guide
J-Turn Detection
Reference for `gameplay_statisticModules_watchJturn`, a statistic submodule that detects J-turn maneuvers (reversing at speed, then spinning 180° to drive forward).
Base Traffic Role
Reference for `gameplay_traffic_baseRole`, the base class for all traffic vehicle driver roles. Provides personality generation, action management, flowgraph support, and common utility methods inheri