space-browser / commits / 5be9bf5
feat: click to orbit a celestial body
@@ -12,7 +12,7 @@ import {
1212 type Ship,
1313 } from './sim';
1414 import { View, CELL, CELL_WORLD, ZOOM_MIN, ZOOM_MAX, type Camera } from './view';
15-import { makeSystem, stepSystem } from './world';
15+import { makeSystem, stepSystem, type Planet } from './world';
1616
1717 const canvas = document.getElementById('view') as HTMLCanvasElement;
1818 const view = new View(canvas);
@@ -34,6 +34,43 @@ const loose: Loose[] = [
3434 ];
3535 let held: Loose | null = null;
3636 let marker: { x: number; y: number } | null = null;
37+
38+// Clear of the hull, so the ring the ship parks on never touches the body.
39+const STANDOFF = 26;
40+const STAR_STANDOFF = 34; // a star kills on contact: its ring is further out
41+const HIT = 1.5; // click radius of a body, as a multiple of its radius: covers its outermost ring
42+
43+// Orbiting parks the ship on a body's ring: the target is a point bolted to the
44+// body's frame, so a planet drags the ship around its star and a star holds still.
45+type Orbit = { body: Planet | 'star'; x: number; y: number; a0: number };
46+let orbit: Orbit | null = null;
47+
48+function bodyAt(x: number, y: number): Planet | 'star' | null {
49+ for (const p of sys.planets) if (Math.hypot(p.x - x, p.y - y) < p.r * HIT) return p;
50+ return Math.hypot(x, y) < sys.star * HIT ? 'star' : null;
51+}
52+
53+function orbitAt(b: Planet | 'star', from: { x: number; y: number }): Orbit {
54+ if (b === 'star') {
55+ const a = Math.atan2(from.y, from.x);
56+ const r = sys.star + STAR_STANDOFF;
57+ return { body: b, x: Math.cos(a) * r, y: Math.sin(a) * r, a0: 0 };
58+ }
59+ // Enter the ring on the side we are already on, so the ship moves radially in.
60+ const a = Math.atan2(from.y - b.y, from.x - b.x);
61+ const r = b.r + STANDOFF;
62+ return { body: b, x: b.x + Math.cos(a) * r, y: b.y + Math.sin(a) * r, a0: b.angle };
63+}
64+
65+// The planet's own rotation carries its ring point around the star with it.
66+function orbitTarget(o: Orbit): { x: number; y: number } {
67+ if (o.body === 'star') return { x: o.x, y: o.y };
68+ const d = o.body.angle - o.a0;
69+ const c = Math.cos(d);
70+ const s = Math.sin(d);
71+ return { x: o.x * c - o.y * s, y: o.x * s + o.y * c };
72+}
73+
3774 const cam: Camera & { follow: boolean } = { x: own.x, y: own.y, zoom: 0.5, follow: true };
3875 const keys = new Set<string>();
3976 let drag: { x: number; y: number } | null = null;
@@ -112,7 +149,13 @@ addEventListener('pointerdown', (e) => {
112149 }
113150 const p = at(e);
114151 held = grab(p.x, p.y);
115- if (held) return;
152+ if (held) {
153+ orbit = null;
154+ return;
155+ }
156+ const body = bodyAt(p.x, p.y);
157+ orbit = body ? orbitAt(body, own) : null;
158+ if (orbit) return; // the frame loop aims at the ring from here on
116159 own.tx = p.x;
117160 own.ty = p.y;
118161 marker = { x: p.x, y: p.y };
@@ -162,6 +205,13 @@ function frame(): void {
162205 last = now;
163206
164207 stepSystem(sys, dt);
208+ if (orbit) {
209+ // Re-aim every frame: the ring point the ship parks on travels with its body.
210+ const t = orbitTarget(orbit);
211+ own.tx = t.x;
212+ own.ty = t.y;
213+ marker = t;
214+ }
165215 step(own, dt);
166216 const k = 1 - Math.exp(-dt * 12);
167217 for (const s of others.values()) {