diff --git a/web/scripts/rocket.test.mjs b/web/scripts/rocket.test.mjs index ec8f972..bebd167 100644 --- a/web/scripts/rocket.test.mjs +++ b/web/scripts/rocket.test.mjs @@ -1,7 +1,7 @@ import assert from "node:assert/strict"; import test from "node:test"; import { - MAX_ROCKETS, SHADOW_EDGE, adoptCircle, applyActivity, createField, fieldActive, holding, infallLook, screenRadius, + ABSORB_LINGER, MAX_ROCKETS, SHADOW_EDGE, adoptCircle, applyActivity, createField, fieldActive, holding, infallLook, screenRadius, starPose, stepField, } from "../src/lib/rocket.ts"; @@ -48,7 +48,7 @@ function fly(s, limit = 12) { return { arrived, wait, flight, sweep: (Math.abs(swept) * 180) / Math.PI }; } -test("every launch, from every shell and phase of the circle, spirals in and reaches the shadow", () => { +test("every launch, from every shell and phase of the circle, spirals in and falls through the shadow to the centre", () => { const sweeps = []; for (const [radius, speed] of SHELLS) { for (const rate of [0, 1]) { @@ -65,6 +65,11 @@ test("every launch, from every shell and phase of the circle, spirals in and rea // The path follows the momentum of the circle round the black hole: it curves, it does not run straight in. assert.ok(result.sweep > 50 && result.sweep < 520, `${label} swept ${result.sweep} degrees`); sweeps.push(result.sweep); + // It ends at the singularity itself, lingers a moment so its trail can finish there, then is removed. + const sunk = s.field.rockets.get("a"); + assert.equal(sunk.x, 0, `${label} did not end at the centre`); + assert.equal(sunk.y, 0, `${label} did not end at the centre`); + s.step(ABSORB_LINGER + 0.1); assert.equal(s.field.rockets.size, 0); } } diff --git a/web/src/components/scene/RocketSprites.tsx b/web/src/components/scene/RocketSprites.tsx index 8138c77..19ded29 100644 --- a/web/src/components/scene/RocketSprites.tsx +++ b/web/src/components/scene/RocketSprites.tsx @@ -1,5 +1,5 @@ import { useId } from "react"; -import { MAX_ROCKETS, infallLook, screenRadius, type Field, type Rocket, type StarPose } from "../../lib/rocket"; +import { ABSORB_LINGER, MAX_ROCKETS, infallLook, screenRadius, type Field, type Rocket, type StarPose } from "../../lib/rocket"; import { ORBIT_TILT } from "../../lib/galaxyLayout"; import { SCENE_UNITS_PER_M } from "../../lib/blackhole/layout"; import { advanceTrail, smoothPath, trailLength, type Trail } from "../../lib/trail"; @@ -193,7 +193,8 @@ function paintRocket(refs: SpriteRefs, slot: number, node: SVGGElement, rocket: } headings.set(node, heading); - const flying = rocket.mode === "launch"; + const absorbed = rocket.mode === "gone" && rocket.arrived; + const flying = rocket.mode === "launch" || absorbed; const look = flying ? infallLook(screenRadius(rocket.x, rocket.y)) : { alpha: 1, stretch: 1, red: 0 }; const radar = lookAt ? lookAt(Math.hypot(sx, sy) / SCENE_UNITS_PER_M) : 0; const along = look.stretch; @@ -201,7 +202,9 @@ function paintRocket(refs: SpriteRefs, slot: number, node: SVGGElement, rocket: const alpha = Math.max(0, rocket.alpha.value) * look.alpha; node.setAttribute("transform", `translate(${sx} ${sy}) rotate(${(heading * 180) / Math.PI}) scale(${along * SIZE} ${across * SIZE})`); node.setAttribute("opacity", String(alpha)); - paintTrail(refs, slot, rocket, sx, sy, alpha, radar, dt); + // The body redshifts out at the shadow edge, but its trail carries on to the centre and fades there. + const trailAlpha = Math.max(0, rocket.alpha.value) * (absorbed ? Math.max(0, 1 - rocket.modeAge / ABSORB_LINGER) : 1); + paintTrail(refs, slot, rocket, sx, sy, trailAlpha, radar, dt); const real = node.firstElementChild as SVGGElement | null; const outline = real?.nextElementSibling as SVGGElement | null; diff --git a/web/src/lib/rocket.ts b/web/src/lib/rocket.ts index e7338db..4f3bc34 100644 --- a/web/src/lib/rocket.ts +++ b/web/src/lib/rocket.ts @@ -10,8 +10,12 @@ 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. */ +/** Screen radius of the shadow, in scene units. A rocket's light is gone by the time it reaches it. */ export const SHADOW_EDGE = 34; +/** A rocket keeps falling through the shadow until it reaches the singularity at the centre, within this radius. */ +export const CORE_RADIUS = 2; +/** How long the trail of a rocket that has reached the centre lingers and fades, in seconds. */ +export const ABSORB_LINGER = 0.3; /** 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. */ @@ -241,9 +245,20 @@ 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; +/** How fast velocity turns toward the centre once inside the shadow, per second. */ +const PLUNGE_RATE = 40; const FALL_STIFFNESS = 90; const FALL_DAMPING = 8; +/** Whether a straight move of (dx, dy) from (x, y) comes within the core radius of the centre, measured on the screen. */ +function passesCore(x: number, y: number, dx: number, dy: number) { + const sy = y * ORBIT_TILT; + const dsy = dy * ORBIT_TILT; + const length2 = dx * dx + dsy * dsy; + const t = length2 > 1e-12 ? Math.min(1, Math.max(0, -(x * dx + sy * dsy) / length2)) : 0; + return Math.hypot(x + dx * t, sy + dsy * t) <= CORE_RADIUS; +} + function stepLaunch(rocket: Rocket, dt: number) { const steps = Math.max(1, Math.ceil(dt / SUBSTEP)); const h = dt / steps; @@ -274,9 +289,30 @@ function stepLaunch(rocket: Rocket, dt: number) { rocket.vx += ax * h; rocket.vy += ay * h; + // Inside the shadow every path leads to the centre: the velocity turns inward at the same speed, so the rocket + // plunges straight to the singularity instead of swinging round it and coming back out. + if (Math.hypot(rocket.x, rocket.y * ORBIT_TILT) < SHADOW_EDGE && rr > 1e-6 && !rocket.failing) { + const speed = Math.hypot(rocket.vx, rocket.vy); + // Faster rockets cross more ground per step, so they turn in proportionally harder and cannot sling past the centre. + const blend = 1 - Math.exp(-PLUNGE_RATE * Math.max(1, speed / 300) * h); + const tx = rocket.vx + (-(rocket.x / rr) * speed - rocket.vx) * blend; + const ty = rocket.vy + (-(rocket.y / rr) * speed - rocket.vy) * blend; + const turned = Math.hypot(tx, ty) || 1; + rocket.vx = (tx / turned) * speed; + rocket.vy = (ty / turned) * speed; + } + // Whether this step carries the rocket through the core: it moves several units a step, so it can cross the centre + // between two checks. + const reaches = !rocket.failing && passesCore(rocket.x, rocket.y, rocket.vx * h, rocket.vy * h); rocket.x += rocket.vx * h; rocket.y += rocket.vy * h; rocket.modeAge += h; + // Reaching the centre ends the flight exactly there, so the trail finishes at the singularity. + if (reaches) { + rocket.x = 0; + rocket.y = 0; + break; + } } 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); @@ -303,8 +339,9 @@ export function stepRocket(rocket: Rocket, star: StarPose, dt: number): FieldEve 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) { + else if (screenRadius(rocket.x, rocket.y) <= CORE_RADIUS) { rocket.mode = "gone"; + rocket.modeAge = 0; rocket.arrived = true; return { kind: "arrive", id: rocket.id }; } @@ -453,6 +490,12 @@ export function stepField(field: Field, dt: number, starOf: (id: string) => Star field.rockets.delete(id); continue; } + // A rocket that reached the centre stays a moment so its trail can be drawn to the singularity and fade there. + if (rocket.mode === "gone" && rocket.arrived) { + rocket.modeAge += dt; + if (rocket.modeAge > ABSORB_LINGER) 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) { @@ -464,7 +507,7 @@ export function stepField(field: Field, dt: number, starOf: (id: string) => Star pulse(field, id); out.push({ kind: "boom", id }); } - field.rockets.delete(id); + if (event.kind !== "arrive") field.rockets.delete(id); } }