@johnmorrisdotca/wakusei-planets 0.30.0 → 0.32.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.
- package/CHANGELOG.md +16 -0
- package/NOTICE.md +5 -0
- package/README.md +98 -12
- package/dist/data-door.d.ts +357 -0
- package/dist/data-door.js +393 -0
- package/dist/door/book.d.ts +133 -0
- package/dist/door/book.js +256 -0
- package/dist/door/host.d.ts +30 -0
- package/dist/door/host.js +44 -0
- package/dist/door/index.d.ts +6 -0
- package/dist/door/index.js +10 -0
- package/dist/door/map.d.ts +152 -0
- package/dist/door/map.js +162 -0
- package/dist/door/own.d.ts +36 -0
- package/dist/door/own.js +86 -0
- package/dist/door/points.d.ts +61 -0
- package/dist/door/points.js +175 -0
- package/dist/door/small-bodies.d.ts +157 -0
- package/dist/door/small-bodies.js +148 -0
- package/dist/element.d.ts +21 -0
- package/dist/element.js +74 -4
- package/dist/scene/body.d.ts +5 -2
- package/dist/scene/body.js +2 -1
- package/dist/scene/detail.d.ts +29 -12
- package/dist/scene/detail.js +217 -135
- package/dist/scene/painter.d.ts +5 -0
- package/dist/scene/painter.js +8 -5
- package/dist/scene/textures.d.ts +9 -2
- package/dist/scene/textures.js +17 -0
- package/dist/space/arrows.d.ts +87 -0
- package/dist/space/arrows.js +117 -0
- package/dist/space/beside.d.ts +74 -0
- package/dist/space/beside.js +76 -0
- package/dist/space/bodies.d.ts +83 -0
- package/dist/space/bodies.js +93 -0
- package/dist/space/element.d.ts +114 -14
- package/dist/space/element.js +344 -28
- package/dist/space/frame.d.ts +45 -2
- package/dist/space/frame.js +56 -4
- package/dist/space/index.d.ts +6 -1
- package/dist/space/index.js +6 -1
- package/dist/space/labels.d.ts +52 -0
- package/dist/space/labels.js +40 -0
- package/dist/space/scale.d.ts +5 -5
- package/dist/space/scale.js +10 -4
- package/dist/space/scene.d.ts +49 -9
- package/dist/space/scene.js +325 -34
- package/dist/space/spin.d.ts +37 -0
- package/dist/space/spin.js +29 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +6 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { type Vec3 } from "../vec.ts";
|
|
2
|
+
/**
|
|
3
|
+
* How many arrowheads an orbit line carries.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { ARROWS_PER_ORBIT } from "@johnmorrisdotca/wakusei-planets/space";
|
|
8
|
+
*
|
|
9
|
+
* console.log(ARROWS_PER_ORBIT);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare const ARROWS_PER_ORBIT = 6;
|
|
13
|
+
/**
|
|
14
|
+
* One arrowhead on an orbit line: where its tip is and the way it points, the way the body goes along the line.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import type { OrbitArrow } from "@johnmorrisdotca/wakusei-planets/space";
|
|
19
|
+
*
|
|
20
|
+
* const arrow: OrbitArrow = { position: [1, 0, 0], direction: [0, 1, 0] };
|
|
21
|
+
* console.log(arrow.direction);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export interface OrbitArrow {
|
|
25
|
+
position: Vec3;
|
|
26
|
+
direction: Vec3;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Where the arrowheads go on an orbit line, and which way each points. The line is the points of `planetOrbit` as the view places them: they run from where the body is now once round in the order the body
|
|
30
|
+
* goes, so the direction of each arrow is the line's own direction there, from the point before to the point after. The arrows are spread evenly round the line and stay clear of the body, which is at its first point.
|
|
31
|
+
* They show the real way the body travels, which is the same whichever way the clock runs.
|
|
32
|
+
*
|
|
33
|
+
* @param line - The orbit line, `count + 1` points whose last is near the first.
|
|
34
|
+
* @param arrows - How many arrowheads (`ARROWS_PER_ORBIT` by default).
|
|
35
|
+
* @returns The arrows, each at a point of the line.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { arrowSites } from "@johnmorrisdotca/wakusei-planets/space";
|
|
40
|
+
*
|
|
41
|
+
* const circle = Array.from({ length: 97 }, (_, i) => [Math.cos((i / 96) * 2 * Math.PI), 0, Math.sin((i / 96) * 2 * Math.PI)] as const);
|
|
42
|
+
* console.log(arrowSites(circle, 4).map((a) => a.direction.map((n) => n.toFixed(2)).join(",")).join(" | "));
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function arrowSites(line: readonly (readonly [number, number, number])[], arrows?: number): OrbitArrow[];
|
|
46
|
+
/**
|
|
47
|
+
* The three points of a small arrowhead that is `size` long in the picture: its tip at `position`, and the ends of its two wings, laid in the plane of the picture so it points the way `direction` goes on the screen.
|
|
48
|
+
* A direction straight into or out of the picture has no way on the screen and gives none.
|
|
49
|
+
*
|
|
50
|
+
* @param position - The tip, in the scene's units.
|
|
51
|
+
* @param direction - The way it points in the scene.
|
|
52
|
+
* @param right - The camera's right-hand direction (a unit vector).
|
|
53
|
+
* @param up - The camera's up direction (a unit vector).
|
|
54
|
+
* @param size - The length of a wing, in the scene's units at the tip (pixels times the size of a pixel there).
|
|
55
|
+
* @returns The tip and the two wing ends, or `null`.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { chevron } from "@johnmorrisdotca/wakusei-planets/space";
|
|
60
|
+
*
|
|
61
|
+
* console.log(chevron([0, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], 2)?.map((p) => p.map((n) => n.toFixed(1)).join(",")).join(" "));
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare function chevron(position: Vec3, direction: Vec3, right: Vec3, up: Vec3, size: number): [Vec3, Vec3, Vec3] | null;
|
|
65
|
+
/**
|
|
66
|
+
* The curved arrow of the help mode: points on a circle round the axis a body spins about, turning the way it turns (by the right-hand rule about `axis`), as `segments + 1` points from its tail to its head, and the
|
|
67
|
+
* two ends of the head's wings. The circle has radius `radius` and sits `lift` along the axis from `centre`; the arc covers 250 degrees, ending where it started 250 degrees on.
|
|
68
|
+
*
|
|
69
|
+
* @param centre - The body's centre.
|
|
70
|
+
* @param axis - The unit axis it spins about (the right-hand rule: the fingers curl the way it turns).
|
|
71
|
+
* @param radius - The circle's radius, in the scene's units.
|
|
72
|
+
* @param lift - How far along the axis the circle is from the centre.
|
|
73
|
+
* @param segments - How many pieces the arc is cut into.
|
|
74
|
+
* @returns The points of the arc, and the points of the arrowhead's wings (the head is at the arc's last point).
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { spinArrow } from "@johnmorrisdotca/wakusei-planets/space";
|
|
79
|
+
*
|
|
80
|
+
* const { arc } = spinArrow([0, 0, 0], [0, 1, 0], 1.3, 0.5, 20);
|
|
81
|
+
* console.log(arc.length);
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare function spinArrow(centre: Vec3, axis: Vec3, radius: number, lift: number, segments?: number): {
|
|
85
|
+
arc: Vec3[];
|
|
86
|
+
head: [Vec3, Vec3];
|
|
87
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// The little arrows on an orbit line that show which way the body goes, and the shapes the help mode draws round the body looked at. Pure: lines in, places and directions out; the scene turns them into lines on the screen.
|
|
2
|
+
import { add, cross, dot, RAD, scale, unit } from "../vec.js";
|
|
3
|
+
/**
|
|
4
|
+
* How many arrowheads an orbit line carries.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { ARROWS_PER_ORBIT } from "@johnmorrisdotca/wakusei-planets/space";
|
|
9
|
+
*
|
|
10
|
+
* console.log(ARROWS_PER_ORBIT);
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export const ARROWS_PER_ORBIT = 6;
|
|
14
|
+
/**
|
|
15
|
+
* Where the arrowheads go on an orbit line, and which way each points. The line is the points of `planetOrbit` as the view places them: they run from where the body is now once round in the order the body
|
|
16
|
+
* goes, so the direction of each arrow is the line's own direction there, from the point before to the point after. The arrows are spread evenly round the line and stay clear of the body, which is at its first point.
|
|
17
|
+
* They show the real way the body travels, which is the same whichever way the clock runs.
|
|
18
|
+
*
|
|
19
|
+
* @param line - The orbit line, `count + 1` points whose last is near the first.
|
|
20
|
+
* @param arrows - How many arrowheads (`ARROWS_PER_ORBIT` by default).
|
|
21
|
+
* @returns The arrows, each at a point of the line.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { arrowSites } from "@johnmorrisdotca/wakusei-planets/space";
|
|
26
|
+
*
|
|
27
|
+
* const circle = Array.from({ length: 97 }, (_, i) => [Math.cos((i / 96) * 2 * Math.PI), 0, Math.sin((i / 96) * 2 * Math.PI)] as const);
|
|
28
|
+
* console.log(arrowSites(circle, 4).map((a) => a.direction.map((n) => n.toFixed(2)).join(",")).join(" | "));
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function arrowSites(line, arrows = ARROWS_PER_ORBIT) {
|
|
32
|
+
const n = line.length - 1;
|
|
33
|
+
const out = [];
|
|
34
|
+
if (n < 4 || arrows < 1)
|
|
35
|
+
return out;
|
|
36
|
+
for (let k = 0; k < arrows; k += 1) {
|
|
37
|
+
// k + 1/2 of the way round in equal steps: never at the body (the first point) or at the join
|
|
38
|
+
const i = Math.min(n - 1, Math.max(1, Math.round(((k + 0.5) / arrows) * n)));
|
|
39
|
+
const before = line[i - 1];
|
|
40
|
+
const after = line[i + 1];
|
|
41
|
+
const direction = unit([after[0] - before[0], after[1] - before[1], after[2] - before[2]]);
|
|
42
|
+
out.push({ position: line[i], direction });
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The three points of a small arrowhead that is `size` long in the picture: its tip at `position`, and the ends of its two wings, laid in the plane of the picture so it points the way `direction` goes on the screen.
|
|
48
|
+
* A direction straight into or out of the picture has no way on the screen and gives none.
|
|
49
|
+
*
|
|
50
|
+
* @param position - The tip, in the scene's units.
|
|
51
|
+
* @param direction - The way it points in the scene.
|
|
52
|
+
* @param right - The camera's right-hand direction (a unit vector).
|
|
53
|
+
* @param up - The camera's up direction (a unit vector).
|
|
54
|
+
* @param size - The length of a wing, in the scene's units at the tip (pixels times the size of a pixel there).
|
|
55
|
+
* @returns The tip and the two wing ends, or `null`.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { chevron } from "@johnmorrisdotca/wakusei-planets/space";
|
|
60
|
+
*
|
|
61
|
+
* console.log(chevron([0, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], 2)?.map((p) => p.map((n) => n.toFixed(1)).join(",")).join(" "));
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export function chevron(position, direction, right, up, size) {
|
|
65
|
+
const dx = dot(direction, right);
|
|
66
|
+
const dy = dot(direction, up);
|
|
67
|
+
const l = Math.hypot(dx, dy);
|
|
68
|
+
if (l < 1e-3)
|
|
69
|
+
return null;
|
|
70
|
+
const [ux, uy] = [dx / l, dy / l];
|
|
71
|
+
const alpha = 28 * RAD;
|
|
72
|
+
const wing = (sign) => {
|
|
73
|
+
// the direction on the screen, turned back by `alpha` to one side or the other
|
|
74
|
+
const vx = -(ux * Math.cos(alpha) - sign * uy * Math.sin(alpha));
|
|
75
|
+
const vy = -(uy * Math.cos(alpha) + sign * ux * Math.sin(alpha));
|
|
76
|
+
return add(position, add(scale(right, vx * size), scale(up, vy * size)));
|
|
77
|
+
};
|
|
78
|
+
return [position, wing(1), wing(-1)];
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The curved arrow of the help mode: points on a circle round the axis a body spins about, turning the way it turns (by the right-hand rule about `axis`), as `segments + 1` points from its tail to its head, and the
|
|
82
|
+
* two ends of the head's wings. The circle has radius `radius` and sits `lift` along the axis from `centre`; the arc covers 250 degrees, ending where it started 250 degrees on.
|
|
83
|
+
*
|
|
84
|
+
* @param centre - The body's centre.
|
|
85
|
+
* @param axis - The unit axis it spins about (the right-hand rule: the fingers curl the way it turns).
|
|
86
|
+
* @param radius - The circle's radius, in the scene's units.
|
|
87
|
+
* @param lift - How far along the axis the circle is from the centre.
|
|
88
|
+
* @param segments - How many pieces the arc is cut into.
|
|
89
|
+
* @returns The points of the arc, and the points of the arrowhead's wings (the head is at the arc's last point).
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { spinArrow } from "@johnmorrisdotca/wakusei-planets/space";
|
|
94
|
+
*
|
|
95
|
+
* const { arc } = spinArrow([0, 0, 0], [0, 1, 0], 1.3, 0.5, 20);
|
|
96
|
+
* console.log(arc.length);
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export function spinArrow(centre, axis, radius, lift, segments = 28) {
|
|
100
|
+
const k = unit(axis);
|
|
101
|
+
const seed = Math.abs(k[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];
|
|
102
|
+
const e1 = unit(cross(seed, k));
|
|
103
|
+
const e2 = cross(k, e1);
|
|
104
|
+
const span = 250 * RAD;
|
|
105
|
+
const at = (phi) => add(add(centre, scale(k, lift)), add(scale(e1, radius * Math.cos(phi)), scale(e2, radius * Math.sin(phi))));
|
|
106
|
+
const arc = [];
|
|
107
|
+
for (let i = 0; i <= segments; i += 1)
|
|
108
|
+
arc.push(at(-span / 2 + (i / segments) * span));
|
|
109
|
+
// the head: two short strokes back from the tip, in the plane of the circle, either side of the way it goes (the tangent, increasing phi)
|
|
110
|
+
const tip = arc[segments];
|
|
111
|
+
const phi = span / 2;
|
|
112
|
+
const tangent = unit(add(scale(e1, -Math.sin(phi)), scale(e2, Math.cos(phi))));
|
|
113
|
+
const outward = unit(add(scale(e1, Math.cos(phi)), scale(e2, Math.sin(phi))));
|
|
114
|
+
const back = 0.16 * radius;
|
|
115
|
+
const head = [add(tip, add(scale(tangent, -back), scale(outward, back * 0.55))), add(tip, add(scale(tangent, -back), scale(outward, -back * 0.55)))];
|
|
116
|
+
return { arc, head };
|
|
117
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type Vec3 } from "../vec.ts";
|
|
2
|
+
/**
|
|
3
|
+
* How many radii of the moon the camera keeps from it at zoom 1: the moon then spans about 20 degrees of the view.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { BESIDE_RADII } from "@johnmorrisdotca/wakusei-planets/space";
|
|
8
|
+
*
|
|
9
|
+
* console.log(BESIDE_RADII);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare const BESIDE_RADII = 5.8;
|
|
13
|
+
/**
|
|
14
|
+
* What `besideMoon` is asked: where the moon, the planet it goes round and the Sun are and how big the moon and the planet are (one unit for all), the shape of the view, and the shared controls' zoom, yaw and tilt.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import type { BesideInput } from "@johnmorrisdotca/wakusei-planets/space";
|
|
19
|
+
*
|
|
20
|
+
* const input: BesideInput = { moon: [60, 0, 0], moonRadius: 0.273, host: [0, 0, 0], hostRadius: 1, sun: [-23000, 0, 0], aspect: 1.6, zoom: 1, yaw: 0, tilt: 0 };
|
|
21
|
+
* console.log(input.moonRadius);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export interface BesideInput {
|
|
25
|
+
moon: Vec3;
|
|
26
|
+
moonRadius: number;
|
|
27
|
+
host: Vec3;
|
|
28
|
+
hostRadius: number;
|
|
29
|
+
sun: Vec3;
|
|
30
|
+
/** The view's width over its height. */
|
|
31
|
+
aspect: number;
|
|
32
|
+
/** 1 keeps `BESIDE_RADII` moon radii from the moon; more goes closer, less goes further. */
|
|
33
|
+
zoom: number;
|
|
34
|
+
/** Degrees the camera swings round the line from the planet through the moon (a drag across the view is 180). */
|
|
35
|
+
yaw: number;
|
|
36
|
+
/** Degrees the camera opens further off that line (a drag down the view is 60). */
|
|
37
|
+
tilt: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The camera `besideMoon` works out: where it is, which way it looks and its up, its vertical field of view in degrees, how far it is from the moon, and the angle in degrees between the moon and the planet as the camera sees them.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* import type { BesideCamera } from "@johnmorrisdotca/wakusei-planets/space";
|
|
45
|
+
*
|
|
46
|
+
* const describe = (camera: BesideCamera): string => `${camera.separation.toFixed(0)} degrees apart in a ${camera.fov.toFixed(0)} degree view`;
|
|
47
|
+
* console.log(typeof describe);
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export interface BesideCamera {
|
|
51
|
+
position: Vec3;
|
|
52
|
+
forward: Vec3;
|
|
53
|
+
up: Vec3;
|
|
54
|
+
fov: number;
|
|
55
|
+
distance: number;
|
|
56
|
+
separation: number;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The camera for a view from near a moon with its planet still in view: beyond the moon on the side away from the planet and a little off the line to it, so the moon is large in the foreground and the planet is behind it,
|
|
60
|
+
* at the angular size it really has from there. The camera sits on the Sun's side of that line where it can, so the moon shows a lit face. It opens off the line, from 25 degrees, until the two discs no longer touch
|
|
61
|
+
* (with 2 degrees to spare), turns the picture so the planet is to one side of the moon, and widens the field of view until both fit the view's narrower side.
|
|
62
|
+
*
|
|
63
|
+
* @param input - The places and sizes, the view's shape, and the zoom, yaw and tilt.
|
|
64
|
+
* @returns The camera.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { besideMoon } from "@johnmorrisdotca/wakusei-planets/space";
|
|
69
|
+
*
|
|
70
|
+
* const camera = besideMoon({ moon: [60.3, 0, 0], moonRadius: 0.273, host: [0, 0, 0], hostRadius: 1, sun: [-23000, 5000, 9000], aspect: 1.6, zoom: 1, yaw: 0, tilt: 0 });
|
|
71
|
+
* console.log(camera.distance.toFixed(2), camera.separation.toFixed(0));
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare function besideMoon(input: BesideInput): BesideCamera;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// A camera placed beside and beyond a moon, off the line to the planet it goes round: the moon is large in the foreground and the planet is in view behind it, at its real angular size. Pure: places in, a camera out.
|
|
2
|
+
import { add, cross, dot, length, RAD, scale, unit } from "../vec.js";
|
|
3
|
+
/**
|
|
4
|
+
* How many radii of the moon the camera keeps from it at zoom 1: the moon then spans about 20 degrees of the view.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { BESIDE_RADII } from "@johnmorrisdotca/wakusei-planets/space";
|
|
9
|
+
*
|
|
10
|
+
* console.log(BESIDE_RADII);
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export const BESIDE_RADII = 5.8;
|
|
14
|
+
/** The angle off the line from the planet through the moon, in degrees, the camera first tries; it opens wider until the planet clears the moon. */
|
|
15
|
+
const FIRST_ANGLE = 25;
|
|
16
|
+
const angleBetween = (a, b) => Math.acos(Math.max(-1, Math.min(1, dot(unit(a), unit(b))))) / RAD;
|
|
17
|
+
const minus = (a, b) => add(a, scale(b, -1));
|
|
18
|
+
/**
|
|
19
|
+
* The camera for a view from near a moon with its planet still in view: beyond the moon on the side away from the planet and a little off the line to it, so the moon is large in the foreground and the planet is behind it,
|
|
20
|
+
* at the angular size it really has from there. The camera sits on the Sun's side of that line where it can, so the moon shows a lit face. It opens off the line, from 25 degrees, until the two discs no longer touch
|
|
21
|
+
* (with 2 degrees to spare), turns the picture so the planet is to one side of the moon, and widens the field of view until both fit the view's narrower side.
|
|
22
|
+
*
|
|
23
|
+
* @param input - The places and sizes, the view's shape, and the zoom, yaw and tilt.
|
|
24
|
+
* @returns The camera.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { besideMoon } from "@johnmorrisdotca/wakusei-planets/space";
|
|
29
|
+
*
|
|
30
|
+
* const camera = besideMoon({ moon: [60.3, 0, 0], moonRadius: 0.273, host: [0, 0, 0], hostRadius: 1, sun: [-23000, 5000, 9000], aspect: 1.6, zoom: 1, yaw: 0, tilt: 0 });
|
|
31
|
+
* console.log(camera.distance.toFixed(2), camera.separation.toFixed(0));
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function besideMoon(input) {
|
|
35
|
+
const { moon, moonRadius, host, hostRadius } = input;
|
|
36
|
+
const away = unit(minus(moon, host));
|
|
37
|
+
const toSun = unit(minus(input.sun, moon));
|
|
38
|
+
let side = minus(toSun, scale(away, dot(toSun, away)));
|
|
39
|
+
if (length(side) < 1e-3)
|
|
40
|
+
side = cross(away, [0, 1, 0]);
|
|
41
|
+
if (length(side) < 1e-3)
|
|
42
|
+
side = cross(away, [1, 0, 0]);
|
|
43
|
+
side = unit(side);
|
|
44
|
+
const distance = Math.max(1.05 * moonRadius, (BESIDE_RADII * moonRadius) / Math.max(1e-9, input.zoom));
|
|
45
|
+
const swing = (v) => {
|
|
46
|
+
// the camera swings round the line from the planet through the moon by `yaw`
|
|
47
|
+
const turn = input.yaw * RAD;
|
|
48
|
+
return add(add(scale(v, Math.cos(turn)), scale(cross(away, v), Math.sin(turn))), scale(away, dot(away, v) * (1 - Math.cos(turn))));
|
|
49
|
+
};
|
|
50
|
+
const place = (angle) => {
|
|
51
|
+
const direction = swing(add(scale(away, Math.cos(angle * RAD)), scale(side, Math.sin(angle * RAD))));
|
|
52
|
+
const position = add(moon, scale(direction, distance));
|
|
53
|
+
return { position, separation: angleBetween(minus(moon, position), minus(host, position)) };
|
|
54
|
+
};
|
|
55
|
+
const moonHalf = (position) => Math.asin(Math.min(1, moonRadius / Math.max(moonRadius * 1.0001, length(minus(moon, position))))) / RAD;
|
|
56
|
+
const hostHalf = (position) => Math.asin(Math.min(1, hostRadius / Math.max(hostRadius * 1.0001, length(minus(host, position))))) / RAD;
|
|
57
|
+
let angle = Math.max(5, Math.min(85, FIRST_ANGLE + input.tilt));
|
|
58
|
+
let spot = place(angle);
|
|
59
|
+
while (angle < 85 && spot.separation < moonHalf(spot.position) + hostHalf(spot.position) + 2) {
|
|
60
|
+
angle += 2;
|
|
61
|
+
spot = place(angle);
|
|
62
|
+
}
|
|
63
|
+
const { position } = spot;
|
|
64
|
+
const toMoon = unit(minus(moon, position));
|
|
65
|
+
const toHost = unit(minus(host, position));
|
|
66
|
+
// look between the two, and turn the picture so the planet is to the side of the moon, not above or below it
|
|
67
|
+
const forward = unit(add(toMoon, toHost));
|
|
68
|
+
const shift = minus(toHost, toMoon);
|
|
69
|
+
const across = unit(minus(shift, scale(forward, dot(shift, forward))));
|
|
70
|
+
const up = unit(cross(across, forward));
|
|
71
|
+
// both discs fit: the half-angle from the middle to the far edge of each, across and up
|
|
72
|
+
const reach = Math.max(angleBetween(forward, toMoon) + moonHalf(position), angleBetween(forward, toHost) + hostHalf(position)) + 3;
|
|
73
|
+
const half = Math.max(Math.tan(reach * RAD) / Math.min(1.6, Math.max(0.3, input.aspect)), Math.tan((moonHalf(position) + 3) * RAD));
|
|
74
|
+
const fov = Math.min(110, Math.max(35, 2 * Math.atan(half) / RAD));
|
|
75
|
+
return { position, forward, up, fov, distance, separation: spot.separation };
|
|
76
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type BodyId, type HostId } from "../facts.ts";
|
|
2
|
+
/**
|
|
3
|
+
* What a body is: the Sun, one of the eight planets, a dwarf planet (Pluto among them) or a moon.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { bodyKindOf, type SpaceKind } from "@johnmorrisdotca/wakusei-planets/space";
|
|
8
|
+
*
|
|
9
|
+
* const kind: SpaceKind = bodyKindOf("titan");
|
|
10
|
+
* console.log(kind);
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export type SpaceKind = "star" | "planet" | "dwarf-planet" | "moon";
|
|
14
|
+
/**
|
|
15
|
+
* Every body the package knows, in the order a hopper goes: the Sun, then each planet and dwarf planet from the Sun outwards, each followed by its moons nearest first (Mercury, Venus, the Earth and the
|
|
16
|
+
* Moon, Mars, Phobos and Deimos, Ceres, Jupiter and its four large moons, and on to Sedna).
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { HOP_ORDER } from "@johnmorrisdotca/wakusei-planets/space";
|
|
21
|
+
*
|
|
22
|
+
* console.log(HOP_ORDER.slice(0, 7).join(", "));
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare const HOP_ORDER: readonly BodyId[];
|
|
26
|
+
/**
|
|
27
|
+
* What kind of body a name is.
|
|
28
|
+
*
|
|
29
|
+
* @param id - A body's name.
|
|
30
|
+
* @returns `star` for the Sun, `planet` for the eight planets, `dwarf-planet` for Pluto and the others, or `moon`.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { bodyKindOf } from "@johnmorrisdotca/wakusei-planets/space";
|
|
35
|
+
*
|
|
36
|
+
* console.log(bodyKindOf("ceres"), bodyKindOf("moon"));
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function bodyKindOf(id: BodyId): SpaceKind;
|
|
40
|
+
/**
|
|
41
|
+
* The planet or dwarf planet a moon goes round, or `undefined` for a body that is not a moon.
|
|
42
|
+
*
|
|
43
|
+
* @param id - A body's name.
|
|
44
|
+
* @returns The host's name for a moon, `undefined` for any other body.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { hostOf } from "@johnmorrisdotca/wakusei-planets/space";
|
|
49
|
+
*
|
|
50
|
+
* console.log(hostOf("titan"), hostOf("mars"));
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function hostOf(id: BodyId): HostId | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Read a body from what a reader or a page might write: its name in the package's lower case, in English or in Japanese, in any case.
|
|
56
|
+
*
|
|
57
|
+
* @param text - `saturn`, `Saturn`, `土星`.
|
|
58
|
+
* @returns The body's name, or `undefined` when no body has it.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { findBody } from "@johnmorrisdotca/wakusei-planets/space";
|
|
63
|
+
*
|
|
64
|
+
* console.log(findBody("Saturn"), findBody("土星"), findBody("nothing"));
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare function findBody(text: string): BodyId | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* The body after `id` in the hopper's order (or before it with a negative `step`), among those `can` accepts, going round from the last to the first.
|
|
70
|
+
*
|
|
71
|
+
* @param id - Where the hopper is.
|
|
72
|
+
* @param step - `1` for the next body, `-1` for the previous one.
|
|
73
|
+
* @param can - Which bodies the view can go to (all of them by default).
|
|
74
|
+
* @returns The next body, or `id` itself when no other is accepted.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { hopFrom } from "@johnmorrisdotca/wakusei-planets/space";
|
|
79
|
+
*
|
|
80
|
+
* console.log(hopFrom("earth", 1), hopFrom("earth", -1), hopFrom("sun", -1, (id) => id !== "sedna"));
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function hopFrom(id: BodyId, step: 1 | -1, can?: (id: BodyId) => boolean): BodyId;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Which bodies the space view can put at the middle of its view, and in what order a reader hops from one to the next. Pure: names in, names out.
|
|
2
|
+
import { BODIES, MOONS_OF } from "../facts.js";
|
|
3
|
+
/** Planets, then dwarf planets, ordered by the distance of their orbits from the Sun, each followed by its moons nearest first. */
|
|
4
|
+
const SUN_ORDER = ["mercury", "venus", "earth", "mars", "ceres", "jupiter", "saturn", "uranus", "neptune", "pluto", "orcus", "haumea", "quaoar", "makemake", "gonggong", "eris", "sedna"];
|
|
5
|
+
/**
|
|
6
|
+
* Every body the package knows, in the order a hopper goes: the Sun, then each planet and dwarf planet from the Sun outwards, each followed by its moons nearest first (Mercury, Venus, the Earth and the
|
|
7
|
+
* Moon, Mars, Phobos and Deimos, Ceres, Jupiter and its four large moons, and on to Sedna).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { HOP_ORDER } from "@johnmorrisdotca/wakusei-planets/space";
|
|
12
|
+
*
|
|
13
|
+
* console.log(HOP_ORDER.slice(0, 7).join(", "));
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export const HOP_ORDER = ["sun", ...SUN_ORDER.flatMap((id) => [id, ...(MOONS_OF[id] ?? [])])];
|
|
17
|
+
/**
|
|
18
|
+
* What kind of body a name is.
|
|
19
|
+
*
|
|
20
|
+
* @param id - A body's name.
|
|
21
|
+
* @returns `star` for the Sun, `planet` for the eight planets, `dwarf-planet` for Pluto and the others, or `moon`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { bodyKindOf } from "@johnmorrisdotca/wakusei-planets/space";
|
|
26
|
+
*
|
|
27
|
+
* console.log(bodyKindOf("ceres"), bodyKindOf("moon"));
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function bodyKindOf(id) {
|
|
31
|
+
return BODIES[id].kind;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The planet or dwarf planet a moon goes round, or `undefined` for a body that is not a moon.
|
|
35
|
+
*
|
|
36
|
+
* @param id - A body's name.
|
|
37
|
+
* @returns The host's name for a moon, `undefined` for any other body.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* import { hostOf } from "@johnmorrisdotca/wakusei-planets/space";
|
|
42
|
+
*
|
|
43
|
+
* console.log(hostOf("titan"), hostOf("mars"));
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function hostOf(id) {
|
|
47
|
+
return BODIES[id].kind === "moon" ? BODIES[id].parent ?? "earth" : undefined;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Read a body from what a reader or a page might write: its name in the package's lower case, in English or in Japanese, in any case.
|
|
51
|
+
*
|
|
52
|
+
* @param text - `saturn`, `Saturn`, `土星`.
|
|
53
|
+
* @returns The body's name, or `undefined` when no body has it.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { findBody } from "@johnmorrisdotca/wakusei-planets/space";
|
|
58
|
+
*
|
|
59
|
+
* console.log(findBody("Saturn"), findBody("土星"), findBody("nothing"));
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function findBody(text) {
|
|
63
|
+
const wanted = text.trim().toLowerCase();
|
|
64
|
+
for (const id of HOP_ORDER)
|
|
65
|
+
if (id === wanted || BODIES[id].name.en.toLowerCase() === wanted || BODIES[id].name.ja === text.trim())
|
|
66
|
+
return id;
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The body after `id` in the hopper's order (or before it with a negative `step`), among those `can` accepts, going round from the last to the first.
|
|
71
|
+
*
|
|
72
|
+
* @param id - Where the hopper is.
|
|
73
|
+
* @param step - `1` for the next body, `-1` for the previous one.
|
|
74
|
+
* @param can - Which bodies the view can go to (all of them by default).
|
|
75
|
+
* @returns The next body, or `id` itself when no other is accepted.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* import { hopFrom } from "@johnmorrisdotca/wakusei-planets/space";
|
|
80
|
+
*
|
|
81
|
+
* console.log(hopFrom("earth", 1), hopFrom("earth", -1), hopFrom("sun", -1, (id) => id !== "sedna"));
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export function hopFrom(id, step, can = () => true) {
|
|
85
|
+
const n = HOP_ORDER.length;
|
|
86
|
+
const at = HOP_ORDER.indexOf(id);
|
|
87
|
+
for (let i = 1; i < n; i += 1) {
|
|
88
|
+
const next = HOP_ORDER[(((at + step * i) % n) + n) % n];
|
|
89
|
+
if (can(next))
|
|
90
|
+
return next;
|
|
91
|
+
}
|
|
92
|
+
return id;
|
|
93
|
+
}
|