@typeonce/ritmo-math 0.14.0 → 0.16.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 +124 -0
- package/README.md +17 -12
- package/dist/Angle.d.ts +49 -0
- package/dist/Angle.d.ts.map +1 -0
- package/dist/Angle.js +67 -0
- package/dist/Angle.js.map +1 -0
- package/dist/Box2.d.ts +20 -4
- package/dist/Box2.d.ts.map +1 -1
- package/dist/Box2.js +16 -1
- package/dist/Box2.js.map +1 -1
- package/dist/Collide2.d.ts +23 -1
- package/dist/Collide2.d.ts.map +1 -1
- package/dist/Collide2.js +23 -1
- package/dist/Collide2.js.map +1 -1
- package/dist/FlowField.d.ts +24 -38
- package/dist/FlowField.d.ts.map +1 -1
- package/dist/FlowField.js +22 -5
- package/dist/FlowField.js.map +1 -1
- package/dist/Scalar.d.ts +19 -166
- package/dist/Scalar.d.ts.map +1 -1
- package/dist/Scalar.js +54 -51
- package/dist/Scalar.js.map +1 -1
- package/dist/Segment2.d.ts +3 -0
- package/dist/Segment2.d.ts.map +1 -1
- package/dist/Segment2.js.map +1 -1
- package/dist/Vec2.d.ts +50 -7
- package/dist/Vec2.d.ts.map +1 -1
- package/dist/Vec2.js +59 -8
- package/dist/Vec2.js.map +1 -1
- package/dist/index.d.ts +9 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -22
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/Angle.ts +65 -0
- package/src/Box2.ts +24 -4
- package/src/Collide2.ts +30 -2
- package/src/FlowField.ts +38 -10
- package/src/Scalar.ts +46 -165
- package/src/Segment2.ts +3 -0
- package/src/Vec2.ts +61 -8
- package/src/index.ts +9 -22
- package/dist/Aabb.d.ts +0 -182
- package/dist/Aabb.d.ts.map +0 -1
- package/dist/Aabb.js +0 -109
- package/dist/Aabb.js.map +0 -1
- package/dist/InputAxis.d.ts +0 -44
- package/dist/InputAxis.d.ts.map +0 -1
- package/dist/InputAxis.js +0 -71
- package/dist/InputAxis.js.map +0 -1
- package/dist/Ops.d.ts +0 -209
- package/dist/Ops.d.ts.map +0 -1
- package/dist/Ops.js +0 -210
- package/dist/Ops.js.map +0 -1
- package/dist/Random.d.ts +0 -124
- package/dist/Random.d.ts.map +0 -1
- package/dist/Random.js +0 -126
- package/dist/Random.js.map +0 -1
- package/dist/Size2.d.ts +0 -118
- package/dist/Size2.d.ts.map +0 -1
- package/dist/Size2.js +0 -54
- package/dist/Size2.js.map +0 -1
- package/dist/SpatialGrid.d.ts +0 -68
- package/dist/SpatialGrid.d.ts.map +0 -1
- package/dist/SpatialGrid.js +0 -112
- package/dist/SpatialGrid.js.map +0 -1
- package/dist/Vector2.d.ts +0 -214
- package/dist/Vector2.d.ts.map +0 -1
- package/dist/Vector2.js +0 -134
- package/dist/Vector2.js.map +0 -1
- package/dist/internal/aabb.d.ts +0 -27
- package/dist/internal/aabb.d.ts.map +0 -1
- package/dist/internal/aabb.js +0 -52
- package/dist/internal/aabb.js.map +0 -1
- package/dist/internal/codec.d.ts +0 -9
- package/dist/internal/codec.d.ts.map +0 -1
- package/dist/internal/codec.js +0 -16
- package/dist/internal/codec.js.map +0 -1
- package/dist/internal/fdlibm.d.ts +0 -43
- package/dist/internal/fdlibm.d.ts.map +0 -1
- package/dist/internal/fdlibm.js +0 -1291
- package/dist/internal/fdlibm.js.map +0 -1
- package/dist/internal/inputAxis.d.ts +0 -11
- package/dist/internal/inputAxis.d.ts.map +0 -1
- package/dist/internal/inputAxis.js +0 -13
- package/dist/internal/inputAxis.js.map +0 -1
- package/dist/internal/scalar.d.ts +0 -11
- package/dist/internal/scalar.d.ts.map +0 -1
- package/dist/internal/scalar.js +0 -68
- package/dist/internal/scalar.js.map +0 -1
- package/dist/internal/size2.d.ts +0 -22
- package/dist/internal/size2.d.ts.map +0 -1
- package/dist/internal/size2.js +0 -38
- package/dist/internal/size2.js.map +0 -1
- package/dist/internal/vector2.d.ts +0 -39
- package/dist/internal/vector2.d.ts.map +0 -1
- package/dist/internal/vector2.js +0 -78
- package/dist/internal/vector2.js.map +0 -1
- package/src/Aabb.ts +0 -180
- package/src/InputAxis.ts +0 -81
- package/src/Ops.ts +0 -232
- package/src/Random.ts +0 -225
- package/src/Size2.ts +0 -122
- package/src/SpatialGrid.ts +0 -172
- package/src/Vector2.ts +0 -216
- package/src/internal/aabb.ts +0 -81
- package/src/internal/codec.ts +0 -21
- package/src/internal/fdlibm.ts +0 -1256
- package/src/internal/inputAxis.ts +0 -21
- package/src/internal/scalar.ts +0 -104
- package/src/internal/size2.ts +0 -54
- package/src/internal/vector2.ts +0 -121
package/dist/Vec2.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Plain two-dimensional vector arithmetic for hot paths.
|
|
3
3
|
*
|
|
4
|
-
* `
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* these helpers without conversion.
|
|
4
|
+
* Values are immutable `{ x, y }` records and every helper returns a new
|
|
5
|
+
* one, so a component's value (validated once by the game's schema when it
|
|
6
|
+
* entered the world) passes straight in. Lengths and angles go through
|
|
7
|
+
* `Ops`, so the results replay the same on every engine.
|
|
9
8
|
*
|
|
10
9
|
* @example
|
|
11
10
|
* ```ts
|
|
11
|
+
* declare const position: Vec2.Vec2, target: Vec2.Vec2, current: Vec2.Vec2
|
|
12
|
+
* declare const speed: number, maxSpeed: number
|
|
13
|
+
* // ---cut---
|
|
12
14
|
* // Steer toward a target at a capped speed.
|
|
13
15
|
* const wanted = Vec2.scale(Vec2.normalizeOr(Vec2.subtract(target, position), Vec2.zero), speed)
|
|
14
16
|
* const velocity = Vec2.clampLength(Vec2.add(current, wanted), maxSpeed)
|
|
@@ -57,17 +59,46 @@ export declare const subtract: (a: Vec2, b: Vec2) => Vec2;
|
|
|
57
59
|
*/
|
|
58
60
|
export declare const scale: (a: Vec2, factor: number) => Vec2;
|
|
59
61
|
/**
|
|
60
|
-
* `a` turned by `angle` radians (counter-clockwise for a y-up frame).
|
|
62
|
+
* `a` turned by `angle` radians (counter-clockwise for a y-up frame). For a
|
|
63
|
+
* quarter turn use {@link perpendicular}, which is exact.
|
|
61
64
|
*
|
|
62
65
|
* @category Operations
|
|
63
66
|
*/
|
|
64
67
|
export declare const rotate: (a: Vec2, angle: number) => Vec2;
|
|
65
68
|
/**
|
|
66
|
-
*
|
|
69
|
+
* `a` turned a quarter turn counter-clockwise (for a y-up frame):
|
|
70
|
+
* `{ x: -a.y, y: a.x }`. Exact. For the normal of a wall or a direction, a
|
|
71
|
+
* sideways push, and the signed side of a point: `dot(perpendicular(forward),
|
|
72
|
+
* offset)` is positive on the left of `forward`, negative on its right.
|
|
73
|
+
*
|
|
74
|
+
* @category Operations
|
|
75
|
+
*/
|
|
76
|
+
export declare const perpendicular: (a: Vec2) => Vec2;
|
|
77
|
+
/**
|
|
78
|
+
* Dot product. With {@link perpendicular} it is also the 2D cross product:
|
|
79
|
+
* `dot(perpendicular(a), b)`.
|
|
67
80
|
*
|
|
68
81
|
* @category Operations
|
|
69
82
|
*/
|
|
70
83
|
export declare const dot: (a: Vec2, b: Vec2) => number;
|
|
84
|
+
/**
|
|
85
|
+
* The unit vector at `angle` radians from the x axis (counter-clockwise for a
|
|
86
|
+
* y-up frame), through `Ops.cos` and `Ops.sin`. Multiply by a length with
|
|
87
|
+
* {@link scale}. The inverse of {@link angle}.
|
|
88
|
+
*
|
|
89
|
+
* @category Operations
|
|
90
|
+
*/
|
|
91
|
+
export declare const fromAngle: (angle: number) => Vec2;
|
|
92
|
+
/**
|
|
93
|
+
* The angle of `a` from the x axis in radians, in (-π, π], through
|
|
94
|
+
* `Ops.atan2` (so no `y, x` argument order to get wrong). The zero vector
|
|
95
|
+
* gives 0, and so does `-0` in either coordinate where `Ops.atan2` would give
|
|
96
|
+
* `-0` or flip to -π. To keep it in `Angle`'s range [-π, π), use
|
|
97
|
+
* `Angle.wrap`; to turn toward it, `Angle.turnToward`.
|
|
98
|
+
*
|
|
99
|
+
* @category Operations
|
|
100
|
+
*/
|
|
101
|
+
export declare const angle: (a: Vec2) => number;
|
|
71
102
|
/**
|
|
72
103
|
* Length of `a`.
|
|
73
104
|
*
|
|
@@ -118,4 +149,16 @@ export declare const moveToward: (current: Vec2, target: Vec2, maxDelta: number)
|
|
|
118
149
|
* @category Operations
|
|
119
150
|
*/
|
|
120
151
|
export declare const clampLength: (a: Vec2, max: number) => Vec2;
|
|
152
|
+
/**
|
|
153
|
+
* Moves `current` toward `target` by the share `rate` closes per second, over
|
|
154
|
+
* `dt` seconds, with one shared factor `exp(-rate * dt)` so the point moves
|
|
155
|
+
* along the straight line: the same total time gives the same result at any
|
|
156
|
+
* step rate. `current` unchanged when `rate * dt` is 0 or less, `target`
|
|
157
|
+
* itself when `rate` is `Infinity` (and `dt` positive). It nears `target`
|
|
158
|
+
* without reaching it; when it must arrive, use {@link moveToward}. For
|
|
159
|
+
* plain numbers use `Scalar.decayToward`.
|
|
160
|
+
*
|
|
161
|
+
* @category Operations
|
|
162
|
+
*/
|
|
163
|
+
export declare const decayToward: (current: Vec2, target: Vec2, rate: number, dt: number) => Vec2;
|
|
121
164
|
//# sourceMappingURL=Vec2.d.ts.map
|
package/dist/Vec2.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Vec2.d.ts","sourceRoot":"","sources":["../src/Vec2.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"Vec2.d.ts","sourceRoot":"","sources":["../src/Vec2.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAIH;;;;GAIG;AACH,MAAM,WAAW,IAAI;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;;;GAIG;AACH,eAAO,MAAM,IAAI,EAAE,IAAoC,CAAA;AAEvD;;;;GAIG;AACH,eA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AO,IAAI,OAAO,MAAM,KAAG,IAGlD,CAAA;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,WAAW,YAAa,IAAI,UAAU,IAAI,QAAQ,MAAM,MAAM,MAAM,KAAG,IAMnF,CAAA"}
|
package/dist/Vec2.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Plain two-dimensional vector arithmetic for hot paths.
|
|
3
3
|
*
|
|
4
|
-
* `
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* these helpers without conversion.
|
|
4
|
+
* Values are immutable `{ x, y }` records and every helper returns a new
|
|
5
|
+
* one, so a component's value (validated once by the game's schema when it
|
|
6
|
+
* entered the world) passes straight in. Lengths and angles go through
|
|
7
|
+
* `Ops`, so the results replay the same on every engine.
|
|
9
8
|
*
|
|
10
9
|
* @example
|
|
11
10
|
* ```ts
|
|
11
|
+
* declare const position: Vec2.Vec2, target: Vec2.Vec2, current: Vec2.Vec2
|
|
12
|
+
* declare const speed: number, maxSpeed: number
|
|
13
|
+
* // ---cut---
|
|
12
14
|
* // Steer toward a target at a capped speed.
|
|
13
15
|
* const wanted = Vec2.scale(Vec2.normalizeOr(Vec2.subtract(target, position), Vec2.zero), speed)
|
|
14
16
|
* const velocity = Vec2.clampLength(Vec2.add(current, wanted), maxSpeed)
|
|
@@ -23,7 +25,7 @@
|
|
|
23
25
|
* @categoryDescription Operations
|
|
24
26
|
* Immutable arithmetic, measurement, and steering helpers.
|
|
25
27
|
*/
|
|
26
|
-
import * as Ops from "
|
|
28
|
+
import * as Ops from "@typeonce/ritmo/Ops";
|
|
27
29
|
/**
|
|
28
30
|
* The zero vector (frozen, safe to share).
|
|
29
31
|
*
|
|
@@ -49,7 +51,8 @@ export const subtract = (a, b) => ({ x: a.x - b.x, y: a.y - b.y });
|
|
|
49
51
|
*/
|
|
50
52
|
export const scale = (a, factor) => ({ x: a.x * factor, y: a.y * factor });
|
|
51
53
|
/**
|
|
52
|
-
* `a` turned by `angle` radians (counter-clockwise for a y-up frame).
|
|
54
|
+
* `a` turned by `angle` radians (counter-clockwise for a y-up frame). For a
|
|
55
|
+
* quarter turn use {@link perpendicular}, which is exact.
|
|
53
56
|
*
|
|
54
57
|
* @category Operations
|
|
55
58
|
*/
|
|
@@ -59,11 +62,39 @@ export const rotate = (a, angle) => {
|
|
|
59
62
|
return { x: a.x * cos - a.y * sin, y: a.x * sin + a.y * cos };
|
|
60
63
|
};
|
|
61
64
|
/**
|
|
62
|
-
*
|
|
65
|
+
* `a` turned a quarter turn counter-clockwise (for a y-up frame):
|
|
66
|
+
* `{ x: -a.y, y: a.x }`. Exact. For the normal of a wall or a direction, a
|
|
67
|
+
* sideways push, and the signed side of a point: `dot(perpendicular(forward),
|
|
68
|
+
* offset)` is positive on the left of `forward`, negative on its right.
|
|
69
|
+
*
|
|
70
|
+
* @category Operations
|
|
71
|
+
*/
|
|
72
|
+
export const perpendicular = (a) => ({ x: -a.y, y: a.x });
|
|
73
|
+
/**
|
|
74
|
+
* Dot product. With {@link perpendicular} it is also the 2D cross product:
|
|
75
|
+
* `dot(perpendicular(a), b)`.
|
|
63
76
|
*
|
|
64
77
|
* @category Operations
|
|
65
78
|
*/
|
|
66
79
|
export const dot = (a, b) => a.x * b.x + a.y * b.y;
|
|
80
|
+
/**
|
|
81
|
+
* The unit vector at `angle` radians from the x axis (counter-clockwise for a
|
|
82
|
+
* y-up frame), through `Ops.cos` and `Ops.sin`. Multiply by a length with
|
|
83
|
+
* {@link scale}. The inverse of {@link angle}.
|
|
84
|
+
*
|
|
85
|
+
* @category Operations
|
|
86
|
+
*/
|
|
87
|
+
export const fromAngle = (angle) => ({ x: Ops.cos(angle), y: Ops.sin(angle) });
|
|
88
|
+
/**
|
|
89
|
+
* The angle of `a` from the x axis in radians, in (-π, π], through
|
|
90
|
+
* `Ops.atan2` (so no `y, x` argument order to get wrong). The zero vector
|
|
91
|
+
* gives 0, and so does `-0` in either coordinate where `Ops.atan2` would give
|
|
92
|
+
* `-0` or flip to -π. To keep it in `Angle`'s range [-π, π), use
|
|
93
|
+
* `Angle.wrap`; to turn toward it, `Angle.turnToward`.
|
|
94
|
+
*
|
|
95
|
+
* @category Operations
|
|
96
|
+
*/
|
|
97
|
+
export const angle = (a) => Ops.atan2(a.y + 0, a.x + 0);
|
|
67
98
|
/**
|
|
68
99
|
* Length of `a`.
|
|
69
100
|
*
|
|
@@ -131,4 +162,24 @@ export const clampLength = (a, max) => {
|
|
|
131
162
|
const len = Ops.hypot(a.x, a.y);
|
|
132
163
|
return len <= max ? a : { x: (a.x / len) * max, y: (a.y / len) * max };
|
|
133
164
|
};
|
|
165
|
+
/**
|
|
166
|
+
* Moves `current` toward `target` by the share `rate` closes per second, over
|
|
167
|
+
* `dt` seconds, with one shared factor `exp(-rate * dt)` so the point moves
|
|
168
|
+
* along the straight line: the same total time gives the same result at any
|
|
169
|
+
* step rate. `current` unchanged when `rate * dt` is 0 or less, `target`
|
|
170
|
+
* itself when `rate` is `Infinity` (and `dt` positive). It nears `target`
|
|
171
|
+
* without reaching it; when it must arrive, use {@link moveToward}. For
|
|
172
|
+
* plain numbers use `Scalar.decayToward`.
|
|
173
|
+
*
|
|
174
|
+
* @category Operations
|
|
175
|
+
*/
|
|
176
|
+
export const decayToward = (current, target, rate, dt) => {
|
|
177
|
+
const k = rate * dt;
|
|
178
|
+
if (!(k > 0))
|
|
179
|
+
return current;
|
|
180
|
+
if (k === Infinity)
|
|
181
|
+
return target;
|
|
182
|
+
const factor = Ops.exp(-k);
|
|
183
|
+
return { x: target.x + (current.x - target.x) * factor, y: target.y + (current.y - target.y) * factor };
|
|
184
|
+
};
|
|
134
185
|
//# sourceMappingURL=Vec2.js.map
|
package/dist/Vec2.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Vec2.js","sourceRoot":"","sources":["../src/Vec2.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"Vec2.js","s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,MAAY,EAAE,IAAY,EAAE,EAAU,EAAQ,EAAE;IACzF,MAAM,CAAC,GAAG,IAAI,GAAG,EAAE,CAAA;IACnB,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAAE,OAAO,OAAO,CAAA;IAC5B,IAAI,CAAC,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAA;IACjC,MAAM,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAC1B,OAAO,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE,CAAA;AACzG,CAAC,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,32 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The 2D helpers most games would otherwise rewrite, as plain data in and
|
|
3
|
+
* plain data out, built on `Ops` so they replay the same on every engine.
|
|
3
4
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* `Box2` edges, `Segment2` walls at any angle, `Collide2` swept circle
|
|
11
|
-
* collision against both, a `SpatialGrid` broad
|
|
12
|
-
* phase, a `FlowField` for finding the way around walls, and a deterministic
|
|
13
|
-
* `Random` whose state is one integer.
|
|
14
|
-
*
|
|
15
|
-
* `Ops` holds the math functions engines are allowed to approximate (`sin`,
|
|
16
|
-
* `cos`, `atan2`, `exp`, `pow`, `hypot`, ...) as ports that return the same
|
|
17
|
-
* bits everywhere: use them in simulation code instead of `Math`'s.
|
|
5
|
+
* `Vec2` arithmetic and angles of vectors, `Angle` for headings, `Scalar` for
|
|
6
|
+
* numbers moving toward a target, `Box2` edge boxes and `Segment2` walls at
|
|
7
|
+
* any angle, `Collide2` swept circle collision against both that slides
|
|
8
|
+
* along walls and never tunnels, and a `FlowField` that leads bodies around
|
|
9
|
+
* walls. Values are validated by the game's own schema when they enter the
|
|
10
|
+
* world; `Ops` and `Random` are in `@typeonce/ritmo`.
|
|
18
11
|
*/
|
|
19
|
-
export * as
|
|
12
|
+
export * as Angle from "./Angle.ts";
|
|
20
13
|
export * as Box2 from "./Box2.ts";
|
|
21
14
|
export * as Collide2 from "./Collide2.ts";
|
|
22
15
|
export * as FlowField from "./FlowField.ts";
|
|
23
|
-
export * as InputAxis from "./InputAxis.ts";
|
|
24
|
-
export * as Ops from "./Ops.ts";
|
|
25
|
-
export * as Random from "./Random.ts";
|
|
26
16
|
export * as Scalar from "./Scalar.ts";
|
|
27
17
|
export * as Segment2 from "./Segment2.ts";
|
|
28
|
-
export * as Size2 from "./Size2.ts";
|
|
29
|
-
export * as SpatialGrid from "./SpatialGrid.ts";
|
|
30
18
|
export * as Vec2 from "./Vec2.ts";
|
|
31
|
-
export * as Vector2 from "./Vector2.ts";
|
|
32
19
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,32 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The 2D helpers most games would otherwise rewrite, as plain data in and
|
|
3
|
+
* plain data out, built on `Ops` so they replay the same on every engine.
|
|
3
4
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* `Box2` edges, `Segment2` walls at any angle, `Collide2` swept circle
|
|
11
|
-
* collision against both, a `SpatialGrid` broad
|
|
12
|
-
* phase, a `FlowField` for finding the way around walls, and a deterministic
|
|
13
|
-
* `Random` whose state is one integer.
|
|
14
|
-
*
|
|
15
|
-
* `Ops` holds the math functions engines are allowed to approximate (`sin`,
|
|
16
|
-
* `cos`, `atan2`, `exp`, `pow`, `hypot`, ...) as ports that return the same
|
|
17
|
-
* bits everywhere: use them in simulation code instead of `Math`'s.
|
|
5
|
+
* `Vec2` arithmetic and angles of vectors, `Angle` for headings, `Scalar` for
|
|
6
|
+
* numbers moving toward a target, `Box2` edge boxes and `Segment2` walls at
|
|
7
|
+
* any angle, `Collide2` swept circle collision against both that slides
|
|
8
|
+
* along walls and never tunnels, and a `FlowField` that leads bodies around
|
|
9
|
+
* walls. Values are validated by the game's own schema when they enter the
|
|
10
|
+
* world; `Ops` and `Random` are in `@typeonce/ritmo`.
|
|
18
11
|
*/
|
|
19
|
-
export * as
|
|
12
|
+
export * as Angle from "./Angle.js";
|
|
20
13
|
export * as Box2 from "./Box2.js";
|
|
21
14
|
export * as Collide2 from "./Collide2.js";
|
|
22
15
|
export * as FlowField from "./FlowField.js";
|
|
23
|
-
export * as InputAxis from "./InputAxis.js";
|
|
24
|
-
export * as Ops from "./Ops.js";
|
|
25
|
-
export * as Random from "./Random.js";
|
|
26
16
|
export * as Scalar from "./Scalar.js";
|
|
27
17
|
export * as Segment2 from "./Segment2.js";
|
|
28
|
-
export * as Size2 from "./Size2.js";
|
|
29
|
-
export * as SpatialGrid from "./SpatialGrid.js";
|
|
30
18
|
export * as Vec2 from "./Vec2.js";
|
|
31
|
-
export * as Vector2 from "./Vector2.js";
|
|
32
19
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AACzC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typeonce/ritmo-math",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.16.0",
|
|
4
|
+
"description": "The 2D helpers most Ritmo games would otherwise rewrite (vectors, angles, swept collision, flow field), plain data in and out, built on Ops so they replay the same on every engine",
|
|
5
5
|
"author": "Sandro Maglione",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"provenance": true
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@typeonce/ritmo": "^0.
|
|
50
|
+
"@typeonce/ritmo": "^0.16.0"
|
|
51
51
|
},
|
|
52
52
|
"engines": {
|
|
53
53
|
"node": ">=20"
|
package/src/Angle.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angles in radians, for headings a system stores as one number: a turret's
|
|
3
|
+
* aim, a body's facing.
|
|
4
|
+
*
|
|
5
|
+
* Angles wrap: 350° and -10° are the same heading, so comparing or turning
|
|
6
|
+
* them needs the wrap first. `wrap` brings any angle into [-π, π), and the
|
|
7
|
+
* difference between two headings is `wrap(to - from)`, the short way round.
|
|
8
|
+
* Both helpers are exact (`%` and `+ -` only), so they replay the same on
|
|
9
|
+
* every engine. For the angle of a vector use `Vec2.angle`, for the unit
|
|
10
|
+
* vector of an angle `Vec2.fromAngle`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* declare const turret: { readonly position: Vec2.Vec2; readonly aim: number }
|
|
15
|
+
* declare const target: Vec2.Vec2
|
|
16
|
+
* declare const turnRate: number
|
|
17
|
+
* declare const dt: number
|
|
18
|
+
* // ---cut---
|
|
19
|
+
* // Turn a turret toward its target at a capped rate.
|
|
20
|
+
* const wanted = Vec2.angle(Vec2.subtract(target, turret.position))
|
|
21
|
+
* const aim = Angle.turnToward(turret.aim, wanted, turnRate * dt)
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @module Angle
|
|
25
|
+
* @docGroup helpers
|
|
26
|
+
*
|
|
27
|
+
* @categoryDescription Operations
|
|
28
|
+
* Wrapping and turning angles.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const TAU = 2 * Math.PI
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* `angle` brought into [-π, π) by whole turns: π and -π both give -π. Use it
|
|
35
|
+
* to keep a stored heading bounded, and for the signed short-way difference
|
|
36
|
+
* between two headings, `wrap(to - from)`. Exact on every engine.
|
|
37
|
+
*
|
|
38
|
+
* @category Operations
|
|
39
|
+
*/
|
|
40
|
+
export const wrap = (angle: number): number => {
|
|
41
|
+
// `%` is exact, and so is adding or removing one turn from a value within
|
|
42
|
+
// one turn of it, so the result is the exact remainder.
|
|
43
|
+
let wrapped = angle % TAU
|
|
44
|
+
if (wrapped >= Math.PI) wrapped -= TAU
|
|
45
|
+
else if (wrapped < -Math.PI) wrapped += TAU
|
|
46
|
+
return wrapped === 0 ? 0 : wrapped
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* `angle` turned toward `target` the short way by at most `maxStep` radians,
|
|
51
|
+
* never overshooting: `wrap(target)` once it is within `maxStep`, and
|
|
52
|
+
* `wrap(angle)` when `maxStep` is 0 or less. Headings exactly half a turn
|
|
53
|
+
* apart turn the negative way. For a capped turn rate (a turret, a homing
|
|
54
|
+
* shot); for plain numbers use `Scalar.moveToward`, for points
|
|
55
|
+
* `Vec2.moveToward`.
|
|
56
|
+
*
|
|
57
|
+
* @category Operations
|
|
58
|
+
*/
|
|
59
|
+
export const turnToward = (angle: number, target: number, maxStep: number): number => {
|
|
60
|
+
const from = wrap(angle)
|
|
61
|
+
if (!(maxStep > 0)) return from
|
|
62
|
+
const gap = wrap(target - from)
|
|
63
|
+
if (Math.abs(gap) <= maxStep) return wrap(target)
|
|
64
|
+
return wrap(gap > 0 ? from + maxStep : from - maxStep)
|
|
65
|
+
}
|
package/src/Box2.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Plain axis-aligned boxes stored by their edges, for hot-path collision.
|
|
3
3
|
*
|
|
4
|
-
* `Aabb` is the validated, centered box for authored and stored values;
|
|
5
4
|
* `Box2` is the edge form the collision helpers (`Collide2`, `FlowField`)
|
|
6
|
-
* read every frame, built once from whatever the game stores
|
|
5
|
+
* read every frame, built once from whatever the game stores (a center and a
|
|
6
|
+
* size, a tile's corner).
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* ```ts
|
|
10
|
+
* declare const x: number, y: number, width: number, height: number, radius: number
|
|
11
|
+
* declare const position: Vec2.Vec2
|
|
12
|
+
* declare const push: (position: Vec2.Vec2) => void
|
|
13
|
+
* // ---cut---
|
|
10
14
|
* const wall = Box2.around(x, y, width / 2, height / 2)
|
|
11
15
|
* if (Box2.overlapsCircle(wall, position, radius)) push(position)
|
|
12
16
|
* ```
|
|
@@ -18,7 +22,7 @@
|
|
|
18
22
|
* The structural box shape.
|
|
19
23
|
*
|
|
20
24
|
* @categoryDescription Operations
|
|
21
|
-
* Building boxes
|
|
25
|
+
* Building boxes, testing them against circles, and the closest point.
|
|
22
26
|
*/
|
|
23
27
|
import type { Vec2 } from "./Vec2.ts"
|
|
24
28
|
|
|
@@ -49,7 +53,9 @@ export const around = (x: number, y: number, halfWidth: number, halfHeight: numb
|
|
|
49
53
|
})
|
|
50
54
|
|
|
51
55
|
/**
|
|
52
|
-
* Whether a circle overlaps (or touches) the box.
|
|
56
|
+
* Whether a circle overlaps (or touches) the box. For two circles use
|
|
57
|
+
* `Collide2.overlapsCircleCircle`, for a wall at any angle
|
|
58
|
+
* `Segment2.overlapsCircle`.
|
|
53
59
|
*
|
|
54
60
|
* @category Operations
|
|
55
61
|
*/
|
|
@@ -58,3 +64,17 @@ export const overlapsCircle = (box: Box2, center: Vec2, radius: number): boolean
|
|
|
58
64
|
const dy = center.y - Math.max(box.minY, Math.min(center.y, box.maxY))
|
|
59
65
|
return dx * dx + dy * dy <= radius * radius
|
|
60
66
|
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The point of `box` closest to `point`: `point` clamped to the box's edges.
|
|
70
|
+
* A point inside the box comes back unchanged, so this keeps a body inside an
|
|
71
|
+
* arena, or gives the direction from an obstacle to a body outside it
|
|
72
|
+
* (`Vec2.subtract(body, closestPoint(box, body))`). To push a circle out of
|
|
73
|
+
* walls it overlaps, use `Collide2.pushOut`.
|
|
74
|
+
*
|
|
75
|
+
* @category Operations
|
|
76
|
+
*/
|
|
77
|
+
export const closestPoint = (box: Box2, point: Vec2): Vec2 => ({
|
|
78
|
+
x: Math.max(box.minX, Math.min(point.x, box.maxX)),
|
|
79
|
+
y: Math.max(box.minY, Math.min(point.y, box.maxY))
|
|
80
|
+
})
|
package/src/Collide2.ts
CHANGED
|
@@ -13,6 +13,11 @@
|
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* ```ts
|
|
16
|
+
* declare const position: Vec2.Vec2, velocity: Vec2.Vec2
|
|
17
|
+
* declare const radius: number, dt: number
|
|
18
|
+
* declare const walls: ReadonlyArray<Box2.Box2>
|
|
19
|
+
* declare const bumpedWall: () => void
|
|
20
|
+
* // ---cut---
|
|
16
21
|
* // Move a body by its velocity this step, sliding along the walls.
|
|
17
22
|
* const moved = Collide2.moveCircle(position, Vec2.scale(velocity, dt), radius, { boxes: walls })
|
|
18
23
|
* if (moved.blocked) bumpedWall()
|
|
@@ -25,13 +30,13 @@
|
|
|
25
30
|
* Contacts and movement results.
|
|
26
31
|
*
|
|
27
32
|
* @categoryDescription Queries
|
|
28
|
-
* First-contact sweeps and overlap
|
|
33
|
+
* First-contact sweeps and overlap tests.
|
|
29
34
|
*
|
|
30
35
|
* @categoryDescription Movement
|
|
31
36
|
* Moving a circle through walls with sliding.
|
|
32
37
|
*/
|
|
33
38
|
import type { Box2 } from "./Box2.ts"
|
|
34
|
-
import * as Ops from "
|
|
39
|
+
import * as Ops from "@typeonce/ritmo/Ops"
|
|
35
40
|
import type { Segment2 } from "./Segment2.ts"
|
|
36
41
|
import type { Vec2 } from "./Vec2.ts"
|
|
37
42
|
|
|
@@ -252,6 +257,9 @@ export const sweepCircleSegment = (origin: Vec2, delta: Vec2, radius: number, se
|
|
|
252
257
|
*
|
|
253
258
|
* @example
|
|
254
259
|
* ```ts
|
|
260
|
+
* declare const eye: Vec2.Vec2, target: Vec2.Vec2
|
|
261
|
+
* declare const walls: ReadonlyArray<Box2.Box2>
|
|
262
|
+
* // ---cut---
|
|
255
263
|
* const sees = Collide2.sweepCircle(eye, Vec2.subtract(target, eye), 0, { boxes: walls }) === undefined
|
|
256
264
|
* ```
|
|
257
265
|
*
|
|
@@ -291,6 +299,22 @@ export const sweepCircleCircle = (
|
|
|
291
299
|
return rayCircle(origin.x, origin.y, delta.x, delta.y, target.x, target.y, r)
|
|
292
300
|
}
|
|
293
301
|
|
|
302
|
+
/**
|
|
303
|
+
* Whether a circle of `radius` at `center` overlaps (or touches) a circle of
|
|
304
|
+
* `otherRadius` at `other`, without a square root. Touching counts, as in
|
|
305
|
+
* `Box2.overlapsCircle` and `Segment2.overlapsCircle`. For a pickup or a hit
|
|
306
|
+
* between bodies that have already moved; when one moves fast enough to pass
|
|
307
|
+
* through the other in one step, use {@link sweepCircleCircle}.
|
|
308
|
+
*
|
|
309
|
+
* @category Queries
|
|
310
|
+
*/
|
|
311
|
+
export const overlapsCircleCircle = (center: Vec2, radius: number, other: Vec2, otherRadius: number): boolean => {
|
|
312
|
+
const r = radius + otherRadius
|
|
313
|
+
const dx = center.x - other.x
|
|
314
|
+
const dy = center.y - other.y
|
|
315
|
+
return dx * dx + dy * dy <= r * r
|
|
316
|
+
}
|
|
317
|
+
|
|
294
318
|
/**
|
|
295
319
|
* Pushes a circle out of every wall it overlaps, boxes first and then
|
|
296
320
|
* segments, each in order, leaving `skin` between it and each wall. Returns
|
|
@@ -363,6 +387,10 @@ export const pushOut = (center: Vec2, radius: number, walls: Walls, skin: number
|
|
|
363
387
|
*
|
|
364
388
|
* @example
|
|
365
389
|
* ```ts
|
|
390
|
+
* declare const position: Vec2.Vec2, delta: Vec2.Vec2
|
|
391
|
+
* declare const radius: number
|
|
392
|
+
* declare const nearbyWalls: ReadonlyArray<Segment2.Segment2>
|
|
393
|
+
* // ---cut---
|
|
366
394
|
* // A map traced from survey data: every wall is a segment.
|
|
367
395
|
* const moved = Collide2.moveCircle(position, delta, radius, { segments: nearbyWalls })
|
|
368
396
|
* ```
|
package/src/FlowField.ts
CHANGED
|
@@ -14,7 +14,13 @@
|
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
16
16
|
* ```ts
|
|
17
|
-
* const
|
|
17
|
+
* declare const cellSize: number, bodyRadius: number
|
|
18
|
+
* declare const walls: ReadonlyArray<Box2.Box2>
|
|
19
|
+
* declare const player: Vec2.Vec2, enemy: Vec2.Vec2
|
|
20
|
+
* // ---cut---
|
|
21
|
+
* const made = FlowField.make(cellSize)
|
|
22
|
+
* if (!made.ok) throw new Error(`bad cell size ${made.error.value}`)
|
|
23
|
+
* const paths = made.value
|
|
18
24
|
* // Each step:
|
|
19
25
|
* paths.setWalls(walls, bodyRadius)
|
|
20
26
|
* paths.setGoal(player)
|
|
@@ -30,9 +36,9 @@
|
|
|
30
36
|
* @categoryDescription Construction
|
|
31
37
|
* Making a flow field.
|
|
32
38
|
*/
|
|
39
|
+
import * as Ops from "@typeonce/ritmo/Ops"
|
|
40
|
+
import * as Result from "@typeonce/ritmo/Result"
|
|
33
41
|
import { type Box2, overlapsCircle } from "./Box2.ts"
|
|
34
|
-
import * as Ops from "./Ops.ts"
|
|
35
|
-
import type * as Scalar from "./Scalar.ts"
|
|
36
42
|
import type { Vec2 } from "./Vec2.ts"
|
|
37
43
|
|
|
38
44
|
/**
|
|
@@ -41,8 +47,8 @@ import type { Vec2 } from "./Vec2.ts"
|
|
|
41
47
|
* @category Field Types
|
|
42
48
|
*/
|
|
43
49
|
export interface FlowField {
|
|
44
|
-
/** The side of one square cell, in world units. */
|
|
45
|
-
readonly cellSize:
|
|
50
|
+
/** The side of one square cell, in world units: finite and above 0. */
|
|
51
|
+
readonly cellSize: number
|
|
46
52
|
/**
|
|
47
53
|
* Takes `boxes` as the walls, blocking every cell whose center is within
|
|
48
54
|
* `clearance` of one, over the area the walls span. Does nothing when the
|
|
@@ -72,7 +78,7 @@ const stepY = Int8Array.of(0, 0, 1, -1, 1, -1, 1, -1)
|
|
|
72
78
|
const stepCost = Float64Array.of(1, 1, 1, 1, Math.SQRT2, Math.SQRT2, Math.SQRT2, Math.SQRT2)
|
|
73
79
|
|
|
74
80
|
class Field implements FlowField {
|
|
75
|
-
readonly cellSize:
|
|
81
|
+
readonly cellSize: number
|
|
76
82
|
private columns = 0
|
|
77
83
|
private rows = 0
|
|
78
84
|
private originX = 0
|
|
@@ -90,7 +96,7 @@ class Field implements FlowField {
|
|
|
90
96
|
private keys = new Float64Array(0)
|
|
91
97
|
private queued = 0
|
|
92
98
|
|
|
93
|
-
constructor(cellSize:
|
|
99
|
+
constructor(cellSize: number) {
|
|
94
100
|
this.cellSize = cellSize
|
|
95
101
|
}
|
|
96
102
|
|
|
@@ -269,11 +275,33 @@ class Field implements FlowField {
|
|
|
269
275
|
}
|
|
270
276
|
}
|
|
271
277
|
|
|
278
|
+
/**
|
|
279
|
+
* Why {@link make} made no flow field: `value`, the cell size it was given,
|
|
280
|
+
* is not a finite number above 0.
|
|
281
|
+
*
|
|
282
|
+
* @category Construction
|
|
283
|
+
*/
|
|
284
|
+
export interface InvalidCellSize {
|
|
285
|
+
readonly _tag: "InvalidCellSize"
|
|
286
|
+
readonly value: number
|
|
287
|
+
}
|
|
288
|
+
|
|
272
289
|
/**
|
|
273
290
|
* An empty flow field (no walls, no goal) with square cells of `cellSize`
|
|
274
|
-
* world units
|
|
275
|
-
*
|
|
291
|
+
* world units, or `InvalidCellSize` when `cellSize` is not a finite number
|
|
292
|
+
* above 0. Smaller cells follow walls more closely and cost more to search;
|
|
293
|
+
* around a body's diameter is a good start. Make it once (in a service or a
|
|
294
|
+
* system's setup) and reuse it every step.
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```ts
|
|
298
|
+
* const paths = FlowField.make(30)
|
|
299
|
+
* if (!paths.ok) throw new Error(`bad cell size ${paths.error.value}`)
|
|
300
|
+
* ```
|
|
276
301
|
*
|
|
277
302
|
* @category Construction
|
|
278
303
|
*/
|
|
279
|
-
export const make = (cellSize:
|
|
304
|
+
export const make = (cellSize: number): Result.Result<FlowField, InvalidCellSize> =>
|
|
305
|
+
cellSize > 0 && cellSize < Infinity
|
|
306
|
+
? Result.success(new Field(cellSize))
|
|
307
|
+
: Result.failure({ _tag: "InvalidCellSize", value: cellSize })
|