@johnmorrisdotca/wakusei-planets 0.47.0 → 0.49.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 +8 -0
- package/NOTICE.md +1 -1
- package/README.md +4 -3
- package/dist/element.d.ts +38 -0
- package/dist/element.js +49 -5
- package/dist/scene/textures.d.ts +2 -2
- package/dist/space/element.d.ts +47 -1
- package/dist/space/element.js +101 -1
- package/dist/space/face.d.ts +132 -0
- package/dist/space/face.js +156 -0
- package/dist/space/index.d.ts +1 -0
- package/dist/space/index.js +1 -0
- package/dist/surface/drive.d.ts +181 -0
- package/dist/surface/drive.js +247 -0
- package/dist/surface/dust.d.ts +46 -0
- package/dist/surface/dust.js +96 -0
- package/dist/surface/element.d.ts +19 -0
- package/dist/surface/element.js +177 -5
- package/dist/surface/glsl.d.ts +3 -0
- package/dist/surface/glsl.js +25 -1
- package/dist/surface/index.d.ts +2 -0
- package/dist/surface/index.js +2 -0
- package/dist/surface/scene.d.ts +7 -0
- package/dist/surface/scene.js +187 -3
- package/dist/surface/shapes.d.ts +2 -2
- package/dist/surface/shapes.js +9 -9
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Which face of the centred body the space view looks at, and the turn that brings a face to the middle: the camera's direction in the body's own fixed frame. Pure.
|
|
2
|
+
import { orientation } from "../scene/orient.js";
|
|
3
|
+
import { cross, dot, RAD, unit } from "../vec.js";
|
|
4
|
+
import { groundDirection } from "./aim.js";
|
|
5
|
+
import { besideMoon } from "./beside.js";
|
|
6
|
+
const wrap = (deg) => ((((deg + 180) % 360) + 360) % 360) - 180;
|
|
7
|
+
/**
|
|
8
|
+
* The longitude and latitude, on the body in the middle of the space view, of the point under the camera: the camera looks along the line from that point through the body's centre. It is the camera's
|
|
9
|
+
* direction (the Earth's ground direction of `center` less the yaw and tilt, as the stars saw it at `anchor`) put into the body's own frame (its pole and prime meridian at `time`, which carry its spin
|
|
10
|
+
* and, for the Moon, its libration). A body that turns with the clock moves under a camera held in the stars, so the same view names another face a minute later.
|
|
11
|
+
*
|
|
12
|
+
* @param body - The body in the middle of the view.
|
|
13
|
+
* @param view - The view's yaw and tilt, degrees.
|
|
14
|
+
* @param centre - The view's `center` (longitude and latitude, degrees).
|
|
15
|
+
* @param anchor - The moment the camera was last pointed, in milliseconds since the Unix epoch.
|
|
16
|
+
* @param time - The moment shown.
|
|
17
|
+
* @returns The point under the camera, longitude from -180 to 180 and latitude from -90 to 90.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { subCameraPoint } from "@johnmorrisdotca/wakusei-planets/space";
|
|
22
|
+
*
|
|
23
|
+
* const at = subCameraPoint("moon", { yaw: 0, tilt: 0 }, { lon: 0, lat: 0 }, Date.UTC(2026, 10, 2), Date.UTC(2026, 10, 2));
|
|
24
|
+
* console.log(at.lon.toFixed(0), at.lat.toFixed(0));
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function subCameraPoint(body, view, centre, anchor, time) {
|
|
28
|
+
const d = groundDirection(centre.lon - view.yaw, Math.max(-90, Math.min(90, centre.lat - view.tilt)), anchor);
|
|
29
|
+
const { pole, meridian } = orientation(body, time);
|
|
30
|
+
const east = cross(pole, meridian);
|
|
31
|
+
return { lon: Math.atan2(dot(d, east), dot(d, meridian)) / RAD, lat: Math.asin(Math.max(-1, Math.min(1, dot(d, pole)))) / RAD };
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The yaw and tilt that put a point of the body under the camera: the other way round from {@link subCameraPoint}, for the same centre, anchor and moment. The yaw is the shorter way round from `from`.
|
|
35
|
+
*
|
|
36
|
+
* @param body - The body in the middle of the view.
|
|
37
|
+
* @param face - The point to bring to the middle.
|
|
38
|
+
* @param centre - The view's `center`.
|
|
39
|
+
* @param anchor - The moment the camera was last pointed.
|
|
40
|
+
* @param time - The moment shown.
|
|
41
|
+
* @param from - The yaw the view has now, so that the turn is the shorter way (0 when left out).
|
|
42
|
+
* @returns The yaw and tilt to give the view.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { faceView, subCameraPoint } from "@johnmorrisdotca/wakusei-planets/space";
|
|
47
|
+
*
|
|
48
|
+
* const ms = Date.UTC(2026, 10, 2);
|
|
49
|
+
* const turn = faceView("mars", { lon: 100, lat: 20 }, { lon: 0, lat: 0 }, ms, ms);
|
|
50
|
+
* console.log(subCameraPoint("mars", turn, { lon: 0, lat: 0 }, ms, ms));
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function faceView(body, face, centre, anchor, time, from = 0) {
|
|
54
|
+
const { pole, meridian } = orientation(body, time);
|
|
55
|
+
const east = cross(pole, meridian);
|
|
56
|
+
const c = Math.cos(face.lat * RAD);
|
|
57
|
+
const d = [0, 1, 2].map((i) => c * Math.cos(face.lon * RAD) * meridian[i] + c * Math.sin(face.lon * RAD) * east[i] + Math.sin(face.lat * RAD) * pole[i]);
|
|
58
|
+
const earth = orientation("earth", anchor);
|
|
59
|
+
const lat = Math.asin(Math.max(-1, Math.min(1, dot(d, earth.pole)))) / RAD;
|
|
60
|
+
const lon = Math.atan2(dot(d, cross(earth.pole, earth.meridian)), dot(d, earth.meridian)) / RAD;
|
|
61
|
+
const yaw = centre.lon - lon;
|
|
62
|
+
return { yaw: from + wrap(yaw - from), tilt: centre.lat - lat };
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The longitude and latitude on a body of a direction from its centre in the scene's axes: what a camera standing along that direction has under it. The body's pole and prime meridian are those at `time`.
|
|
66
|
+
*
|
|
67
|
+
* @param body - The body.
|
|
68
|
+
* @param direction - A direction from the body's centre (any length).
|
|
69
|
+
* @param time - The moment, in milliseconds since the Unix epoch.
|
|
70
|
+
* @returns The point on the body under that direction.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* import { facingPoint } from "@johnmorrisdotca/wakusei-planets/space";
|
|
75
|
+
*
|
|
76
|
+
* const ms = Date.UTC(2026, 10, 2);
|
|
77
|
+
* console.log(facingPoint("moon", [0, 1, 0], ms));
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export function facingPoint(body, direction, time) {
|
|
81
|
+
const d = unit(direction);
|
|
82
|
+
const { pole, meridian } = orientation(body, time);
|
|
83
|
+
const east = cross(pole, meridian);
|
|
84
|
+
return { lon: Math.atan2(dot(d, east), dot(d, meridian)) / RAD, lat: Math.asin(Math.max(-1, Math.min(1, dot(d, pole)))) / RAD };
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The direction from a body's centre, in the scene's axes, of a point on its surface at a moment: the unit vector the other way from {@link facingPoint}.
|
|
88
|
+
*
|
|
89
|
+
* @param body - The body.
|
|
90
|
+
* @param face - The point on its surface.
|
|
91
|
+
* @param time - The moment, in milliseconds since the Unix epoch.
|
|
92
|
+
* @returns The unit direction.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* import { surfaceDirection } from "@johnmorrisdotca/wakusei-planets/space";
|
|
97
|
+
*
|
|
98
|
+
* console.log(surfaceDirection("moon", { lon: 0, lat: 0 }, Date.UTC(2026, 10, 2)).map((n) => n.toFixed(2)));
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export function surfaceDirection(body, face, time) {
|
|
102
|
+
const { pole, meridian } = orientation(body, time);
|
|
103
|
+
const east = cross(pole, meridian);
|
|
104
|
+
const c = Math.cos(face.lat * RAD);
|
|
105
|
+
return [0, 1, 2].map((i) => c * Math.cos(face.lon * RAD) * meridian[i] + c * Math.sin(face.lon * RAD) * east[i] + Math.sin(face.lat * RAD) * pole[i]);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The yaw and tilt of a camera beside a moon (the view `ride(moon, { view: "beside" })` gives) that put it on the side of the moon the point is on: the camera stands along the direction from the moon's
|
|
109
|
+
* centre through the point. The camera there is placed by `besideMoon` (beyond the moon, off the line to its planet), so not every point can be reached: the pair returned brings the camera as near as
|
|
110
|
+
* the view goes, and `error` says by how many degrees it is still off.
|
|
111
|
+
*
|
|
112
|
+
* @param input - What `besideMoon` is given, without the yaw and tilt.
|
|
113
|
+
* @param body - The moon.
|
|
114
|
+
* @param face - The point to bring under the camera.
|
|
115
|
+
* @param time - The moment shown.
|
|
116
|
+
* @returns The yaw and tilt, and the angle in degrees between the camera's direction and the wanted one.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* import { besideFace } from "@johnmorrisdotca/wakusei-planets/space";
|
|
121
|
+
*
|
|
122
|
+
* const input = { moon: [60, 0, 0], moonRadius: 0.273, host: [0, 0, 0], hostRadius: 1, sun: [-23000, 0, 0], aspect: 1.6, zoom: 1 } as const;
|
|
123
|
+
* console.log(besideFace(input, "moon", { lon: 180, lat: 0 }, Date.UTC(2026, 10, 2)).error < 90);
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export function besideFace(input, body, face, time) {
|
|
127
|
+
const wanted = unit(surfaceDirection(body, face, time));
|
|
128
|
+
const at = (yaw, tilt) => {
|
|
129
|
+
const camera = besideMoon({ ...input, yaw, tilt });
|
|
130
|
+
const away = unit([camera.position[0] - input.moon[0], camera.position[1] - input.moon[1], camera.position[2] - input.moon[2]]);
|
|
131
|
+
return Math.acos(Math.max(-1, Math.min(1, dot(away, wanted)))) / RAD;
|
|
132
|
+
};
|
|
133
|
+
let best = { yaw: 0, tilt: 0, error: Infinity };
|
|
134
|
+
for (let yaw = -180; yaw < 180; yaw += 15) {
|
|
135
|
+
for (let tilt = -60; tilt <= 80; tilt += 10) {
|
|
136
|
+
const error = at(yaw, tilt);
|
|
137
|
+
if (error < best.error)
|
|
138
|
+
best = { yaw, tilt, error };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
for (let step = 8; step > 0.01; step *= 0.6) {
|
|
142
|
+
for (let round = 0; round < 6; round += 1) {
|
|
143
|
+
let moved = false;
|
|
144
|
+
for (const [dy, dt] of [[step, 0], [-step, 0], [0, step], [0, -step]]) {
|
|
145
|
+
const error = at(best.yaw + dy, best.tilt + dt);
|
|
146
|
+
if (error < best.error) {
|
|
147
|
+
best = { yaw: best.yaw + dy, tilt: best.tilt + dt, error };
|
|
148
|
+
moved = true;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (!moved)
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return { yaw: wrap(best.yaw), tilt: best.tilt, error: best.error };
|
|
156
|
+
}
|
package/dist/space/index.d.ts
CHANGED
|
@@ -11,3 +11,4 @@ export { COVER_ARROW_GAP, COVER_MIN_PX, coverArrows, coveredBy, type CoverArrow,
|
|
|
11
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";
|
|
12
12
|
export { regionOrbits, regionView, SUN_REGIONS, type SunRegion } from "./regions.ts";
|
|
13
13
|
export { SIGHT_STEP_PX, sightChanged, sightOf, type Sight, type SightEdge, type SightInput } from "./sight.ts";
|
|
14
|
+
export { besideFace, facingPoint, faceView, subCameraPoint, surfaceDirection, type Face, type FaceView } from "./face.ts";
|
package/dist/space/index.js
CHANGED
|
@@ -12,3 +12,4 @@ export { COVER_ARROW_GAP, COVER_MIN_PX, coverArrows, coveredBy } from "./covered
|
|
|
12
12
|
export { TRAVEL_CLEARANCE, arrivalSide, travelArrival, travelPlan, travelPose } from "./travel.js";
|
|
13
13
|
export { regionOrbits, regionView, SUN_REGIONS } from "./regions.js";
|
|
14
14
|
export { SIGHT_STEP_PX, sightChanged, sightOf } from "./sight.js";
|
|
15
|
+
export { besideFace, facingPoint, faceView, subCameraPoint, surfaceDirection } from "./face.js";
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { type Part, type V3 } from "./shapes.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The rover's measures: those published (its length and width, the wheels' size and the wheelbase, the gravity of the Moon) and those chosen here (the speeds, the steering, the slope it will not climb, the spring).
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { ROVER } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
8
|
+
*
|
|
9
|
+
* console.log(`${(ROVER.maxSpeed * 3.6).toFixed(0)} km/h at most, wheelbase ${ROVER.wheelbase} m`);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare const ROVER: {
|
|
13
|
+
readonly length: 3.05;
|
|
14
|
+
readonly width: 2.13;
|
|
15
|
+
readonly wheelbase: 2.3;
|
|
16
|
+
/** Metres between the wheels' middles across the rover. */
|
|
17
|
+
readonly track: 1.9;
|
|
18
|
+
readonly wheelRadius: 0.41;
|
|
19
|
+
/** The Moon's surface gravity, m/s2. */
|
|
20
|
+
readonly gravity: 1.62;
|
|
21
|
+
/** About 12 km/h, a little under the speed the rover was designed for. */
|
|
22
|
+
readonly maxSpeed: 3.4;
|
|
23
|
+
readonly maxReverse: 1.4;
|
|
24
|
+
readonly accel: 1.1;
|
|
25
|
+
readonly brake: 2.6;
|
|
26
|
+
/** The most a wheel is turned, radians (30 degrees), at the front and, the other way, at the back. */
|
|
27
|
+
readonly maxSteer: 0.52;
|
|
28
|
+
/** The steepest slope it will climb, radians (25 degrees). */
|
|
29
|
+
readonly maxSlope: 0.436;
|
|
30
|
+
/** The wheels' middles in the rover's frame (x right, y up, z forward), front left, front right, rear left, rear right. */
|
|
31
|
+
readonly wheels: readonly V3[];
|
|
32
|
+
/** The commander's eyes in the rover's frame, the left seat. */
|
|
33
|
+
readonly seat: V3;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Where the rover is and how it moves: all of it, so a step needs nothing else.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { startDrive, type DriveState } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
41
|
+
*
|
|
42
|
+
* const rover: DriveState = startDrive(0, 0, 90, 0);
|
|
43
|
+
* console.log(`${rover.heading} degrees, ${rover.speed} m/s, wheels turned ${rover.spin.length}`);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export interface DriveState {
|
|
47
|
+
/** Metres east and north of the site point, and the heading in degrees clockwise from north. */
|
|
48
|
+
east: number;
|
|
49
|
+
north: number;
|
|
50
|
+
heading: number;
|
|
51
|
+
/** Metres a second along the heading (negative going back) and the wheels' turn in radians (positive to the right). */
|
|
52
|
+
speed: number;
|
|
53
|
+
steer: number;
|
|
54
|
+
/** The height of the ground plane under the wheels over the site's ground, in metres, and how fast it is changing. */
|
|
55
|
+
y: number;
|
|
56
|
+
vy: number;
|
|
57
|
+
/** Nose up and right side up, in radians. */
|
|
58
|
+
pitch: number;
|
|
59
|
+
roll: number;
|
|
60
|
+
/** How far each wheel has turned, in radians. */
|
|
61
|
+
spin: number[];
|
|
62
|
+
/** Whether the wheels are off the ground. */
|
|
63
|
+
airborne: boolean;
|
|
64
|
+
/** Metres driven. */
|
|
65
|
+
distance: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* What the driver asks for: the throttle (-1 full reverse to 1 full ahead) and the steering (-1 full left to 1 full right).
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { driveStep, startDrive, type DriveInput } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
73
|
+
*
|
|
74
|
+
* const input: DriveInput = { throttle: 1, steer: -0.5 };
|
|
75
|
+
* console.log(driveStep(startDrive(0, 0, 0, 0), input, 0.02, () => 0).speed > 0);
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export interface DriveInput {
|
|
79
|
+
throttle: number;
|
|
80
|
+
steer: number;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* A rover at rest at a place, on the ground.
|
|
84
|
+
*
|
|
85
|
+
* @param east - Metres east of the site point.
|
|
86
|
+
* @param north - Metres north of it.
|
|
87
|
+
* @param heading - Degrees clockwise from north.
|
|
88
|
+
* @param ground - The ground's height over the site's ground at the place.
|
|
89
|
+
* @returns The state.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
94
|
+
*
|
|
95
|
+
* const rover = startDrive(77, 11, 264, 0);
|
|
96
|
+
* console.log(rover.heading, rover.speed);
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export declare function startDrive(east: number, north: number, heading: number, ground: number): DriveState;
|
|
100
|
+
/** Where a point of the rover's frame is in the site's plane and height (before the wheels' own turns): the heading, then the pitch, then the roll. */
|
|
101
|
+
export declare function toWorld(state: Pick<DriveState, "east" | "north" | "heading" | "pitch" | "roll" | "y">, p: V3): V3;
|
|
102
|
+
/**
|
|
103
|
+
* One step of driving: the speed follows the throttle (slowed uphill by the Moon's gravity, stopped by a slope over 25 degrees and by anything in `blocked`), the heading follows the steering (the four wheels steer, the
|
|
104
|
+
* back ones the other way), the rover moves, and its height, pitch and roll follow the four places its wheels touch, the height on a spring and in free fall (at lunar gravity) when the ground drops away.
|
|
105
|
+
*
|
|
106
|
+
* @param state - The rover now.
|
|
107
|
+
* @param input - The throttle and the steering.
|
|
108
|
+
* @param dt - Seconds (at most 0.05 are used; call it more often for a longer time).
|
|
109
|
+
* @param ground - The ground's height over the site's ground at a place.
|
|
110
|
+
* @param blocked - True at a place the rover cannot go (a big rock, the lunar module).
|
|
111
|
+
* @returns The rover a moment later.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* import { driveStep, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
116
|
+
*
|
|
117
|
+
* let rover = startDrive(0, 0, 0, 0);
|
|
118
|
+
* for (let i = 0; i < 120; i += 1) rover = driveStep(rover, { throttle: 1, steer: 0 }, 1 / 60, () => 0);
|
|
119
|
+
* console.log(`${rover.north.toFixed(1)} m north after two seconds, at ${rover.speed.toFixed(1)} m/s`);
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export declare function driveStep(state: DriveState, input: DriveInput, dt: number, ground: (east: number, north: number) => number, blocked?: (east: number, north: number) => boolean): DriveState;
|
|
123
|
+
/**
|
|
124
|
+
* The rover's shape as it is now, turned and placed: the parts of `roverParts` with each wheel turned by how far it has rolled and steered (the back wheels the other way), then moved by the rover's heading, pitch, roll
|
|
125
|
+
* and place. Everything is in the site's plane (x east, y up, z north).
|
|
126
|
+
*
|
|
127
|
+
* @param state - The rover.
|
|
128
|
+
* @returns The parts.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* import { driveParts, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
133
|
+
*
|
|
134
|
+
* const parts = driveParts(startDrive(0, 0, 90, 0));
|
|
135
|
+
* console.log(parts.length, "parts");
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export declare function driveParts(state: DriveState): Part[];
|
|
139
|
+
/**
|
|
140
|
+
* Where the eyes are and where they look from a view of the rover: from behind and above (`chase`) or from the left seat (`seat`).
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* import { driveView, startDrive, type DriveView } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
145
|
+
*
|
|
146
|
+
* const view: DriveView = driveView(startDrive(0, 0, 0, 0), "seat", { azimuth: 0, pitch: 0 }, () => 0);
|
|
147
|
+
* console.log(view.azimuth, view.eye.up);
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export interface DriveView {
|
|
151
|
+
eye: {
|
|
152
|
+
east: number;
|
|
153
|
+
north: number;
|
|
154
|
+
up: number;
|
|
155
|
+
};
|
|
156
|
+
/** Degrees clockwise from north and degrees up. */
|
|
157
|
+
azimuth: number;
|
|
158
|
+
pitch: number;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The eyes for a view of the rover. The chase view stands 6.5 m behind it and 2.8 m up (never under the ground), looking at it; the seat view is the commander's eyes in the left seat, looking along the rover's heading
|
|
162
|
+
* and tilting with it. `offset` is the driver's own turn of the view from a drag (degrees), added to the direction either looks in.
|
|
163
|
+
*
|
|
164
|
+
* @param state - The rover.
|
|
165
|
+
* @param view - `chase` or `seat`.
|
|
166
|
+
* @param offset - The driver's turn of the view, degrees right and up.
|
|
167
|
+
* @param ground - The ground's height over the site's ground at a place.
|
|
168
|
+
* @returns The eyes.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* import { driveView, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
173
|
+
*
|
|
174
|
+
* const view = driveView(startDrive(0, 0, 0, 0), "chase", { azimuth: 0, pitch: 0 }, () => 0);
|
|
175
|
+
* console.log(view.eye.north.toFixed(1), view.eye.up.toFixed(1));
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
export declare function driveView(state: DriveState, view: "chase" | "seat", offset: {
|
|
179
|
+
azimuth: number;
|
|
180
|
+
pitch: number;
|
|
181
|
+
}, ground: (east: number, north: number) => number): DriveView;
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// Driving the lunar rover: a vehicle on four wheels over the same ground the eyes walk on. The step is pure and small: throttle and steering in, the rover's place, heading, height, pitch and roll out, the chassis
|
|
2
|
+
// settling on a spring over the four places its wheels touch, free in the air where the ground falls away (the Moon's gravity is a sixth of ours, so a crest throws it). The ground is whatever function the page gives:
|
|
3
|
+
// the measured heights and the made craters, so the rover follows the craters exactly. What is real: the rover's size, the gravity and a top speed near the published one. What is made: the feel of the spring, the
|
|
4
|
+
// steering, the limits on a slope. No DOM.
|
|
5
|
+
import { roverParts } from "./shapes.js";
|
|
6
|
+
/**
|
|
7
|
+
* The rover's measures: those published (its length and width, the wheels' size and the wheelbase, the gravity of the Moon) and those chosen here (the speeds, the steering, the slope it will not climb, the spring).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { ROVER } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
12
|
+
*
|
|
13
|
+
* console.log(`${(ROVER.maxSpeed * 3.6).toFixed(0)} km/h at most, wheelbase ${ROVER.wheelbase} m`);
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export const ROVER = {
|
|
17
|
+
length: 3.05,
|
|
18
|
+
width: 2.13,
|
|
19
|
+
wheelbase: 2.3,
|
|
20
|
+
/** Metres between the wheels' middles across the rover. */
|
|
21
|
+
track: 1.9,
|
|
22
|
+
wheelRadius: 0.41,
|
|
23
|
+
/** The Moon's surface gravity, m/s2. */
|
|
24
|
+
gravity: 1.62,
|
|
25
|
+
/** About 12 km/h, a little under the speed the rover was designed for. */
|
|
26
|
+
maxSpeed: 3.4,
|
|
27
|
+
maxReverse: 1.4,
|
|
28
|
+
accel: 1.1,
|
|
29
|
+
brake: 2.6,
|
|
30
|
+
/** The most a wheel is turned, radians (30 degrees), at the front and, the other way, at the back. */
|
|
31
|
+
maxSteer: 0.52,
|
|
32
|
+
/** The steepest slope it will climb, radians (25 degrees). */
|
|
33
|
+
maxSlope: 0.436,
|
|
34
|
+
/** The wheels' middles in the rover's frame (x right, y up, z forward), front left, front right, rear left, rear right. */
|
|
35
|
+
wheels: [
|
|
36
|
+
[-0.95, 0.41, 1.15],
|
|
37
|
+
[0.95, 0.41, 1.15],
|
|
38
|
+
[-0.95, 0.41, -1.15],
|
|
39
|
+
[0.95, 0.41, -1.15],
|
|
40
|
+
],
|
|
41
|
+
/** The commander's eyes in the rover's frame, the left seat. */
|
|
42
|
+
seat: [-0.42, 1.45, -0.3],
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* A rover at rest at a place, on the ground.
|
|
46
|
+
*
|
|
47
|
+
* @param east - Metres east of the site point.
|
|
48
|
+
* @param north - Metres north of it.
|
|
49
|
+
* @param heading - Degrees clockwise from north.
|
|
50
|
+
* @param ground - The ground's height over the site's ground at the place.
|
|
51
|
+
* @returns The state.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
56
|
+
*
|
|
57
|
+
* const rover = startDrive(77, 11, 264, 0);
|
|
58
|
+
* console.log(rover.heading, rover.speed);
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export function startDrive(east, north, heading, ground) {
|
|
62
|
+
return { east, north, heading, speed: 0, steer: 0, y: ground, vy: 0, pitch: 0, roll: 0, spin: [0, 0, 0, 0], airborne: false, distance: 0 };
|
|
63
|
+
}
|
|
64
|
+
const RAD = Math.PI / 180;
|
|
65
|
+
/** Where a point of the rover's frame is in the site's plane and height (before the wheels' own turns): the heading, then the pitch, then the roll. */
|
|
66
|
+
export function toWorld(state, p) {
|
|
67
|
+
const [x, y, z] = p;
|
|
68
|
+
const [cr, sr, cp, sp] = [Math.cos(state.roll), Math.sin(state.roll), Math.cos(state.pitch), Math.sin(state.pitch)];
|
|
69
|
+
// roll about the forward axis, then pitch about the right axis
|
|
70
|
+
const x1 = x * cr - y * sr;
|
|
71
|
+
const y1 = x * sr + y * cr;
|
|
72
|
+
const y2 = y1 * cp + z * sp;
|
|
73
|
+
const z2 = -y1 * sp + z * cp;
|
|
74
|
+
const h = state.heading * RAD;
|
|
75
|
+
const [c, s] = [Math.cos(h), Math.sin(h)];
|
|
76
|
+
return [state.east + x1 * c + z2 * s, state.y + y2, state.north - x1 * s + z2 * c];
|
|
77
|
+
}
|
|
78
|
+
/** The ground under each wheel, at the places the four wheels are. */
|
|
79
|
+
function wheelGrounds(state, ground) {
|
|
80
|
+
const h = state.heading * RAD;
|
|
81
|
+
const [c, s] = [Math.cos(h), Math.sin(h)];
|
|
82
|
+
return ROVER.wheels.map(([x, , z]) => ground(state.east + x * c + z * s, state.north - x * s + z * c));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* One step of driving: the speed follows the throttle (slowed uphill by the Moon's gravity, stopped by a slope over 25 degrees and by anything in `blocked`), the heading follows the steering (the four wheels steer, the
|
|
86
|
+
* back ones the other way), the rover moves, and its height, pitch and roll follow the four places its wheels touch, the height on a spring and in free fall (at lunar gravity) when the ground drops away.
|
|
87
|
+
*
|
|
88
|
+
* @param state - The rover now.
|
|
89
|
+
* @param input - The throttle and the steering.
|
|
90
|
+
* @param dt - Seconds (at most 0.05 are used; call it more often for a longer time).
|
|
91
|
+
* @param ground - The ground's height over the site's ground at a place.
|
|
92
|
+
* @param blocked - True at a place the rover cannot go (a big rock, the lunar module).
|
|
93
|
+
* @returns The rover a moment later.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* import { driveStep, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
98
|
+
*
|
|
99
|
+
* let rover = startDrive(0, 0, 0, 0);
|
|
100
|
+
* for (let i = 0; i < 120; i += 1) rover = driveStep(rover, { throttle: 1, steer: 0 }, 1 / 60, () => 0);
|
|
101
|
+
* console.log(`${rover.north.toFixed(1)} m north after two seconds, at ${rover.speed.toFixed(1)} m/s`);
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export function driveStep(state, input, dt, ground, blocked) {
|
|
105
|
+
const d = Math.min(Math.max(dt, 0), 0.05);
|
|
106
|
+
const next = { ...state, spin: [...state.spin] };
|
|
107
|
+
const throttle = Math.min(1, Math.max(-1, input.throttle));
|
|
108
|
+
const wanted = Math.min(1, Math.max(-1, input.steer));
|
|
109
|
+
next.steer += (wanted * ROVER.maxSteer - next.steer) * (1 - Math.exp(-d * 6));
|
|
110
|
+
// the speed: toward what the throttle asks, quickly when braking; uphill the Moon's gravity takes some, and past 25 degrees nothing goes uphill
|
|
111
|
+
const target = throttle >= 0 ? throttle * ROVER.maxSpeed : throttle * ROVER.maxReverse;
|
|
112
|
+
const braking = Math.sign(target) !== Math.sign(next.speed) && next.speed !== 0;
|
|
113
|
+
const a = braking || Math.abs(target) < Math.abs(next.speed) ? ROVER.brake : ROVER.accel;
|
|
114
|
+
const delta = target - next.speed;
|
|
115
|
+
next.speed += Math.sign(delta) * Math.min(Math.abs(delta), a * d);
|
|
116
|
+
if (Math.abs(throttle) < 0.02)
|
|
117
|
+
next.speed -= Math.sign(next.speed) * Math.min(Math.abs(next.speed), 0.35 * d); // a little rolling resistance
|
|
118
|
+
next.speed -= ROVER.gravity * Math.sin(state.pitch) * d;
|
|
119
|
+
if ((state.pitch > ROVER.maxSlope && next.speed > 0) || (state.pitch < -ROVER.maxSlope && next.speed < 0))
|
|
120
|
+
next.speed = 0; // uphill past 25 degrees: it does not go
|
|
121
|
+
// the brake holds it once it has nearly stopped, on any slope it could climb: a rover left alone stays where it is
|
|
122
|
+
if (Math.abs(throttle) < 0.02 && Math.abs(next.speed) < 0.08 && Math.abs(state.pitch) < ROVER.maxSlope)
|
|
123
|
+
next.speed = 0;
|
|
124
|
+
// the heading: four wheels steer, so the turn is tight; the move
|
|
125
|
+
const yaw = (next.speed * 2 * Math.tan(next.steer)) / ROVER.wheelbase;
|
|
126
|
+
next.heading = (((state.heading + yaw * d * (180 / Math.PI)) % 360) + 360) % 360;
|
|
127
|
+
const h = next.heading * RAD;
|
|
128
|
+
const [e, n] = [state.east + Math.sin(h) * next.speed * d, state.north + Math.cos(h) * next.speed * d];
|
|
129
|
+
const front = [e + Math.sin(h) * ROVER.length * 0.5, n + Math.cos(h) * ROVER.length * 0.5];
|
|
130
|
+
if (next.speed !== 0 && blocked !== undefined && (blocked(e, n) || blocked(front[0], front[1]))) {
|
|
131
|
+
next.speed = 0;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
next.east = e;
|
|
135
|
+
next.north = n;
|
|
136
|
+
next.distance += Math.abs(next.speed) * d;
|
|
137
|
+
}
|
|
138
|
+
// the ground under the four wheels: the plane they make, and where the rover sits over it
|
|
139
|
+
const g = wheelGrounds(next, ground);
|
|
140
|
+
const [fl, fr, rl, rr] = [g[0], g[1], g[2], g[3]];
|
|
141
|
+
const rest = (fl + fr + rl + rr) / 4;
|
|
142
|
+
const pitchTarget = Math.atan2((fl + fr) / 2 - (rl + rr) / 2, ROVER.wheelbase);
|
|
143
|
+
const rollTarget = Math.atan2((fr + rr) / 2 - (fl + rl) / 2, ROVER.track);
|
|
144
|
+
const settle = 1 - Math.exp(-d * 10);
|
|
145
|
+
next.pitch += (pitchTarget - next.pitch) * settle;
|
|
146
|
+
next.roll += (rollTarget - next.roll) * settle;
|
|
147
|
+
// the height: on its springs while the wheels are down (stiffness 81, damping 12.6, a quick and well damped settle), free where the ground has dropped more than a few centimetres away
|
|
148
|
+
next.airborne = next.y - rest > 0.03;
|
|
149
|
+
if (next.airborne)
|
|
150
|
+
next.vy -= ROVER.gravity * d;
|
|
151
|
+
else
|
|
152
|
+
next.vy += (81 * (rest - next.y) - 12.6 * next.vy) * d;
|
|
153
|
+
next.y += next.vy * d;
|
|
154
|
+
if (next.y < rest - 0.04) {
|
|
155
|
+
next.y = rest - 0.04;
|
|
156
|
+
if (next.vy < 0)
|
|
157
|
+
next.vy *= -0.15; // the springs take most of a landing
|
|
158
|
+
}
|
|
159
|
+
const turn = (next.speed * d) / ROVER.wheelRadius;
|
|
160
|
+
for (let i = 0; i < 4; i += 1)
|
|
161
|
+
next.spin[i] = (next.spin[i] + turn) % (Math.PI * 2);
|
|
162
|
+
return next;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The rover's shape as it is now, turned and placed: the parts of `roverParts` with each wheel turned by how far it has rolled and steered (the back wheels the other way), then moved by the rover's heading, pitch, roll
|
|
166
|
+
* and place. Everything is in the site's plane (x east, y up, z north).
|
|
167
|
+
*
|
|
168
|
+
* @param state - The rover.
|
|
169
|
+
* @returns The parts.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* import { driveParts, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
174
|
+
*
|
|
175
|
+
* const parts = driveParts(startDrive(0, 0, 90, 0));
|
|
176
|
+
* console.log(parts.length, "parts");
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
export function driveParts(state) {
|
|
180
|
+
const [cr, sr, cp, sp] = [Math.cos(state.roll), Math.sin(state.roll), Math.cos(state.pitch), Math.sin(state.pitch)];
|
|
181
|
+
const h = state.heading * RAD;
|
|
182
|
+
const [ch, sh] = [Math.cos(h), Math.sin(h)];
|
|
183
|
+
const rotate = (p) => {
|
|
184
|
+
const x1 = p[0] * cr - p[1] * sr;
|
|
185
|
+
const y1 = p[0] * sr + p[1] * cr;
|
|
186
|
+
const y2 = y1 * cp + p[2] * sp;
|
|
187
|
+
const z2 = -y1 * sp + p[2] * cp;
|
|
188
|
+
return [x1 * ch + z2 * sh, y2, -x1 * sh + z2 * ch];
|
|
189
|
+
};
|
|
190
|
+
return roverParts().map((part) => {
|
|
191
|
+
const out = { ...part, position: [], normal: [] };
|
|
192
|
+
const wheel = part.wheel;
|
|
193
|
+
const centre = wheel === undefined ? [0, 0, 0] : [...ROVER.wheels[wheel]];
|
|
194
|
+
const steer = wheel === undefined ? 0 : (wheel < 2 ? 1 : -1) * state.steer;
|
|
195
|
+
const spin = wheel === undefined ? 0 : state.spin[wheel];
|
|
196
|
+
const [cs, ss, cy, sy] = [Math.cos(spin), Math.sin(spin), Math.cos(steer), Math.sin(steer)];
|
|
197
|
+
const turnWheel = (p) => {
|
|
198
|
+
// about the wheel's own axle (x), then about the up axis through its middle
|
|
199
|
+
const y1 = p[1] * cs - p[2] * ss;
|
|
200
|
+
const z1 = p[1] * ss + p[2] * cs;
|
|
201
|
+
return [p[0] * cy + z1 * sy, y1, -p[0] * sy + z1 * cy];
|
|
202
|
+
};
|
|
203
|
+
for (let i = 0; i < part.position.length; i += 3) {
|
|
204
|
+
let p = [part.position[i], part.position[i + 1], part.position[i + 2]];
|
|
205
|
+
let nrm = [part.normal[i], part.normal[i + 1], part.normal[i + 2]];
|
|
206
|
+
if (wheel !== undefined) {
|
|
207
|
+
p = turnWheel([p[0] - centre[0], p[1] - centre[1], p[2] - centre[2]]);
|
|
208
|
+
p = [p[0] + centre[0], p[1] + centre[1], p[2] + centre[2]];
|
|
209
|
+
nrm = turnWheel(nrm);
|
|
210
|
+
}
|
|
211
|
+
const w = rotate(p);
|
|
212
|
+
out.position.push(state.east + w[0], state.y + w[1], state.north + w[2]);
|
|
213
|
+
const m = rotate(nrm);
|
|
214
|
+
out.normal.push(m[0], m[1], m[2]);
|
|
215
|
+
}
|
|
216
|
+
return out;
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* The eyes for a view of the rover. The chase view stands 6.5 m behind it and 2.8 m up (never under the ground), looking at it; the seat view is the commander's eyes in the left seat, looking along the rover's heading
|
|
221
|
+
* and tilting with it. `offset` is the driver's own turn of the view from a drag (degrees), added to the direction either looks in.
|
|
222
|
+
*
|
|
223
|
+
* @param state - The rover.
|
|
224
|
+
* @param view - `chase` or `seat`.
|
|
225
|
+
* @param offset - The driver's turn of the view, degrees right and up.
|
|
226
|
+
* @param ground - The ground's height over the site's ground at a place.
|
|
227
|
+
* @returns The eyes.
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```ts
|
|
231
|
+
* import { driveView, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
232
|
+
*
|
|
233
|
+
* const view = driveView(startDrive(0, 0, 0, 0), "chase", { azimuth: 0, pitch: 0 }, () => 0);
|
|
234
|
+
* console.log(view.eye.north.toFixed(1), view.eye.up.toFixed(1));
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
export function driveView(state, view, offset, ground) {
|
|
238
|
+
if (view === "seat") {
|
|
239
|
+
const eye = toWorld(state, ROVER.seat);
|
|
240
|
+
return { eye: { east: eye[0], north: eye[2], up: eye[1] }, azimuth: (((state.heading + offset.azimuth) % 360) + 360) % 360, pitch: state.pitch / RAD + offset.pitch };
|
|
241
|
+
}
|
|
242
|
+
const a = (state.heading + offset.azimuth) * RAD;
|
|
243
|
+
const [east, north] = [state.east - Math.sin(a) * 6.5, state.north - Math.cos(a) * 6.5];
|
|
244
|
+
const up = Math.max(state.y + 2.8, ground(east, north) + 1.4);
|
|
245
|
+
const drop = (up - (state.y + 1.0)) / 6.5;
|
|
246
|
+
return { eye: { east, north, up }, azimuth: (((state.heading + offset.azimuth) % 360) + 360) % 360, pitch: -Math.atan(drop) / RAD + offset.pitch };
|
|
247
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A pool of grains in flight, made with `new DustField(size, seed)`: `size` is how many grains it holds (the oldest is reused when it is full) and `seed` makes the scatter the same for the same drive.
|
|
3
|
+
*
|
|
4
|
+
* @param size - How many grains the pool holds.
|
|
5
|
+
* @param seed - A seed for the scatter.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { DustField } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
10
|
+
*
|
|
11
|
+
* const dust = new DustField(64, 1);
|
|
12
|
+
* dust.emit([0, 0.3, 0], [2, 1.5, 0], 2);
|
|
13
|
+
* dust.step(0.5, () => 0);
|
|
14
|
+
* console.log(dust.live, dust.position[1]!.toFixed(2));
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare class DustField {
|
|
18
|
+
#private;
|
|
19
|
+
/** Each grain's place, three numbers (east, up, north), and its velocity; a grain not in flight sits far below the ground. */
|
|
20
|
+
readonly position: Float32Array;
|
|
21
|
+
readonly velocity: Float32Array;
|
|
22
|
+
readonly age: Float32Array;
|
|
23
|
+
readonly life: Float32Array;
|
|
24
|
+
/** The grains in flight. */
|
|
25
|
+
live: number;
|
|
26
|
+
/**
|
|
27
|
+
* @param size - How many grains the pool holds; the oldest is reused when it is full.
|
|
28
|
+
* @param seed - A seed for the scatter, so the same drive is the same.
|
|
29
|
+
*/
|
|
30
|
+
constructor(size: number, seed?: number);
|
|
31
|
+
/**
|
|
32
|
+
* Throw one grain: from a place, with a velocity, scattered a little, for up to `life` seconds.
|
|
33
|
+
*
|
|
34
|
+
* @param from - East, up, north in metres.
|
|
35
|
+
* @param velocity - East, up, north in metres a second.
|
|
36
|
+
* @param life - The longest it flies, in seconds.
|
|
37
|
+
*/
|
|
38
|
+
emit(from: readonly [number, number, number], velocity: readonly [number, number, number], life: number): void;
|
|
39
|
+
/**
|
|
40
|
+
* Move every grain on: its velocity falls at the Moon's gravity and it goes where the velocity takes it; a grain that reaches the ground, or is older than its life, is let go.
|
|
41
|
+
*
|
|
42
|
+
* @param dt - Seconds.
|
|
43
|
+
* @param ground - The ground's height over the site's ground at a place.
|
|
44
|
+
*/
|
|
45
|
+
step(dt: number, ground: (east: number, north: number) => number): void;
|
|
46
|
+
}
|