feat: show what each backup is doing as a light that moves like a rocket
Replace the single-shot meteor with a per-orbit activity system. A backup's state is read from how a small glowing triangle moves, not from a drawing of a rocket. - Events come from status changes between polls, so scheduled backups animate too. A click shows its own verdict until a poll that began after it has landed, which keeps one backup from being animated twice or flipping through a stale status. Loading the page only sets a baseline and never replays history. - Queued waits on its star. Running circles it, climbing and spinning up. - Success waits for a launch window, where the light's own motion already carries it round the black hole, then departs with that velocity. It falls under a pseudo-Newtonian pull after a burn that trims angular momentum to just under the capture limit, so the path spirals with the orbits, always arrives, and never slows on the way in. Near the shadow it reddens, dims and stretches, and the disk spins up as it feeds. - Failure explodes in place or, when the hub says the transfer failed, launches, loses power and falls back. The star keeps its busy look until the outcome and then turns red. Rehearsal is a faint dashed light that lands on its star. - Position and velocity are integrated and a mode change only swaps forces, so every transition is continuous and interruptible. Parameters use the shared spring tokens. Trails follow the path actually flown. - At most ten lights are drawn; reduced motion draws none and shows outcomes at once. - Pure modules with tests for activity, physics and trails. The Playwright capture script now checks lights by data-orbit. The animation guideline in docs/UI.md describes the simulation approach in place of the old single progress value. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -102,23 +102,3 @@ export function groupSnapshots(orbits: Orbit[], snapshots: Snapshot[]): Map<stri
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
export function spiralPoint(source: Vec2, progress: number): Vec2 {
|
||||
const u = Math.min(1, Math.max(0, progress));
|
||||
const angle = Math.atan2(source.y, source.x) + u * Math.PI * 1.15;
|
||||
const radius = Math.hypot(source.x, source.y) * (1 - u ** 1.4);
|
||||
return { x: Math.cos(angle) * radius, y: Math.sin(angle) * radius };
|
||||
}
|
||||
|
||||
export const CAPTURE_HIGHLIGHT_START = 0.94;
|
||||
|
||||
export function capturePhases(progress: number) {
|
||||
const stage = (start: number, end: number) => Math.min(1, Math.max(0, (progress - start) / (end - start)));
|
||||
return {
|
||||
flare: stage(0, 0.06) * (1 - stage(0.08, 0.24)),
|
||||
flight: stage(0.09, 0.79),
|
||||
absorption: stage(0.76, 0.84) * (1 - stage(0.85, CAPTURE_HIGHLIGHT_START)),
|
||||
highlight: stage(CAPTURE_HIGHLIGHT_START, 0.97) * (1 - stage(0.97, 0.998)),
|
||||
failure: stage(0, 0.15) * (1 - stage(0.17, 0.9)),
|
||||
};
|
||||
}
|
||||
|
||||
92
web/src/lib/orbitActivity.ts
Normal file
92
web/src/lib/orbitActivity.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
// What each orbit is doing, derived from backup statuses. This module has no DOM and no animation: it turns a stream
|
||||
// of polled statuses into intents ("this orbit should have a rocket circling it") and one-shot events ("the backup
|
||||
// just succeeded"), which the rocket simulation then plays out.
|
||||
|
||||
export type Intent = "none" | "pad" | "circle" | "ghost";
|
||||
|
||||
/** Why a backup failed, when the hub can tell: before the rocket could leave, or while it carried the payload. */
|
||||
export type FailureCause = "prepare" | "transfer" | "unknown";
|
||||
|
||||
export type ActivityEvent =
|
||||
| { kind: "begin"; id: string; intent: Exclude<Intent, "none"> }
|
||||
| { kind: "launch"; id: string }
|
||||
| { kind: "fail"; id: string; cause: FailureCause }
|
||||
| { kind: "land"; id: string }
|
||||
| { kind: "dismiss"; id: string };
|
||||
|
||||
export type ActivityState = {
|
||||
/** Last status seen for each orbit. Absent until the first observation. */
|
||||
seen: Map<string, string>;
|
||||
intents: Map<string, Intent>;
|
||||
baselined: boolean;
|
||||
};
|
||||
|
||||
export function createActivity(): ActivityState {
|
||||
return { seen: new Map(), intents: new Map(), baselined: false };
|
||||
}
|
||||
|
||||
/** The intent a status asks for while the backup is still going. */
|
||||
export function intentFor(status: string): Intent {
|
||||
switch (status) {
|
||||
case "queued": return "pad";
|
||||
case "running": return "circle";
|
||||
case "rehearsing": return "ghost";
|
||||
default: return "none";
|
||||
}
|
||||
}
|
||||
|
||||
const busy = (status: string | undefined) => !!status && intentFor(status) !== "none";
|
||||
|
||||
/**
|
||||
* Compare the latest statuses with the previous ones and report what happened.
|
||||
*
|
||||
* The first call only records a baseline: an orbit that is already running starts out circling, and one that has
|
||||
* already finished starts out settled, so opening the page never replays history. An orbit that appears later is
|
||||
* treated the same way. A backup that finished between two polls (idle to completed, or failed to completed) still
|
||||
* gets its outcome, because the change itself is the evidence.
|
||||
*/
|
||||
export function observeStatuses(
|
||||
state: ActivityState,
|
||||
statuses: ReadonlyMap<string, string>,
|
||||
causes: ReadonlyMap<string, FailureCause> = new Map(),
|
||||
): ActivityEvent[] {
|
||||
const events: ActivityEvent[] = [];
|
||||
|
||||
for (const [id, next] of statuses) {
|
||||
const previous = state.seen.get(id);
|
||||
const nextIntent = intentFor(next);
|
||||
state.seen.set(id, next);
|
||||
state.intents.set(id, nextIntent);
|
||||
|
||||
if (!state.baselined || previous === undefined) {
|
||||
if (nextIntent !== "none") events.push({ kind: "begin", id, intent: nextIntent });
|
||||
continue;
|
||||
}
|
||||
if (previous === next) continue;
|
||||
|
||||
if (nextIntent !== "none") {
|
||||
// Queued to running is the same rocket changing pace; anything else is a fresh start.
|
||||
events.push({ kind: "begin", id, intent: nextIntent });
|
||||
} else if (next === "completed") {
|
||||
events.push({ kind: "launch", id });
|
||||
} else if (next === "failed") {
|
||||
events.push({ kind: "fail", id, cause: causes.get(id) ?? "unknown" });
|
||||
} else if (next === "rehearsed") {
|
||||
events.push({ kind: "land", id });
|
||||
} else if (busy(previous)) {
|
||||
// The backup stopped without a verdict, for example the orbit was reset: let the rocket go quietly.
|
||||
events.push({ kind: "dismiss", id });
|
||||
}
|
||||
}
|
||||
|
||||
for (const id of [...state.seen.keys()]) {
|
||||
if (statuses.has(id)) continue;
|
||||
// The orbit was deleted.
|
||||
if (busy(state.seen.get(id))) events.push({ kind: "dismiss", id });
|
||||
state.seen.delete(id);
|
||||
state.intents.delete(id);
|
||||
}
|
||||
|
||||
state.baselined = true;
|
||||
return events;
|
||||
}
|
||||
494
web/src/lib/rocket.ts
Normal file
494
web/src/lib/rocket.ts
Normal file
@@ -0,0 +1,494 @@
|
||||
// Rockets for the orbit map. A rocket is a small physical body: its position and velocity are integrated, and the
|
||||
// mode it is in (circling, launching, falling) only decides which forces act on it. A mode change never resets
|
||||
// velocity, so every transition is continuous and any of them can be interrupted by another.
|
||||
//
|
||||
// Everything is in orbit-plane coordinates: the plane the orbits lie in, with the black hole at the origin. The
|
||||
// screen position is (x, y * ORBIT_TILT). Distances that decide what the viewer sees (when a rocket fades into the
|
||||
// black disk) are measured on the screen, because the shadow is round there.
|
||||
|
||||
import { ORBIT_TILT } from "./galaxyLayout.ts";
|
||||
import { ambientSpring, gentleSpring, uiSpring } from "./motion.ts";
|
||||
import type { ActivityEvent, FailureCause, Intent } from "./orbitActivity.ts";
|
||||
|
||||
/** Screen radius of the shadow, in scene units. A rocket is gone by the time it reaches it. */
|
||||
export const SHADOW_EDGE = 34;
|
||||
/** Screen radius where gravitational redshift and tidal stretching begin to show. */
|
||||
export const FADE_START = 80;
|
||||
/** Strength of the black hole's pull, in scene units cubed per second squared. */
|
||||
/** Pull at the reference launch radius. It scales with the cube of the launch radius so every shell takes the same time. */
|
||||
export const GRAVITY = 2.4e6;
|
||||
export const REFERENCE_RADIUS = 190;
|
||||
/**
|
||||
* Schwarzschild radius in scene units (2 M, with M the shadow radius over 3 root 3). The pull follows the
|
||||
* Paczynski-Wiita potential, mu / (r - r_s)^2: it grows sharply toward the horizon the way general relativity's does, so
|
||||
* a body with plenty of sideways momentum still falls in, and it keeps speeding up all the way, with no friction.
|
||||
*/
|
||||
export const HORIZON_RADIUS = 13;
|
||||
/** Keeps the pull finite if a rocket ever strays this close to the horizon. */
|
||||
export const MIN_GAP = 6;
|
||||
export const MAX_ROCKETS = 10;
|
||||
|
||||
const SUBSTEP = 1 / 240;
|
||||
|
||||
export type Spring = { value: number; velocity: number };
|
||||
type Tokens = { stiffness: number; damping: number; mass?: number };
|
||||
|
||||
export const makeSpring = (value = 0): Spring => ({ value, velocity: 0 });
|
||||
|
||||
/** Advance a spring toward a target, with the same physics as the app's shared spring tokens. */
|
||||
export function stepSpring(spring: Spring, target: number, tokens: Tokens, dt: number) {
|
||||
const steps = Math.max(1, Math.ceil(dt / SUBSTEP));
|
||||
const h = dt / steps;
|
||||
const mass = tokens.mass ?? 1;
|
||||
for (let i = 0; i < steps; i++) {
|
||||
spring.velocity += ((tokens.stiffness * (target - spring.value) - tokens.damping * spring.velocity) / mass) * h;
|
||||
spring.value += spring.velocity * h;
|
||||
}
|
||||
}
|
||||
|
||||
export type StarPose = { x: number; y: number; vx: number; vy: number };
|
||||
|
||||
/** A star's pose in the orbit plane, from its slot angle. `rate` is how fast the map is moving right now, 0 to 1. */
|
||||
export function starPose(slot: { radius: number; angle: number; speed: number }, time: number, rate: number): StarPose {
|
||||
const angle = slot.angle + time * slot.speed;
|
||||
const omega = slot.speed * rate;
|
||||
return {
|
||||
x: Math.cos(angle) * slot.radius,
|
||||
y: Math.sin(angle) * slot.radius,
|
||||
vx: -Math.sin(angle) * slot.radius * omega,
|
||||
vy: Math.cos(angle) * slot.radius * omega,
|
||||
};
|
||||
}
|
||||
|
||||
export type RocketMode = "pad" | "circle" | "ghost" | "land" | "launch" | "fall" | "gone";
|
||||
|
||||
export type Rocket = {
|
||||
id: string;
|
||||
mode: RocketMode;
|
||||
age: number;
|
||||
modeAge: number;
|
||||
x: number;
|
||||
y: number;
|
||||
vx: number;
|
||||
vy: number;
|
||||
/** Circling controller, relative to the star: angle, angular speed and radius. */
|
||||
phi: number;
|
||||
omega: Spring;
|
||||
radius: Spring;
|
||||
thrust: Spring;
|
||||
alpha: Spring;
|
||||
/** A launch that will lose power partway and fall back. */
|
||||
failing: boolean;
|
||||
/** Waiting for a good launch window while still circling. */
|
||||
pendingLaunch: boolean;
|
||||
waitAge: number;
|
||||
/** Distance from the black hole when the rocket left its circle: the engine works harder from farther out. */
|
||||
launchRadius: number;
|
||||
/** Angular momentum the launch burn aims for. */
|
||||
targetMomentum: number;
|
||||
arrived: boolean;
|
||||
seed: number;
|
||||
};
|
||||
|
||||
export type Debris = { x: number; y: number; vx: number; vy: number; age: number; life: number; size: number };
|
||||
|
||||
export type FieldEvent = { kind: "arrive" | "impact" | "boom" | "landed"; id: string };
|
||||
|
||||
export type Field = {
|
||||
rockets: Map<string, Rocket>;
|
||||
debris: Debris[];
|
||||
/** Flash at the spot of an explosion, per orbit. */
|
||||
flares: Map<string, Spring>;
|
||||
/** A brief swell of a star when its backup resolves. */
|
||||
pulses: Map<string, Spring>;
|
||||
/** How much the black hole is feeding: drives the disk's spin-up. */
|
||||
absorption: Spring;
|
||||
events: FieldEvent[];
|
||||
};
|
||||
|
||||
export function createField(): Field {
|
||||
return { rockets: new Map(), debris: [], flares: new Map(), pulses: new Map(), absorption: makeSpring(), events: [] };
|
||||
}
|
||||
|
||||
const smoothstep = (edge0: number, edge1: number, x: number) => {
|
||||
const t = Math.min(1, Math.max(0, (x - edge0) / (edge1 - edge0)));
|
||||
return t * t * (3 - 2 * t);
|
||||
};
|
||||
|
||||
export const screenRadius = (x: number, y: number) => Math.hypot(x, y * ORBIT_TILT);
|
||||
|
||||
/**
|
||||
* How a falling rocket looks from far away. Near the black hole its light is redshifted and dims, and tidal forces
|
||||
* stretch it along its fall, so it fades out by the edge of the shadow instead of popping out of existence.
|
||||
*/
|
||||
export function infallLook(radius: number) {
|
||||
// 0 far away, 1 at the shadow edge.
|
||||
const near = 1 - smoothstep(SHADOW_EDGE + 2, FADE_START, radius);
|
||||
return { alpha: 1 - near ** 1.3, stretch: 1 + 2.4 * near, red: near };
|
||||
}
|
||||
|
||||
export function spawnRocket(id: string, star: StarPose, intent: Exclude<Intent, "none">, seed = 1): Rocket {
|
||||
const rocket: Rocket = {
|
||||
id, mode: intent, age: 0, modeAge: 0,
|
||||
x: star.x, y: star.y, vx: star.vx, vy: star.vy,
|
||||
phi: (seed * 2.399) % (Math.PI * 2),
|
||||
omega: makeSpring(0), radius: makeSpring(0), thrust: makeSpring(0), alpha: makeSpring(0),
|
||||
failing: false, pendingLaunch: false, waitAge: 0, launchRadius: Math.hypot(star.x, star.y), targetMomentum: 0, arrived: false, seed,
|
||||
};
|
||||
return rocket;
|
||||
}
|
||||
|
||||
/** Recover the circling controller's state from a rocket's position and velocity, so a body that was flying can circle. */
|
||||
export function adoptCircle(rocket: Rocket, star: StarPose) {
|
||||
const ox = rocket.x - star.x;
|
||||
const oy = rocket.y - star.y;
|
||||
const rho = Math.hypot(ox, oy);
|
||||
const rvx = rocket.vx - star.vx;
|
||||
const rvy = rocket.vy - star.vy;
|
||||
if (rho < 1e-6) {
|
||||
rocket.radius.value = 0;
|
||||
rocket.radius.velocity = 0;
|
||||
rocket.omega.velocity = 0;
|
||||
return;
|
||||
}
|
||||
rocket.phi = Math.atan2(oy, ox);
|
||||
rocket.radius.value = rho;
|
||||
rocket.radius.velocity = (rvx * ox + rvy * oy) / rho;
|
||||
rocket.omega.value = (ox * rvy - oy * rvx) / (rho * rho);
|
||||
rocket.omega.velocity = 0;
|
||||
}
|
||||
|
||||
export function setMode(rocket: Rocket, mode: RocketMode, star: StarPose) {
|
||||
if (rocket.mode === mode) return;
|
||||
const circling = (m: RocketMode) => m === "pad" || m === "circle" || m === "ghost" || m === "land";
|
||||
if (circling(mode) && !circling(rocket.mode)) adoptCircle(rocket, star);
|
||||
rocket.mode = mode;
|
||||
rocket.modeAge = 0;
|
||||
}
|
||||
|
||||
/** Leave the circle. The rocket keeps exactly the velocity it had, so the departure is seamless. */
|
||||
export function startLaunch(rocket: Rocket, failing = false) {
|
||||
rocket.mode = "launch";
|
||||
rocket.modeAge = 0;
|
||||
rocket.failing = failing;
|
||||
rocket.pendingLaunch = false;
|
||||
rocket.launchRadius = Math.hypot(rocket.x, rocket.y);
|
||||
// The capture limit for a body falling from rest, in this launch's own scale of time: 4 sqrt(mu M).
|
||||
const pull = GRAVITY * Math.pow(rocket.launchRadius / REFERENCE_RADIUS, 3);
|
||||
rocket.targetMomentum = MOMENTUM_FRACTION * 4 * Math.sqrt(pull * (HORIZON_RADIUS / 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask a circling rocket to leave. It does not leave at once: like a real transfer burn it waits for the point on its
|
||||
* circle where its own motion already carries it the right way round the black hole and a little inward, then departs
|
||||
* with the velocity it has. The curved path is momentum and gravity, not steering.
|
||||
*/
|
||||
export function requestLaunch(rocket: Rocket, failing = false) {
|
||||
rocket.pendingLaunch = true;
|
||||
rocket.failing = failing;
|
||||
rocket.waitAge = 0;
|
||||
}
|
||||
|
||||
/** Is the rocket moving prograde around the hole and a little inward, so releasing it now makes a good spiral? */
|
||||
export function inLaunchWindow(rocket: Rocket) {
|
||||
const rr = Math.hypot(rocket.x, rocket.y);
|
||||
const speed = Math.hypot(rocket.vx, rocket.vy);
|
||||
if (rr < 1e-6 || speed < 1e-6) return true;
|
||||
const inward = -(rocket.x * rocket.vx + rocket.y * rocket.vy) / (rr * speed);
|
||||
const prograde = (rocket.x * rocket.vy - rocket.y * rocket.vx) / (rr * speed);
|
||||
// Mostly tangential, a little inward: between roughly 5 and 33 degrees inward of the prograde tangent.
|
||||
return prograde > 0.8 && inward > 0.09 && inward < 0.55;
|
||||
}
|
||||
|
||||
const CIRCLE = {
|
||||
pad: { radius: 0, omega: 0.5, thrust: 0.12, alpha: 1 },
|
||||
ghost: { radius: 12, omega: 2.4, thrust: 0.18, alpha: 0.5 },
|
||||
land: { radius: 0, omega: 5, thrust: 0, alpha: 0 },
|
||||
};
|
||||
|
||||
/** Where the circling controller is heading. A working rocket climbs and spins up the longer it has been preparing. */
|
||||
function circleTargets(rocket: Rocket) {
|
||||
if (rocket.mode === "circle") {
|
||||
const spin = 1 - Math.exp(-rocket.modeAge / 7);
|
||||
// Waiting to depart, the engine lights and the lap quickens.
|
||||
if (rocket.pendingLaunch) return { radius: 12, omega: 4.2, thrust: 1, alpha: 1 };
|
||||
return { radius: 9 + 6 * spin, omega: 2 + 3 * spin, thrust: 0.3 + 0.35 * spin, alpha: 1 };
|
||||
}
|
||||
return CIRCLE[rocket.mode as keyof typeof CIRCLE] ?? CIRCLE.pad;
|
||||
}
|
||||
|
||||
function stepCircling(rocket: Rocket, star: StarPose, dt: number) {
|
||||
const target = circleTargets(rocket);
|
||||
stepSpring(rocket.radius, target.radius, gentleSpring, dt);
|
||||
stepSpring(rocket.omega, target.omega, ambientSpring, dt);
|
||||
stepSpring(rocket.thrust, target.thrust, uiSpring, dt);
|
||||
stepSpring(rocket.alpha, target.alpha, gentleSpring, dt);
|
||||
rocket.phi += rocket.omega.value * dt;
|
||||
const rho = Math.max(0, rocket.radius.value);
|
||||
const cos = Math.cos(rocket.phi);
|
||||
const sin = Math.sin(rocket.phi);
|
||||
rocket.x = star.x + rho * cos;
|
||||
rocket.y = star.y + rho * sin;
|
||||
rocket.vx = star.vx + rocket.radius.velocity * cos - rho * rocket.omega.value * sin;
|
||||
rocket.vy = star.vy + rocket.radius.velocity * sin + rho * rocket.omega.value * cos;
|
||||
}
|
||||
|
||||
const LAUNCH_BURN = 0.6;
|
||||
/** How hard the burn pulls angular momentum toward its target, per second. */
|
||||
const BURN_GAIN = 5;
|
||||
/** The fraction of the capture limit the burn aims for: close enough to sweep a long curve, far enough to fall in. */
|
||||
const MOMENTUM_FRACTION = 0.97;
|
||||
/** The longest a rocket waits for a good launch window: about one lap of its circle. */
|
||||
const MAX_WAIT = 3;
|
||||
const FAILING_CUT = 0.55;
|
||||
const FALL_STIFFNESS = 90;
|
||||
const FALL_DAMPING = 8;
|
||||
|
||||
function stepLaunch(rocket: Rocket, dt: number) {
|
||||
const steps = Math.max(1, Math.ceil(dt / SUBSTEP));
|
||||
const h = dt / steps;
|
||||
for (let i = 0; i < steps; i++) {
|
||||
const r2 = rocket.x * rocket.x + rocket.y * rocket.y;
|
||||
const rr = Math.sqrt(r2);
|
||||
const gap = Math.max(rr - HORIZON_RADIUS, MIN_GAP);
|
||||
const pull = (GRAVITY * Math.pow(rocket.launchRadius / REFERENCE_RADIUS, 3)) / (gap * gap * Math.max(rr, 1e-6));
|
||||
let ax = -pull * rocket.x;
|
||||
let ay = -pull * rocket.y;
|
||||
|
||||
// The engine burn trims the rocket's angular momentum toward a fixed target just under the capture limit, with a
|
||||
// prograde or retrograde push along the circle's own direction. Every launch then spirals the same way round, as far
|
||||
// as its momentum carries it, and still falls in. After the burn it coasts and gravity does the rest.
|
||||
const burn = Math.exp(-rocket.modeAge / LAUNCH_BURN);
|
||||
if (rr > 1e-6 && burn > 1e-3) {
|
||||
const momentum = rocket.x * rocket.vy - rocket.y * rocket.vx;
|
||||
const push = (BURN_GAIN * (rocket.targetMomentum - momentum) * burn) / rr;
|
||||
ax += (-rocket.y / rr) * push;
|
||||
ay += (rocket.x / rr) * push;
|
||||
}
|
||||
|
||||
// A backstop so no launch can orbit forever: after a few seconds the pull gets a nudge.
|
||||
if (rocket.modeAge > 6 && rr > 1e-6) {
|
||||
ax -= (rocket.x / rr) * 500;
|
||||
ay -= (rocket.y / rr) * 500;
|
||||
}
|
||||
|
||||
rocket.vx += ax * h;
|
||||
rocket.vy += ay * h;
|
||||
rocket.x += rocket.vx * h;
|
||||
rocket.y += rocket.vy * h;
|
||||
rocket.modeAge += h;
|
||||
}
|
||||
stepSpring(rocket.thrust, rocket.failing && rocket.modeAge > FAILING_CUT ? 0 : rocket.modeAge < LAUNCH_BURN ? 1 : 0.25, uiSpring, dt);
|
||||
stepSpring(rocket.alpha, 1, gentleSpring, dt);
|
||||
}
|
||||
|
||||
function stepFall(rocket: Rocket, star: StarPose, dt: number) {
|
||||
const steps = Math.max(1, Math.ceil(dt / SUBSTEP));
|
||||
const h = dt / steps;
|
||||
for (let i = 0; i < steps; i++) {
|
||||
const ax = -FALL_STIFFNESS * (rocket.x - star.x) - FALL_DAMPING * (rocket.vx - star.vx);
|
||||
const ay = -FALL_STIFFNESS * (rocket.y - star.y) - FALL_DAMPING * (rocket.vy - star.vy);
|
||||
rocket.vx += ax * h;
|
||||
rocket.vy += ay * h;
|
||||
rocket.x += rocket.vx * h;
|
||||
rocket.y += rocket.vy * h;
|
||||
rocket.modeAge += h;
|
||||
}
|
||||
stepSpring(rocket.thrust, 0, uiSpring, dt);
|
||||
stepSpring(rocket.alpha, 1, gentleSpring, dt);
|
||||
}
|
||||
|
||||
export function stepRocket(rocket: Rocket, star: StarPose, dt: number): FieldEvent | undefined {
|
||||
rocket.age += dt;
|
||||
if (rocket.mode === "launch") {
|
||||
stepLaunch(rocket, dt);
|
||||
if (rocket.failing && rocket.modeAge > FAILING_CUT) setMode(rocket, "fall", star);
|
||||
else if (screenRadius(rocket.x, rocket.y) <= SHADOW_EDGE) {
|
||||
rocket.mode = "gone";
|
||||
rocket.arrived = true;
|
||||
return { kind: "arrive", id: rocket.id };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
if (rocket.mode === "fall") {
|
||||
stepFall(rocket, star, dt);
|
||||
const gap = Math.hypot(rocket.x - star.x, rocket.y - star.y);
|
||||
const closing = Math.hypot(rocket.vx - star.vx, rocket.vy - star.vy);
|
||||
if (rocket.modeAge > 0.4 && gap < 2.5 && closing < 20) {
|
||||
rocket.mode = "gone";
|
||||
return { kind: "impact", id: rocket.id };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
rocket.modeAge += dt;
|
||||
stepCircling(rocket, star, dt);
|
||||
if (rocket.pendingLaunch) {
|
||||
rocket.waitAge += dt;
|
||||
if (inLaunchWindow(rocket) || rocket.waitAge > MAX_WAIT) startLaunch(rocket, rocket.failing);
|
||||
}
|
||||
if (rocket.mode === "land" && rocket.alpha.value < 0.03 && rocket.radius.value < 1.5) {
|
||||
rocket.mode = "gone";
|
||||
return { kind: "landed", id: rocket.id };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** A deterministic scatter, so an explosion looks the same in a test and on screen. */
|
||||
function scatter(seed: number, index: number) {
|
||||
const value = Math.sin(seed * 127.1 + index * 311.7) * 43758.5453;
|
||||
return value - Math.floor(value);
|
||||
}
|
||||
|
||||
export function explode(rocket: Rocket): Debris[] {
|
||||
const pieces: Debris[] = [];
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const angle = scatter(rocket.seed, i) * Math.PI * 2;
|
||||
const speed = 28 + 70 * scatter(rocket.seed, i + 40);
|
||||
pieces.push({
|
||||
x: rocket.x, y: rocket.y,
|
||||
vx: rocket.vx * 0.35 + Math.cos(angle) * speed,
|
||||
vy: rocket.vy * 0.35 + Math.sin(angle) * speed,
|
||||
age: 0, life: 0.7 + 0.6 * scatter(rocket.seed, i + 80), size: 0.8 + 1.3 * scatter(rocket.seed, i + 120),
|
||||
});
|
||||
}
|
||||
return pieces;
|
||||
}
|
||||
|
||||
const DEBRIS_DRAG = 2.6;
|
||||
|
||||
function stepDebris(debris: Debris[], dt: number) {
|
||||
const slow = Math.exp(-DEBRIS_DRAG * dt);
|
||||
for (const piece of debris) {
|
||||
piece.age += dt;
|
||||
piece.vx *= slow;
|
||||
piece.vy *= slow;
|
||||
piece.x += piece.vx * dt;
|
||||
piece.y += piece.vy * dt;
|
||||
}
|
||||
return debris.filter((piece) => piece.age < piece.life);
|
||||
}
|
||||
|
||||
function pulse(field: Field, id: string) {
|
||||
const swell = makeSpring(1);
|
||||
swell.velocity = 0;
|
||||
field.pulses.set(id, swell);
|
||||
}
|
||||
|
||||
function burst(field: Field, rocket: Rocket) {
|
||||
field.debris.push(...explode(rocket));
|
||||
field.flares.set(rocket.id, makeSpring(1));
|
||||
field.events.push({ kind: "boom", id: rocket.id });
|
||||
pulse(field, rocket.id);
|
||||
}
|
||||
|
||||
/** Apply something that happened to an orbit's backup. Returns false when there is no room for another rocket. */
|
||||
export function applyActivity(field: Field, event: ActivityEvent, star: StarPose, seed = 1): boolean {
|
||||
const existing = field.rockets.get(event.id);
|
||||
|
||||
if (event.kind === "dismiss") {
|
||||
if (existing && existing.mode !== "launch") setMode(existing, "land", star);
|
||||
return true;
|
||||
}
|
||||
|
||||
const ensure = (intent: Exclude<Intent, "none">) => {
|
||||
if (existing && existing.mode !== "gone") return existing;
|
||||
if (field.rockets.size >= MAX_ROCKETS) return undefined;
|
||||
const rocket = spawnRocket(event.id, star, intent, seed);
|
||||
field.rockets.set(event.id, rocket);
|
||||
return rocket;
|
||||
};
|
||||
|
||||
if (event.kind === "begin") {
|
||||
const rocket = ensure(event.intent);
|
||||
if (!rocket) return false;
|
||||
// A new run on an orbit whose rocket is still falling or flying takes over from where the rocket is.
|
||||
rocket.pendingLaunch = false;
|
||||
setMode(rocket, event.intent, star);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (event.kind === "launch") {
|
||||
const rocket = ensure("circle");
|
||||
if (!rocket) return false;
|
||||
if (rocket.mode === "pad" || rocket.mode === "ghost" || rocket.mode === "land") setMode(rocket, "circle", star);
|
||||
if (rocket.mode === "circle" && rocket.alpha.value < 0.4) rocket.alpha.value = 1;
|
||||
if (rocket.mode === "circle") requestLaunch(rocket);
|
||||
else startLaunch(rocket);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (event.kind === "land") {
|
||||
if (existing) setMode(existing, "land", star);
|
||||
return true;
|
||||
}
|
||||
|
||||
// fail
|
||||
const cause: FailureCause = event.cause;
|
||||
const rocket = ensure("circle");
|
||||
if (!rocket) {
|
||||
field.flares.set(event.id, makeSpring(1));
|
||||
field.events.push({ kind: "boom", id: event.id });
|
||||
return false;
|
||||
}
|
||||
if (cause === "transfer") {
|
||||
if (rocket.mode === "pad" || rocket.mode === "ghost" || rocket.mode === "land") setMode(rocket, "circle", star);
|
||||
if (rocket.mode === "circle") {
|
||||
rocket.alpha.value = 1;
|
||||
requestLaunch(rocket, true);
|
||||
} else startLaunch(rocket, true);
|
||||
return true;
|
||||
}
|
||||
burst(field, rocket);
|
||||
field.rockets.delete(event.id);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function stepField(field: Field, dt: number, starOf: (id: string) => StarPose | undefined): FieldEvent[] {
|
||||
const out: FieldEvent[] = field.events.splice(0);
|
||||
let feeding = 0;
|
||||
|
||||
for (const [id, rocket] of field.rockets) {
|
||||
const star = starOf(id);
|
||||
if (!star) {
|
||||
field.rockets.delete(id);
|
||||
continue;
|
||||
}
|
||||
const event = stepRocket(rocket, star, dt);
|
||||
if (rocket.mode === "launch") feeding = Math.max(feeding, smoothstep(140, SHADOW_EDGE, screenRadius(rocket.x, rocket.y)));
|
||||
if (event) {
|
||||
out.push(event);
|
||||
if (event.kind === "arrive") pulse(field, id);
|
||||
if (event.kind === "impact") {
|
||||
// The rocket comes down hard: the star flares and the failure is final.
|
||||
field.flares.set(id, makeSpring(0.6));
|
||||
pulse(field, id);
|
||||
out.push({ kind: "boom", id });
|
||||
}
|
||||
field.rockets.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
stepSpring(field.absorption, feeding, ambientSpring, dt);
|
||||
field.debris = stepDebris(field.debris, dt);
|
||||
for (const [id, flare] of field.flares) {
|
||||
stepSpring(flare, 0, gentleSpring, dt);
|
||||
if (flare.value < 0.01 && Math.abs(flare.velocity) < 0.05) field.flares.delete(id);
|
||||
}
|
||||
for (const [id, swell] of field.pulses) {
|
||||
stepSpring(swell, 0, gentleSpring, dt);
|
||||
if (swell.value < 0.01 && Math.abs(swell.velocity) < 0.05) field.pulses.delete(id);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Whether anything in the field still needs frames. */
|
||||
export function fieldActive(field: Field): boolean {
|
||||
return field.rockets.size > 0 || field.debris.length > 0 || field.flares.size > 0 || field.pulses.size > 0
|
||||
|| Math.abs(field.absorption.value) > 0.004 || Math.abs(field.absorption.velocity) > 0.004;
|
||||
}
|
||||
|
||||
/** True while an orbit's outcome is still playing out, so its star keeps looking busy until the moment of truth. */
|
||||
export function holding(field: Field, id: string): boolean {
|
||||
const rocket = field.rockets.get(id);
|
||||
return !!rocket && (rocket.pendingLaunch || rocket.mode === "launch" || rocket.mode === "fall");
|
||||
}
|
||||
57
web/src/lib/trail.ts
Normal file
57
web/src/lib/trail.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
// A trail is the path a thing actually flew: its recent positions, drawn as a smooth curve. Because it is built from
|
||||
// where the rocket has been rather than from where it is pointing, it curves when the path curves, loops when the rocket
|
||||
// circles, and lengthens as the rocket speeds up.
|
||||
|
||||
export type TrailPoint = { x: number; y: number; t: number };
|
||||
export type Trail = { id: string; clock: number; points: TrailPoint[] };
|
||||
|
||||
/** How much of its recent path a rocket remembers, in seconds of scene time. */
|
||||
export const TRAIL_SECONDS = 0.65;
|
||||
export const TRAIL_MAX_POINTS = 48;
|
||||
/** Points closer together than this add nothing to the curve. */
|
||||
export const TRAIL_MIN_SPACING = 0.7;
|
||||
/** A move bigger than this in one frame is a teleport, not motion: the old path no longer applies. */
|
||||
export const TRAIL_JUMP = 60;
|
||||
|
||||
export function createTrail(id: string): Trail {
|
||||
return { id, clock: 0, points: [] };
|
||||
}
|
||||
|
||||
/** Record the rocket's position for this frame. Returns the same trail, or a fresh one if the path was broken. */
|
||||
export function advanceTrail(trail: Trail | undefined, id: string, x: number, y: number, dt: number): Trail {
|
||||
const tail = trail?.points.at(-1);
|
||||
let current = trail;
|
||||
if (!current || current.id !== id || (tail && Math.hypot(x - tail.x, y - tail.y) > TRAIL_JUMP)) current = createTrail(id);
|
||||
current.clock += dt;
|
||||
const last = current.points.at(-1);
|
||||
if (!last || Math.hypot(x - last.x, y - last.y) >= TRAIL_MIN_SPACING) current.points.push({ x, y, t: current.clock });
|
||||
else last.t = current.clock;
|
||||
while (
|
||||
current.points.length > TRAIL_MAX_POINTS
|
||||
|| (current.points.length > 2 && current.clock - current.points[0].t > TRAIL_SECONDS)
|
||||
) current.points.shift();
|
||||
return current;
|
||||
}
|
||||
|
||||
/** Length of the trail along the path it was flown. */
|
||||
export function trailLength(points: readonly { x: number; y: number }[]) {
|
||||
let length = 0;
|
||||
for (let i = 1; i < points.length; i++) length += Math.hypot(points[i].x - points[i - 1].x, points[i].y - points[i - 1].y);
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* A smooth path through a trail: quadratic segments that run through the midpoints of the polyline, so the corners
|
||||
* between frames are rounded off while the curve stays within the path that was flown.
|
||||
*/
|
||||
export function smoothPath(points: readonly { x: number; y: number }[]) {
|
||||
if (points.length < 2) return "";
|
||||
let d = `M${points[0].x.toFixed(2)} ${points[0].y.toFixed(2)}`;
|
||||
for (let i = 1; i < points.length - 1; i++) {
|
||||
const mx = (points[i].x + points[i + 1].x) / 2;
|
||||
const my = (points[i].y + points[i + 1].y) / 2;
|
||||
d += `Q${points[i].x.toFixed(2)} ${points[i].y.toFixed(2)} ${mx.toFixed(2)} ${my.toFixed(2)}`;
|
||||
}
|
||||
const last = points[points.length - 1];
|
||||
return `${d}L${last.x.toFixed(2)} ${last.y.toFixed(2)}`;
|
||||
}
|
||||
Reference in New Issue
Block a user