Back to Drift City
SOURCE / PINNED RELEASE

Made of little things.

Drift City

Release
098731aa0f0f…
Author-recorded commit
2822ea0a4f5b…
License
LICENSE
Author’s source reference
nostr://npub12lm3rxsva8kfempyjkxupt09e02n3fezurkyadyqfejdc5e0r2kscqty6r/wss%3A%2F%2Fgit.napplet.soy%2F/n-a4c0039c855

Archive hash verified: b3323e408235502b…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/game/cockpit.ts
import * as THREE from 'three';

/**
 * The view from the driver's seat: a low-poly cabin, working mirrors, and the driver's
 * arms and legs, which steer, change gear, pull the handbrake and work the three pedals
 * from the player's input.
 *
 * Everything is laid out in the car's own frame (x to the car's left, y up, z forward).
 * The car is left-hand drive, so the driver sits at +x.
 */

const DRIVER_X = 0.36;
/** The driver's eyes. */
export const EYE = new THREE.Vector3(DRIVER_X, 1.2, -0.28);

const Z = new THREE.Vector3(0, 0, 1);
/** How far the steering wheel turns at full lock: a full turn. */
const WHEEL_TURN = Math.PI * 2;
/** Where each hand likes to hold the rim (quarter to three), and how far it'll go round before re-gripping. */
const REST = [0, Math.PI];
const REACH = 1.35;
/** Fastest the steering wheel turns, radians a second (about 500°/s, a quick driver). */
const WHEEL_SPEED = 8.7;
/** How long a hand takes to let go and grip the rim again, seconds. */
const REGRIP_TIME = 0.28;
const _m = new THREE.Matrix4();
const _q = new THREE.Quaternion();
const _e = new THREE.Euler();
const _s = new THREE.Vector3();
const _a = new THREE.Vector3();
const _b = new THREE.Vector3();
const _c = new THREE.Vector3();
const _d = new THREE.Vector3();

const COLORS = {
  trim: 0x34373e,
  dash: 0x3d4149,
  pad: 0x4a4f59,
  seat: 0x4d5464,
  seatStripe: 0xff4f5e,
  floor: 0x2a2c32,
  liner: 0xd2cdc4,
  metal: 0xb9bec6,
  suit: 0x1b2140,
  suitStripe: 0xffc93c,
  glove: 0xe8e2d6,
  shoe: 0xe0434f,
  face: 0x111317,
  needle: 0xff4f5e,
  tick: 0xf4f1e6,
};

const mats = new Map<number, THREE.MeshLambertMaterial>();
function mat(color: number): THREE.MeshLambertMaterial {
  let m = mats.get(color);
  if (!m) mats.set(color, (m = new THREE.MeshLambertMaterial({ color, flatShading: true })));
  return m;
}

const BOX = new THREE.BoxGeometry(1, 1, 1);
/** A unit box running from z = 0 to z = 1, for parts stretched between two points. */
const BAR = new THREE.BoxGeometry(1, 1, 1).translate(0, 0, 0.5);
/** A tapered six-sided limb running from z = 0 to z = 1. */
function limbGeometry(r0: number, r1: number): THREE.BufferGeometry {
  return new THREE.CylinderGeometry(r1, r0, 1, 6).rotateX(Math.PI / 2).translate(0, 0, 0.5);
}

function box(parent: THREE.Object3D, x0: number, y0: number, z0: number, x1: number, y1: number, z1: number, color: number): THREE.Mesh {
  const m = new THREE.Mesh(BOX, mat(color));
  m.position.set((x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2);
  m.scale.set(Math.abs(x1 - x0), Math.abs(y1 - y0), Math.abs(z1 - z0));
  m.receiveShadow = true;
  parent.add(m);
  return m;
}

/** Stretch a BAR or limb mesh from a to b (width w and thickness t across it). */
function span(mesh: THREE.Mesh, a: THREE.Vector3, b: THREE.Vector3, w = 1, t = 1): void {
  _d.subVectors(b, a);
  const len = _d.length() || 1e-4;
  mesh.position.copy(a);
  mesh.quaternion.setFromUnitVectors(Z, _d.multiplyScalar(1 / len));
  mesh.scale.set(w, t, len);
}

function bar(parent: THREE.Object3D, a: [number, number, number], b: [number, number, number], w: number, t: number, color: number): THREE.Mesh {
  const m = new THREE.Mesh(BAR, mat(color));
  m.receiveShadow = true;
  span(m, new THREE.Vector3(...a), new THREE.Vector3(...b), w, t);
  parent.add(m);
  return m;
}

/**
 * Two-bone reach: where the middle joint (elbow, knee) goes when a limb of lengths l1 and
 * l2 reaches from root to target, bending towards pole.
 */
function solve(root: THREE.Vector3, target: THREE.Vector3, l1: number, l2: number, pole: THREE.Vector3, out: THREE.Vector3): THREE.Vector3 {
  const dir = _a.subVectors(target, root);
  let d = dir.length();
  dir.normalize();
  d = Math.min(Math.max(d, Math.abs(l1 - l2) + 1e-3), l1 + l2 - 1e-3);
  const along = (l1 * l1 - l2 * l2 + d * d) / (2 * d);
  const up = Math.sqrt(Math.max(0, l1 * l1 - along * along));
  const bend = _b.subVectors(pole, root);
  bend.addScaledVector(dir, -bend.dot(dir)).normalize();
  return out.copy(root).addScaledVector(dir, along).addScaledVector(bend, up);
}

class Limb {
  readonly upper: THREE.Mesh;
  readonly lower: THREE.Mesh;
  readonly end: THREE.Mesh;
  private readonly joint = new THREE.Vector3();
  private readonly reach = new THREE.Vector3();

  constructor(
    parent: THREE.Object3D,
    readonly l1: number,
    readonly l2: number,
    radius: number,
    color: number,
    endColor: number,
    endSize: [number, number, number],
  ) {
    this.upper = new THREE.Mesh(limbGeometry(radius, radius * 0.85), mat(color));
    this.lower = new THREE.Mesh(limbGeometry(radius * 0.85, radius * 0.65), mat(color));
    this.end = new THREE.Mesh(new THREE.BoxGeometry(...endSize).translate(0, 0, endSize[2] / 2), mat(endColor));
    for (const m of [this.upper, this.lower, this.end]) {
      m.receiveShadow = true;
      parent.add(m);
    }
  }

  /** Reach from root to target (the wrist or ankle), bending towards pole; the hand or foot then points at aim. */
  set(root: THREE.Vector3, target: THREE.Vector3, pole: THREE.Vector3, aim?: THREE.Vector3): void {
    solve(root, target, this.l1, this.l2, pole, this.joint);
    // If the target is out of reach, the limb stretches straight towards it.
    this.reach.copy(target).sub(root);
    const tip = this.reach.length() > this.l1 + this.l2 ? root.clone().addScaledVector(this.reach.normalize(), this.l1 + this.l2) : target;
    span(this.upper, root, this.joint);
    span(this.lower, this.joint, tip);
    this.end.position.copy(tip);
    // Without an aim, a hand carries on along the forearm.
    if (aim) _c.subVectors(aim, tip).normalize();
    else _c.subVectors(tip, this.joint).normalize();
    this.end.quaternion.setFromUnitVectors(Z, _c);
  }
}

interface Mirror {
  glass: THREE.Mesh;
  camera: THREE.PerspectiveCamera;
  target: THREE.WebGLRenderTarget;
  /** Where the camera sits and looks, in the car's frame. */
  from: THREE.Vector3;
  to: THREE.Vector3;
  material: THREE.MeshBasicMaterial;
  plain: THREE.Material;
}

/** What the driver's hands and feet are doing this step. */
export interface DriverInput {
  /** Steering wheel turn, -1 (full left) to 1 (full right). */
  wheel: number;
  gas: number;
  brake: number;
  clutch: number;
  /** 0 down, 1 pulled. */
  handbrake: number;
  /** 0 reverse, 1 to 5 forward. */
  gear: number;
  /** Seconds left in a gear change (the hand is on the stick). */
  shifting: number;
  /** Road speed as a fraction of top speed, and engine revs 0..1. */
  speed: number;
  rev: number;
}

// The H-pattern: where the gear knob sits for each gear (sideways, forward).
const GATE: [number, number][] = [
  [-0.05, -0.05], // R
  [0.05, 0.05],
  [0.05, -0.05],
  [0, 0.05],
  [0, -0.05],
  [-0.05, 0.05],
];

export class Cockpit {
  readonly root = new THREE.Group();
  readonly mirrors: Mirror[] = [];
  /** Which door mirror is redrawn this frame (they take turns). */
  private mirrorTurn = 0;
  private mirrorFrame = 0;
  private readonly wheel = new THREE.Group();
  private readonly pedals: THREE.Group[] = [];
  private readonly shifter = new THREE.Group();
  private readonly knob: THREE.Mesh;
  private readonly lever = new THREE.Group();
  private readonly needles: THREE.Object3D[] = [];
  private readonly leftArm: Limb;
  private readonly rightArm: Limb;
  private readonly leftLeg: Limb;
  private readonly rightLeg: Limb;
  private readonly press = { gas: 0, brake: 0, clutch: 0, lever: 0, rightFoot: 0, rightHand: 0, knobX: 0.05, knobZ: 0.05 };
  private readonly footRest = new THREE.Vector3();
  /** Each hand's hold on the wheel: its angle on the rim, and a re-grip in progress (moving < 1). */
  private readonly grips = [0, 1].map((i) => ({ on: REST[i]!, from: 0, to: 0, moving: 1 }));
  /** The steering wheel's turn, following the steering at a hand's pace. */
  private wheelAngle = 0;
  /** How long the wheel has been held still, seconds. */
  private wheelStill = 0;

  constructor(
    body: THREE.Object3D,
    /** The two door mirrors' glass, on the car's body. */
    doorGlass: THREE.Mesh[],
  ) {
    this.root.visible = false;
    body.add(this.root);
    this.buildCabin();
    this.knob = this.buildShifter();
    this.buildHandbrake();
    this.buildPedals();
    this.buildDriver();
    this.leftArm = new Limb(this.root, 0.3, 0.32, 0.045, COLORS.suit, COLORS.glove, [0.07, 0.04, 0.1]);
    this.rightArm = new Limb(this.root, 0.3, 0.32, 0.045, COLORS.suit, COLORS.glove, [0.07, 0.04, 0.1]);
    this.leftLeg = new Limb(this.root, 0.56, 0.54, 0.07, COLORS.suit, COLORS.shoe, [0.09, 0.07, 0.24]);
    this.rightLeg = new Limb(this.root, 0.56, 0.54, 0.07, COLORS.suit, COLORS.shoe, [0.09, 0.07, 0.24]);
    // The interior mirror up at the top of the windscreen, then the two door mirrors.
    const inner = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), mat(COLORS.metal));
    inner.position.set(0, 1.3, 0.14);
    inner.scale.set(0.44, 0.085, 1);
    this.root.add(inner);
    bar(this.root, [0, 1.34, 0.17], [0, 1.4, 0.15], 0.03, 0.03, COLORS.trim);
    // Its camera sits up by the roof, looking out over the headrests and the boot.
    this.addMirror(inner, new THREE.Vector3(0, 1.37, 0.1), new THREE.Vector3(0, 1.2, -8), 512, 100, 24);
    // The door mirrors look back along the car's flanks, angled a little outwards.
    this.addMirror(doorGlass[0]!, new THREE.Vector3(1.12, 0.96, 0.7), new THREE.Vector3(2.3, 0.75, -6), 256, 164, 38);
    this.addMirror(doorGlass[1]!, new THREE.Vector3(-1.12, 0.96, 0.7), new THREE.Vector3(-2.3, 0.75, -6), 256, 164, 38);
    // The interior mirror is tilted to face the driver; the door mirrors' glass lies flat in
    // the back of the housings (their cameras do the aiming).
    inner.lookAt(_a.copy(inner.position).multiplyScalar(2).sub(EYE));
    // The interior mirror's housing, turned with the glass, just behind it.
    const housing = new THREE.Mesh(BOX, mat(COLORS.trim));
    housing.position.copy(inner.position);
    housing.quaternion.copy(inner.quaternion);
    housing.translateZ(0.02);
    housing.scale.set(0.47, 0.105, 0.03);
    this.root.add(housing);
  }

  private addMirror(glass: THREE.Mesh, from: THREE.Vector3, to: THREE.Vector3, w: number, h: number, fov: number): void {
    const target = new THREE.WebGLRenderTarget(w, h, { depthBuffer: true });
    target.texture.colorSpace = THREE.SRGBColorSpace;
    const camera = new THREE.PerspectiveCamera(fov, w / h, 0.3, 900);
    // Seen from the front of the plane its picture would read backwards; the driver sees
    // its back face, which shows it the right way round for a mirror.
    const material = new THREE.MeshBasicMaterial({ map: target.texture, side: THREE.DoubleSide, toneMapped: false });
    this.mirrors.push({ glass, camera, target, from, to, material, plain: glass.material as THREE.Material });
  }

  // ---------------------------------------------------------------------------
  // The cabin

  private buildCabin(): void {
    const r = this.root;
    const C = COLORS;
    // Floor, transmission tunnel and the console between the seats.
    box(r, -0.86, 0.18, -1.1, 0.86, 0.24, 1.12, C.floor);
    box(r, -0.13, 0.24, -0.9, 0.13, 0.46, 0.55, C.trim);
    box(r, -0.12, 0.46, -0.62, 0.12, 0.53, 0.1, C.dash);
    // Firewall and the footwell's sloped toe board.
    box(r, -0.86, 0.24, 1.08, 0.86, 0.76, 1.14, C.trim);
    bar(r, [0, 0.3, 0.98], [0, 0.55, 1.1], 1.7, 0.04, C.floor);
    // Seats: cushion, backrest and headrest, with a stripe down the middle.
    for (const sx of [DRIVER_X, -DRIVER_X]) {
      box(r, sx - 0.23, 0.3, -0.62, sx + 0.23, 0.46, -0.02, C.seat);
      box(r, sx - 0.24, 0.42, -0.62, sx - 0.18, 0.52, -0.05, C.seat);
      box(r, sx + 0.18, 0.42, -0.62, sx + 0.24, 0.52, -0.05, C.seat);
      bar(r, [sx, 0.44, -0.6], [sx, 1.1, -0.78], 0.46, 0.13, C.seat);
      bar(r, [sx, 0.5, -0.66], [sx, 1.05, -0.83], 0.1, 0.135, C.seatStripe);
      bar(r, [sx, 1.12, -0.8], [sx, 1.3, -0.84], 0.26, 0.1, C.seat);
    }
    // Dashboard: a long top, a padded lip, a glovebox on the passenger side and a centre
    // stack with a radio. The driver's side is open underneath for the legs.
    box(r, -0.88, 0.76, 0.52, 0.88, 0.93, 0.98, C.dash);
    box(r, -0.88, 0.93, 0.56, 0.88, 0.97, 0.92, C.pad);
    box(r, -0.88, 0.56, 0.64, -0.16, 0.76, 0.96, C.dash);
    box(r, -0.55, 0.64, 0.635, -0.25, 0.72, 0.64, C.trim);
    box(r, -0.16, 0.46, 0.38, 0.16, 0.8, 0.66, C.trim);
    box(r, -0.1, 0.66, 0.375, 0.1, 0.72, 0.38, C.face);
    for (const vx of [-0.62, -0.08, 0.08, 0.7]) box(r, vx - 0.05, 0.84, 0.515, vx + 0.05, 0.89, 0.52, C.face);
    // The instrument cluster in front of the driver: a hood, and two dials whose needles
    // show speed and revs.
    box(r, DRIVER_X - 0.21, 0.93, 0.47, DRIVER_X + 0.21, 1.03, 0.62, C.pad);
    for (const [dx, sweep] of [
      [0.09, 0],
      [-0.09, 1],
    ] as const) {
      const g = new THREE.Group();
      g.position.set(DRIVER_X + dx, 0.95, 0.49);
      g.rotation.set(0.35, Math.PI, 0);
      const face = new THREE.Mesh(new THREE.CircleGeometry(0.07, 14), mat(C.face));
      g.add(face);
      for (let k = 0; k <= 8; k++) {
        const a = -2.2 + (k / 8) * 4.4;
        const t = new THREE.Mesh(BOX, mat(k > 6 && sweep ? C.needle : C.tick));
        t.position.set(Math.sin(a) * 0.058, Math.cos(a) * 0.058, 0.002);
        t.rotation.z = -a;
        t.scale.set(0.006, 0.016, 0.002);
        g.add(t);
      }
      const needle = new THREE.Group();
      needle.position.z = 0.004;
      const n = new THREE.Mesh(BOX, mat(C.needle));
      n.position.y = 0.025;
      n.scale.set(0.005, 0.055, 0.002);
      needle.add(n);
      g.add(needle);
      this.needles.push(needle);
      this.root.add(g);
    }
    // Steering column and the wheel.
    bar(r, [DRIVER_X, 0.8, 0.62], [DRIVER_X, 0.95, 0.3], 0.07, 0.07, C.trim);
    const w = this.wheel;
    w.position.set(DRIVER_X, 0.97, 0.27);
    const rim = new THREE.Mesh(new THREE.TorusGeometry(0.18, 0.024, 6, 18), mat(C.trim));
    w.add(rim);
    box(w, -0.17, -0.015, -0.012, 0.17, 0.015, 0.012, C.dash);
    box(w, -0.015, -0.17, -0.012, 0.015, 0, 0.012, C.dash);
    box(w, -0.045, -0.045, -0.03, 0.045, 0.045, 0.01, C.pad);
    // A stripe at the top of the rim shows where straight ahead is.
    box(w, -0.012, 0.16, -0.03, 0.012, 0.2, 0.03, C.seatStripe);
    r.add(w);
    // Door cards with armrests, and the window sills.
    for (const s of [1, -1]) {
      box(r, s * 0.845, 0.3, -0.95, s * 0.9, 0.86, 0.6, C.dash);
      box(r, s * 0.8, 0.6, -0.55, s * 0.87, 0.66, 0.15, C.pad);
      box(r, s * 0.78, 0.84, -0.95, s * 0.9, 0.88, 0.62, C.trim);
      // Lining down the footwell and behind the seats, so the body's paint doesn't show inside.
      box(r, s * 0.85, 0.24, 0.55, s * 0.865, 0.84, 1.12, C.trim);
      box(r, s * 0.85, 0.24, -1.22, s * 0.865, 0.84, -0.9, C.trim);
      // Windscreen pillars, roof rails and the B-pillars behind the seats.
      bar(r, [s * 0.84, 0.88, 0.78], [s * 0.67, 1.38, 0.18], 0.07, 0.06, C.trim);
      bar(r, [s * 0.67, 1.38, 0.18], [s * 0.64, 1.39, -0.8], 0.06, 0.06, C.trim);
      bar(r, [s * 0.86, 0.86, -0.95], [s * 0.66, 1.38, -0.82], 0.1, 0.07, C.trim);
    }
    // Headlining above, a bar across the top of the windscreen and the rear window frame.
    const liner = box(r, -0.66, 1.39, -0.86, 0.66, 1.42, 0.18, C.liner);
    liner.castShadow = true;
    box(r, -0.68, 1.36, 0.14, 0.68, 1.42, 0.2, C.trim);
    bar(r, [-0.86, 0.86, -1.2], [0.86, 0.86, -1.2], 0.06, 0.06, C.trim);
    bar(r, [0.66, 1.38, -0.84], [0.84, 0.9, -1.22], 0.06, 0.06, C.trim);
    bar(r, [-0.66, 1.38, -0.84], [-0.84, 0.9, -1.22], 0.06, 0.06, C.trim);
  }

  private buildShifter(): THREE.Mesh {
    const s = this.shifter;
    s.position.set(0.02, 0.53, 0.24);
    box(s, -0.06, -0.01, -0.06, 0.06, 0.02, 0.06, COLORS.floor);
    const stick = new THREE.Mesh(BAR, mat(COLORS.metal));
    stick.name = 'stick';
    s.add(stick);
    const knob = new THREE.Mesh(new THREE.IcosahedronGeometry(0.03, 0), mat(COLORS.face));
    s.add(knob);
    this.root.add(s);
    return knob;
  }

  private buildHandbrake(): void {
    const l = this.lever;
    l.position.set(0, 0.54, -0.28);
    box(l, -0.018, -0.018, 0, 0.018, 0.018, 0.26, COLORS.metal);
    box(l, -0.025, -0.025, 0.2, 0.025, 0.025, 0.3, COLORS.face);
    this.root.add(l);
  }

  private buildPedals(): void {
    // Clutch, brake and gas, left to right from the driver's seat.
    for (const [x, wide] of [
      [DRIVER_X + 0.15, 0.06],
      [DRIVER_X - 0.01, 0.08],
      [DRIVER_X - 0.17, 0.05],
    ] as const) {
      const p = new THREE.Group();
      p.position.set(x, 0.66, 0.9);
      box(p, -0.012, -0.32, -0.012, 0.012, 0, 0.012, COLORS.metal);
      box(p, -wide / 2, -0.38, -0.02, wide / 2, -0.28, 0.0, COLORS.metal);
      this.root.add(p);
      this.pedals.push(p);
    }
  }

  private buildDriver(): void {
    // The driver's body, seen when looking down: a race suit with a stripe.
    bar(this.root, [DRIVER_X, 0.48, -0.36], [DRIVER_X, 1.02, -0.44], 0.38, 0.22, COLORS.suit);
    bar(this.root, [DRIVER_X - 0.1, 0.5, -0.24], [DRIVER_X - 0.1, 1.0, -0.33], 0.05, 0.225, COLORS.suitStripe);
    bar(this.root, [DRIVER_X, 0.47, -0.35], [DRIVER_X, 0.47, -0.05], 0.36, 0.14, COLORS.suit);
  }

  /** Where a pedal's pad is now, in the car's frame. */
  private pad(i: number, out: THREE.Vector3): THREE.Vector3 {
    const p = this.pedals[i]!;
    const a = -p.rotation.x;
    return out.set(p.position.x, p.position.y - 0.33 * Math.cos(a), p.position.z + 0.33 * Math.sin(a) - 0.02);
  }

  set visible(on: boolean) {
    this.root.visible = on;
    for (const m of this.mirrors) m.glass.material = on ? m.material : m.plain;
    // The interior mirror only exists inside; the door mirrors are always on the car.
    this.mirrors[0]!.glass.visible = on;
  }

  get visible(): boolean {
    return this.root.visible;
  }

  /** Move the driver's hands and feet and the controls they work. */
  update(dt: number, d: DriverInput): void {
    const k = (rate: number) => Math.min(1, dt * rate);
    const p = this.press;
    p.gas += (d.gas - p.gas) * k(14);
    p.brake += (d.brake - p.brake) * k(14);
    p.clutch += (d.clutch - p.clutch) * k(16);
    p.lever += (d.handbrake - p.lever) * k(14);
    // The right foot hovers over the gas, and moves across to brake.
    p.rightFoot += ((d.brake > 0.05 ? 1 : 0) - p.rightFoot) * k(12);
    // The right hand leaves the wheel for the handbrake or a gear change.
    const handOff = d.handbrake > 0.5 ? 2 : d.shifting > 0 ? 1 : 0;
    p.rightHand += ((handOff ? 1 : 0) - p.rightHand) * k(handOff ? 16 : 10);
    const [gx, gz] = GATE[Math.max(0, Math.min(5, d.gear))]!;
    p.knobX += (gx - p.knobX) * k(d.shifting > 0.12 ? 0 : 18);
    p.knobZ += (gz - p.knobZ) * k(d.shifting > 0.12 ? 0 : 18);

    // Wheel, pedals, gear stick, handbrake lever, dials.
    // The wheel turns no faster than hands can go hand over hand (about 500° a second),
    // easing in as it reaches where the steering wants it.
    const want = d.wheel * WHEEL_TURN;
    const step = Math.max(-WHEEL_SPEED * dt, Math.min(WHEEL_SPEED * dt, (want - this.wheelAngle) * Math.min(1, dt * 10)));
    this.wheelAngle += step;
    this.wheelStill = Math.abs(step) < 0.6 * dt ? this.wheelStill + dt : 0;
    this.wheel.rotation.set(0.35, 0, this.wheelAngle);
    this.pedals[0]!.rotation.x = -p.clutch * 0.4;
    this.pedals[1]!.rotation.x = -p.brake * 0.3;
    this.pedals[2]!.rotation.x = -p.gas * 0.35;
    // Mid-change the stick passes through neutral in the middle of the gate.
    const mid = d.shifting > 0.12 ? 1 : 0;
    const knob = _a.set(p.knobX * (1 - mid * 0.6), 0.22, p.knobZ * (1 - mid));
    const stick = this.shifter.getObjectByName('stick') as THREE.Mesh;
    span(stick, _b.set(0, 0, 0), knob, 0.018, 0.018);
    this.knob.position.copy(knob);
    this.lever.rotation.x = -(0.12 + p.lever * 0.6);
    this.needles[0]!.rotation.z = 2.2 - Math.min(1, d.speed) * 4.4;
    this.needles[1]!.rotation.z = 2.2 - Math.min(1.05, d.rev) * 4.4;

    // Arms: shoulders to the wheel's rim, the hands going round with it hand over hand.
    this.root.updateMatrixWorld(true);
    this.updateGrips(dt, this.wheel.rotation.z, handOff === 0 && p.rightHand < 0.5);
    const lShoulder = new THREE.Vector3(DRIVER_X + 0.2, 1.0, -0.36);
    const rShoulder = new THREE.Vector3(DRIVER_X - 0.2, 1.0, -0.36);
    const rimL = this.gripPoint(0);
    const rimR = this.gripPoint(1);
    this.leftArm.set(lShoulder, rimL, lShoulder.clone().add(new THREE.Vector3(0.4, -0.35, 0)));
    let right = rimR;
    if (p.rightHand > 0.01) {
      const there = handOff === 2 || (handOff === 0 && p.lever > 0.3) ? this.leverHandle() : this.shifter.position.clone().add(this.knob.position).add(new THREE.Vector3(0, 0.03, -0.02));
      right = rimR.clone().lerp(there, p.rightHand);
    }
    this.rightArm.set(rShoulder, right, rShoulder.clone().add(new THREE.Vector3(-0.4, -0.35, 0)));

    // Legs: hips to the pedals. The left foot works the clutch (resting beside it); the
    // right foot moves between gas and brake.
    const lHip = new THREE.Vector3(DRIVER_X + 0.1, 0.5, -0.22);
    const rHip = new THREE.Vector3(DRIVER_X - 0.1, 0.5, -0.22);
    const clutch = this.pad(0, new THREE.Vector3());
    const rest = this.footRest.set(DRIVER_X + 0.3, 0.3, 0.84);
    const lFoot = rest.clone().lerp(clutch, Math.min(1, 0.55 + p.clutch));
    const brake = this.pad(1, new THREE.Vector3());
    const gas = this.pad(2, new THREE.Vector3());
    const rFoot = gas.clone().lerp(brake, p.rightFoot);
    const ankle = (foot: THREE.Vector3) => foot.clone().add(new THREE.Vector3(0, 0.06, -0.13));
    this.leftLeg.set(lHip, ankle(lFoot), lHip.clone().add(new THREE.Vector3(0.08, 0.6, 0.35)), lFoot.clone().add(new THREE.Vector3(0, 0.02, 0.05)));
    this.rightLeg.set(rHip, ankle(rFoot), rHip.clone().add(new THREE.Vector3(-0.05, 0.6, 0.35)), rFoot.clone().add(new THREE.Vector3(0, 0.02, 0.05)));
  }

  /**
   * Hand over hand: each hand rides the rim until it's turned too far from where it
   * likes to hold (quarter to three), then lets go and grips again further back. Only one
   * hand moves at a time, so one always has hold of the wheel. Angles are around the
   * wheel's face, measured in the car's frame (0 is the driver's left, π the right).
   */
  private updateGrips(dt: number, turn: number, rightOnWheel: boolean): void {
    const wrap = (a: number) => Math.atan2(Math.sin(a), Math.cos(a));
    for (let i = 0; i < 2; i++) {
      const g = this.grips[i]!;
      const other = this.grips[1 - i]!;
      if (g.moving < 1) {
        g.moving = Math.min(1, g.moving + dt / REGRIP_TIME);
        if (g.moving >= 1) g.on = g.to - turn;
        continue;
      }
      // A hand away from the wheel (right hand on the stick or handbrake) re-grips at rest.
      if (i === 1 && !rightOnWheel) {
        g.on = REST[1]! - turn;
        continue;
      }
      const off = wrap(g.on + turn - REST[i]!);
      const otherBusy = other.moving < 1;
      if (Math.abs(off) > REACH && (!otherBusy || Math.abs(off) > REACH + 0.7)) {
        g.from = g.on + turn;
        g.to = REST[i]! - Math.sign(off) * 0.85;
        g.moving = 0;
      } else if (this.wheelStill > 0.35 && Math.abs(off) > 0.45 && !otherBusy) {
        // Holding the wheel still, a hand that has wandered round settles back to
        // quarter to three, one hand at a time.
        g.from = g.on + turn;
        g.to = REST[i]!;
        g.moving = 0;
      }
    }
  }

  /** Where a hand is on (or just off) the rim, in the car's frame. */
  private gripPoint(i: number): THREE.Vector3 {
    const g = this.grips[i]!;
    const wrap = (a: number) => Math.atan2(Math.sin(a), Math.cos(a));
    const turn = this.wheel.rotation.z;
    let angle = g.on + turn;
    let lift = 0;
    if (g.moving < 1) {
      const t = g.moving * g.moving * (3 - 2 * g.moving);
      angle = g.from + wrap(g.to - g.from) * t;
      lift = Math.sin(Math.PI * g.moving) * 0.07;
    }
    // The wheel's face without its spin: tilted back towards the driver.
    _m.compose(this.wheel.position, _q.setFromEuler(_e.set(this.wheel.rotation.x, 0, 0)), _s.set(1, 1, 1));
    return new THREE.Vector3(Math.cos(angle) * 0.18, Math.sin(angle) * 0.18, -lift).applyMatrix4(_m);
  }

  private leverHandle(): THREE.Vector3 {
    const a = -this.lever.rotation.x;
    return this.lever.position.clone().add(new THREE.Vector3(0, Math.sin(a) * 0.25 + 0.02, Math.cos(a) * 0.25));
  }

  /**
   * Draw what each mirror sees into its picture. The car's frame (matrixWorld of the body
   * the cockpit hangs on) places the mirror cameras.
   */
  /** Draw the mirrors (lite: on slow machines, the interior one every other frame, the door mirrors every fourth). */
  renderMirrors(renderer: THREE.WebGLRenderer, scene: THREE.Scene, frame: THREE.Matrix4, lite = false): void {
    this.mirrorFrame++;
    if (lite && this.mirrorFrame % 2) return;
    const up = _c.set(0, 1, 0).transformDirection(frame);
    for (const m of this.mirrors) m.glass.visible = false;
    // The interior mirror every frame; the two small door mirrors take turns, each at half
    // the frame rate, which the eye can't tell from the side of the view.
    this.mirrorTurn = 1 - this.mirrorTurn;
    for (const [i, m] of this.mirrors.entries()) {
      if (i > 0 && i % 2 !== this.mirrorTurn) continue;
      m.camera.position.copy(m.from).applyMatrix4(frame);
      m.camera.up.copy(up);
      m.camera.lookAt(_d.copy(m.to).applyMatrix4(frame));
      m.camera.updateMatrixWorld();
      renderer.setRenderTarget(m.target);
      renderer.render(scene, m.camera);
    }
    renderer.setRenderTarget(null);
    for (const m of this.mirrors) m.glass.visible = true;
  }
}