@inventure71/paddockjs 0.1.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.
Files changed (44) hide show
  1. package/AGENTS.md +86 -0
  2. package/CHANGELOG.md +5 -0
  3. package/LICENSE +176 -0
  4. package/README.md +165 -0
  5. package/assets/f1-broadcast-panel-surface.png +0 -0
  6. package/assets/f1-car-sprite-game.png +0 -0
  7. package/assets/f1-driver-helmet.png +0 -0
  8. package/assets/f1-safety-car-sprite.png +0 -0
  9. package/assets/f1-texture-asphalt.png +0 -0
  10. package/assets/f1Logo.png +0 -0
  11. package/docs/architecture.md +209 -0
  12. package/docs/concepts.md +178 -0
  13. package/docs/data_contract.md +360 -0
  14. package/docs/data_model.md +167 -0
  15. package/docs/index.md +15 -0
  16. package/docs/learnings.md +47 -0
  17. package/docs/rules.md +181 -0
  18. package/docs/system_specs.md +211 -0
  19. package/package.json +85 -0
  20. package/src/api/PaddockSimulatorController.js +233 -0
  21. package/src/app/F1SimulatorApp.js +1293 -0
  22. package/src/app/domBindings.js +64 -0
  23. package/src/config/defaultAssets.js +29 -0
  24. package/src/config/defaultOptions.js +184 -0
  25. package/src/data/championship.js +225 -0
  26. package/src/data/customFields.js +14 -0
  27. package/src/data/demoDrivers.js +134 -0
  28. package/src/data/driverData.js +71 -0
  29. package/src/data/normalizeDrivers.js +21 -0
  30. package/src/data/vehicleData.js +81 -0
  31. package/src/index.d.ts +399 -0
  32. package/src/index.js +116 -0
  33. package/src/rendering/proceduralTrackAsset.js +517 -0
  34. package/src/rendering/renderSnapshot.js +23 -0
  35. package/src/simulation/driverController.js +253 -0
  36. package/src/simulation/raceSimulation.js +933 -0
  37. package/src/simulation/simMath.js +34 -0
  38. package/src/simulation/trackModel.js +607 -0
  39. package/src/simulation/units.js +30 -0
  40. package/src/simulation/vehiclePhysics.js +104 -0
  41. package/src/standalone.js +15 -0
  42. package/src/styles.css +1818 -0
  43. package/src/ui/componentTemplates.js +229 -0
  44. package/src/ui/shellTemplate.js +43 -0
@@ -0,0 +1,517 @@
1
+ import { Container, Graphics, Texture, TilingSprite } from 'pixi.js';
2
+ import { nearestTrackState, offsetTrackPoint, pointAt, WORLD } from '../simulation/trackModel.js';
3
+
4
+ const MATERIAL_TILE_SCALE = {
5
+ asphalt: { x: 0.66, y: 0.66 },
6
+ };
7
+ const WORLD_BACKGROUND_PADDING = 2200;
8
+ const GRASS_COLOR = 0x2e7d32;
9
+ const GRAVEL_COLOR = 0xb49a68;
10
+ const ASPHALT_COLOR = 0x4a4d52;
11
+ const EDGE_REVEAL_OFFSET = 5;
12
+ const EDGE_REVEAL_WIDTH = 16;
13
+ const OUTER_BOUNDARY_OFFSET = 23;
14
+ const OUTER_BOUNDARY_WIDTH = 16;
15
+ const KERB_OFFSET = 9;
16
+ const KERB_WIDTH = 29;
17
+ const FINISH_LINE_DEPTH = 58;
18
+ const FINISH_LINE_COLUMNS = 10;
19
+ const START_GRID_SLOT_COUNT = 20;
20
+ const START_GRID_SLOT_SPACING = 82;
21
+ const START_GRID_FIRST_DISTANCE = -42;
22
+ const START_GRID_LATERAL_OFFSET = 42;
23
+ const START_GRID_BOX_LENGTH = 58;
24
+ const START_GRID_BOX_WIDTH = 34;
25
+ const SEGMENTED_STROKE_STEP = 2;
26
+ const KERB_STEP = 4;
27
+ const KERB_CURVATURE_THRESHOLD = 0.00038;
28
+ const OFFSET_SEGMENT_SAMPLE_COUNT = 7;
29
+ const NON_LOCAL_SAMPLE_STEP = 8;
30
+ const OFFSET_GAP_SAMPLE_COUNT = 4;
31
+
32
+ function makeTrackPath(track, offset = 0) {
33
+ const path = new Graphics();
34
+ const samples = track.samples.slice(0, -1);
35
+ const first = offset === 0 ? samples[0] : offsetTrackPoint(samples[0], offset);
36
+ path.moveTo(first.x, first.y);
37
+
38
+ samples.slice(1).forEach((sample) => {
39
+ const point = offset === 0 ? sample : offsetTrackPoint(sample, offset);
40
+ path.lineTo(point.x, point.y);
41
+ });
42
+ path.closePath();
43
+
44
+ return path;
45
+ }
46
+
47
+ function textureOrWhite(texture) {
48
+ return texture ?? Texture.WHITE;
49
+ }
50
+
51
+ function arcDistance(track, first, second) {
52
+ const delta = Math.abs(first - second);
53
+ return Math.min(delta, track.length - delta);
54
+ }
55
+
56
+ function offsetPointIsLocal(track, source, point, offset) {
57
+ const state = nearestTrackState(track, point);
58
+ const localTolerance = Math.max(220, Math.abs(offset) * 2.1);
59
+ const minimumEdgeDistance = Math.min(track.width / 2 - 8, Math.abs(offset) * 0.72);
60
+
61
+ return (
62
+ arcDistance(track, state.distance, source.distance) <= localTolerance &&
63
+ state.crossTrackError >= minimumEdgeDistance
64
+ );
65
+ }
66
+
67
+ function offsetPointOverlapsNonLocalRoad(track, source, point, offset) {
68
+ const localTolerance = Math.max(220, Math.abs(offset) * 2.1);
69
+ const roadBand = track.width / 2 + (track.kerbWidth ?? 0) + 8;
70
+ const roadBandSquared = (roadBand + 42) ** 2;
71
+
72
+ for (let index = 0; index < track.samples.length - 1; index += NON_LOCAL_SAMPLE_STEP) {
73
+ const sample = track.samples[index];
74
+ if (arcDistance(track, sample.distance, source.distance) <= localTolerance) continue;
75
+
76
+ const dx = point.x - sample.x;
77
+ const dy = point.y - sample.y;
78
+ if (dx * dx + dy * dy > roadBandSquared) continue;
79
+
80
+ const signedOffset = dx * sample.normalX + dy * sample.normalY;
81
+ if (Math.abs(signedOffset) <= roadBand) return true;
82
+ }
83
+
84
+ return false;
85
+ }
86
+
87
+ function interpolatedSegmentPoint(start, end, amount) {
88
+ return {
89
+ x: start.x + (end.x - start.x) * amount,
90
+ y: start.y + (end.y - start.y) * amount,
91
+ };
92
+ }
93
+
94
+ function pointDistance(first, second) {
95
+ return Math.hypot(second.x - first.x, second.y - first.y);
96
+ }
97
+
98
+ export function offsetSegmentIsSafe(track, current, next, start, end, offset) {
99
+ const centerDistance = Math.hypot(next.x - current.x, next.y - current.y);
100
+ const edgeDistance = Math.hypot(end.x - start.x, end.y - start.y);
101
+ const maxEdgeDistance = Math.max(centerDistance * 2.6, track.width * 0.58);
102
+ if (edgeDistance > maxEdgeDistance || Math.abs(offset) > track.width + track.gravelWidth + track.runoffWidth) {
103
+ return false;
104
+ }
105
+
106
+ const forwardDistance = ((next.distance - current.distance + track.length) % track.length) || centerDistance;
107
+
108
+ for (let sample = 0; sample <= OFFSET_SEGMENT_SAMPLE_COUNT; sample += 1) {
109
+ const amount = sample / OFFSET_SEGMENT_SAMPLE_COUNT;
110
+ const source = {
111
+ distance: (current.distance + forwardDistance * amount) % track.length,
112
+ };
113
+ const point = interpolatedSegmentPoint(start, end, amount);
114
+ if (!offsetPointIsLocal(track, source, point, offset)) return false;
115
+ if (offsetPointOverlapsNonLocalRoad(track, source, point, offset)) return false;
116
+ }
117
+
118
+ return true;
119
+ }
120
+
121
+ export function offsetGapBridgeIsSafe(track, start, end, width) {
122
+ const bridgeDistance = pointDistance(start, end);
123
+ const maximumBridgeDistance = Math.max(track.width * 0.08, width * 3.4);
124
+ if (bridgeDistance > maximumBridgeDistance) return false;
125
+
126
+ const minimumEdgeDistance = track.width / 2 - Math.max(width * 2.5, 10);
127
+ for (let sample = 1; sample < OFFSET_GAP_SAMPLE_COUNT; sample += 1) {
128
+ const amount = sample / OFFSET_GAP_SAMPLE_COUNT;
129
+ const point = interpolatedSegmentPoint(start, end, amount);
130
+ const state = nearestTrackState(track, point);
131
+ if (state.crossTrackError < minimumEdgeDistance) return false;
132
+ }
133
+
134
+ return true;
135
+ }
136
+
137
+ export function getOffsetStrokeSegments(track, {
138
+ side,
139
+ offset,
140
+ step = SEGMENTED_STROKE_STEP,
141
+ }) {
142
+ const samples = track.samples.slice(0, -1);
143
+ const signedOffset = side * offset;
144
+
145
+ return samples.map((current, index) => {
146
+ if (index % step !== 0) return null;
147
+
148
+ const next = samples[(index + step) % samples.length];
149
+ const start = offsetTrackPoint(current, signedOffset);
150
+ const end = offsetTrackPoint(next, signedOffset);
151
+
152
+ return {
153
+ current,
154
+ next,
155
+ start,
156
+ end,
157
+ safe: offsetSegmentIsSafe(track, current, next, start, end, signedOffset),
158
+ };
159
+ }).filter(Boolean);
160
+ }
161
+
162
+ export function getOffsetGapBridges(track, segments, width) {
163
+ if (segments.length < 2) return [];
164
+
165
+ return segments.flatMap((segment, index) => {
166
+ if (!segment.safe) return [];
167
+
168
+ const previousIndex = (index - 1 + segments.length) % segments.length;
169
+ if (segments[previousIndex].safe) return [];
170
+
171
+ let previousSafe = null;
172
+ for (let lookup = 2; lookup <= segments.length; lookup += 1) {
173
+ const candidate = segments[(index - lookup + segments.length) % segments.length];
174
+ if (!candidate.safe) continue;
175
+ previousSafe = candidate;
176
+ break;
177
+ }
178
+
179
+ if (!previousSafe || previousSafe === segment) return [];
180
+ if (!offsetGapBridgeIsSafe(track, previousSafe.end, segment.start, width)) return [];
181
+
182
+ return [{
183
+ start: previousSafe.end,
184
+ end: segment.start,
185
+ }];
186
+ });
187
+ }
188
+
189
+ function drawOffsetSegment(graphics, start, end, {
190
+ width,
191
+ color,
192
+ alpha,
193
+ cap,
194
+ }) {
195
+ graphics.moveTo(start.x, start.y);
196
+ graphics.lineTo(end.x, end.y);
197
+ graphics.stroke({
198
+ width,
199
+ color,
200
+ alpha,
201
+ join: 'round',
202
+ cap,
203
+ });
204
+ }
205
+
206
+ function drawContinuousOffsetStroke(graphics, track, {
207
+ side,
208
+ offset,
209
+ width,
210
+ color,
211
+ alpha = 1,
212
+ step = SEGMENTED_STROKE_STEP,
213
+ }) {
214
+ const samples = track.samples.slice(0, -1).filter((_, index) => index % step === 0);
215
+ if (samples.length < 2) return;
216
+
217
+ const first = offsetTrackPoint(samples[0], side * offset);
218
+ graphics.moveTo(first.x, first.y);
219
+ samples.slice(1).forEach((sample) => {
220
+ const point = offsetTrackPoint(sample, side * offset);
221
+ graphics.lineTo(point.x, point.y);
222
+ });
223
+ graphics.closePath();
224
+ graphics.stroke({
225
+ width,
226
+ color,
227
+ alpha,
228
+ join: 'round',
229
+ cap: 'round',
230
+ });
231
+ }
232
+
233
+ function drawSegmentedOffsetStroke(graphics, track, {
234
+ side,
235
+ offset,
236
+ width,
237
+ color,
238
+ alpha = 1,
239
+ step = SEGMENTED_STROKE_STEP,
240
+ cap = 'round',
241
+ }) {
242
+ const segments = getOffsetStrokeSegments(track, { side, offset, step });
243
+
244
+ segments.forEach((segment) => {
245
+ if (!segment.safe) return;
246
+ drawOffsetSegment(graphics, segment.start, segment.end, {
247
+ width,
248
+ color,
249
+ alpha,
250
+ cap,
251
+ });
252
+ });
253
+
254
+ getOffsetGapBridges(track, segments, width).forEach((bridge) => {
255
+ drawOffsetSegment(graphics, bridge.start, bridge.end, {
256
+ width,
257
+ color,
258
+ alpha,
259
+ cap: 'round',
260
+ });
261
+ });
262
+ }
263
+
264
+ export class ProceduralTrackAsset {
265
+ constructor({ textures = {}, world = WORLD } = {}) {
266
+ this.textures = textures;
267
+ this.world = world;
268
+ this.container = new Container();
269
+ }
270
+
271
+ render(track) {
272
+ this.container.removeChildren();
273
+ this.addGrass();
274
+ this.addGravelRunoff(track);
275
+ this.addBoundaryUnderlay(track);
276
+ this.addAsphalt(track);
277
+ this.addKerbs(track);
278
+ this.addBorders(track);
279
+ this.addStartingGrid(track);
280
+ this.addFinishLine(track);
281
+ }
282
+
283
+ addGrass() {
284
+ const grass = new Graphics();
285
+ grass.rect(
286
+ -WORLD_BACKGROUND_PADDING,
287
+ -WORLD_BACKGROUND_PADDING,
288
+ this.world.width + WORLD_BACKGROUND_PADDING * 2,
289
+ this.world.height + WORLD_BACKGROUND_PADDING * 2,
290
+ ).fill(GRASS_COLOR);
291
+ this.container.addChild(grass);
292
+ }
293
+
294
+ addGravelRunoff(track) {
295
+ const gravel = new Graphics();
296
+ const innerOffset = track.width / 2 + 8;
297
+ const outerOffset = track.width / 2 + track.gravelWidth;
298
+ const centerOffset = (innerOffset + outerOffset) / 2;
299
+ const width = outerOffset - innerOffset;
300
+
301
+ [-1, 1].forEach((side) => {
302
+ drawContinuousOffsetStroke(gravel, track, {
303
+ side,
304
+ offset: centerOffset,
305
+ width,
306
+ color: GRAVEL_COLOR,
307
+ step: 4,
308
+ });
309
+ });
310
+ this.container.addChild(gravel);
311
+ }
312
+
313
+ addBoundaryUnderlay(track) {
314
+ const boundary = new Graphics();
315
+ [-1, 1].forEach((side) => {
316
+ drawContinuousOffsetStroke(boundary, track, {
317
+ side,
318
+ offset: track.width / 2 + OUTER_BOUNDARY_OFFSET,
319
+ width: OUTER_BOUNDARY_WIDTH,
320
+ color: 0x090a0d,
321
+ alpha: 0.64,
322
+ step: 2,
323
+ });
324
+ });
325
+
326
+ this.drawKerbStripes(boundary, track, { clipUnsafeSegments: false });
327
+
328
+ [-1, 1].forEach((side) => {
329
+ drawContinuousOffsetStroke(boundary, track, {
330
+ side,
331
+ offset: track.width / 2 + EDGE_REVEAL_OFFSET,
332
+ width: EDGE_REVEAL_WIDTH,
333
+ color: 0xf8fafc,
334
+ alpha: 0.94,
335
+ step: 2,
336
+ });
337
+ });
338
+
339
+ this.container.addChild(boundary);
340
+ }
341
+
342
+ drawKerbStripes(kerbs, track, { clipUnsafeSegments }) {
343
+ const samples = track.samples.slice(0, -1);
344
+
345
+ for (let index = 0; index < samples.length; index += KERB_STEP) {
346
+ const sample = samples[index];
347
+ const next = samples[(index + KERB_STEP) % samples.length];
348
+ const curvature = Math.max(sample.curvature, next.curvature);
349
+ if (curvature < KERB_CURVATURE_THRESHOLD) continue;
350
+
351
+ const color = Math.floor(index / KERB_STEP) % 2 === 0 ? 0xe10600 : 0xf8fafc;
352
+ [-1, 1].forEach((side) => {
353
+ const offset = side * (track.width / 2 + KERB_OFFSET);
354
+ const start = offsetTrackPoint(sample, offset);
355
+ const end = offsetTrackPoint(next, offset);
356
+ if (clipUnsafeSegments && !offsetSegmentIsSafe(track, sample, next, start, end, offset)) return;
357
+ kerbs.moveTo(start.x, start.y);
358
+ kerbs.lineTo(end.x, end.y);
359
+ kerbs.stroke({
360
+ width: KERB_WIDTH,
361
+ color,
362
+ alpha: 1,
363
+ join: 'round',
364
+ cap: 'butt',
365
+ });
366
+ });
367
+ }
368
+ }
369
+
370
+ addMaskedMaterial({ track, texture, strokeWidth, alpha, tileScale }) {
371
+ const sprite = new TilingSprite({
372
+ texture: textureOrWhite(texture),
373
+ width: this.world.width,
374
+ height: this.world.height,
375
+ tileScale,
376
+ });
377
+ sprite.alpha = alpha;
378
+
379
+ const mask = makeTrackPath(track);
380
+ mask.stroke({
381
+ width: strokeWidth,
382
+ color: 0xffffff,
383
+ alpha: 1,
384
+ join: 'round',
385
+ cap: 'butt',
386
+ });
387
+ mask.renderable = false;
388
+ sprite.mask = mask;
389
+ this.container.addChild(sprite, mask);
390
+ }
391
+
392
+ addAsphalt(track) {
393
+ const asphaltBase = makeTrackPath(track);
394
+ asphaltBase.stroke({
395
+ width: track.width,
396
+ color: ASPHALT_COLOR,
397
+ alpha: 1,
398
+ join: 'round',
399
+ cap: 'butt',
400
+ });
401
+ this.container.addChild(asphaltBase);
402
+
403
+ this.addMaskedMaterial({
404
+ track,
405
+ texture: this.textures.asphalt,
406
+ strokeWidth: track.width,
407
+ alpha: 0.18,
408
+ tileScale: MATERIAL_TILE_SCALE.asphalt,
409
+ });
410
+
411
+ }
412
+
413
+ addBorders(track) {
414
+ const borders = new Graphics();
415
+
416
+ [-1, 1].forEach((side) => {
417
+ drawSegmentedOffsetStroke(borders, track, {
418
+ side,
419
+ offset: track.width / 2,
420
+ width: 5,
421
+ color: 0xf8fafc,
422
+ alpha: 0.86,
423
+ step: 4,
424
+ });
425
+
426
+ drawSegmentedOffsetStroke(borders, track, {
427
+ side,
428
+ offset: track.width / 2 + 15,
429
+ width: 7,
430
+ color: 0x090a0d,
431
+ alpha: 0.56,
432
+ step: 4,
433
+ });
434
+ });
435
+
436
+ this.container.addChild(borders);
437
+ }
438
+
439
+ addKerbs(track) {
440
+ const kerbs = new Graphics();
441
+ this.drawKerbStripes(kerbs, track, { clipUnsafeSegments: true });
442
+ this.container.addChild(kerbs);
443
+ }
444
+
445
+ addStartingGrid(track) {
446
+ const grid = new Graphics();
447
+
448
+ for (let index = 0; index < START_GRID_SLOT_COUNT; index += 1) {
449
+ const base = pointAt(track, START_GRID_FIRST_DISTANCE - index * START_GRID_SLOT_SPACING);
450
+ const lateralOffset = index % 2 === 0 ? -START_GRID_LATERAL_OFFSET : START_GRID_LATERAL_OFFSET;
451
+ const center = offsetTrackPoint(base, lateralOffset);
452
+ const forwardX = Math.cos(base.heading);
453
+ const forwardY = Math.sin(base.heading);
454
+ const normalX = base.normalX;
455
+ const normalY = base.normalY;
456
+ const halfLength = START_GRID_BOX_LENGTH / 2;
457
+ const halfWidth = START_GRID_BOX_WIDTH / 2;
458
+ const corners = [
459
+ {
460
+ x: center.x + forwardX * halfLength + normalX * halfWidth,
461
+ y: center.y + forwardY * halfLength + normalY * halfWidth,
462
+ },
463
+ {
464
+ x: center.x + forwardX * halfLength - normalX * halfWidth,
465
+ y: center.y + forwardY * halfLength - normalY * halfWidth,
466
+ },
467
+ {
468
+ x: center.x - forwardX * halfLength - normalX * halfWidth,
469
+ y: center.y - forwardY * halfLength - normalY * halfWidth,
470
+ },
471
+ {
472
+ x: center.x - forwardX * halfLength + normalX * halfWidth,
473
+ y: center.y - forwardY * halfLength + normalY * halfWidth,
474
+ },
475
+ ];
476
+
477
+ grid.poly(corners.flatMap((corner) => [corner.x, corner.y])).stroke({
478
+ width: 3,
479
+ color: 0xf8fafc,
480
+ alpha: 0.8,
481
+ join: 'round',
482
+ });
483
+ }
484
+
485
+ this.container.addChild(grid);
486
+ }
487
+
488
+ addFinishLine(track) {
489
+ const finishLine = new Graphics();
490
+ const halfDepth = FINISH_LINE_DEPTH / 2;
491
+ this.addFinishLineHalf(finishLine, track, -halfDepth, 0, 0);
492
+ this.addFinishLineHalf(finishLine, track, 0, halfDepth, 1);
493
+ this.container.addChild(finishLine);
494
+ }
495
+
496
+ addFinishLineHalf(graphics, track, startDistance, endDistance, rowOffset) {
497
+ const start = pointAt(track, startDistance);
498
+ const end = pointAt(track, endDistance);
499
+ const roadPadding = 10;
500
+ const width = track.width - roadPadding * 2;
501
+ const leftEdge = -width / 2;
502
+ const cellWidth = width / FINISH_LINE_COLUMNS;
503
+
504
+ for (let column = 0; column < FINISH_LINE_COLUMNS; column += 1) {
505
+ const innerOffset = leftEdge + column * cellWidth;
506
+ const outerOffset = innerOffset + cellWidth;
507
+ const color = (column + rowOffset) % 2 === 0 ? 0xf8fafc : 0x0b0d12;
508
+ const a = offsetTrackPoint(start, innerOffset);
509
+ const b = offsetTrackPoint(start, outerOffset);
510
+ const c = offsetTrackPoint(end, outerOffset);
511
+ const d = offsetTrackPoint(end, innerOffset);
512
+
513
+ graphics.poly([a.x, a.y, b.x, b.y, c.x, c.y, d.x, d.y]).fill(color);
514
+ }
515
+ }
516
+
517
+ }
@@ -0,0 +1,23 @@
1
+ import { lerp, normalizeAngle } from '../simulation/simMath.js';
2
+
3
+ function interpolateAngle(previous, current, amount) {
4
+ if (!Number.isFinite(previous)) return current;
5
+ return previous + normalizeAngle(current - previous) * amount;
6
+ }
7
+
8
+ function interpolateMovingEntity(entity, alpha) {
9
+ return {
10
+ ...entity,
11
+ x: lerp(entity.previousX ?? entity.x, entity.x, alpha),
12
+ y: lerp(entity.previousY ?? entity.y, entity.y, alpha),
13
+ heading: interpolateAngle(entity.previousHeading ?? entity.heading, entity.heading, alpha),
14
+ };
15
+ }
16
+
17
+ export function createRenderSnapshot(snapshot, alpha) {
18
+ return {
19
+ ...snapshot,
20
+ cars: snapshot.cars.map((car) => interpolateMovingEntity(car, alpha)),
21
+ safetyCar: interpolateMovingEntity(snapshot.safetyCar, alpha),
22
+ };
23
+ }