@inventure71/paddockjs 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/INSTALL_AND_UPDATE.md +4 -3
- package/README.md +194 -12
- package/docs/architecture.md +119 -44
- package/docs/concepts.md +4 -2
- package/docs/custom_model_controller.md +329 -0
- package/docs/data_contract.md +299 -17
- package/docs/index.md +1 -0
- package/docs/learnings.md +22 -0
- package/docs/rules.md +37 -10
- package/docs/system_specs.md +23 -10
- package/docs/training.md +272 -30
- package/examples/python/README.md +62 -0
- package/examples/python/base_policy_server.py +191 -0
- package/examples/python/requirements.txt +2 -0
- package/package.json +12 -4
- package/src/api/PaddockSimulatorController.js +4 -0
- package/src/app/BrowserExpertAdapter.js +367 -9
- package/src/app/F1SimulatorApp.js +305 -1603
- package/src/app/banners/bannerFormatters.js +55 -0
- package/src/app/banners/raceDataBannerController.js +141 -0
- package/src/app/banners/stewardMessageController.js +99 -0
- package/src/app/camera/cameraConstants.js +21 -0
- package/src/app/camera/cameraController.js +265 -0
- package/src/app/camera/cameraGeometry.js +176 -0
- package/src/app/domBindings.js +4 -0
- package/src/app/readouts/carOverviewRenderer.js +109 -0
- package/src/app/readouts/controlStateRenderer.js +37 -0
- package/src/app/readouts/raceStatusRenderer.js +133 -0
- package/src/app/readouts/readoutFormatters.js +58 -0
- package/src/app/readouts/telemetryRenderer.js +113 -0
- package/src/app/readouts/timingTowerRenderer.js +177 -0
- package/src/app/rendering/appAssets.js +38 -0
- package/src/app/rendering/carRenderer.js +286 -0
- package/src/app/rendering/displayUtils.js +22 -0
- package/src/app/rendering/drsTrailRenderer.js +90 -0
- package/src/app/rendering/expertSensorRenderer.js +126 -0
- package/src/app/rendering/pitLaneStatusRenderer.js +50 -0
- package/src/app/rendering/replayGhostRenderer.js +124 -0
- package/src/app/rendering/trackRenderer.js +25 -0
- package/src/app/runtime/frameLoop.js +66 -0
- package/src/app/runtime/runtimeTiming.js +34 -0
- package/src/app/runtime/runtimeVisibility.js +45 -0
- package/src/config/defaultOptions.js +20 -0
- package/src/environment/actions.js +13 -5
- package/src/environment/controllerLoop.js +246 -0
- package/src/environment/episode.js +86 -2
- package/src/environment/evaluation.js +54 -32
- package/src/environment/index.d.ts +361 -9
- package/src/environment/index.js +1 -0
- package/src/environment/metrics.js +159 -0
- package/src/environment/observationOptions.js +22 -1
- package/src/environment/observations.js +325 -40
- package/src/environment/options.js +37 -5
- package/src/environment/runtime.js +222 -21
- package/src/environment/scenarios.js +37 -8
- package/src/environment/sensors/bodySenses.js +23 -0
- package/src/environment/sensors/boundarySenses.js +17 -0
- package/src/environment/sensors/carRays.js +75 -0
- package/src/environment/sensors/contactSenses.js +32 -0
- package/src/environment/sensors/index.js +157 -0
- package/src/environment/sensors/indexedRayBands.js +103 -0
- package/src/environment/sensors/nearbyCars.js +52 -0
- package/src/environment/sensors/opponentRadar.js +65 -0
- package/src/environment/sensors/rayConfig.js +110 -0
- package/src/environment/sensors/rayDefaults.js +6 -0
- package/src/environment/sensors/rayGeometry.js +60 -0
- package/src/environment/sensors/rayGuards.js +19 -0
- package/src/environment/sensors/sensorTargets.js +60 -0
- package/src/environment/sensors/surfaceRays.js +296 -0
- package/src/environment/sensors/trackRays.js +261 -0
- package/src/environment/sensors.js +1 -367
- package/src/environment/specs.js +100 -5
- package/src/environment/workerProtocol.js +13 -1
- package/src/index.d.ts +169 -6
- package/src/index.js +5 -0
- package/src/rendering/proceduralTrackAsset.js +20 -823
- package/src/rendering/renderSnapshot.js +38 -12
- package/src/rendering/track/finishLineRenderer.js +34 -0
- package/src/rendering/track/gridRenderer.js +47 -0
- package/src/rendering/track/offsetStrokeSafety.js +136 -0
- package/src/rendering/track/pitLaneRenderer.js +248 -0
- package/src/rendering/track/trackMaterialRenderer.js +326 -0
- package/src/rendering/track/trackRenderConstants.js +38 -0
- package/src/rendering/track/trackRenderGeometry.js +94 -0
- package/src/simulation/collisionGeometry.js +1 -1
- package/src/simulation/driver/driverControlConstants.js +28 -0
- package/src/simulation/driver/driverController.js +47 -0
- package/src/simulation/driver/driverInput.js +28 -0
- package/src/simulation/driver/driverMath.js +6 -0
- package/src/simulation/driver/driverPersonality.js +25 -0
- package/src/simulation/driver/edgeRecovery.js +48 -0
- package/src/simulation/driver/racingControls.js +399 -0
- package/src/simulation/driver/racingLinePlan.js +166 -0
- package/src/simulation/driver/recoveryDynamics.js +38 -0
- package/src/simulation/driver/rejoinControls.js +213 -0
- package/src/simulation/driver/safetyCarControls.js +35 -0
- package/src/simulation/driver/trafficScan.js +51 -0
- package/src/simulation/driverController.js +4 -619
- package/src/simulation/participants/participantInteractions.js +120 -0
- package/src/simulation/pit/pitFlow.js +234 -0
- package/src/simulation/pit/pitIntent.js +92 -0
- package/src/simulation/pit/pitOccupancy.js +47 -0
- package/src/simulation/pit/pitPenaltyService.js +71 -0
- package/src/simulation/pit/pitQueue.js +55 -0
- package/src/simulation/pit/pitRouteMovement.js +160 -0
- package/src/simulation/pit/pitRouting.js +319 -0
- package/src/simulation/pit/pitService.js +41 -0
- package/src/simulation/pit/pitServiceConstants.js +21 -0
- package/src/simulation/pit/pitServiceProfile.js +45 -0
- package/src/simulation/pit/pitSnapshots.js +61 -0
- package/src/simulation/pit/pitState.js +113 -0
- package/src/simulation/pit/pitTireService.js +92 -0
- package/src/simulation/race/classification.js +104 -0
- package/src/simulation/race/gridPenalties.js +37 -0
- package/src/simulation/race/raceControlState.js +38 -0
- package/src/simulation/race/raceDistance.js +32 -0
- package/src/simulation/race/raceFinish.js +65 -0
- package/src/simulation/race/raceLifecycle.js +71 -0
- package/src/simulation/race/raceOrder.js +113 -0
- package/src/simulation/race/raceProgress.js +124 -0
- package/src/simulation/race/raceSetup.js +93 -0
- package/src/simulation/race/raceStep.js +80 -0
- package/src/simulation/race/redFlag.js +15 -0
- package/src/simulation/race/retirements.js +44 -0
- package/src/simulation/race/safetyCar.js +86 -0
- package/src/simulation/race/stalledDnf.js +86 -0
- package/src/simulation/raceSimulation.js +230 -2817
- package/src/simulation/replay/replayGhosts.js +128 -0
- package/src/simulation/rules/moduleConfig.js +72 -0
- package/src/simulation/rules/penaltyConfig.js +99 -0
- package/src/simulation/rules/penaltyEvents.js +18 -0
- package/src/simulation/rules/penaltyStats.js +26 -0
- package/src/simulation/rules/ruleConfigMerge.js +41 -0
- package/src/simulation/rules/ruleDefaults.js +85 -0
- package/src/simulation/rules/rulesReview.js +141 -0
- package/src/simulation/rules/rulesetPresets.js +47 -0
- package/src/simulation/rules/trackLimitsSteward.js +1 -1
- package/src/simulation/rulesConfig.js +5 -323
- package/src/simulation/snapshots/raceSnapshots.js +192 -0
- package/src/simulation/timing/drsTiming.js +87 -0
- package/src/simulation/timing/gapEstimation.js +38 -0
- package/src/simulation/timing/lapTelemetry.js +182 -0
- package/src/simulation/timing/raceTiming.js +15 -0
- package/src/simulation/timing/sectorPerformance.js +60 -0
- package/src/simulation/timing/timingConstants.js +5 -0
- package/src/simulation/timing/timingHistory.js +46 -0
- package/src/simulation/timing/timingLines.js +79 -0
- package/src/simulation/track/centerlineSpline.js +59 -0
- package/src/simulation/track/drsZones.js +67 -0
- package/src/simulation/track/pitLaneAccess.js +103 -0
- package/src/simulation/track/pitLaneLayout.js +395 -0
- package/src/simulation/track/pitLaneState.js +160 -0
- package/src/simulation/track/proceduralCenterline.js +237 -0
- package/src/simulation/track/sampleGeometry.js +49 -0
- package/src/simulation/track/spatialQueries.js +154 -0
- package/src/simulation/track/startStraight.js +141 -0
- package/src/simulation/track/trackConstants.js +109 -0
- package/src/simulation/track/trackGenerationOptions.js +283 -0
- package/src/simulation/track/trackMath.js +241 -0
- package/src/simulation/track/trackModel.js +240 -0
- package/src/simulation/track/trackQueryIndex.js +945 -0
- package/src/simulation/track/trackSectors.js +19 -0
- package/src/simulation/track/trackStatePolicy.js +24 -0
- package/src/simulation/track/trackValidation.js +98 -0
- package/src/simulation/trackModel.js +1 -1706
- package/src/simulation/vehicle/contactResolution.js +116 -0
- package/src/simulation/vehicle/mainTrackWheelSurface.js +82 -0
- package/src/simulation/vehicle/pitWheelSurface.js +69 -0
- package/src/simulation/vehicle/runoffResponse.js +88 -0
- package/src/simulation/vehicle/surfacePriority.js +28 -0
- package/src/simulation/vehicle/vehicleGeometry.js +247 -0
- package/src/simulation/vehicle/vehiclePhysics.js +363 -0
- package/src/simulation/vehicle/vehicleSnapshots.js +316 -0
- package/src/simulation/vehicle/vehicleState.js +186 -0
- package/src/simulation/vehicle/wheelSurface.js +150 -0
- package/src/simulation/vehicleGeometry.js +1 -247
- package/src/simulation/vehiclePhysics.js +1 -136
- package/src/simulation/warmup/runtimeWarmup.js +224 -0
- package/src/simulation/wheelSurface.js +1 -338
- package/src/styles.css +42 -0
- package/src/ui/cameraControlsTemplate.js +20 -0
- package/src/ui/carOverviewTemplate.js +33 -0
- package/src/ui/componentTemplates.js +14 -360
- package/src/ui/raceCanvasTemplate.js +69 -0
- package/src/ui/raceControlsTemplate.js +36 -0
- package/src/ui/shellTemplate.js +5 -4
- package/src/ui/telemetryDrawerTemplate.js +55 -0
- package/src/ui/telemetryTemplates.js +136 -0
- package/src/ui/timingTowerTemplate.js +34 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { buildDriverPersonality } from '../driverController.js';
|
|
2
|
+
import { clamp, normalizeAngle, seededRange } from '../simMath.js';
|
|
3
|
+
import { offsetTrackPoint, pointAt } from '../track/trackModel.js';
|
|
4
|
+
import { kphToSimSpeed, metersToSimUnits } from '../units.js';
|
|
5
|
+
import { VEHICLE_LIMITS } from './vehiclePhysics.js';
|
|
6
|
+
import { applyWheelSurfaceState } from './wheelSurface.js';
|
|
7
|
+
import { nearestTrackStateForCar } from '../track/trackStatePolicy.js';
|
|
8
|
+
import { clearCarDnf } from '../race/retirements.js';
|
|
9
|
+
|
|
10
|
+
const GRID_SLOT_SPACING = metersToSimUnits(8);
|
|
11
|
+
const ROLLING_START_SLOT_SPACING = metersToSimUnits(35);
|
|
12
|
+
const GRID_FIRST_SLOT_DISTANCE = metersToSimUnits(-6);
|
|
13
|
+
const ROLLING_START_FIRST_DISTANCE = metersToSimUnits(-30);
|
|
14
|
+
const GRID_LATERAL_OFFSET = metersToSimUnits(3.2);
|
|
15
|
+
|
|
16
|
+
export function getStartGridSlot(index, { standingStart = false } = {}) {
|
|
17
|
+
const gridDistance = standingStart
|
|
18
|
+
? GRID_FIRST_SLOT_DISTANCE - index * GRID_SLOT_SPACING
|
|
19
|
+
: ROLLING_START_FIRST_DISTANCE - index * ROLLING_START_SLOT_SPACING;
|
|
20
|
+
const gridOffset = index % 2 === 0 ? -GRID_LATERAL_OFFSET : GRID_LATERAL_OFFSET;
|
|
21
|
+
return { gridDistance, gridOffset };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createCar(driver, index, random, track, { standingStart = false, createLapTelemetry } = {}) {
|
|
25
|
+
const { gridDistance, gridOffset } = getStartGridSlot(index, { standingStart });
|
|
26
|
+
const start = pointAt(track, gridDistance);
|
|
27
|
+
const position = offsetTrackPoint(start, gridOffset);
|
|
28
|
+
const pace = clamp(driver.pace ?? seededRange(random, 0.95, 1.05), 0.88, 1.12);
|
|
29
|
+
const racecraft = clamp(driver.racecraft ?? seededRange(random, 0.65, 0.92), 0.45, 1);
|
|
30
|
+
const personality = buildDriverPersonality(driver, index, racecraft, random);
|
|
31
|
+
const launchSpeed = kphToSimSpeed(Math.max(70, 84 - index * 0.55 + pace * 4));
|
|
32
|
+
const vehicle = driver.vehicle ?? {};
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
id: driver.id ?? `car-${index + 1}`,
|
|
36
|
+
code: driver.code ?? `C${index + 1}`,
|
|
37
|
+
timingCode: driver.timingCode ?? driver.code ?? `C${index + 1}`,
|
|
38
|
+
driverNumber: driver.driverNumber ?? index + 1,
|
|
39
|
+
icon: driver.icon ?? String(index + 1).padStart(2, '0'),
|
|
40
|
+
raceName: driver.raceName ?? driver.code ?? `CAR${index + 1}`,
|
|
41
|
+
name: driver.name ?? `Car ${index + 1}`,
|
|
42
|
+
color: driver.color ?? '#e10600',
|
|
43
|
+
team: driver.team ? { ...driver.team } : null,
|
|
44
|
+
tire: driver.tire ?? ['M', 'H', 'S'][index % 3],
|
|
45
|
+
index,
|
|
46
|
+
x: position.x,
|
|
47
|
+
y: position.y,
|
|
48
|
+
previousX: position.x,
|
|
49
|
+
previousY: position.y,
|
|
50
|
+
heading: start.heading,
|
|
51
|
+
previousHeading: start.heading,
|
|
52
|
+
steeringAngle: 0,
|
|
53
|
+
yawRate: 0,
|
|
54
|
+
turnRadius: Infinity,
|
|
55
|
+
speed: standingStart ? 0 : launchSpeed,
|
|
56
|
+
longitudinalAcceleration: 0,
|
|
57
|
+
lateralAcceleration: 0,
|
|
58
|
+
lateralG: 0,
|
|
59
|
+
longitudinalG: 0,
|
|
60
|
+
gripUsage: 0,
|
|
61
|
+
slipAngleRadians: 0,
|
|
62
|
+
tractionLimited: false,
|
|
63
|
+
stabilityState: 'stable',
|
|
64
|
+
throttle: 0,
|
|
65
|
+
brake: 0,
|
|
66
|
+
vehicleId: vehicle.id ?? null,
|
|
67
|
+
vehicleName: vehicle.name ?? null,
|
|
68
|
+
vehicleRatings: vehicle.ratings ? { ...vehicle.ratings } : null,
|
|
69
|
+
mass: vehicle.mass ?? 798 + seededRange(random, -5, 5),
|
|
70
|
+
powerNewtons: vehicle.powerNewtons ?? 43000 * pace,
|
|
71
|
+
brakeNewtons: vehicle.brakeNewtons ?? 59000,
|
|
72
|
+
dragCoefficient: vehicle.dragCoefficient ?? 0.33 + seededRange(random, -0.026, 0.026),
|
|
73
|
+
downforceCoefficient: vehicle.downforceCoefficient ?? 6.1 + seededRange(random, -0.18, 0.18),
|
|
74
|
+
tireGrip: vehicle.tireGrip ?? 2.22 + racecraft * 0.28 + seededRange(random, -0.03, 0.03),
|
|
75
|
+
tireCare: vehicle.tireCare ?? 1,
|
|
76
|
+
pace,
|
|
77
|
+
racecraft,
|
|
78
|
+
personality,
|
|
79
|
+
aggression: personality.baseAggression,
|
|
80
|
+
gridLocked: standingStart,
|
|
81
|
+
gridDistance,
|
|
82
|
+
gridOffset,
|
|
83
|
+
desiredOffset: gridOffset,
|
|
84
|
+
progress: start.distance,
|
|
85
|
+
raceDistance: gridDistance,
|
|
86
|
+
lap: 1,
|
|
87
|
+
rank: index + 1,
|
|
88
|
+
gapAhead: Infinity,
|
|
89
|
+
gapAheadSeconds: Infinity,
|
|
90
|
+
leaderGapSeconds: 0,
|
|
91
|
+
intervalAheadSeconds: Infinity,
|
|
92
|
+
gapAheadLaps: 0,
|
|
93
|
+
intervalAheadLaps: 0,
|
|
94
|
+
leaderGapLaps: 0,
|
|
95
|
+
drsEligible: false,
|
|
96
|
+
drsActive: false,
|
|
97
|
+
drsZoneId: null,
|
|
98
|
+
drsZoneEnabled: false,
|
|
99
|
+
timingHistory: [],
|
|
100
|
+
lapTelemetry: createLapTelemetry?.(track, 0, gridDistance) ?? null,
|
|
101
|
+
drsDetection: {},
|
|
102
|
+
canAttack: true,
|
|
103
|
+
trackState: start,
|
|
104
|
+
contactCooldown: 0,
|
|
105
|
+
tireEnergy: 100,
|
|
106
|
+
usedTireCompounds: [driver.tire ?? ['M', 'H', 'S'][index % 3]],
|
|
107
|
+
automaticPitIntentEnabled: true,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function clearCarDrsState(car) {
|
|
112
|
+
car.drsZoneId = null;
|
|
113
|
+
car.drsZoneEnabled = false;
|
|
114
|
+
car.drsActive = false;
|
|
115
|
+
car.drsEligible = false;
|
|
116
|
+
car.drsDetection = {};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function applyExternalCarState(car, partial, context) {
|
|
120
|
+
const {
|
|
121
|
+
cars,
|
|
122
|
+
computeLap,
|
|
123
|
+
nearestDistanceOnRoute,
|
|
124
|
+
progressDelta,
|
|
125
|
+
raceControl,
|
|
126
|
+
releaseRaceStart,
|
|
127
|
+
resetLapTelemetry,
|
|
128
|
+
resetTimingHistory,
|
|
129
|
+
resetTimingLineCrossings,
|
|
130
|
+
time,
|
|
131
|
+
totalLaps,
|
|
132
|
+
track,
|
|
133
|
+
} = context;
|
|
134
|
+
const hasExplicitRaceDistance = partial.raceDistance != null;
|
|
135
|
+
const nextPartial = { ...partial };
|
|
136
|
+
if (raceControl.finished && (car.destroyed || car.outOfRace)) {
|
|
137
|
+
if (partial.destroyed === false) nextPartial.destroyed = car.destroyed;
|
|
138
|
+
if (partial.outOfRace === false) nextPartial.outOfRace = car.outOfRace;
|
|
139
|
+
}
|
|
140
|
+
Object.assign(car, nextPartial);
|
|
141
|
+
if (
|
|
142
|
+
!raceControl.finished &&
|
|
143
|
+
nextPartial.destroyed === false &&
|
|
144
|
+
nextPartial.outOfRace === false
|
|
145
|
+
) {
|
|
146
|
+
clearCarDnf(car);
|
|
147
|
+
car.destroyReason = null;
|
|
148
|
+
car.destroyedAt = null;
|
|
149
|
+
car.dnfReason = null;
|
|
150
|
+
car.dnfAt = null;
|
|
151
|
+
}
|
|
152
|
+
if (
|
|
153
|
+
nextPartial.x != null ||
|
|
154
|
+
nextPartial.y != null ||
|
|
155
|
+
nextPartial.progress != null ||
|
|
156
|
+
nextPartial.raceDistance != null
|
|
157
|
+
) {
|
|
158
|
+
car.gridLocked = false;
|
|
159
|
+
if (raceControl.mode === 'pre-start' && cars.every((item) => !item.gridLocked)) {
|
|
160
|
+
releaseRaceStart();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
car.speed = clamp(car.speed, 0, VEHICLE_LIMITS.maxSpeed);
|
|
164
|
+
car.heading = normalizeAngle(car.heading);
|
|
165
|
+
const centerState = nearestTrackStateForCar(track, car, car, car.progress ?? car.raceDistance);
|
|
166
|
+
applyWheelSurfaceState(car, track, { centerState });
|
|
167
|
+
if (
|
|
168
|
+
nextPartial.desiredOffset == null &&
|
|
169
|
+
(nextPartial.x != null || nextPartial.y != null || nextPartial.progress != null || nextPartial.raceDistance != null)
|
|
170
|
+
) {
|
|
171
|
+
car.desiredOffset = centerState.signedOffset ?? 0;
|
|
172
|
+
}
|
|
173
|
+
if ((nextPartial.x != null || nextPartial.y != null) && car.pitStop?.route) {
|
|
174
|
+
car.pitStop.routeProgress = nearestDistanceOnRoute(car.pitStop.route, car, car.pitStop.routeProgress ?? 0);
|
|
175
|
+
}
|
|
176
|
+
if (!hasExplicitRaceDistance) {
|
|
177
|
+
const delta = progressDelta(car.trackState.distance, car.progress ?? car.trackState.distance, track.length);
|
|
178
|
+
car.raceDistance = (car.raceDistance ?? car.trackState.distance) + delta;
|
|
179
|
+
}
|
|
180
|
+
car.progress = car.trackState.distance;
|
|
181
|
+
car.lap = computeLap(car.raceDistance);
|
|
182
|
+
clearCarDrsState(car);
|
|
183
|
+
resetTimingHistory(car, time);
|
|
184
|
+
resetTimingLineCrossings(car, time);
|
|
185
|
+
resetLapTelemetry(car, time, track, totalLaps);
|
|
186
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { getVehicleGeometryState } from './vehicleGeometry.js';
|
|
2
|
+
import { analyticWheelState, wheelFullyOutside } from './mainTrackWheelSurface.js';
|
|
3
|
+
import { analyticPitWheelState, canUseAnalyticPitWheels, isNearPitConnector, patchSamples } from './pitWheelSurface.js';
|
|
4
|
+
import { getEffectiveSurface, worstState } from './surfacePriority.js';
|
|
5
|
+
import { nearestTrackStateForCar, pitOverrideAllowedForCar } from '../track/trackStatePolicy.js';
|
|
6
|
+
|
|
7
|
+
function finiteSignatureValue(value) {
|
|
8
|
+
return Number.isFinite(value) ? Number(value).toFixed(4) : '';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function centerStateSignature(centerState) {
|
|
12
|
+
if (!centerState) return 'auto';
|
|
13
|
+
return [
|
|
14
|
+
finiteSignatureValue(centerState.distance),
|
|
15
|
+
finiteSignatureValue(centerState.signedOffset),
|
|
16
|
+
finiteSignatureValue(centerState.crossTrackError),
|
|
17
|
+
centerState.surface ?? '',
|
|
18
|
+
centerState.inPitLane ? 1 : 0,
|
|
19
|
+
centerState.pitLanePart ?? '',
|
|
20
|
+
centerState.pitBoxId ?? '',
|
|
21
|
+
].join(':');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { getEffectiveSurface } from './surfacePriority.js';
|
|
25
|
+
|
|
26
|
+
export function isWholeCarOutsideTrackLimits(wheels = [], track, relaxedMargin = 0) {
|
|
27
|
+
if (!wheels.length || !track) {
|
|
28
|
+
return {
|
|
29
|
+
violating: false,
|
|
30
|
+
side: 0,
|
|
31
|
+
outsideBy: 0,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const trackLimit = track.width / 2 + relaxedMargin;
|
|
35
|
+
const right = wheels.every((wheel) => wheel.outsideSide === 1);
|
|
36
|
+
const left = wheels.every((wheel) => wheel.outsideSide === -1);
|
|
37
|
+
if (!right && !left) {
|
|
38
|
+
return {
|
|
39
|
+
violating: false,
|
|
40
|
+
side: 0,
|
|
41
|
+
outsideBy: 0,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const side = right ? 1 : -1;
|
|
45
|
+
const outsideBy = right
|
|
46
|
+
? Math.min(...wheels.map((wheel) => wheel.minimumSignedOffset)) - trackLimit
|
|
47
|
+
: -trackLimit - Math.max(...wheels.map((wheel) => wheel.maximumSignedOffset));
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
violating: outsideBy > 0,
|
|
51
|
+
side,
|
|
52
|
+
outsideBy: Math.max(0, outsideBy),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function calculateWheelSurfaceState({ car, track, centerState: providedCenterState = null }) {
|
|
57
|
+
const geometry = getVehicleGeometryState(car);
|
|
58
|
+
const trackLimit = track.width / 2;
|
|
59
|
+
const allowPitOverride = pitOverrideAllowedForCar(car);
|
|
60
|
+
const centerState = providedCenterState == null
|
|
61
|
+
? nearestTrackStateForCar(track, car)
|
|
62
|
+
: !allowPitOverride && providedCenterState.inPitLane
|
|
63
|
+
? nearestTrackStateForCar(track, car, car, car.progress, { allowPitOverride: false })
|
|
64
|
+
: providedCenterState;
|
|
65
|
+
const useAnalyticPitSampling = canUseAnalyticPitWheels(geometry, centerState);
|
|
66
|
+
const useFullSampling = !useAnalyticPitSampling && Boolean(centerState.inPitLane || isNearPitConnector(track, centerState));
|
|
67
|
+
const wheels = useAnalyticPitSampling
|
|
68
|
+
? geometry.contactPatches.map((patch) => analyticPitWheelState(patch, centerState))
|
|
69
|
+
: useFullSampling
|
|
70
|
+
? geometry.contactPatches.map((patch) => {
|
|
71
|
+
const sampleState = (point) => nearestTrackStateForCar(track, car, point, car.progress);
|
|
72
|
+
const sampledStates = patchSamples(patch).map(sampleState);
|
|
73
|
+
const state = worstState(sampledStates);
|
|
74
|
+
const offsets = sampledStates.map((sample) => sample.signedOffset);
|
|
75
|
+
const outside = wheelFullyOutside(sampledStates, trackLimit);
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
id: patch.id,
|
|
79
|
+
x: patch.center.x,
|
|
80
|
+
y: patch.center.y,
|
|
81
|
+
signedOffset: state.signedOffset,
|
|
82
|
+
crossTrackError: state.crossTrackError,
|
|
83
|
+
surface: state.surface,
|
|
84
|
+
onTrack: Boolean(state.onTrack),
|
|
85
|
+
inPitLane: Boolean(state.inPitLane),
|
|
86
|
+
pitLanePart: state.pitLanePart ?? null,
|
|
87
|
+
pitBoxId: state.pitBoxId ?? null,
|
|
88
|
+
minimumSignedOffset: Math.min(...offsets),
|
|
89
|
+
maximumSignedOffset: Math.max(...offsets),
|
|
90
|
+
fullyOutsideWhiteLine: outside.fullyOutsideWhiteLine,
|
|
91
|
+
outsideSide: outside.outsideSide,
|
|
92
|
+
sampledStates,
|
|
93
|
+
};
|
|
94
|
+
})
|
|
95
|
+
: geometry.contactPatches.map((patch) => analyticWheelState(patch, centerState, track, trackLimit));
|
|
96
|
+
const effectiveSurface = getEffectiveSurface(wheels);
|
|
97
|
+
const representative = wheels.reduce((best, wheel) => (
|
|
98
|
+
Math.abs(wheel.signedOffset) > Math.abs(best.signedOffset) ? wheel : best
|
|
99
|
+
), wheels[0]);
|
|
100
|
+
const trackLimits = isWholeCarOutsideTrackLimits(wheels, track);
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
wheels,
|
|
104
|
+
effectiveSurface,
|
|
105
|
+
trackLimits,
|
|
106
|
+
sampleMode: useAnalyticPitSampling ? 'pit-analytic' : useFullSampling ? 'full' : 'analytic',
|
|
107
|
+
representativeState: representative ? {
|
|
108
|
+
...centerState,
|
|
109
|
+
surface: effectiveSurface,
|
|
110
|
+
onTrack: wheels.every((wheel) => wheel.onTrack || wheel.inPitLane),
|
|
111
|
+
inPitLane: wheels.every((wheel) => wheel.inPitLane),
|
|
112
|
+
signedOffset: representative.signedOffset,
|
|
113
|
+
crossTrackError: representative.crossTrackError,
|
|
114
|
+
} : nearestTrackStateForCar(track, car),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function applyWheelSurfaceState(car, track, options = {}) {
|
|
119
|
+
const geometry = getVehicleGeometryState(car);
|
|
120
|
+
const cacheKey = {
|
|
121
|
+
track,
|
|
122
|
+
geometrySignature: geometry.signature,
|
|
123
|
+
centerSignature: centerStateSignature(options.centerState),
|
|
124
|
+
};
|
|
125
|
+
const cached = car.wheelSurfaceCache;
|
|
126
|
+
if (
|
|
127
|
+
cached?.track === cacheKey.track &&
|
|
128
|
+
cached.geometrySignature === cacheKey.geometrySignature &&
|
|
129
|
+
cached.centerSignature === cacheKey.centerSignature
|
|
130
|
+
) {
|
|
131
|
+
car.wheelStates = cached.result.wheels;
|
|
132
|
+
car.trackLimitState = cached.result.trackLimits;
|
|
133
|
+
car.trackState = cached.trackState;
|
|
134
|
+
return cached.result;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const result = calculateWheelSurfaceState({ car, track, centerState: options.centerState });
|
|
138
|
+
car.wheelStates = result.wheels;
|
|
139
|
+
car.trackLimitState = result.trackLimits;
|
|
140
|
+
car.trackState = {
|
|
141
|
+
...result.representativeState,
|
|
142
|
+
wheelSurface: result.effectiveSurface,
|
|
143
|
+
};
|
|
144
|
+
car.wheelSurfaceCache = {
|
|
145
|
+
...cacheKey,
|
|
146
|
+
result,
|
|
147
|
+
trackState: car.trackState,
|
|
148
|
+
};
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
@@ -1,247 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
REAL_F1_CAR_LENGTH_METERS,
|
|
4
|
-
REAL_F1_CAR_WIDTH_METERS,
|
|
5
|
-
REAL_F1_WHEELBASE_METERS,
|
|
6
|
-
metersToSimUnits,
|
|
7
|
-
} from './units.js';
|
|
8
|
-
|
|
9
|
-
export const VEHICLE_GEOMETRY = {
|
|
10
|
-
visualLength: metersToSimUnits(REAL_F1_CAR_LENGTH_METERS),
|
|
11
|
-
visualWidth: metersToSimUnits(REAL_F1_CAR_WIDTH_METERS),
|
|
12
|
-
bodyLength: metersToSimUnits(4.65),
|
|
13
|
-
bodyWidth: metersToSimUnits(1.24),
|
|
14
|
-
wheelLength: metersToSimUnits(0.72),
|
|
15
|
-
wheelWidth: metersToSimUnits(0.38),
|
|
16
|
-
wheelLongitudinalOffset: metersToSimUnits(REAL_F1_WHEELBASE_METERS / 2),
|
|
17
|
-
wheelLateralOffset: metersToSimUnits((REAL_F1_CAR_WIDTH_METERS - 0.38) / 2),
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const WHEEL_SPECS = [
|
|
21
|
-
{ id: 'front-left', longitudinal: 1, lateral: -1 },
|
|
22
|
-
{ id: 'front-right', longitudinal: 1, lateral: 1 },
|
|
23
|
-
{ id: 'rear-left', longitudinal: -1, lateral: -1 },
|
|
24
|
-
{ id: 'rear-right', longitudinal: -1, lateral: 1 },
|
|
25
|
-
];
|
|
26
|
-
|
|
27
|
-
function finiteOr(value, fallback) {
|
|
28
|
-
return Number.isFinite(value) ? value : fallback;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function vehiclePose(car, { previous = false } = {}) {
|
|
32
|
-
const x = previous ? finiteOr(car.previousX, car.x) : car.x;
|
|
33
|
-
const y = previous ? finiteOr(car.previousY, car.y) : car.y;
|
|
34
|
-
const heading = previous ? finiteOr(car.previousHeading, car.heading) : car.heading;
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
x: finiteOr(x, 0),
|
|
38
|
-
y: finiteOr(y, 0),
|
|
39
|
-
heading: normalizeAngle(finiteOr(heading, 0)),
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function vehicleAxes(heading) {
|
|
44
|
-
const cos = Math.cos(heading);
|
|
45
|
-
const sin = Math.sin(heading);
|
|
46
|
-
return {
|
|
47
|
-
forward: { x: cos, y: sin },
|
|
48
|
-
right: { x: -sin, y: cos },
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function offsetPoint(center, axes, longitudinal, lateral) {
|
|
53
|
-
return {
|
|
54
|
-
x: center.x + axes.forward.x * longitudinal + axes.right.x * lateral,
|
|
55
|
-
y: center.y + axes.forward.y * longitudinal + axes.right.y * lateral,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function createOrientedRect({ id, type, center, heading, length, width }) {
|
|
60
|
-
const axes = vehicleAxes(heading);
|
|
61
|
-
const halfLength = length / 2;
|
|
62
|
-
const halfWidth = width / 2;
|
|
63
|
-
const corners = [
|
|
64
|
-
offsetPoint(center, axes, halfLength, halfWidth),
|
|
65
|
-
offsetPoint(center, axes, halfLength, -halfWidth),
|
|
66
|
-
offsetPoint(center, axes, -halfLength, -halfWidth),
|
|
67
|
-
offsetPoint(center, axes, -halfLength, halfWidth),
|
|
68
|
-
];
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
id,
|
|
72
|
-
type,
|
|
73
|
-
center,
|
|
74
|
-
heading,
|
|
75
|
-
length,
|
|
76
|
-
width,
|
|
77
|
-
halfLength,
|
|
78
|
-
halfWidth,
|
|
79
|
-
forward: axes.forward,
|
|
80
|
-
right: axes.right,
|
|
81
|
-
corners,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function createVehicleGeometry(car, options = {}) {
|
|
86
|
-
const pose = vehiclePose(car, options);
|
|
87
|
-
const axes = vehicleAxes(pose.heading);
|
|
88
|
-
const center = { x: pose.x, y: pose.y };
|
|
89
|
-
const body = createOrientedRect({
|
|
90
|
-
id: 'body',
|
|
91
|
-
type: 'body',
|
|
92
|
-
center,
|
|
93
|
-
heading: pose.heading,
|
|
94
|
-
length: VEHICLE_GEOMETRY.bodyLength,
|
|
95
|
-
width: VEHICLE_GEOMETRY.bodyWidth,
|
|
96
|
-
});
|
|
97
|
-
const wheels = WHEEL_SPECS.map((spec) => {
|
|
98
|
-
const wheelCenter = offsetPoint(
|
|
99
|
-
center,
|
|
100
|
-
axes,
|
|
101
|
-
spec.longitudinal * VEHICLE_GEOMETRY.wheelLongitudinalOffset,
|
|
102
|
-
spec.lateral * VEHICLE_GEOMETRY.wheelLateralOffset,
|
|
103
|
-
);
|
|
104
|
-
return createOrientedRect({
|
|
105
|
-
id: spec.id,
|
|
106
|
-
type: 'wheel',
|
|
107
|
-
center: wheelCenter,
|
|
108
|
-
heading: pose.heading,
|
|
109
|
-
length: VEHICLE_GEOMETRY.wheelLength,
|
|
110
|
-
width: VEHICLE_GEOMETRY.wheelWidth,
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
return {
|
|
115
|
-
body,
|
|
116
|
-
wheels,
|
|
117
|
-
contactPatches: wheels,
|
|
118
|
-
shapes: [body, ...wheels],
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function geometryPoseSignature(pose) {
|
|
123
|
-
return [
|
|
124
|
-
pose.x,
|
|
125
|
-
pose.y,
|
|
126
|
-
pose.heading,
|
|
127
|
-
pose.previousX,
|
|
128
|
-
pose.previousY,
|
|
129
|
-
pose.previousHeading,
|
|
130
|
-
].join(':');
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function geometryPose(car) {
|
|
134
|
-
const x = finiteOr(car.x, 0);
|
|
135
|
-
const y = finiteOr(car.y, 0);
|
|
136
|
-
const heading = normalizeAngle(finiteOr(car.heading, 0));
|
|
137
|
-
return {
|
|
138
|
-
x,
|
|
139
|
-
y,
|
|
140
|
-
heading,
|
|
141
|
-
previousX: finiteOr(car.previousX, x),
|
|
142
|
-
previousY: finiteOr(car.previousY, y),
|
|
143
|
-
previousHeading: normalizeAngle(finiteOr(car.previousHeading, heading)),
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function geometryStateMatches(car, state) {
|
|
148
|
-
const pose = state?.pose;
|
|
149
|
-
if (!pose) return false;
|
|
150
|
-
const x = finiteOr(car.x, 0);
|
|
151
|
-
const y = finiteOr(car.y, 0);
|
|
152
|
-
const heading = normalizeAngle(finiteOr(car.heading, 0));
|
|
153
|
-
return (
|
|
154
|
-
pose.x === x &&
|
|
155
|
-
pose.y === y &&
|
|
156
|
-
pose.heading === heading &&
|
|
157
|
-
pose.previousX === finiteOr(car.previousX, x) &&
|
|
158
|
-
pose.previousY === finiteOr(car.previousY, y) &&
|
|
159
|
-
pose.previousHeading === normalizeAngle(finiteOr(car.previousHeading, heading))
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function createVehicleShapeAabb(shape) {
|
|
164
|
-
let minX = Infinity;
|
|
165
|
-
let maxX = -Infinity;
|
|
166
|
-
let minY = Infinity;
|
|
167
|
-
let maxY = -Infinity;
|
|
168
|
-
for (let index = 0; index < shape.corners.length; index += 1) {
|
|
169
|
-
const corner = shape.corners[index];
|
|
170
|
-
if (corner.x < minX) minX = corner.x;
|
|
171
|
-
if (corner.x > maxX) maxX = corner.x;
|
|
172
|
-
if (corner.y < minY) minY = corner.y;
|
|
173
|
-
if (corner.y > maxY) maxY = corner.y;
|
|
174
|
-
}
|
|
175
|
-
return {
|
|
176
|
-
minX,
|
|
177
|
-
maxX,
|
|
178
|
-
minY,
|
|
179
|
-
maxY,
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
export function mergeAabbs(aabbs) {
|
|
184
|
-
let minX = Infinity;
|
|
185
|
-
let maxX = -Infinity;
|
|
186
|
-
let minY = Infinity;
|
|
187
|
-
let maxY = -Infinity;
|
|
188
|
-
for (let index = 0; index < aabbs.length; index += 1) {
|
|
189
|
-
const aabb = aabbs[index];
|
|
190
|
-
if (aabb.minX < minX) minX = aabb.minX;
|
|
191
|
-
if (aabb.maxX > maxX) maxX = aabb.maxX;
|
|
192
|
-
if (aabb.minY < minY) minY = aabb.minY;
|
|
193
|
-
if (aabb.maxY > maxY) maxY = aabb.maxY;
|
|
194
|
-
}
|
|
195
|
-
return { minX, maxX, minY, maxY };
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
export function createVehicleGeometryState(car) {
|
|
199
|
-
const pose = geometryPose(car);
|
|
200
|
-
const current = createVehicleGeometry(car);
|
|
201
|
-
const previous = createVehicleGeometry(car, { previous: true });
|
|
202
|
-
const bodyAabb = createVehicleShapeAabb(current.body);
|
|
203
|
-
const previousBodyAabb = createVehicleShapeAabb(previous.body);
|
|
204
|
-
const sweptBodyAabb = mergeAabbs([previousBodyAabb, bodyAabb]);
|
|
205
|
-
|
|
206
|
-
return {
|
|
207
|
-
pose,
|
|
208
|
-
signature: geometryPoseSignature(pose),
|
|
209
|
-
current,
|
|
210
|
-
previous,
|
|
211
|
-
body: current.body,
|
|
212
|
-
wheels: current.wheels,
|
|
213
|
-
contactPatches: current.contactPatches,
|
|
214
|
-
bodyAabb,
|
|
215
|
-
previousBodyAabb,
|
|
216
|
-
sweptBodyAabb,
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export function getVehicleGeometryState(car) {
|
|
221
|
-
if (geometryStateMatches(car, car.geometryState)) return car.geometryState;
|
|
222
|
-
car.geometryState = createVehicleGeometryState(car);
|
|
223
|
-
return car.geometryState;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
export function createVehicleAabb(car, options = {}) {
|
|
227
|
-
const geometry = createVehicleGeometry(car, options);
|
|
228
|
-
return mergeAabbs(geometry.shapes.map(createVehicleShapeAabb));
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export function interpolateVehiclePose(car, amount) {
|
|
232
|
-
const previousX = finiteOr(car.previousX, car.x);
|
|
233
|
-
const previousY = finiteOr(car.previousY, car.y);
|
|
234
|
-
const previousHeading = finiteOr(car.previousHeading, car.heading);
|
|
235
|
-
const headingDelta = normalizeAngle(finiteOr(car.heading, 0) - previousHeading);
|
|
236
|
-
|
|
237
|
-
return {
|
|
238
|
-
...car,
|
|
239
|
-
x: previousX + (finiteOr(car.x, previousX) - previousX) * amount,
|
|
240
|
-
y: previousY + (finiteOr(car.y, previousY) - previousY) * amount,
|
|
241
|
-
heading: normalizeAngle(previousHeading + headingDelta * amount),
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
export function getCarCorners(car) {
|
|
246
|
-
return createVehicleGeometry(car).body.corners;
|
|
247
|
-
}
|
|
1
|
+
export * from './vehicle/vehicleGeometry.js';
|