@johnmorrisdotca/wakusei-planets 0.48.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 +4 -0
- package/NOTICE.md +1 -1
- package/README.md +3 -3
- package/dist/scene/textures.d.ts +2 -2
- 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
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.49.0] - 2026-11-13
|
|
6
|
+
|
|
7
|
+
- **Drive the Apollo 17 rover** (MODULES-1331). `drive="on"` on `<wakusei-surface>` puts the eyes behind the rover at its published place and lets you drive it: W and S (or the arrows, or the walking stick) for the throttle, A and D for the steering, C between the chase view and the commander's seat, a drag to turn the view round it. A pure drive step (`driveStep`) reads the four places the wheels touch on the same ground the eyes walk on, the measured heights and the made craters, so the rover tilts and settles on a spring and, where the ground falls away, is in free fall at the Moon's gravity; the speed follows the throttle to about 12 km/h, is slowed uphill, stops at 25 degrees and at big rocks and the things at the site, and a brake holds it still once it has nearly stopped. The wheels throw fine grey grains up and back in a fan, in parabolas at 1.62 m/s2 (made, not measured), and leave tracks like the published traverse. One draw per step while moving and nothing at rest; `driveFor(seconds, input)` drives by a set time, `vehicle` and `driveCamera` are its state and view. The rover's drawing is corrected to the published sizes (about 3.05 m by 2.13 m, wheels 0.81 m, wheelbase 2.29 m), the lunar module and the rover read as themselves (gold body, ladder and porch, fenders, seats, masts), and the dish stands on a taller mast so the seat view sees under it.
|
|
8
|
+
|
|
5
9
|
## [0.48.0] - 2026-11-13
|
|
6
10
|
|
|
7
11
|
- **Which face of the body the view looks at** (MODULES-1330). `<wakusei-space>` has `subCameraPoint()`, the longitude (east) and latitude (north), in the centred body's own fixed frame at the moment shown, of the point under the camera (`null` while riding along), and `faceTo({ lon, lat }, { smooth })`, which turns the view round the body to bring a point there (the glide of `goToPlace`); `stats.camera.sub` says the same point as of the last draw. Beside a moon (`ride(moon, { view: "beside" })`, the way a moon is visited) `subCameraPoint()` is the point under the line from the moon's centre to the camera, and `faceTo` stands the camera as near that side as the view allows. `<wakusei-body>` has the same pair for its picture (the date's or the portrait's pose, the view's turn, tilt and roll counted in). The pure functions `subCameraPoint` and `faceView` are exported from `/space`, with `besideFace`, `facingPoint` and `surfaceDirection`. For a card picture that follows the main view, and the reverse. Guide: `docs/guides/which-face.md` and its Japanese twin.
|
package/NOTICE.md
CHANGED
|
@@ -269,7 +269,7 @@ The height tiles of `<wakusei-surface>` (`scripts/make-moon-terrain.py`; they ar
|
|
|
269
269
|
- **The ground picture**: the browse picture of `NAC_ROI_APOLLO17LOC_E205N0306_5M`, the Apollo 17 low-Sun controlled narrow angle camera mosaic, 5 m a pixel (https://pds.lroc.im-ldi.com/data/LRO-L-LROC-5-RDR-V1.0/LROLRC_2001/EXTRAS/BROWSE/NAC_ROI/APOLLO17LOC/NAC_ROI_APOLLO17LOC_E205N0306_5M.TIF, 108,949,491 bytes). The build fits the Sun the mosaic was lit by to the heights, divides that shading out and keeps the ground's own light and dark (the fit for Apollo 17: azimuth 264 degrees, elevation 7 degrees, correlation 0.76).
|
|
270
270
|
- **Heights beyond the model**: the Lunar Orbiter Laser Altimeter (LOLA) global height map at 64 pixels a degree (`ldem_64.img`, data set `LRO-L-LOLA-4-GDR-V1.0`, https://pds-geosciences.wustl.edu/lro/lro-l-lola-3-rdr-v1/lrolol_1xxx/data/lola_gdr/cylindrical/img/ldem_64.img, 23,040 x 11,520 samples of 16 bits, height = 0.5 m x value above a sphere of radius 1,737.4 km), of which only the window round the site is read, by HTTP byte ranges (about 25 MB of a 531 MB file).
|
|
271
271
|
- **Terms**: the PDS Geosciences Node's FAQ (https://pds-geosciences.wustl.edu/faq.html, read 2026-11-13): "Data sets archived in the PDS are in the public domain." The LROC terms of use (https://lroc.im-ldi.com/about/terms, read 2026-11-13) say the products of the LROC data node and the PDS archive are in the public domain, that images elsewhere on the LROC website (featured images and curated products) are copyrighted and are not used here, and ask for the credit "NASA/GSFC/Arizona State University". The credit lines the page shows are that, and "LOLA Team (NASA/GSFC)", with the Planetary Data System as the publisher of the data sets (the PDS asks that a data set be cited like published research; its DOI is in the PDS DOI portal).
|
|
272
|
-
- **Things at the Apollo 17 site** (`src/surface/objects-apollo-17.ts`, made by `scripts/make-apollo17-objects.py`, read on 2026-11-13): the positions of the lunar module's descent stage, the flag, the ALSEP central station and the surface electrical properties transmitter, and the rover's nine traverses, from the map of Haase, Wählisch, Gläser, Oberst and Robinson, "Coordinates and Maps of the Apollo 17 Landing Site", Earth and Space Science 6 (2019), https://doi.org/10.1029/2018EA000408, as archived by the PDS Cartography and Imaging Sciences Node and published by USGS Astrogeology (https://astrogeology.usgs.gov/search/map/moon_apollo_17_lroc_nac_landing_site_orthomosaic_50cm, whose use constraint reads "Please cite authors"). Two files are read out of its 1.7 GB zip by HTTP byte ranges (about 94 KB fetched; the zip is never downloaded): `APOLLO17_CONTROLPOINTS_2018.TAB` (body-fixed X, Y, Z in metres, the published positions) and `APOLLO17_TRAVERSE_2018.SHP` (the traverses, Transverse Mercator on a sphere of 1,737.4 km, central meridian 30.7 degrees). They are coordinates, which are facts; no picture, text or map of the paper is used. The rover's place and heading are this package's reading of the end of the last traverse line: the paper's own table of the rover's parking place was not available to this build (the publisher's page refused programs), so the rover's place is not that published position and the page says so. The shapes of all five things are plain drawings made here (boxes, cylinders and struts, from sizes of about 3.2 m for the descent stage's deck, 4.7 m from its middle to a footpad, and 3.
|
|
272
|
+
- **Things at the Apollo 17 site** (`src/surface/objects-apollo-17.ts`, made by `scripts/make-apollo17-objects.py`, read on 2026-11-13): the positions of the lunar module's descent stage, the flag, the ALSEP central station and the surface electrical properties transmitter, and the rover's nine traverses, from the map of Haase, Wählisch, Gläser, Oberst and Robinson, "Coordinates and Maps of the Apollo 17 Landing Site", Earth and Space Science 6 (2019), https://doi.org/10.1029/2018EA000408, as archived by the PDS Cartography and Imaging Sciences Node and published by USGS Astrogeology (https://astrogeology.usgs.gov/search/map/moon_apollo_17_lroc_nac_landing_site_orthomosaic_50cm, whose use constraint reads "Please cite authors"). Two files are read out of its 1.7 GB zip by HTTP byte ranges (about 94 KB fetched; the zip is never downloaded): `APOLLO17_CONTROLPOINTS_2018.TAB` (body-fixed X, Y, Z in metres, the published positions) and `APOLLO17_TRAVERSE_2018.SHP` (the traverses, Transverse Mercator on a sphere of 1,737.4 km, central meridian 30.7 degrees). They are coordinates, which are facts; no picture, text or map of the paper is used. The rover's place and heading are this package's reading of the end of the last traverse line: the paper's own table of the rover's parking place was not available to this build (the publisher's page refused programs), so the rover's place is not that published position and the page says so. The shapes of all five things are plain drawings made here (boxes, cylinders and struts, from sizes of about 3.2 m for the descent stage's deck, 4.7 m from its middle to a footpad, and about 3.05 m by 2.13 m for the rover, its wheels 0.81 m across, its wheelbase 2.29 m, its design speed about 13 km/h: read on 2026-11-13 from secondary tables, for example https://space-robotics.org/robots/lunar/apollo-lrv/, and to be checked against NASA's Lunar Roving Vehicle Operations Handbook); no flag, name or mark is drawn. uchusen-rockets has a lunar module (its descent and ascent stages, plain shapes) that this package does not use; no lunar rover model was found in it.
|
|
273
273
|
- **Left out**: the merged LOLA and Kaguya (SELENE) height grid. The JAXA part's terms could not be found, and an MIT-licensed package can pass on only what it knows to be free to pass on.
|
|
274
274
|
- **Made, not measured**: everything below the 5 m grid (small craters from 0.5 to 20 m, the roughness of the dust, rocks of 0.1 to 1.5 m) is made by this package from a seeded random grid, by the lunar size-frequency law, and is not a picture or a measurement of any place. The picture says so under the ground.
|
|
275
275
|
|
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ Wakusei Planets runs in the browser of a phone, a tablet or a computer: nothing
|
|
|
19
19
|
|
|
20
20
|
<p align="center"><img alt="The demo on a desk: Saturn in the painted look, turning in the big view, with the page's links to the portrait builder, the colouring sheets, the space view, the star page and the size line-up above it, and the choice of body and its moons below" src="https://raw.githubusercontent.com/johnmorrisdotca/wakusei-planets/main/docs/images/home-desk.webp" width="620"> <img alt="The same page on a phone: the same links in rows, and Saturn in the painted look below them" src="https://raw.githubusercontent.com/johnmorrisdotca/wakusei-planets/main/docs/images/home-phone.webp" width="200"></p>
|
|
21
21
|
|
|
22
|
-
<p align="center"><img alt="Every body at one scale, the Earth among them: each small body has a magnified picture joined by a line, and the Sun shows only its edge" src="https://raw.githubusercontent.com/johnmorrisdotca/wakusei-planets/v0.
|
|
22
|
+
<p align="center"><img alt="Every body at one scale, the Earth among them: each small body has a magnified picture joined by a line, and the Sun shows only its edge" src="https://raw.githubusercontent.com/johnmorrisdotca/wakusei-planets/v0.49.0/docs/images/lineup-desk.png" width="640"></p>
|
|
23
23
|
|
|
24
24
|
## Who it is for
|
|
25
25
|
|
|
@@ -388,7 +388,7 @@ The tiles of every body share one 48 MB budget, six loads at a time for a source
|
|
|
388
388
|
|
|
389
389
|
## The portrait builder
|
|
390
390
|
|
|
391
|
-
<p align="center"><img alt="The Sun and the planets in an arc, painted, with their names, in the portrait builder" src="https://raw.githubusercontent.com/johnmorrisdotca/wakusei-planets/v0.
|
|
391
|
+
<p align="center"><img alt="The Sun and the planets in an arc, painted, with their names, in the portrait builder" src="https://raw.githubusercontent.com/johnmorrisdotca/wakusei-planets/v0.49.0/docs/images/portrait-family.png" width="640"></p>
|
|
392
392
|
|
|
393
393
|
[The portrait builder](https://johnmorrisdotca.github.io/wakusei-planets/portrait.html) puts any bodies in one picture: drag them, size them, turn them, order them front to back, arrange them in a row, an arc, a grid or at true orbit distance (true or readable sizes, a planet's moons round it in orbit order), light them from one direction, and take the code, a PNG or an SVG. The scene is kept in the address, and every body is drawn in one WebGL scene, so the page holds one WebGL context. [The portrait builder](docs/guides/portrait-builder.md) is its guide.
|
|
394
394
|
|
|
@@ -536,7 +536,7 @@ BODIES.pluto.kind; // "dwarf-planet"
|
|
|
536
536
|
|
|
537
537
|
If this package helps a paper, a talk or a class, please cite it as software. GitHub's "Cite this repository" button reads [CITATION.cff](CITATION.cff) and gives BibTeX and APA; in plain text:
|
|
538
538
|
|
|
539
|
-
> Morris, J. (2026). *Wakusei Planets* (version 0.
|
|
539
|
+
> Morris, J. (2026). *Wakusei Planets* (version 0.49.0) [Computer software]. https://github.com/johnmorrisdotca/wakusei-planets
|
|
540
540
|
|
|
541
541
|
Cite the version you used (`npm ls @johnmorrisdotca/wakusei-planets`), and cite the sources of the numbers you use as well: [NOTICE.md](NOTICE.md) names each one, with the paper or the page it was read from (the IAU working group's report for sizes and poles, VSOP87B, ELP2000-82B, L1.2 and TASS1.7 for positions, and NASA's JPL Horizons for the fits and the spacecraft).
|
|
542
542
|
|
package/dist/scene/textures.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { type BodyMapSource } from "../door/map.ts";
|
|
|
11
11
|
* console.log(TEXTURE_BASE);
|
|
12
12
|
* ```
|
|
13
13
|
*/
|
|
14
|
-
export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdotca/wakusei-planets@0.
|
|
14
|
+
export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdotca/wakusei-planets@0.49.0/dist/textures/";
|
|
15
15
|
/**
|
|
16
16
|
* Where the sharpest surface pictures are: the files of this exact version in the repository, on jsDelivr's GitHub address. The npm package holds only the two smaller widths, so a page that
|
|
17
17
|
* never zooms in never fetches them. Set `texture-far-base` on an element to serve them yourself.
|
|
@@ -23,7 +23,7 @@ export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdot
|
|
|
23
23
|
* console.log(TEXTURE_FAR_BASE);
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
|
-
export declare const TEXTURE_FAR_BASE = "https://cdn.jsdelivr.net/gh/johnmorrisdotca/wakusei-planets@v0.
|
|
26
|
+
export declare const TEXTURE_FAR_BASE = "https://cdn.jsdelivr.net/gh/johnmorrisdotca/wakusei-planets@v0.49.0/textures/";
|
|
27
27
|
/**
|
|
28
28
|
* Where the finer map tiles are by default: the demo site's `tiles/` folder (GitHub Pages sends `access-control-allow-origin: *`). The tiles are made by `scripts/make-body-tiles.py` and are in no package and no repository:
|
|
29
29
|
* a page that serves its own gives the folder in the `tiles` attribute, and `tiles="off"` turns them off. A page that sets `texture-base` (it serves its own pictures) has none unless it says where they are.
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// The dust a wheel throws: grains in free flight. On the Moon there is no air, so a grain thrown up and back goes in a clean parabola at the Moon's gravity and falls where it falls: no cloud, no drift. The field is
|
|
2
|
+
// a fixed pool of grains, each with a place, a velocity and an age; a step moves them and lets them go when they reach the ground or are old. Made, not measured: how many grains, how fast, how big. No DOM.
|
|
3
|
+
/** The Moon's surface gravity, m/s2. */
|
|
4
|
+
const G = 1.62;
|
|
5
|
+
/**
|
|
6
|
+
* 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.
|
|
7
|
+
*
|
|
8
|
+
* @param size - How many grains the pool holds.
|
|
9
|
+
* @param seed - A seed for the scatter.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { DustField } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
14
|
+
*
|
|
15
|
+
* const dust = new DustField(64, 1);
|
|
16
|
+
* dust.emit([0, 0.3, 0], [2, 1.5, 0], 2);
|
|
17
|
+
* dust.step(0.5, () => 0);
|
|
18
|
+
* console.log(dust.live, dust.position[1]!.toFixed(2));
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export class DustField {
|
|
22
|
+
/** Each grain's place, three numbers (east, up, north), and its velocity; a grain not in flight sits far below the ground. */
|
|
23
|
+
position;
|
|
24
|
+
velocity;
|
|
25
|
+
age;
|
|
26
|
+
life;
|
|
27
|
+
#next = 0;
|
|
28
|
+
#seed;
|
|
29
|
+
/** The grains in flight. */
|
|
30
|
+
live = 0;
|
|
31
|
+
/**
|
|
32
|
+
* @param size - How many grains the pool holds; the oldest is reused when it is full.
|
|
33
|
+
* @param seed - A seed for the scatter, so the same drive is the same.
|
|
34
|
+
*/
|
|
35
|
+
constructor(size, seed = 1) {
|
|
36
|
+
this.position = new Float32Array(size * 3).fill(0);
|
|
37
|
+
this.velocity = new Float32Array(size * 3);
|
|
38
|
+
this.age = new Float32Array(size).fill(1e9);
|
|
39
|
+
this.life = new Float32Array(size).fill(0);
|
|
40
|
+
this.#seed = seed >>> 0 || 1;
|
|
41
|
+
for (let i = 0; i < size; i += 1)
|
|
42
|
+
this.position[i * 3 + 1] = -1e5;
|
|
43
|
+
}
|
|
44
|
+
/** A number from 0 to 1 that is the same for the same seed (mulberry32). */
|
|
45
|
+
#random() {
|
|
46
|
+
this.#seed = (this.#seed + 0x6d2b79f5) >>> 0;
|
|
47
|
+
let t = this.#seed;
|
|
48
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
49
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
50
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Throw one grain: from a place, with a velocity, scattered a little, for up to `life` seconds.
|
|
54
|
+
*
|
|
55
|
+
* @param from - East, up, north in metres.
|
|
56
|
+
* @param velocity - East, up, north in metres a second.
|
|
57
|
+
* @param life - The longest it flies, in seconds.
|
|
58
|
+
*/
|
|
59
|
+
emit(from, velocity, life) {
|
|
60
|
+
const i = this.#next;
|
|
61
|
+
this.#next = (this.#next + 1) % this.age.length;
|
|
62
|
+
const scatter = 0.35;
|
|
63
|
+
this.position.set(from, i * 3);
|
|
64
|
+
this.velocity[i * 3] = velocity[0] + (this.#random() - 0.5) * scatter * 2;
|
|
65
|
+
this.velocity[i * 3 + 1] = velocity[1] * (0.7 + 0.6 * this.#random());
|
|
66
|
+
this.velocity[i * 3 + 2] = velocity[2] + (this.#random() - 0.5) * scatter * 2;
|
|
67
|
+
this.age[i] = 0;
|
|
68
|
+
this.life[i] = life * (0.6 + 0.4 * this.#random());
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 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.
|
|
72
|
+
*
|
|
73
|
+
* @param dt - Seconds.
|
|
74
|
+
* @param ground - The ground's height over the site's ground at a place.
|
|
75
|
+
*/
|
|
76
|
+
step(dt, ground) {
|
|
77
|
+
let live = 0;
|
|
78
|
+
for (let i = 0; i < this.age.length; i += 1) {
|
|
79
|
+
if (this.age[i] >= this.life[i])
|
|
80
|
+
continue;
|
|
81
|
+
this.age[i] += dt;
|
|
82
|
+
this.velocity[i * 3 + 1] -= G * dt;
|
|
83
|
+
const [x, y, z] = [this.position[i * 3] + this.velocity[i * 3] * dt, this.position[i * 3 + 1] + this.velocity[i * 3 + 1] * dt, this.position[i * 3 + 2] + this.velocity[i * 3 + 2] * dt];
|
|
84
|
+
if (this.age[i] >= this.life[i] || y < ground(x, z)) {
|
|
85
|
+
this.age[i] = 1e9;
|
|
86
|
+
this.position[i * 3 + 1] = -1e5;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
this.position[i * 3] = x;
|
|
90
|
+
this.position[i * 3 + 1] = y;
|
|
91
|
+
this.position[i * 3 + 2] = z;
|
|
92
|
+
live += 1;
|
|
93
|
+
}
|
|
94
|
+
this.live = live;
|
|
95
|
+
}
|
|
96
|
+
}
|