// Everything else that moves: F-line streetcars on the Embarcadero, cable cars on California // St, and cars on a loop through the neighborhood. Pure functions of time: a seed and a clock // place every vehicle, so the rules, the screenshots and the renderer always agree. import { CALIFORNIA, LANES } from "./city.js"; import { between, pick, rng } from "./rng.js"; /** A closed loop through corner points, with rounded corners, sampled every meter. */ function loop(corners, radius) { const raw = []; const n = corners.length; for (let k = 0; k < n; k++) { const p = corners[k]; const a = corners[(k - 1 + n) % n]; const b = corners[(k + 1) % n]; const la = Math.hypot(p[0] - a[0], p[1] - a[1]); const lb = Math.hypot(b[0] - p[0], b[1] - p[1]); const r = Math.min(radius, la / 2, lb / 2); const p0 = [p[0] + ((a[0] - p[0]) / la) * r, p[1] + ((a[1] - p[1]) / la) * r]; const p1 = [p[0] + ((b[0] - p[0]) / lb) * r, p[1] + ((b[1] - p[1]) / lb) * r]; for (let i = 0; i <= 12; i++) { const t = i / 12; const u = 1 - t; raw.push([u * u * p0[0] + 2 * u * t * p[0] + t * t * p1[0], u * u * p0[1] + 2 * u * t * p[1] + t * t * p1[1]]); } } raw.push(raw[0]); // resample evenly const pts = [raw[0]]; let carry = 0; for (let k = 1; k < raw.length; k++) { const [x0, z0] = raw[k - 1]; const [x1, z1] = raw[k]; const len = Math.hypot(x1 - x0, z1 - z0); let s = 1 - carry; while (s <= len) { pts.push([x0 + ((x1 - x0) * s) / len, z0 + ((z1 - z0) * s) / len]); s += 1; } carry = len - (s - 1); } return pts; } function along(path, s) { const n = path.length - 1; const u = ((s % n) + n) % n; const k = Math.floor(u); const f = u - k; const a = path[k]; const b = path[k + 1] ?? path[0]; const x = a[0] + (b[0] - a[0]) * f; const z = a[1] + (b[1] - a[1]) * f; return { x, z, heading: Math.atan2(b[0] - a[0], -(b[1] - a[1])) }; } /** Back and forth along a line with a pause at each end. */ function pingPong(t, length, speed, dwell) { const run = length / speed; const period = 2 * (run + dwell); let u = ((t % period) + period) % period; if (u < dwell) return { s: 0, dir: 1, speed: 0 }; u -= dwell; if (u < run) return { s: u * speed, dir: 1, speed }; u -= run; if (u < dwell) return { s: length, dir: -1, speed: 0 }; u -= dwell; return { s: length - u * speed, dir: -1, speed }; } const CAR_LOOP = loop( [ [LANES.carNorth, 552], [LANES.carNorth, -662], [-668.5, -662], [-668.5, LANES.marketCarEast], [LANES.carSouth, LANES.marketCarEast], [LANES.carSouth, 552], ], 9, ); const STREETCAR_LOOP = loop( [ [LANES.trackNorth, 545], [LANES.trackNorth, -655], [LANES.trackSouth, -655], [LANES.trackSouth, 545], ], 3.5, ); const CABLE = { x0: -645, x1: -80, tracks: [CALIFORNIA.c - 1.6, CALIFORNIA.c + 1.6] }; const LIVERIES = [ { body: 0x3f6b4f, trim: 0xeee4c8 }, // green and cream { body: 0xd77a2c, trim: 0xf1e7d0 }, // Milan orange { body: 0xe2b74a, trim: 0x3d3b39 }, // yellow ]; const CAR_COLORS = [0xe8e5df, 0x2c2f35, 0x8d939a, 0x41546b, 0x7a2e33, 0xcfc8b8, 0x37443a, 0x23262b]; export function createTraffic(seed = 1) { const random = rng(seed * 7919 + 17); const list = []; const carGap = (CAR_LOOP.length - 1) / 6; for (let k = 0; k < 6; k++) list.push({ kind: "car", id: `car${k}`, len: 4.5, wid: 1.9, hgt: 1.45, speed: between(random, 10, 12), phase: k * carGap + between(random, 0, carGap * 0.5), color: pick(random, CAR_COLORS), path: CAR_LOOP }); const tramGap = (STREETCAR_LOOP.length - 1) / 3; for (let k = 0; k < 3; k++) list.push({ kind: "streetcar", id: `tram${k}`, len: 14.5, wid: 2.6, hgt: 3.2, speed: 8.5, phase: k * tramGap + between(random, 0, 60), livery: LIVERIES[k], path: STREETCAR_LOOP }); for (let k = 0; k < 2; k++) list.push({ kind: "cable", id: `cable${k}`, len: 9, wid: 2.5, hgt: 3.4, speed: 4.2, phase: between(random, 0, 140) + k * 90, track: CABLE.tracks[k] }); return { list }; } /** Where vehicle v is at time t: { x, z, heading, speed }. */ export function poseAt(v, t) { if (v.kind === "cable") { const p = pingPong(t + v.phase, CABLE.x1 - CABLE.x0, v.speed, 14); return { x: CABLE.x1 - p.s, z: v.track, heading: p.dir > 0 ? -Math.PI / 2 : Math.PI / 2, speed: p.speed }; } const p = along(v.path, v.phase + t * v.speed); return { ...p, speed: v.speed }; } /** All vehicles' poses at time t. */ export function posesAt(traffic, t) { return traffic.list.map((v) => ({ v, ...poseAt(v, t) })); } /** Does a circle at (x, z) of radius r touch vehicle pose p? Returns a push-out normal if so. */ export function vehicleHit(p, x, z, r) { const fx = Math.sin(p.heading); const fz = -Math.cos(p.heading); const dx = x - p.x; const dz = z - p.z; const along = dx * fx + dz * fz; const side = dx * -fz + dz * fx; const hl = p.v.len / 2; const hw = p.v.wid / 2; const ca = Math.max(-hl, Math.min(hl, along)); const cs = Math.max(-hw, Math.min(hw, side)); const da = along - ca; const ds = side - cs; const d = Math.hypot(da, ds); if (d >= r) return null; let na; let ns; if (d > 1e-6) { na = da / d; ns = ds / d; } else { // center inside the box: leave by the nearer side const out = hw - Math.abs(side) < hl - Math.abs(along); na = out ? 0 : Math.sign(along) || 1; ns = out ? Math.sign(side) || 1 : 0; } return { nx: na * fx + ns * -fz, nz: na * fz + ns * fx, depth: r - d }; } export const PATHS = { CAR_LOOP, STREETCAR_LOOP, CABLE };