File size: 2,787 Bytes
00a81c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as THREE from 'three';

export const START_GRID_Y = 0.28;
export const START_GRID_LATERAL_OFFSET = 2.35;
export const START_GRID_ROW_SPACING = 6.0;
export const START_GRID_INITIAL_OFFSET = 8.0;
export const MULTIPLAYER_COUNTDOWN_MS = 3600;
export const START_SYNC_GRACE_MS = 5000;
export const START_SYNC_ANCHOR_TOLERANCE = 24;
export const CENTER_FALLBACK_RADIUS = 18;

const TRACK_POINTS = [
  new THREE.Vector3(0, 0, 100),
  new THREE.Vector3(50, 0, 90),
  new THREE.Vector3(80, 0, 60),
  new THREE.Vector3(120, 0, 40),
  new THREE.Vector3(120, 0, -20),
  new THREE.Vector3(105, 0, -50),
  new THREE.Vector3(120, 0, -80),
  new THREE.Vector3(120, 0, -140),
  new THREE.Vector3(70, 0, -140),
  new THREE.Vector3(70, 0, -70),
  new THREE.Vector3(10, 0, -70),
  new THREE.Vector3(10, 0, -140),
  new THREE.Vector3(-40, 0, -140),
  new THREE.Vector3(-80, 0, -120),
  new THREE.Vector3(-140, 0, -80),
  new THREE.Vector3(-150, 0, -20),
  new THREE.Vector3(-110, 0, 30),
  new THREE.Vector3(-140, 0, 70),
  new THREE.Vector3(-90, 0, 110),
  new THREE.Vector3(-40, 0, 100),
];

const SHARED_CURVE = new THREE.CatmullRomCurve3(TRACK_POINTS, true, 'centripetal', 0.5);
const SHARED_TRACK_LENGTH = SHARED_CURVE.getLength();
const UP = new THREE.Vector3(0, 1, 0);

function normalizeSlotIndex(slotIndex = 0) {
  if (!Number.isFinite(slotIndex)) return 0;
  return Math.max(0, Math.floor(slotIndex));
}

export function getStartTransformForSlot(slotIndex = 0) {
  const normalizedSlot = normalizeSlotIndex(slotIndex);
  const row = Math.floor(normalizedSlot / 2);
  const side = normalizedSlot % 2 === 0 ? -1 : 1;
  const baseT = (1 - ((row * START_GRID_ROW_SPACING + START_GRID_INITIAL_OFFSET) / SHARED_TRACK_LENGTH) + 1) % 1;
  const point = SHARED_CURVE.getPointAt(baseT);
  const tangent = SHARED_CURVE.getTangentAt(baseT).normalize();
  const lateral = new THREE.Vector3().crossVectors(UP, tangent).normalize();
  const heading = Math.atan2(tangent.x, tangent.z);
  const position = point.clone().addScaledVector(lateral, side * START_GRID_LATERAL_OFFSET).setY(START_GRID_Y);
  return {
    slotIndex: normalizedSlot,
    progress: baseT,
    heading,
    position: { x: position.x, y: position.y, z: position.z },
  };
}

export function isNearCenterPosition(position, radius = CENTER_FALLBACK_RADIUS) {
  if (!position) return true;
  const x = Number(position.x ?? 0);
  const z = Number(position.z ?? 0);
  return Number.isFinite(x) && Number.isFinite(z) ? Math.hypot(x, z) <= radius : true;
}

export function distanceBetweenPositions(a, b) {
  if (!a || !b) return Number.POSITIVE_INFINITY;
  const dx = Number(a.x ?? 0) - Number(b.x ?? 0);
  const dy = Number(a.y ?? 0) - Number(b.y ?? 0);
  const dz = Number(a.z ?? 0) - Number(b.z ?? 0);
  return Math.hypot(dx, dy, dz);
}