@johnmorrisdotca/wakusei-planets 0.53.0 → 0.55.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,248 @@
1
+ // How the camera holds its direction while time runs: fixed to the stars (the view's own way, and the default), turning with the body in focus (its ground stays still under the viewer as it spins), or turning with a
2
+ // pair (the body in focus and a companion keep the same framing as the two move: the Sun stays behind Mercury at the back left while the camera circles Mercury). Pure: no DOM, no three.js, no clock.
3
+ //
4
+ // The camera looks at the body in focus from a direction `aim`, a unit vector from the body to the camera. Fixed to the stars, `aim` is the same vector for ever. In the other two frames it is carried by a basis that
5
+ // turns with the thing it is held to: `aim` at a later moment is the vector that has the same three numbers in that moment's basis as `aim` had in the first moment's. The body's basis is its meridian, its east and
6
+ // its pole; the pair's is the direction to the companion, the side of it round the axis of their orbit, and the axis. A drag changes `aim` and the new framing is then carried the same way.
7
+ import { BODIES } from "../facts.js";
8
+ import { orientation, moonOffset } from "../scene/orient.js";
9
+ import { add, cross, dot, RAD, scale, sub } from "../vec.js";
10
+ import { frameAt } from "./frame.js";
11
+ import { hostOf } from "./bodies.js";
12
+ import { moonDistance, placeBodies, radiusIn } from "./scale.js";
13
+ /**
14
+ * The frames, in the order a control lists them.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { FRAMES } from "@johnmorrisdotca/wakusei-planets/space";
19
+ *
20
+ * console.log(FRAMES.join(", "));
21
+ * ```
22
+ */
23
+ export const FRAMES = ["stars", "body", "pair"];
24
+ /**
25
+ * A frame from what a page wrote (an attribute or a property): `body` and `pair` as written, anything else `stars`.
26
+ *
27
+ * @param value - What was written.
28
+ * @returns The frame.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import { readFrame } from "@johnmorrisdotca/wakusei-planets/space";
33
+ *
34
+ * console.log(readFrame("pair"), readFrame("nonsense"));
35
+ * ```
36
+ */
37
+ export function readFrame(value) {
38
+ return value === "body" || value === "pair" ? value : "stars";
39
+ }
40
+ const length = (v) => Math.hypot(v[0], v[1], v[2]);
41
+ const unit = (v) => {
42
+ const n = length(v);
43
+ return n === 0 ? [0, 0, 0] : [v[0] / n, v[1] / n, v[2] / n];
44
+ };
45
+ /**
46
+ * Whether a body has a rotation the package knows: a moon that keeps one face to its planet, or a body whose prime meridian turns. A body without one cannot be turned with.
47
+ *
48
+ * @param id - A body's name.
49
+ * @returns True when the body's ground turns in a way the package can work out.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * import { hasRotation } from "@johnmorrisdotca/wakusei-planets/space";
54
+ *
55
+ * console.log(hasRotation("mars"));
56
+ * ```
57
+ */
58
+ export function hasRotation(id) {
59
+ const facts = BODIES[id];
60
+ return facts.synchronous === true || facts.prime.rate !== 0;
61
+ }
62
+ /**
63
+ * The basis that turns with a body's ground at a moment: its prime meridian, its east and its north pole, in the scene's axes. Null for a body with no known rotation.
64
+ *
65
+ * @param id - A body's name.
66
+ * @param ms - The moment, in milliseconds since the Unix epoch.
67
+ * @returns The basis, or null.
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * import { bodyBasis } from "@johnmorrisdotca/wakusei-planets/space";
72
+ *
73
+ * const basis = bodyBasis("mars", Date.UTC(2026, 10, 2));
74
+ * console.log(basis !== null);
75
+ * ```
76
+ */
77
+ export function bodyBasis(id, ms) {
78
+ if (!hasRotation(id))
79
+ return null;
80
+ const { pole, meridian } = orientation(id, ms);
81
+ return { a: meridian, b: cross(pole, meridian), c: pole };
82
+ }
83
+ /**
84
+ * The basis that turns with a pair: `a` towards the companion, `c` the axis (the part of `axis` square to `a`, so `axis` need not be), and `b` completing it. As the companion goes round the body in focus the basis
85
+ * turns about the axis by the same angle, and never jumps while `toward` is not along `axis`, which the axis of the pair's own orbit (`pairAxis`) never is.
86
+ *
87
+ * @param toward - The direction from the body in focus to the companion.
88
+ * @param axis - A direction the pair turns about.
89
+ * @returns The basis.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * import { pairBasis } from "@johnmorrisdotca/wakusei-planets/space";
94
+ *
95
+ * const basis = pairBasis([1, 0, 0], [0, 1, 0]);
96
+ * console.log(basis.b);
97
+ * ```
98
+ */
99
+ export function pairBasis(toward, axis) {
100
+ const a = unit(toward);
101
+ const across = sub(axis, scale(a, dot(axis, a)));
102
+ const c = length(across) < 1e-12 ? unit(cross(a, Math.abs(a[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0])) : unit(across);
103
+ return { a, b: cross(c, a), c };
104
+ }
105
+ /**
106
+ * The axis a pair turns about: the direction of the angular momentum of the companion's motion about the body in focus at a moment, from where `toward` puts the companion half an hour either side. It is the same axis
107
+ * all the way round an orbit (a moon of Uranus, which goes round over the poles, gets its own), so a basis made with it turns about it and never jumps.
108
+ *
109
+ * @param toward - The direction from the body in focus to the companion at a moment.
110
+ * @param ms - The moment the axis is taken at.
111
+ * @returns A unit vector.
112
+ *
113
+ * @example
114
+ * ```ts
115
+ * import { pairAxis } from "@johnmorrisdotca/wakusei-planets/space";
116
+ *
117
+ * const axis = pairAxis((ms) => [Math.cos(ms / 1e8), 0, Math.sin(ms / 1e8)], 0);
118
+ * console.log(axis.map((n) => n.toFixed(2)).join(", "));
119
+ * ```
120
+ */
121
+ export function pairAxis(toward, ms) {
122
+ const half = 1_800_000;
123
+ const was = unit(toward(ms - half));
124
+ const will = unit(toward(ms + half));
125
+ const now = unit(toward(ms));
126
+ const axis = cross(now, sub(will, was));
127
+ return length(axis) < 1e-15 ? unit(cross(now, Math.abs(now[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0])) : unit(axis);
128
+ }
129
+ /**
130
+ * A direction carried from one basis to another: the vector that has in `to` the three numbers `aim` has in `from`.
131
+ *
132
+ * @param aim - The direction, from the body in focus to the camera.
133
+ * @param from - The basis it was framed in.
134
+ * @param to - The basis at the later moment.
135
+ * @returns The direction at the later moment.
136
+ *
137
+ * @example
138
+ * ```ts
139
+ * import { carry } from "@johnmorrisdotca/wakusei-planets/space";
140
+ *
141
+ * const turned = carry([1, 0, 0], { a: [1, 0, 0], b: [0, 1, 0], c: [0, 0, 1] }, { a: [0, 1, 0], b: [-1, 0, 0], c: [0, 0, 1] });
142
+ * console.log(turned.map((n) => n.toFixed(2)).join(", "));
143
+ * ```
144
+ */
145
+ export function carry(aim, from, to) {
146
+ const x = dot(aim, from.a);
147
+ const y = dot(aim, from.b);
148
+ const z = dot(aim, from.c);
149
+ return [to.a[0] * x + to.b[0] * y + to.c[0] * z, to.a[1] * x + to.b[1] * y + to.c[1] * z, to.a[2] * x + to.b[2] * y + to.c[2] * z];
150
+ }
151
+ /**
152
+ * A direction pushed away from the line to a companion as far as it takes to keep the companion out of sight behind the body in focus, and no further: the camera on the far side of the body from the companion, looking at
153
+ * the body, has the companion directly behind it. Where `aim` is within `least` radians of the direction away from the companion it is moved to that distance, along the way from the companion's far side that it leans
154
+ * to, so the change is continuous (the companion passing behind the body slides the camera to one side and never makes it jump).
155
+ *
156
+ * @param aim - The direction from the body in focus to the camera.
157
+ * @param toward - The direction from the body in focus to the companion.
158
+ * @param least - How far from straight behind it the camera may be, in radians.
159
+ * @returns The direction, `aim` itself when it is already clear.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * import { keepClear } from "@johnmorrisdotca/wakusei-planets/space";
164
+ *
165
+ * console.log(keepClear([-1, 0, 0], [1, 0, 0], 0.3).map((n) => n.toFixed(2)).join(", "));
166
+ * ```
167
+ */
168
+ export function keepClear(aim, toward, least) {
169
+ const away = scale(unit(toward), -1);
170
+ const a = unit(aim);
171
+ const angle = Math.atan2(length(cross(a, away)), dot(a, away));
172
+ if (angle >= least)
173
+ return a;
174
+ const side = sub(a, scale(away, dot(a, away)));
175
+ const out = length(side) < 1e-12 ? unit(cross(away, Math.abs(away[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0])) : unit(side);
176
+ return add(scale(away, Math.cos(least)), scale(out, Math.sin(least)));
177
+ }
178
+ /**
179
+ * Where a body is at a moment, in the scene's places for a scale (Earth radii from the Earth's centre) and its drawn radius. A moon is at its planet's place and offset by its orbit at the scale's distance.
180
+ *
181
+ * @param id - A body's name.
182
+ * @param mode - The scale.
183
+ * @param ms - The moment.
184
+ * @returns The place.
185
+ *
186
+ * @example
187
+ * ```ts
188
+ * import { holdSite } from "@johnmorrisdotca/wakusei-planets/space";
189
+ *
190
+ * console.log(holdSite("sun", "compressed", Date.UTC(2026, 10, 2)).radius > 0);
191
+ * ```
192
+ */
193
+ export function holdSite(id, mode, ms) {
194
+ const places = placeBodies(mode, frameAt(ms));
195
+ const host = hostOf(id);
196
+ if (host === undefined || id === "moon")
197
+ return { at: places[id], radius: radiusIn(id, mode) };
198
+ const real = moonOffset(id, ms);
199
+ const d = length(real);
200
+ const hostRadius = radiusIn(host, mode);
201
+ const k = (d === 0 ? 1 : moonDistance(mode, host, d) / d) * hostRadius;
202
+ const centre = places[host];
203
+ return { at: add(centre, scale(real, k)), radius: Math.max(1e-6, (BODIES[id].radiusKm / BODIES[host].radiusKm) * hostRadius) };
204
+ }
205
+ /**
206
+ * The unit direction from one body to another at a moment, in the scene's places for a scale.
207
+ *
208
+ * @param from - The body in focus.
209
+ * @param to - The companion.
210
+ * @param mode - The scale.
211
+ * @param ms - The moment.
212
+ * @returns A unit vector.
213
+ *
214
+ * @example
215
+ * ```ts
216
+ * import { directionBetween } from "@johnmorrisdotca/wakusei-planets/space";
217
+ *
218
+ * console.log(directionBetween("mercury", "sun", "compressed", Date.UTC(2026, 10, 2)).length);
219
+ * ```
220
+ */
221
+ export function directionBetween(from, to, mode, ms) {
222
+ return unit(sub(holdSite(to, mode, ms).at, holdSite(from, mode, ms).at));
223
+ }
224
+ /**
225
+ * The controls' yaw and tilt, in degrees, that make the stars' direction (`groundDirection`) at a moment the given direction, for a view whose centre is a longitude and latitude: the inverse of how the view turns yaw and tilt
226
+ * into a direction. A frame that carries the direction uses it to take the stars' direction again from now, with the direction it has now.
227
+ *
228
+ * @param aim - The direction, from the body in focus to the camera.
229
+ * @param centre - The view's centre, east longitude and north latitude in degrees.
230
+ * @param ms - The moment the stars' direction is taken at.
231
+ * @returns The yaw and the tilt.
232
+ *
233
+ * @example
234
+ * ```ts
235
+ * import { controlsFor, groundDirection } from "@johnmorrisdotca/wakusei-planets/space";
236
+ *
237
+ * const now = Date.UTC(2026, 10, 2);
238
+ * const { yaw, tilt } = controlsFor(groundDirection(20 - 30, 10 - 5, now), { lon: 20, lat: 10 }, now);
239
+ * console.log(yaw.toFixed(1), tilt.toFixed(1));
240
+ * ```
241
+ */
242
+ export function controlsFor(aim, centre, ms) {
243
+ const { pole, meridian } = orientation("earth", ms);
244
+ const east = cross(pole, meridian);
245
+ const lat = Math.asin(Math.max(-1, Math.min(1, dot(aim, pole)))) / RAD;
246
+ const lon = Math.atan2(dot(aim, east), dot(aim, meridian)) / RAD;
247
+ return { yaw: ((((centre.lon - lon + 180) % 360) + 360) % 360) - 180, tilt: centre.lat - lat };
248
+ }
@@ -8,7 +8,8 @@ export { besideMoon, BESIDE_RADII, type BesideCamera, type BesideInput } from ".
8
8
  export { ARROWS_PER_ORBIT, arrowCount, arrowSites, MAX_ARROWS_PER_ORBIT, chevron, helpSpin, readHelpPlace, spinArrow, type HelpPlace, type OrbitArrow } from "./arrows.ts";
9
9
  export { spinAxis, type SpinAxis } from "./spin.ts";
10
10
  export { COVER_ARROW_GAP, COVER_MIN_PX, coverArrows, coveredBy, type CoverArrow, type CoverEntry } from "./covered.ts";
11
- export { TRAVEL_CLEARANCE, arrivalSide, travelArrival, travelPlan, travelPose, type ArrivalCompanion, type TravelBody, type TravelInput, type TravelKind, type TravelPlan, type TravelSample, type TravelSight, type TravelView } from "./travel.ts";
11
+ export { TRAVEL_BUDGET_MS, TRAVEL_CLEARANCE, arrivalSide, travelArrival, travelPlan, travelPose, type ArrivalCompanion, type TravelBody, type TravelInput, type TravelKind, type TravelPlan, type TravelSample, type TravelSight, type TravelView } from "./travel.ts";
12
+ export { bodyBasis, carry, controlsFor, directionBetween, FRAMES, hasRotation, holdSite, keepClear, pairAxis, pairBasis, readFrame, type FrameKind, type HoldBasis } from "./hold.ts";
12
13
  export { regionOrbits, regionView, SUN_REGIONS, type SunRegion } from "./regions.ts";
13
14
  export { SIGHT_STEP_PX, sightChanged, sightOf, withinMargin, type Sight, type SightEdge, type SightInput } from "./sight.ts";
14
15
  export { besideFace, facingPoint, faceView, subCameraPoint, surfaceDirection, type Face, type FaceView } from "./face.ts";
@@ -9,7 +9,8 @@ export { besideMoon, BESIDE_RADII } from "./beside.js";
9
9
  export { ARROWS_PER_ORBIT, arrowCount, arrowSites, MAX_ARROWS_PER_ORBIT, chevron, helpSpin, readHelpPlace, spinArrow } from "./arrows.js";
10
10
  export { spinAxis } from "./spin.js";
11
11
  export { COVER_ARROW_GAP, COVER_MIN_PX, coverArrows, coveredBy } from "./covered.js";
12
- export { TRAVEL_CLEARANCE, arrivalSide, travelArrival, travelPlan, travelPose } from "./travel.js";
12
+ export { TRAVEL_BUDGET_MS, TRAVEL_CLEARANCE, arrivalSide, travelArrival, travelPlan, travelPose } from "./travel.js";
13
+ export { bodyBasis, carry, controlsFor, directionBetween, FRAMES, hasRotation, holdSite, keepClear, pairAxis, pairBasis, readFrame } from "./hold.js";
13
14
  export { regionOrbits, regionView, SUN_REGIONS } from "./regions.js";
14
15
  export { SIGHT_STEP_PX, sightChanged, sightOf, withinMargin } from "./sight.js";
15
16
  export { besideFace, facingPoint, faceView, subCameraPoint, surfaceDirection } from "./face.js";
@@ -16,6 +16,7 @@ import { type RideLook } from "./ride.ts";
16
16
  import { type ScaleMode } from "./scale.ts";
17
17
  import { type SunRegion } from "./regions.ts";
18
18
  import { type TravelInput, type TravelKind, type TravelPlan } from "./travel.ts";
19
+ import { type FrameKind, type HoldBasis } from "./hold.ts";
19
20
  /** The parts of three.js the space view uses: the bodies' and a perspective camera. */
20
21
  export type SpaceThree = ThreeParts & Pick<typeof THREE, "PerspectiveCamera">;
21
22
  /** The camera's distance, in radii of the body it looks at, at which that body fills the view at zoom 1. */
@@ -170,6 +171,28 @@ export interface SpaceModel {
170
171
  focus: BodyId;
171
172
  /** The body kept in the picture beside the one arrived at, or null. Its name is not dropped for a clash. */
172
173
  companion: string | null;
174
+ /** How the camera holds its direction while time runs (`frame`): the one asked for, and the body a pair is asked to be with (null: the companion of the last arrival). */
175
+ frame: FrameKind;
176
+ pairWith: BodyId | null;
177
+ /** The frame the last draw really held: a body with no known rotation, or a pair with no companion, is held to the stars. */
178
+ frameDrawn: {
179
+ asked: FrameKind;
180
+ drawn: FrameKind;
181
+ pair: string | null;
182
+ };
183
+ /** The basis the held frame had at the anchor moment, kept while the frame, the body, the pair, the scale and the anchor stay the same. */
184
+ holdFrom: {
185
+ key: string;
186
+ from: HoldBasis;
187
+ axis: Vec3 | null;
188
+ } | null;
189
+ /** What a drag in a held frame added to the controls' yaw and tilt so that the stars' direction could be taken again from that moment (`anchor`); it holds only while the anchor is that moment. */
190
+ holdBias: {
191
+ anchor: number;
192
+ yaw: number;
193
+ tilt: number;
194
+ up: Vec3;
195
+ } | null;
173
196
  /** How many times larger than life the squeezed view draws moons. Ignored at true distances. 2 by default. */
174
197
  moonSize: number;
175
198
  /** The factor actually used for the system in the middle, and whether a limit cut the one asked for. */
@@ -380,6 +403,15 @@ export interface SpaceTransition {
380
403
  /** The scene's origin (the place of the body looked at) when the path was worked out: the path is in places relative to it. */
381
404
  origin: Vec3;
382
405
  }
406
+ /**
407
+ * The direction the camera looks from, held in the frame the view is set to. `aim` is the direction from the body in focus to the camera as the stars hold it (fixed since the anchor moment); `body` turns it with the body's
408
+ * ground and `pair` with the direction to a companion, from the anchor moment to now, and a pair keeps the companion from passing behind the body. Records the frame it held in `model.frameDrawn`. Worked out in the draw
409
+ * the clock already makes; at rest, or with the frame on the stars, it costs nothing.
410
+ */
411
+ export declare function heldAim(model: SpaceModel, focus: BodyId, aim: Vec3, up: Vec3, mode: ScaleMode, siteNow: (id: BodyId) => Vec3, limb: number): {
412
+ aim: Vec3;
413
+ up: Vec3;
414
+ };
383
415
  export declare const newSpaceModel: () => SpaceModel;
384
416
  /**
385
417
  * Where a body is on the screen: its centre and its apparent radius, in CSS pixels in the box.
@@ -20,7 +20,8 @@ import { BALL_FRAGMENT, VERTEX } from "../scene/glsl.js";
20
20
  import { ridePose } from "./ride.js";
21
21
  import { moonDistance, moonSizeFactor, orbitLine, placeAround, placeBodies, placeMoon, radiusIn } from "./scale.js";
22
22
  import { regionOrbits, regionView } from "./regions.js";
23
- import { travelPlan, travelPose } from "./travel.js";
23
+ import { TRAVEL_BUDGET_MS, travelPlan, travelPose } from "./travel.js";
24
+ import { bodyBasis, carry, controlsFor, directionBetween, hasRotation, keepClear, pairAxis, pairBasis } from "./hold.js";
24
25
  const RAD = Math.PI / 180;
25
26
  /** The camera's distance, in radii of the body it looks at, at which that body fills the view at zoom 1. */
26
27
  export const FIT_DISTANCE = 4.1;
@@ -72,6 +73,46 @@ const ROADSTER_ORBIT_KEEP_MS = 30 * 86_400_000;
72
73
  * ```
73
74
  */
74
75
  export const SPACE_BODIES = ["earth", "sun", "moon", ...SPACE_PLANETS, ...SPACE_DWARFS, ...SPACE_MINORS];
76
+ /**
77
+ * The direction the camera looks from, held in the frame the view is set to. `aim` is the direction from the body in focus to the camera as the stars hold it (fixed since the anchor moment); `body` turns it with the body's
78
+ * ground and `pair` with the direction to a companion, from the anchor moment to now, and a pair keeps the companion from passing behind the body. Records the frame it held in `model.frameDrawn`. Worked out in the draw
79
+ * the clock already makes; at rest, or with the frame on the stars, it costs nothing.
80
+ */
81
+ export function heldAim(model, focus, aim, up, mode, siteNow, limb) {
82
+ const asked = model.frame;
83
+ let drawn = "stars";
84
+ let pair = null;
85
+ if (asked === "body" && hasRotation(focus))
86
+ drawn = "body";
87
+ else if (asked === "pair") {
88
+ const id = (model.pairWith ?? model.companion);
89
+ if (id !== null && id !== focus && id in BODIES) {
90
+ drawn = "pair";
91
+ pair = id;
92
+ }
93
+ }
94
+ model.frameDrawn = { asked, drawn, pair };
95
+ const anchor = model.anchor;
96
+ if (drawn === "stars" || anchor === null)
97
+ return { aim, up };
98
+ const key = `${drawn}|${focus}|${pair}|${mode}|${anchor}`;
99
+ if (model.holdFrom?.key !== key) {
100
+ if (drawn === "body")
101
+ model.holdFrom = { key, from: bodyBasis(focus, anchor), axis: null };
102
+ else {
103
+ const axis = pairAxis((ms) => directionBetween(focus, pair, mode, ms), anchor);
104
+ model.holdFrom = { key, from: pairBasis(directionBetween(focus, pair, mode, anchor), axis), axis };
105
+ }
106
+ }
107
+ const from = model.holdFrom;
108
+ // the whole camera turns as one, the picture's up with the look direction: carried the same way, so that what is in the picture stays where it is in it
109
+ if (drawn === "body")
110
+ return anchor === model.time ? { aim, up } : { aim: carry(aim, from.from, bodyBasis(focus, model.time)), up: carry(up, from.from, bodyBasis(focus, model.time)) };
111
+ const toward = unit(minus(siteNow(pair), siteNow(focus)));
112
+ const to = pairBasis(toward, from.axis);
113
+ const turned = anchor === model.time ? { aim, up } : { aim: carry(aim, from.from, to), up: carry(up, from.from, to) };
114
+ return { aim: keepClear(turned.aim, toward, limb), up: turned.up };
115
+ }
75
116
  export const newSpaceModel = () => ({
76
117
  time: Date.now(),
77
118
  mode: "true",
@@ -86,6 +127,11 @@ export const newSpaceModel = () => ({
86
127
  speed: 1,
87
128
  focus: "earth",
88
129
  companion: null,
130
+ frame: "stars",
131
+ pairWith: null,
132
+ frameDrawn: { asked: "stars", drawn: "stars", pair: null },
133
+ holdFrom: null,
134
+ holdBias: null,
89
135
  moonSize: 2,
90
136
  moonDrawn: { factor: 1, clamped: false },
91
137
  light: "sun",
@@ -525,6 +571,8 @@ export function createSpaceItem(model, deps) {
525
571
  if (position && ride.view === "from")
526
572
  rideFrom = { around, centre, unitWorld, position, velocity, perSecond, hiddenBody, hiddenMoon };
527
573
  }
574
+ // what the draw holds the camera's direction to: the stars unless the frame (`heldAim`) says otherwise
575
+ model.frameDrawn = { asked: model.frame, drawn: "stars", pair: null };
528
576
  let fovNow = FOV;
529
577
  let distance = 0;
530
578
  if (besideIn) {
@@ -556,12 +604,20 @@ export function createSpaceItem(model, deps) {
556
604
  else {
557
605
  // looks at the focus body from the direction the sky was in when it was last moved, turned by yaw and tilt, as far as the zoom says
558
606
  model.anchor ??= model.time;
559
- const lon = model.centre.lon - aimWay * model.view.yaw;
560
- const lat = Math.max(-90, Math.min(90, model.centre.lat - aimWay * model.view.tilt));
561
- const aim = groundDirection(lon, lat, model.anchor);
607
+ const bias = model.holdBias !== null && model.holdBias.anchor === model.anchor ? model.holdBias : { yaw: 0, tilt: 0, up: [0, 1, 0] };
608
+ const lon = model.centre.lon - aimWay * (model.view.yaw + bias.yaw);
609
+ const lat = Math.max(-90, Math.min(90, model.centre.lat - aimWay * (model.view.tilt + bias.tilt)));
610
+ let aim = groundDirection(lon, lat, model.anchor);
562
611
  // at zoom 1 the whole picture of the body fits the shorter side of the view, in any shape: a tall view is as far again as it is taller than wide, and a ringed body is framed with its rings
563
612
  const across = focusRadius * (FRAMED[focus] ?? 1) * Math.max(1, size.height / Math.max(1, size.width));
564
613
  distance = Math.max(1.05 * focusRadius, (FIT_DISTANCE * across) / Math.max(1e-9, model.view.zoom));
614
+ // held in the frame the view is set to (`frame`): turned with the body's ground or with a pair, from the anchor moment to now
615
+ let upNow = bias.up;
616
+ if (model.frame !== "stars" && model.ride === null && model.region === null) {
617
+ const held = heldAim(model, focus, aim, bias.up, mode, absoluteSite, Math.asin(Math.min(0.99, focusRadius / Math.max(1e-9, distance))) * 1.05);
618
+ aim = held.aim;
619
+ upNow = held.up;
620
+ }
565
621
  target.set(focusPos[0], focusPos[1], focusPos[2]);
566
622
  model.regionDrawn = null;
567
623
  if (model.region !== null && focus === "sun") {
@@ -574,7 +630,7 @@ export function createSpaceItem(model, deps) {
574
630
  model.regionDrawn = { id: model.region, reach: framed.reach, distance };
575
631
  }
576
632
  camera.position.set(target.x + aim[0] * distance, target.y + aim[1] * distance, target.z + aim[2] * distance);
577
- camera.up.set(0, 1, 0);
633
+ camera.up.set(upNow[0], upNow[1], upNow[2]);
578
634
  camera.lookAt(target);
579
635
  camera.near = Math.max(0.0005, distance * 0.02 - focusRadius * 0.5, focusRadius * 0.01);
580
636
  camera.far = Math.max(2000, distance * 4 + (mode === "true" ? 40 * 23_481 : 400));
@@ -629,7 +685,7 @@ export function createSpaceItem(model, deps) {
629
685
  const end = { position: [camera.position.x + shift[0], camera.position.y + shift[1], camera.position.z + shift[2]], forward: [ef.x, ef.y, ef.z], up: [eu.x, eu.y, eu.z], fov: fovNow };
630
686
  // every body of the view is kept clear of, the two ends of the move and the planet circled aside
631
687
  const others = SPACE_BODIES.filter((id) => id !== glide.leaving && id !== glide.destination && id !== family).map((id) => bodyOf(id));
632
- const input = { start, end, leaving, destination, others, aspect: size.width / Math.max(1, size.height), kind: glide.kind, pace: glide.pace };
688
+ const input = { start, end, leaving, destination, others, aspect: size.width / Math.max(1, size.height), kind: glide.kind, pace: glide.pace, budget: { now: () => performance.now(), ms: TRAVEL_BUDGET_MS } };
633
689
  if (around !== null)
634
690
  input.around = bodyOf(around);
635
691
  return { input, plan: travelPlan(input) };
@@ -660,6 +716,13 @@ export function createSpaceItem(model, deps) {
660
716
  if (k >= 1) {
661
717
  model.transition = null;
662
718
  model.travelling = null;
719
+ // in a held frame the camera is where the move put it, and the frame carries it on from here: the stars' direction is taken again from this moment, with the direction the last frame had
720
+ if (model.frame !== "stars" && model.ride === null && model.region === null) {
721
+ const away = unit(minus([camera.position.x, camera.position.y, camera.position.z], focusPos));
722
+ const turned = controlsFor(away, model.centre, model.time);
723
+ model.anchor = model.time;
724
+ model.holdBias = { anchor: model.time, yaw: turned.yaw - model.view.yaw, tilt: turned.tilt - model.view.tilt, up: [camera.up.x, camera.up.y, camera.up.z] };
725
+ }
663
726
  }
664
727
  }
665
728
  // the controls' roll turns the picture clockwise about the line of sight, and a pan moves everything by that many CSS pixels at any depth: the window of the projection moves the other way
@@ -14,7 +14,7 @@ import { type Vec3 } from "../vec.ts";
14
14
  */
15
15
  export type TravelKind = "turn-travel" | "direct" | "look-back" | "auto" | "cut";
16
16
  /**
17
- * How near a move may come to a body, in that body's radii above its surface: 1, so a body passed is never more than 60 degrees across. A move that starts or ends nearer than one and a half times that keeps two thirds of the nearer distance.
17
+ * How near a move may come to a body, in that body's radii above its surface: a third of a radius (0.35), a margin so the camera never flies through a body. Bodies near the way are passed, not routed round: a body passed close is one more thing to see on the way. A move that starts or ends nearer than one and a half times that keeps two thirds of the nearer distance.
18
18
  *
19
19
  * @example
20
20
  * ```ts
@@ -23,7 +23,23 @@ export type TravelKind = "turn-travel" | "direct" | "look-back" | "auto" | "cut"
23
23
  * console.log(TRAVEL_CLEARANCE);
24
24
  * ```
25
25
  */
26
- export declare const TRAVEL_CLEARANCE = 1;
26
+ export declare const TRAVEL_CLEARANCE = 0.35;
27
+ /**
28
+ * The time in milliseconds, counted from the start of the plan, that the space view lets the planner spend on the other ways of a move within a planet's family (`TravelInput.budget`); the first way, with its aim
29
+ * turn, is always worked out. Measured on the author's Mac mini over the 360 surveyed plain-view moves of six pairs of moons (most need no other way): the first way alone takes a median of 4 ms, 90 percent under
30
+ * 10 ms and at most 37 ms; with this budget a plan takes a median of 4.5 ms (up to 9.5 under load), 90 percent under 36 to 45 ms and at most 75 to 105 ms in quiet runs (126 ms in one loaded run), against the same median, 34 ms and 105 to 119 ms with no budget. A phone is several times
31
+ * slower, so there the planner stops sooner and keeps the best way found, and the same move can be planned differently on two devices (never with more faults than the first way). On this machine the budget costs
32
+ * little in faults, and what it costs depends on the load: over the 360 walks, 61 to 69 drifting samples, 32 to 70 empty and 12 to 15 lost in six runs (three of them with two other copies running at once),
33
+ * against 54, 27 and 12 with no budget and 605, 394 and 45 before the other ways; never more faults of any kind than the first way.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { TRAVEL_BUDGET_MS } from "@johnmorrisdotca/wakusei-planets/space";
38
+ *
39
+ * console.log(TRAVEL_BUDGET_MS);
40
+ * ```
41
+ */
42
+ export declare const TRAVEL_BUDGET_MS = 60;
27
43
  /**
28
44
  * A camera: where it is, the unit vectors it looks along and has up in the picture, and its vertical field of view in degrees. The same four as `besideMoon` and `ridePose` give.
29
45
  *
@@ -65,6 +81,9 @@ export interface TravelBody {
65
81
  * What `travelPose` and `travelPlan` are asked: the camera now (`start`) and the framed view to end in (`end`, worked out for the destination's place at the end of the move), the body being left and the
66
82
  * destination, the planet when both are of one planet's family, any other body to keep clear of, and the picture's width over its height.
67
83
  *
84
+ * A move is planned once for the input it is given (a second call, or `travelPose` without a plan, uses the first plan), so an input is not to be changed after it has been planned: `travelPlan` and `travelPose`
85
+ * freeze it (its top level only: nested arrays and objects such as `start.position` are not frozen, and must not be changed either), and a changed move is a new input.
86
+ *
68
87
  * @example
69
88
  * ```ts
70
89
  * import type { TravelInput } from "@johnmorrisdotca/wakusei-planets/space";
@@ -100,6 +119,15 @@ export interface TravelInput {
100
119
  pace?: number;
101
120
  /** How near the path may come to a body, in its radii above its surface: `TRAVEL_CLEARANCE` when left out. */
102
121
  clearance?: number;
122
+ /**
123
+ * A limit on the time the planner spends on a move within a planet's family: `now` gives the time in milliseconds (`performance.now`), and once `ms` have gone on the aim turn and the other ways the planner stops
124
+ * and keeps the best so far, which is never worse than the first way it made. The planner reads no clock itself: without `budget` it tries every way, and the same input always gives the
125
+ * same plan. With it, the plan can differ with the speed of the device (a slower one may stop sooner), though never to one with more faults than the first way. `TRAVEL_BUDGET_MS` is the value the space view passes.
126
+ */
127
+ budget?: {
128
+ now: () => number;
129
+ ms: number;
130
+ };
103
131
  }
104
132
  /**
105
133
  * Where a body is in the picture at one moment: `inside` when its centre (the place of its marker) is within the frame, `x` and `y` from -1 at the left and bottom edges to 1 at the right and top, and `radius`,
@@ -169,9 +197,31 @@ export interface TravelPlan {
169
197
  };
170
198
  /** The angle the camera turns round the destination as it arrives, in degrees: 0 when the whole turn is made before the way on. */
171
199
  settle: number;
200
+ /** How much of the turn the way on overlaps: 0 when the way on starts when the turn ends, and up to 0.8 of the turn when it starts earlier so that the camera never stops between the two (the move is then shorter by the time they share). */
201
+ blend: number;
172
202
  /** The path itself, for `travelPose`. */
173
203
  route: unknown;
174
204
  }
205
+ /** The faults a walk of a move has that the way on is judged by: samples with neither body in the picture, with the destination out of it after the turn, where the destination moves away from its last place on the screen (and the most it does in a sample), and where it moves a third of the frame in one. */
206
+ export interface Faults {
207
+ empty: number;
208
+ lost: number;
209
+ drift: number;
210
+ driftWorst: number;
211
+ step: number;
212
+ /** Moments the look direction turns six degrees or more, turns away from the last view's, or the camera jumps a sixth of the way, and whether the whole turn is over 1.25 times the least plus 2 degrees. */
213
+ pace: number;
214
+ /** Samples with the planet circled out of the picture (not a fault: it breaks a tie). */
215
+ planet: number;
216
+ /** Samples of the turn with the body left (and the planet circled, if there is one) out of the picture: the turn holds it, and a way on that starts during the turn must not let it go. */
217
+ held: number;
218
+ }
219
+ export declare const total: (f: Faults) => number;
220
+ /**
221
+ * `now` has no kind of fault more than `was` (and no more of the drift in its worst sample) and fewer in all. A way that keeps the planet in the picture all through (`was.planet` is 0: the views promised it) must go
222
+ * on keeping it; where the first way did not (plain views never promised it) the planet out of the picture is not a fault, and only breaks a tie between two ways.
223
+ */
224
+ export declare const betterThan: (now: Faults, was: Faults) => boolean;
175
225
  /**
176
226
  * The plan of a move: its stages and the share of the time each takes, how long it should last, and how it is eased. The length is a suggestion from how far the picture changes (16 milliseconds a degree
177
227
  * turned, 320 for each time the distance changes by a factor of e, between 1.2 and 9 seconds, times `pace`); a page may use its own, since `travelPose` takes the moment as a share of the move.
@@ -250,8 +300,12 @@ export interface ArrivalCompanion {
250
300
  * A side more than 90 degrees from the Sun is refused, so a lit globe is never traded for a black one. With no companion that fits, the side is the
251
301
  * approach, swung toward the Sun when it would show less than about three fifths of the body lit (never more than 75 degrees off the Sun).
252
302
  *
253
- * @param input - The body, where the move comes from, the Sun, and the companions in rank order.
254
- * @returns A unit side from the body toward the camera, how much of the disc is lit, and the companion's id (null when none was framed).
303
+ * `behind` is the body the move comes from: a side that puts it in the picture at the back left or right of the body, clear of the disc, is taken before any companion where the body is still at least half lit
304
+ * (`BEHIND_LIT`); a side that also frames a moon of the body is preferred, and that moon is the companion returned. Where the body would show its night side, or no side frames the body left (a picture too narrow),
305
+ * the rules above stand: the lit side wins, with a moon where one fits, and the body left is not in the last view.
306
+ *
307
+ * @param input - The body, where the move comes from, the Sun, the companions in rank order, and optionally `behind`.
308
+ * @returns A unit side from the body toward the camera, how much of the disc is lit, the companion's id (null when none was framed), and whether the side is the one `behind` asked for.
255
309
  *
256
310
  * @example
257
311
  * ```ts
@@ -267,6 +321,7 @@ export declare function arrivalSide(input: {
267
321
  from: Vec3;
268
322
  sun?: Vec3;
269
323
  companions?: readonly ArrivalCompanion[];
324
+ behind?: Vec3;
270
325
  fov?: number;
271
326
  aspect?: number;
272
327
  fit?: number;
@@ -274,6 +329,7 @@ export declare function arrivalSide(input: {
274
329
  side: Vec3;
275
330
  lit: number;
276
331
  companion: string | null;
332
+ behind: boolean;
277
333
  };
278
334
  /**
279
335
  * A framed view of a whole body to arrive at, from the side the move comes from, so the approach is straight and nothing has to be turned back: the camera stands on the line towards `from`, moved round
@@ -301,7 +357,9 @@ export declare function travelArrival(input: {
301
357
  fit?: number;
302
358
  north?: Vec3;
303
359
  companions?: readonly ArrivalCompanion[];
360
+ behind?: Vec3;
304
361
  }): TravelView & {
305
362
  lit: number;
306
363
  companion: string | null;
364
+ behind: boolean;
307
365
  };