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).
Reference for gameplay_statisticModules_watchJturn, a statistic submodule that detects J-turn maneuvers (reversing at speed, then spinning 180° to drive forward).
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 player vehicle through a state machine:
- Idle - Waiting for reverse driving at speed (≥ 8 m/s)
- Triggered - Vehicle is driving backward (direction vector · velocity < -0.96). Records the backing direction
- Completion check - While triggered, checks if the vehicle now drives forward (dot > 0.96) AND the forward direction is nearly opposite to the original backing direction (dot < -0.90)
- Timeout - If more than 4 seconds pass since the last backward detection, the attempt is cancelled
Constants
| Constant | Value | Description |
|---|---|---|
THRESHOLD | 0.96 | Dot product threshold for forward/backward alignment |
THRESHOLDUTURN | -0.90 | Dot product threshold for 180° turn completion |
REFRESH | 1 | Minimum seconds between backing direction updates |
TIMEOUT | 4 | Seconds before a triggered attempt expires |
| Minimum speed | 8 m/s | ~29 km/h minimum to begin detection |
M.onExtensionLoaded | () | - |
M.onVehicleResetted | (vid) | - |
M.onVehicleSwitched | (oldid, newid, player) | - |
M.workload | (v , vid , dtSim) | - |
Metric Recorded
On successful J-turn detection:
gameplay_statistic.metricAdd("vehicle/jturn", 1)How It Works
- The
workloadfunction is registered withgameplay_statistic.addSchedule()during module load - Each frame (round-robin), checks the player vehicle's velocity vs direction vectors
- When the vehicle drives backward fast enough, records the backing direction and enters triggered state
- If the vehicle subsequently drives forward in the opposite direction (within timeout), records a J-turn
- Vehicle resets and switches cancel any in-progress detection
Usage Example
-- This module is auto-loaded by gameplay_statistic
-- To check J-turn count:
local jturnData = gameplay_statistic.metricGet("vehicle/jturn", true)
if jturnData then
log("I", "", "J-turns: " .. jturnData.value)
endSee Also
- gameplay/statisticModules/watchRollover - Rollover Detection - Related reference
- Gameplay Systems Guide - Guide
Sites Zone
Class representing a 2D polygon zone with optional top/bottom bounding planes. Used for defining areas (parking zones, restricted areas, etc.) within the sites system.
Rollover Detection
Reference for `gameplay_statisticModules_watchRollover`, a statistic submodule that detects vehicle rollovers (flipping upside-down and back upright).