// The camera rig: low and behind Claudia, leaning into turns and pulling back at speed, never // through a building. Pure and deterministic, so a screenshot at t seconds always frames the // same shot. It lives in the game state and steps with the rules. import { groundAt, sightline } from "./city.js"; const lerp = (a, b, t) => a + (b - a) * t; const wrap = (a) => Math.atan2(Math.sin(a), Math.cos(a)); const ease = (t) => t * t * (3 - 2 * t); export function createCam() { return { x: 0, y: 0, z: 0, lx: 0, ly: 0, lz: 0, roll: 0, fov: 50, yaw: 0, blend: 0, chase: null, blocked: false }; } /** The title shot: a slow drift over the bay, looking back at the city. */ export function titlePose(t) { // From over the water south of the bridge, looking northwest: the bridge sweeps across the // right of the frame, the Ferry Building and the skyline sit in the sun, Telegraph Hill beyond. const a = 0.95 - t * 0.0035; return { x: -100 + Math.cos(a) * 820, y: 122 + 5 * Math.sin(t * 0.06), z: -60 + Math.sin(a) * 820, lx: -220, ly: 34, lz: -200, roll: 0, fov: 46 }; } function chaseTarget(game, yaw) { const p = game.p; const portrait = (game.view?.aspect ?? 1.6) < 1; const speed = Math.abs(p.speed); const fx = Math.sin(yaw); const fz = -Math.cos(yaw); // Higher than her head and looking down a little, so the street ahead opens up. const dist = (portrait ? 3.7 : 3.05) + speed * (portrait ? 0.035 : 0.03); const height = (portrait ? 2.35 : 2.1) + speed * 0.018; const hx = p.x; const hy = p.y + 1.35; const hz = p.z; let x = p.x - fx * dist; let z = p.z - fz * dist; let y = Math.max(p.y + height, groundAt(x, z) + 0.9); const f = sightline(hx, hy, hz, x, y, z); let blocked = false; if (f < 1) { blocked = true; const k = Math.max(0.25, f - 0.08); x = hx + (x - hx) * k; z = hz + (z - hz) * k; y = Math.max(hy + 0.4, hy + (y - hy) * k + 0.6); } return { x, y, z, lx: p.x + fx * 8, ly: p.y + (portrait ? 1.6 : 1.15), lz: p.z + fz * 8, blocked }; } /** Moves the camera one step toward where it wants to be. */ export function stepCam(cam, game, dt) { const p = game.p; if (!cam.chase) { cam.yaw = p.heading; cam.chase = chaseTarget(game, cam.yaw); } cam.yaw += wrap(p.heading - cam.yaw) * (1 - Math.exp(-dt * 3.4)); const t = chaseTarget(game, cam.yaw); const k = 1 - Math.exp(-dt * 7); const kl = 1 - Math.exp(-dt * 12); const c = cam.chase; c.x = lerp(c.x, t.x, k); c.y = lerp(c.y, t.y, k); c.z = lerp(c.z, t.z, k); c.lx = lerp(c.lx, t.lx, kl); c.ly = lerp(c.ly, t.ly, kl); c.lz = lerp(c.lz, t.lz, kl); // The smoothed position can cut a corner the target did not: check it again. const f = sightline(p.x, p.y + 1.35, p.z, c.x, c.y, c.z); if (f < 1) { const kk = Math.max(0.25, f - 0.08); c.x = p.x + (c.x - p.x) * kk; c.z = p.z + (c.z - p.z) * kk; c.y = Math.max(p.y + 1.75, p.y + 1.35 + (c.y - p.y - 1.35) * kk + 0.6); } c.y = Math.max(c.y, groundAt(c.x, c.z) + 0.6); cam.blocked = t.blocked; cam.roll = lerp(cam.roll, Math.max(-0.12, Math.min(0.12, -p.yawRate * 0.045)), 1 - Math.exp(-dt * 5)); const portrait = (game.view?.aspect ?? 1.6) < 1; // The view widens with speed, and kicks wider still while boosting. cam.kick = lerp(cam.kick ?? 0, p.burrito > 0 ? 1.6 : p.boost ? 1 : 0, 1 - Math.exp(-dt * 4)); const chaseFov = (portrait ? 62 : 54) + Math.min(8, Math.abs(p.speed) * 0.3) + cam.kick * 4; cam.blend = game.state === "title" ? 0 : Math.min(1, cam.blend + dt / 1.8); const e = ease(cam.blend); const tp = titlePose(game.time); cam.x = lerp(tp.x, c.x, e); cam.y = lerp(tp.y, c.y, e); cam.z = lerp(tp.z, c.z, e); cam.lx = lerp(tp.lx, c.lx, e); cam.ly = lerp(tp.ly, c.ly, e); cam.lz = lerp(tp.lz, c.lz, e); cam.fov = lerp(tp.fov, chaseFov, e); cam.rollNow = cam.roll * e; return cam; }