← AI & Me Studios

05 Update and physics

The game's brain: the governor burn, gravity, scrolling, collisions, and the landing rules. This is where Glidepath actually plays.

Code
// ---------- update ----------
let last = performance.now();
function update(dt) {
  frames++;
  for (const s of stars) {
    s.x -= s.sp * dt * (state === 'playing' ? 1 : 0.35);
    if (s.x < -4) { s.x = W + 4; s.y = Math.random() * H; }
    s.tw += dt * 3;
  }
  for (const p of rocks) {
    p.x -= 8 * dt * (state === 'playing' ? 1 : 0.35);
    if (p.x < -50) { p.x = W + 50; p.y = 40 + Math.random() * (H - 80); }
  }
  if (flash > 0) flash--;
  if (shake > 0) shake--;

  // Exhaust drifts and fades in every state, so a crash doesn't freeze it.
  for (const p of parts) { p.x += p.vx * dt; p.y += p.vy * dt; p.life -= dt; }
  parts = parts.filter(p => p.life > 0);

  if (state !== 'playing') return;

  // Rocket burn (governor): steer toward the same climb speed on every
  // burn. Target -665 offsets gravity (+1750/s over the burn) so the burn
  // ENDS near -540. Never brakes a climb already faster than the target.
  if (burnT > 0) {
    const bt = Math.min(dt, burnT);
    burnT -= dt;
    if (shipV > -665) shipV += (-665 - shipV) * Math.min(1, bt * 14);
    emitExhaust();
  }

  shipV = Math.min(shipV + GRAV * dt, MAXFALL);
  shipY += shipV * dt;
  shipTilt = Math.max(-0.45, Math.min(0.55, shipV / 900));

  // The world scrolls: course coordinates stay fixed, the camera moves.
  courseX += speed * dt;
  const gx = (g) => g.cx - courseX;

  // Scoring + difficulty.
  for (const g of gates) {
    if (!g.passed && gx(g) + GATE_W < SHIP_X - SHIP_R) {
      g.passed = true; score++; levelScore++;
      speed = Math.min(280, speed + 3);
      sfx('score');
    }
  }
  gates = gates.filter(g => gx(g) > -120);

  // The pad: land on it gently, or miss it and the run ends.
  const padX = pad.cx - courseX;
  const overPad = SHIP_X + SHIP_R > padX && SHIP_X - SHIP_R < padX + pad.w;
  // Landing assist: the pad steadies your descent when you're over it and low.
  if (overPad && shipY > PAD_TOP - 140 && shipV > 420) shipV = 420;
  if (overPad && shipY + SHIP_R >= PAD_TOP) {
    if (shipV <= 660) { landPad(); return; }
    shipY = PAD_TOP - SHIP_R; gameOver('TOO FAST TO LAND!'); return;
  }
  if (padX + pad.w < SHIP_X - SHIP_R) { gameOver('MISSED THE PAD!'); return; }

  // Collisions: forgiving circle vs gate slabs + floor/ceiling.
  if (shipY + SHIP_R >= H || shipY - SHIP_R <= 0) { shipY = Math.min(Math.max(shipY, SHIP_R), H - SHIP_R); gameOver(); return; }
  for (const g of gates) {
    if (SHIP_X + SHIP_R > gx(g) && SHIP_X - SHIP_R < gx(g) + GATE_W) {
      const topBot = g.cy - g.gap / 2, botTop = g.cy + g.gap / 2;
      if (shipY - SHIP_R < topBot || shipY + SHIP_R > botTop) { gameOver(); return; }
    }
  }
}
What it does

This file is the game's brain. update(dt) runs every frame and moves everything, checks every collision, and decides whether you score, crash, or land. If you understand this file, you understand how Glidepath plays.

THE FRAME RHYTHM

update(dt) receives dt — the seconds since the last frame, usually about 0.016 (60 frames per second). Every movement in the file is multiplied by dt, so the game runs at the same speed no matter the frame rate. A ship falling at 880 pixels per second moves 880 * 0.016 = about 14 pixels per frame.

BACKGROUND MOTION (ALWAYS RUNS)

The first part of update() runs in every state — even on the title screen — so the game never looks frozen:

  • Stars drift left at their layer speed (near stars faster), and wrap around to the right edge when they leave. They drift at full speed while playing, 0.35x on the menus, so the background feels calmer when you're not flying.
  • The two distant planets drift left slowly and wrap around too.
  • flash and shake count down by 1 per frame.
  • Exhaust particles drift (vx, vy), lose life, and are filtered out when life hits 0. This runs in every state "so a crash doesn't freeze it" — the exhaust keeps falling even after you die.

Then: if (state !== 'playing') return; — if you're not in a live run, nothing else happens. The ship doesn't fall, gates don't scroll, no collisions. Only the background moves.

THE GOVERNOR BURN

    if (burnT > 0) {
      const bt = Math.min(dt, burnT);
      burnT -= dt;
      if (shipV > -665) shipV += (-665 - shipV) * Math.min(1, bt * 14);
      emitExhaust();
    }

This is the core mechanic. While burnT (seconds of burn left) is above zero:

  • bt is the smaller of dt and the remaining burn, so the last frame of a burn doesn't overshoot.
  • If the ship's velocity is above -665 (falling, or climbing slower than 665), it is steered toward -665. The steering rate is bt * 14, capped at 1 — that means within a few frames the velocity is very close to -665. It's a "governor": it forces the velocity to a fixed target instead of adding a fixed push.
  • If the ship is ALREADY climbing faster than -665, nothing happens — the burn never brakes a climb. That's the "never brakes" rule in the comment.
  • emitExhaust() spawns the orange particles under the ship.

Why -665 and not -540? Because gravity (+1750 per second) acts during the 0.15-second burn. Starting at -665 and adding gravity over the burn, you end near -540. So every tap ends at the same climb speed, -540, no matter how fast you were falling when you tapped. That's why the game feels consistent.

GRAVITY AND MOVEMENT

    shipV = Math.min(shipV + GRAV * dt, MAXFALL);
    shipY += shipV * dt;
    shipTilt = Math.max(-0.45, Math.min(0.55, shipV / 900));
  • Gravity adds GRAV * dt to the velocity each frame, capped at MAXFALL (880) so the ship never falls faster than terminal velocity.
  • The ship's height changes by shipV * dt.
  • shipTilt (how much the ship leans) is shipV / 900, clamped between -0.45 (nose up, climbing) and 0.55 (nose down, falling). So the ship visibly tilts with its motion.

SCROLLING — THE CAMERA

    courseX += speed * dt;
    const gx = (g) => g.cx - courseX;

courseX is the camera. It increases by speed * dt every frame. Every gate has a fixed course x-position (g.cx); its screen position is gx(g) = g.cx - courseX. The gates never move — the camera moves past them. When a gate's screen position goes off the left edge, it's removed from the list.

SCORING AND DIFFICULTY

    for (const g of gates) {
      if (!g.passed && gx(g) + GATE_W < SHIP_X - SHIP_R) {
        g.passed = true; score++; levelScore++;
        speed = Math.min(280, speed + 3);
        sfx('score');
      }
    }

A gate counts as passed when its right edge has gone fully behind the ship's left edge. When that happens: the gate is marked passed, score and levelScore each go up by 1, the scroll speed goes up by 3 (capped at 280), and the score sound plays. levelScore also shrinks future gate gaps (see file 02), so scoring makes the game harder.

THE PAD — LANDING, ASSIST, AND MISSING

    const padX = pad.cx - courseX;
    const overPad = SHIP_X + SHIP_R > padX &&
                    SHIP_X - SHIP_R < padX + pad.w;

overPad is true when the ship's circle horizontally overlaps the pad.

  • LANDING ASSIST: if you're over the pad, low (shipY > PAD_TOP - 140), and falling fast (shipV > 420), your fall speed is capped at 420. This steadies your descent right before touchdown and makes landing much more forgiving.
  • LANDING: if you're over the pad and the bottom of the ship reaches the pad top (shipY + SHIP_R >= PAD_TOP): if shipV <= 660 you land (landPad() runs, file 02) and the function returns. If you're faster than 660, the ship is snapped to the pad top and it's "TOO FAST TO LAND!" — a crash.
  • MISSING: if the pad's right edge has gone fully behind the ship's left edge (padX + pad.w < SHIP_X - SHIP_R), you flew past it: "MISSED THE PAD!".

COLLISIONS — FLOOR, CEILING, AND GATES

  • FLOOR / CEILING: if the ship's circle touches the bottom (shipY + SHIP_R >= H) or the top (shipY - SHIP_R <= 0), the ship is clamped inside and it's a crash (gameOver with no reason, so it shows the default 'CRASHED!').
  • GATES: for each gate, if the ship's circle overlaps the gate slab horizontally (SHIP_X + SHIP_R > gx(g) and SHIP_X - SHIP_R < gx(g) + GATE_W), then check vertically: the gap is from g.cy - g.gap/2 to g.cy + g.gap/2. If the top of the ship is above the gap's top edge OR the bottom of the ship is below the gap's bottom edge, you hit the slab — crash. The collision uses a FORGIVING circle (radius 15) against the gate rectangles, so near-misses feel fair.

ORDER MATTERS

update() checks things in this order: burn, gravity, scrolling, gate passing, pad, then collisions. The pad is checked BEFORE the gates, so if you land on the pad on the same frame you'd also clip a gate, the landing wins. And landPad() / gameOver() both return immediately, so only one outcome happens per frame.