@flare-engine/physics 0.3.1 → 0.4.1
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/README.md +48 -2
- package/dist/index.d.mts +111 -44
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +111 -44
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +386 -104
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +383 -105
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @flare-engine/physics
|
|
2
2
|
|
|
3
|
-
Built-in AABB / Circle collision detection — `CollisionWorld` with enter/exit signals, `SpatialIndex`
|
|
3
|
+
Built-in AABB / Circle collision detection — `CollisionWorld` with enter/exit signals, `SpatialIndex` uniform spatial-hash broadphase, `CollisionWorld.raycast` for broadphase-backed ray queries plus a standalone `raycast` for an explicit target list, and `overlapInto` for minimum-translation resolution. Trigger / layer / mask filtering. Solid-tile grid movement: `buildSolidGrid` / `isBlockedAABB` / `moveAndSlideInto` (structural `SolidTile` input, no tilemap dep, half-open AABB-vs-tile, axis-separated slide, zero-alloc). There is no third-party physics dependency: rigid-body response lives in the first-party [`@flare-engine/dynamics`](https://github.com/grzott/flare-engine/tree/main/packages/dynamics), and the planck.js option evaluated in the design docs was never adopted. Measured cost by collider count — with the capture date and run-to-run spread — is published in [benchmarks](https://github.com/grzott/flare-engine/blob/main/docs/references/benchmarks.md). Those rows time a whole physics frame (move every collider, then `step()`), not `step()` in isolation. Colliders wrap at a hard field boundary so density is constant for the whole sample window, and a probe fails the capture if one escapes; read the rows as an order-of-magnitude guide, which is where the `<1000` figure above comes from.
|
|
4
4
|
|
|
5
5
|
`@flare-engine/physics` answers *did these overlap*; `@flare-engine/dynamics` answers *where do they end up*. Neither package depends on the other — reach for [`@flare-engine/dynamics`](https://github.com/grzott/flare-engine/tree/main/packages/dynamics) when bodies need to push each other around, not just report contact.
|
|
6
6
|
|
|
@@ -22,10 +22,56 @@ const world = new CollisionWorld();
|
|
|
22
22
|
world.onCollisionStart.add((pair) => console.log(pair.entityA, pair.entityB));
|
|
23
23
|
|
|
24
24
|
const colliderA = createCollider(AABB(32, 32), { layer: 1, mask: 2 });
|
|
25
|
-
world.
|
|
25
|
+
world.addCollider(entityA, colliderA, x, y);
|
|
26
26
|
world.step();
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
+
## Resolving a collision
|
|
30
|
+
|
|
31
|
+
`shapeOverlap` answers *whether* two shapes collide, which leaves you knowing there is a problem and
|
|
32
|
+
not how to fix it. `overlapInto` gives the minimum translation vector — the shortest push that
|
|
33
|
+
separates them:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { AABB, CircleShape, createOverlap, overlapInto } from '@flare-engine/physics';
|
|
37
|
+
|
|
38
|
+
const mtv = createOverlap(); // reuse across pairs — no per-pair allocation
|
|
39
|
+
if (overlapInto(AABB(32, 32), 0, 0, CircleShape(8), 18, 4, mtv) !== null) {
|
|
40
|
+
// mtv.x / mtv.y is a unit axis pointing from a toward b; mtv.depth is how far
|
|
41
|
+
// b must move along it to just stop touching.
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Returns `null` when the shapes are apart, leaving your buffer untouched. Concentric circles have no
|
|
46
|
+
meaningful axis, so `(1, 0)` is returned rather than `NaN` from normalising a zero vector.
|
|
47
|
+
|
|
48
|
+
**Touching is not colliding.** Every shape pair is strict: two shapes whose edges exactly meet do not
|
|
49
|
+
overlap, in `shapeOverlap` and `overlapInto` alike, so the two always agree. A zero-radius circle
|
|
50
|
+
collides only when strictly inside a box, as a zero-size box does.
|
|
51
|
+
|
|
52
|
+
## Raycasting the world
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { CollisionWorld } from '@flare-engine/physics';
|
|
56
|
+
|
|
57
|
+
declare const world: CollisionWorld;
|
|
58
|
+
|
|
59
|
+
// Nearest hit along +x, limited to colliders on layers 1 and 2.
|
|
60
|
+
const hit = world.raycast(0, 0, 1, 0, 500, 0b011);
|
|
61
|
+
if (hit !== null) {
|
|
62
|
+
// hit.entity / hit.distance / hit.point / hit.normal
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`CollisionWorld.raycast` walks only the broadphase cells the ray's bounding box touches, and filters
|
|
67
|
+
by the `layer` bits colliders already carry (`mask` defaults to every layer). The standalone
|
|
68
|
+
`raycast(ox, oy, dx, dy, maxDist, targets)` export remains for casting against an explicit list that
|
|
69
|
+
is not in a world.
|
|
70
|
+
|
|
71
|
+
**The returned hit record is reused between calls** — read it, or copy what you need, before casting
|
|
72
|
+
again. That is what keeps a per-frame ray allocation-free; the standalone helper by contrast builds a
|
|
73
|
+
fresh hit (plus two nested `Vec2`s) for *every candidate it tests* and discards all but the closest.
|
|
74
|
+
|
|
29
75
|
See [API spec](https://github.com/grzott/flare-engine/blob/main/docs/design/api-specs.md#flare-enginephysics--4kb-aabb).
|
|
30
76
|
|
|
31
77
|
## License
|
package/dist/index.d.mts
CHANGED
|
@@ -26,6 +26,34 @@ declare function shapeBounds(shape: Shape, x: number, y: number): Rect;
|
|
|
26
26
|
declare function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect;
|
|
27
27
|
/** Test overlap between two positioned shapes. Returns true if overlapping. */
|
|
28
28
|
declare function shapeOverlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Minimum translation vector: the shortest push that separates two overlapping
|
|
31
|
+
* shapes.
|
|
32
|
+
*
|
|
33
|
+
* `shapeOverlap` answers *whether* two shapes collide and nothing more, which
|
|
34
|
+
* leaves a detection-only consumer with no way to resolve the collision. This is
|
|
35
|
+
* that missing step: `out.x`/`out.y` are a unit axis and `out.depth` is how far
|
|
36
|
+
* along it `b` must move to just stop touching `a`.
|
|
37
|
+
*
|
|
38
|
+
* Returns `null` when the shapes are not overlapping, leaving `out` untouched so
|
|
39
|
+
* a caller may reuse one buffer across many pairs.
|
|
40
|
+
*
|
|
41
|
+
* The degenerate concentric case (identical centres) has no meaningful axis;
|
|
42
|
+
* `(1, 0)` is returned so the caller still gets a usable separation instead of
|
|
43
|
+
* `NaN` from normalising a zero vector.
|
|
44
|
+
*/
|
|
45
|
+
interface Overlap {
|
|
46
|
+
/** Unit separation axis, pointing from `a` toward `b`. */
|
|
47
|
+
x: number;
|
|
48
|
+
y: number;
|
|
49
|
+
/** Penetration depth along that axis. */
|
|
50
|
+
depth: number;
|
|
51
|
+
}
|
|
52
|
+
/** A fresh, zeroed overlap record. Allocate once outside the loop and reuse. */
|
|
53
|
+
declare function createOverlap(): Overlap;
|
|
54
|
+
declare function overlapInto(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number, out: Overlap): Overlap | null;
|
|
55
|
+
/** Minimum translation vector. Allocates — see `overlapInto` for the hot path. */
|
|
56
|
+
declare function overlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): Overlap | null;
|
|
29
57
|
//#endregion
|
|
30
58
|
//#region src/collider.d.ts
|
|
31
59
|
/** Collider component data attached to an ECS entity. */
|
|
@@ -42,6 +70,46 @@ interface Collider {
|
|
|
42
70
|
/** Create a collider with default settings. */
|
|
43
71
|
declare function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider;
|
|
44
72
|
//#endregion
|
|
73
|
+
//#region src/raycast.d.ts
|
|
74
|
+
/** Result of a raycast hit. */
|
|
75
|
+
interface RaycastHit {
|
|
76
|
+
/** The entity that was hit. */
|
|
77
|
+
entity: number;
|
|
78
|
+
/** Distance from ray origin to hit point. */
|
|
79
|
+
distance: number;
|
|
80
|
+
/** World-space hit point. */
|
|
81
|
+
point: Vec2;
|
|
82
|
+
/** Surface normal at hit point. */
|
|
83
|
+
normal: Vec2;
|
|
84
|
+
}
|
|
85
|
+
/** Entity entry for raycast — position + shape. */
|
|
86
|
+
interface RaycastTarget {
|
|
87
|
+
entity: number;
|
|
88
|
+
shape: Shape;
|
|
89
|
+
x: number;
|
|
90
|
+
y: number;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Cast a ray against a list of targets. Returns the closest hit, or null.
|
|
94
|
+
* @param ox Ray origin X
|
|
95
|
+
* @param oy Ray origin Y
|
|
96
|
+
* @param dx Ray direction X (should be normalized)
|
|
97
|
+
* @param dy Ray direction Y (should be normalized)
|
|
98
|
+
* @param maxDist Maximum raycast distance
|
|
99
|
+
* @param targets Array of entities to test against
|
|
100
|
+
*/
|
|
101
|
+
declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
|
|
102
|
+
/**
|
|
103
|
+
* Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns
|
|
104
|
+
* `null` without touching `out`.
|
|
105
|
+
*
|
|
106
|
+
* The allocating `raycast()` above builds a fresh `RaycastHit` — with two nested
|
|
107
|
+
* `Vec2` objects — for **every candidate it tests**, then discards all but the
|
|
108
|
+
* closest. That is three objects per candidate per ray. This variant exists so
|
|
109
|
+
* `CollisionWorld.raycast` can test a whole broadphase cell without allocating.
|
|
110
|
+
*/
|
|
111
|
+
declare function rayVsShapeInto(ox: number, oy: number, dx: number, dy: number, maxDist: number, entity: number, shape: Shape, x: number, y: number, out: RaycastHit): RaycastHit | null;
|
|
112
|
+
//#endregion
|
|
45
113
|
//#region src/collision-world.d.ts
|
|
46
114
|
/** Collision pair data emitted by collision signals. */
|
|
47
115
|
interface CollisionPair {
|
|
@@ -70,8 +138,14 @@ declare class CollisionWorld {
|
|
|
70
138
|
private queryBuf;
|
|
71
139
|
/** Reusable bounds rect — avoids per-entity Rect allocation in step(). */
|
|
72
140
|
private boundsBuf;
|
|
73
|
-
/** Add an entity with a shape. */
|
|
141
|
+
/** Add an entity with a shape. Positional twin of `addCollider`. */
|
|
74
142
|
add(entity: number, shape: Shape, x: number, y: number, isTrigger?: boolean, layer?: number, mask?: number): void;
|
|
143
|
+
/**
|
|
144
|
+
* Add an entity from a `Collider`. Its `shape`, `isTrigger`, `layer` and `mask`
|
|
145
|
+
* are copied at add time — later edits to the collider do not reach the world;
|
|
146
|
+
* re-add to change them. Re-adding an existing entity replaces its data.
|
|
147
|
+
*/
|
|
148
|
+
addCollider(entity: number, collider: Collider, x: number, y: number): void;
|
|
75
149
|
/** Remove an entity from the world. Emits exit signals for any active pairs. */
|
|
76
150
|
remove(entity: number): void;
|
|
77
151
|
/** Update an entity's position. */
|
|
@@ -88,38 +162,39 @@ declare class CollisionWorld {
|
|
|
88
162
|
getColliderBounds(entity: number): Rect | null;
|
|
89
163
|
/** Remove all entities. */
|
|
90
164
|
clear(): void;
|
|
165
|
+
/** Reused across raycasts — see `raycast`. */
|
|
166
|
+
private raycastBounds;
|
|
167
|
+
private raycastCandidates;
|
|
168
|
+
private raycastHit;
|
|
169
|
+
/**
|
|
170
|
+
* Cast a ray against the colliders in this world.
|
|
171
|
+
*
|
|
172
|
+
* The standalone `raycast()` export tests a caller-supplied target array, so
|
|
173
|
+
* it neither sees the world's colliders nor benefits from the broadphase — an
|
|
174
|
+
* O(n) scan sitting beside an O(1) index. This walks only the cells the ray's
|
|
175
|
+
* bounding box touches.
|
|
176
|
+
*
|
|
177
|
+
* `mask` is a layer bitmask filtered against each collider's `layer`, using the
|
|
178
|
+
* bits colliders already carry; it defaults to every layer. Triggers are
|
|
179
|
+
* included — a ray asking "what is over there" wants them.
|
|
180
|
+
*
|
|
181
|
+
* The returned record is **reused between calls**: read it, or copy what you
|
|
182
|
+
* need, before casting again. That is what keeps a per-frame ray allocation-
|
|
183
|
+
* free, and it is why the standalone helper's fresh-object-per-candidate cost
|
|
184
|
+
* was worth removing.
|
|
185
|
+
*
|
|
186
|
+
* `maxDist` must be finite — this requires a bounded ray, not the
|
|
187
|
+
* `Number.POSITIVE_INFINITY` "unbounded ray" idiom some engines accept. The
|
|
188
|
+
* broadphase below is a uniform grid: an infinite `maxDist` derives an
|
|
189
|
+
* infinite cell range and never terminates the walk. Pass a large finite
|
|
190
|
+
* bound instead (e.g. the map's diagonal) to cast "until it leaves the
|
|
191
|
+
* populated area". A non-finite `maxDist` returns `null`.
|
|
192
|
+
*/
|
|
193
|
+
raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, mask?: number): RaycastHit | null;
|
|
194
|
+
/** Scratch record handed to `rayVsShapeInto` per candidate. */
|
|
195
|
+
private scratchHit;
|
|
91
196
|
}
|
|
92
197
|
//#endregion
|
|
93
|
-
//#region src/raycast.d.ts
|
|
94
|
-
/** Result of a raycast hit. */
|
|
95
|
-
interface RaycastHit {
|
|
96
|
-
/** The entity that was hit. */
|
|
97
|
-
entity: number;
|
|
98
|
-
/** Distance from ray origin to hit point. */
|
|
99
|
-
distance: number;
|
|
100
|
-
/** World-space hit point. */
|
|
101
|
-
point: Vec2;
|
|
102
|
-
/** Surface normal at hit point. */
|
|
103
|
-
normal: Vec2;
|
|
104
|
-
}
|
|
105
|
-
/** Entity entry for raycast — position + shape. */
|
|
106
|
-
interface RaycastTarget {
|
|
107
|
-
entity: number;
|
|
108
|
-
shape: Shape;
|
|
109
|
-
x: number;
|
|
110
|
-
y: number;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Cast a ray against a list of targets. Returns the closest hit, or null.
|
|
114
|
-
* @param ox Ray origin X
|
|
115
|
-
* @param oy Ray origin Y
|
|
116
|
-
* @param dx Ray direction X (should be normalized)
|
|
117
|
-
* @param dy Ray direction Y (should be normalized)
|
|
118
|
-
* @param maxDist Maximum raycast distance
|
|
119
|
-
* @param targets Array of entities to test against
|
|
120
|
-
*/
|
|
121
|
-
declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
|
|
122
|
-
//#endregion
|
|
123
198
|
//#region src/solid-grid.d.ts
|
|
124
199
|
/**
|
|
125
200
|
* Minimal structural type for a solid tile position in grid coordinates.
|
|
@@ -131,14 +206,15 @@ interface SolidTile {
|
|
|
131
206
|
readonly col: number;
|
|
132
207
|
}
|
|
133
208
|
/**
|
|
134
|
-
* Precomputed
|
|
135
|
-
*
|
|
209
|
+
* Precomputed solid-tile lookup used by `isBlockedAABB` and `moveAndSlideInto`.
|
|
210
|
+
* `solid` has `width * height` cells indexed `row * width + col`; non-zero means
|
|
211
|
+
* solid. Build once per map load via `buildSolidGrid`.
|
|
136
212
|
*/
|
|
137
213
|
interface SolidGrid {
|
|
138
214
|
readonly width: number;
|
|
139
215
|
readonly height: number;
|
|
140
216
|
readonly tileSize: number;
|
|
141
|
-
readonly solid:
|
|
217
|
+
readonly solid: Uint8Array;
|
|
142
218
|
}
|
|
143
219
|
/**
|
|
144
220
|
* Build a `SolidGrid` from a list of solid tile positions.
|
|
@@ -161,15 +237,6 @@ declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: num
|
|
|
161
237
|
declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: number, dy: number, halfW: number, halfH: number, out: Vec2): Vec2;
|
|
162
238
|
//#endregion
|
|
163
239
|
//#region src/spatial-index.d.ts
|
|
164
|
-
/** Item stored in the spatial index. */
|
|
165
|
-
interface SpatialEntry<T> {
|
|
166
|
-
readonly item: T;
|
|
167
|
-
/** Bounding rect (minX, minY, maxX, maxY). */
|
|
168
|
-
minX: number;
|
|
169
|
-
minY: number;
|
|
170
|
-
maxX: number;
|
|
171
|
-
maxY: number;
|
|
172
|
-
}
|
|
173
240
|
/**
|
|
174
241
|
* Uniform spatial-hash broadphase.
|
|
175
242
|
* `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.
|
|
@@ -201,5 +268,5 @@ declare class SpatialIndex<T> {
|
|
|
201
268
|
private _unlink;
|
|
202
269
|
}
|
|
203
270
|
//#endregion
|
|
204
|
-
export { AABB, type AABBShape, CircleShape, type CircleShape as CircleShapeType, type Collider, type CollisionPair, CollisionWorld, type RaycastHit, type RaycastTarget, type Shape, type SolidGrid, type SolidTile,
|
|
271
|
+
export { AABB, type AABBShape, CircleShape, type CircleShape as CircleShapeType, type Collider, type CollisionPair, CollisionWorld, type Overlap, type RaycastHit, type RaycastTarget, type Shape, type SolidGrid, type SolidTile, SpatialIndex, buildSolidGrid, createCollider, createOverlap, isBlockedAABB, moveAndSlideInto, overlap, overlapInto, rayVsShapeInto, raycast, shapeBounds, shapeBoundsInto, shapeOverlap };
|
|
205
272
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/shape.ts","../src/collider.ts","../src/
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/shape.ts","../src/collider.ts","../src/raycast.ts","../src/collision-world.ts","../src/solid-grid.ts","../src/spatial-index.ts"],"mappings":";;;;UAGiB;WACN;;WAEA;;WAEA;;;UAIM;WACN;WACA;;;KAIC,QAAQ,YAAY;;iBAGhB,KAAK,eAAe,iBAAiB;;iBAKrC,YAAY,iBAAiB;;iBAK7B,YAAY,OAAO,OAAO,WAAW,YAAY;;iBAajD,gBAAgB,OAAO,OAAO,WAAW,WAAW,KAAK,OAAO;;iBAgBhE,aACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA;;;;;;;;;;;;;;;;;UA2Fe;;EAEf;EACA;;EAEA;;;iBAIc,iBAAiB;iBAIjB,YACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,YACA,KAAK,UACJ;;iBA0Ba,QACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,aACC;;;;UChNc;;EAEf,OAAO;;EAEP;;EAEA;;EAEA;;;iBAIc,eAAe,OAAO,OAAO,OAAO,QAAQ,KAAK,sBAAsB;;;;UCXtE;;EAEf;;EAEA;;EAEA,OAAO;;EAEP,QAAQ;;;UAIO;EACf;EACA,OAAO;EACP;EACA;;;;;;;;;;;iBAYc,QACd,YACA,YACA,YACA,YACA,iBACA,kBAAkB,kBACjB;;;;;;;;;;iBAkKa,eACd,YACA,YACA,YACA,YACA,iBACA,gBACA,OAAO,OACP,WACA,WACA,KAAK,aACJ;;;;UC1Mc;WACN;WACA;;;;;;cAuCE;WACF,kBAAgB,OAAA;WAChB,gBAAc,OAAA;WACd,gBAAc,OAAA;WACd,eAAa,OAAA;UAEd;UACA;;;;;UAKA;UACA;UACA;;UAEA;;UAEA;;EAGR,IACE,gBACA,OAAO,OACP,WACA,WACA,qBACA,gBACA;;;;;;EAkBF,YAAY,gBAAgB,UAAU,UAAU,WAAW;;EAK3D,OAAO;;EAyBP,YAAY,gBAAgB,WAAW;;MAUnC;;MAKA;;EAKJ;;EAiEA,sBAAsB,gBAAgB,KAAK,OAAO;;EAQlD,kBAAkB,iBAAiB;;EAOnC;;UASQ;UACA;UACA;;;;;;;;;;;;;;;;;;;;;;;;;EA+BR,QACE,YACA,YACA,YACA,YACA,iBACA,gBACC;;UAyDK;;;;;;;;;UCtUO;WACN;WACA;;;;;;;UAQM;WACN;WACA;WACA;WACA,OAAO;;;;;;;iBAQF,eACd,eACA,gBACA,kBACA,gBAAgB,cACf;;;;;;iBAmBa,cACd,MAAM,WACN,WACA,WACA,eACA;;;;;;;iBA0Bc,iBACd,MAAM,WACN,WACA,WACA,YACA,YACA,eACA,eACA,KAAK,OACJ;;;;;;;;;;cC9BU,aAAa;UAChB;UACA;UACA;UACA;;MAGJ;;EAKJ,OAAO,MAAM,GAAG,QAAQ;;EAexB,OAAO,MAAM;;EAgBb,OAAO,MAAM,GAAG,QAAQ;;;;;EAgBxB,MAAM,QAAQ,MAAM,SAAS,MAAM;;EAgCnC;UAOQ;UAmBA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -26,6 +26,34 @@ declare function shapeBounds(shape: Shape, x: number, y: number): Rect;
|
|
|
26
26
|
declare function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect;
|
|
27
27
|
/** Test overlap between two positioned shapes. Returns true if overlapping. */
|
|
28
28
|
declare function shapeOverlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Minimum translation vector: the shortest push that separates two overlapping
|
|
31
|
+
* shapes.
|
|
32
|
+
*
|
|
33
|
+
* `shapeOverlap` answers *whether* two shapes collide and nothing more, which
|
|
34
|
+
* leaves a detection-only consumer with no way to resolve the collision. This is
|
|
35
|
+
* that missing step: `out.x`/`out.y` are a unit axis and `out.depth` is how far
|
|
36
|
+
* along it `b` must move to just stop touching `a`.
|
|
37
|
+
*
|
|
38
|
+
* Returns `null` when the shapes are not overlapping, leaving `out` untouched so
|
|
39
|
+
* a caller may reuse one buffer across many pairs.
|
|
40
|
+
*
|
|
41
|
+
* The degenerate concentric case (identical centres) has no meaningful axis;
|
|
42
|
+
* `(1, 0)` is returned so the caller still gets a usable separation instead of
|
|
43
|
+
* `NaN` from normalising a zero vector.
|
|
44
|
+
*/
|
|
45
|
+
interface Overlap {
|
|
46
|
+
/** Unit separation axis, pointing from `a` toward `b`. */
|
|
47
|
+
x: number;
|
|
48
|
+
y: number;
|
|
49
|
+
/** Penetration depth along that axis. */
|
|
50
|
+
depth: number;
|
|
51
|
+
}
|
|
52
|
+
/** A fresh, zeroed overlap record. Allocate once outside the loop and reuse. */
|
|
53
|
+
declare function createOverlap(): Overlap;
|
|
54
|
+
declare function overlapInto(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number, out: Overlap): Overlap | null;
|
|
55
|
+
/** Minimum translation vector. Allocates — see `overlapInto` for the hot path. */
|
|
56
|
+
declare function overlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): Overlap | null;
|
|
29
57
|
//#endregion
|
|
30
58
|
//#region src/collider.d.ts
|
|
31
59
|
/** Collider component data attached to an ECS entity. */
|
|
@@ -42,6 +70,46 @@ interface Collider {
|
|
|
42
70
|
/** Create a collider with default settings. */
|
|
43
71
|
declare function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider;
|
|
44
72
|
//#endregion
|
|
73
|
+
//#region src/raycast.d.ts
|
|
74
|
+
/** Result of a raycast hit. */
|
|
75
|
+
interface RaycastHit {
|
|
76
|
+
/** The entity that was hit. */
|
|
77
|
+
entity: number;
|
|
78
|
+
/** Distance from ray origin to hit point. */
|
|
79
|
+
distance: number;
|
|
80
|
+
/** World-space hit point. */
|
|
81
|
+
point: Vec2;
|
|
82
|
+
/** Surface normal at hit point. */
|
|
83
|
+
normal: Vec2;
|
|
84
|
+
}
|
|
85
|
+
/** Entity entry for raycast — position + shape. */
|
|
86
|
+
interface RaycastTarget {
|
|
87
|
+
entity: number;
|
|
88
|
+
shape: Shape;
|
|
89
|
+
x: number;
|
|
90
|
+
y: number;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Cast a ray against a list of targets. Returns the closest hit, or null.
|
|
94
|
+
* @param ox Ray origin X
|
|
95
|
+
* @param oy Ray origin Y
|
|
96
|
+
* @param dx Ray direction X (should be normalized)
|
|
97
|
+
* @param dy Ray direction Y (should be normalized)
|
|
98
|
+
* @param maxDist Maximum raycast distance
|
|
99
|
+
* @param targets Array of entities to test against
|
|
100
|
+
*/
|
|
101
|
+
declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
|
|
102
|
+
/**
|
|
103
|
+
* Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns
|
|
104
|
+
* `null` without touching `out`.
|
|
105
|
+
*
|
|
106
|
+
* The allocating `raycast()` above builds a fresh `RaycastHit` — with two nested
|
|
107
|
+
* `Vec2` objects — for **every candidate it tests**, then discards all but the
|
|
108
|
+
* closest. That is three objects per candidate per ray. This variant exists so
|
|
109
|
+
* `CollisionWorld.raycast` can test a whole broadphase cell without allocating.
|
|
110
|
+
*/
|
|
111
|
+
declare function rayVsShapeInto(ox: number, oy: number, dx: number, dy: number, maxDist: number, entity: number, shape: Shape, x: number, y: number, out: RaycastHit): RaycastHit | null;
|
|
112
|
+
//#endregion
|
|
45
113
|
//#region src/collision-world.d.ts
|
|
46
114
|
/** Collision pair data emitted by collision signals. */
|
|
47
115
|
interface CollisionPair {
|
|
@@ -70,8 +138,14 @@ declare class CollisionWorld {
|
|
|
70
138
|
private queryBuf;
|
|
71
139
|
/** Reusable bounds rect — avoids per-entity Rect allocation in step(). */
|
|
72
140
|
private boundsBuf;
|
|
73
|
-
/** Add an entity with a shape. */
|
|
141
|
+
/** Add an entity with a shape. Positional twin of `addCollider`. */
|
|
74
142
|
add(entity: number, shape: Shape, x: number, y: number, isTrigger?: boolean, layer?: number, mask?: number): void;
|
|
143
|
+
/**
|
|
144
|
+
* Add an entity from a `Collider`. Its `shape`, `isTrigger`, `layer` and `mask`
|
|
145
|
+
* are copied at add time — later edits to the collider do not reach the world;
|
|
146
|
+
* re-add to change them. Re-adding an existing entity replaces its data.
|
|
147
|
+
*/
|
|
148
|
+
addCollider(entity: number, collider: Collider, x: number, y: number): void;
|
|
75
149
|
/** Remove an entity from the world. Emits exit signals for any active pairs. */
|
|
76
150
|
remove(entity: number): void;
|
|
77
151
|
/** Update an entity's position. */
|
|
@@ -88,38 +162,39 @@ declare class CollisionWorld {
|
|
|
88
162
|
getColliderBounds(entity: number): Rect | null;
|
|
89
163
|
/** Remove all entities. */
|
|
90
164
|
clear(): void;
|
|
165
|
+
/** Reused across raycasts — see `raycast`. */
|
|
166
|
+
private raycastBounds;
|
|
167
|
+
private raycastCandidates;
|
|
168
|
+
private raycastHit;
|
|
169
|
+
/**
|
|
170
|
+
* Cast a ray against the colliders in this world.
|
|
171
|
+
*
|
|
172
|
+
* The standalone `raycast()` export tests a caller-supplied target array, so
|
|
173
|
+
* it neither sees the world's colliders nor benefits from the broadphase — an
|
|
174
|
+
* O(n) scan sitting beside an O(1) index. This walks only the cells the ray's
|
|
175
|
+
* bounding box touches.
|
|
176
|
+
*
|
|
177
|
+
* `mask` is a layer bitmask filtered against each collider's `layer`, using the
|
|
178
|
+
* bits colliders already carry; it defaults to every layer. Triggers are
|
|
179
|
+
* included — a ray asking "what is over there" wants them.
|
|
180
|
+
*
|
|
181
|
+
* The returned record is **reused between calls**: read it, or copy what you
|
|
182
|
+
* need, before casting again. That is what keeps a per-frame ray allocation-
|
|
183
|
+
* free, and it is why the standalone helper's fresh-object-per-candidate cost
|
|
184
|
+
* was worth removing.
|
|
185
|
+
*
|
|
186
|
+
* `maxDist` must be finite — this requires a bounded ray, not the
|
|
187
|
+
* `Number.POSITIVE_INFINITY` "unbounded ray" idiom some engines accept. The
|
|
188
|
+
* broadphase below is a uniform grid: an infinite `maxDist` derives an
|
|
189
|
+
* infinite cell range and never terminates the walk. Pass a large finite
|
|
190
|
+
* bound instead (e.g. the map's diagonal) to cast "until it leaves the
|
|
191
|
+
* populated area". A non-finite `maxDist` returns `null`.
|
|
192
|
+
*/
|
|
193
|
+
raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, mask?: number): RaycastHit | null;
|
|
194
|
+
/** Scratch record handed to `rayVsShapeInto` per candidate. */
|
|
195
|
+
private scratchHit;
|
|
91
196
|
}
|
|
92
197
|
//#endregion
|
|
93
|
-
//#region src/raycast.d.ts
|
|
94
|
-
/** Result of a raycast hit. */
|
|
95
|
-
interface RaycastHit {
|
|
96
|
-
/** The entity that was hit. */
|
|
97
|
-
entity: number;
|
|
98
|
-
/** Distance from ray origin to hit point. */
|
|
99
|
-
distance: number;
|
|
100
|
-
/** World-space hit point. */
|
|
101
|
-
point: Vec2;
|
|
102
|
-
/** Surface normal at hit point. */
|
|
103
|
-
normal: Vec2;
|
|
104
|
-
}
|
|
105
|
-
/** Entity entry for raycast — position + shape. */
|
|
106
|
-
interface RaycastTarget {
|
|
107
|
-
entity: number;
|
|
108
|
-
shape: Shape;
|
|
109
|
-
x: number;
|
|
110
|
-
y: number;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Cast a ray against a list of targets. Returns the closest hit, or null.
|
|
114
|
-
* @param ox Ray origin X
|
|
115
|
-
* @param oy Ray origin Y
|
|
116
|
-
* @param dx Ray direction X (should be normalized)
|
|
117
|
-
* @param dy Ray direction Y (should be normalized)
|
|
118
|
-
* @param maxDist Maximum raycast distance
|
|
119
|
-
* @param targets Array of entities to test against
|
|
120
|
-
*/
|
|
121
|
-
declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
|
|
122
|
-
//#endregion
|
|
123
198
|
//#region src/solid-grid.d.ts
|
|
124
199
|
/**
|
|
125
200
|
* Minimal structural type for a solid tile position in grid coordinates.
|
|
@@ -131,14 +206,15 @@ interface SolidTile {
|
|
|
131
206
|
readonly col: number;
|
|
132
207
|
}
|
|
133
208
|
/**
|
|
134
|
-
* Precomputed
|
|
135
|
-
*
|
|
209
|
+
* Precomputed solid-tile lookup used by `isBlockedAABB` and `moveAndSlideInto`.
|
|
210
|
+
* `solid` has `width * height` cells indexed `row * width + col`; non-zero means
|
|
211
|
+
* solid. Build once per map load via `buildSolidGrid`.
|
|
136
212
|
*/
|
|
137
213
|
interface SolidGrid {
|
|
138
214
|
readonly width: number;
|
|
139
215
|
readonly height: number;
|
|
140
216
|
readonly tileSize: number;
|
|
141
|
-
readonly solid:
|
|
217
|
+
readonly solid: Uint8Array;
|
|
142
218
|
}
|
|
143
219
|
/**
|
|
144
220
|
* Build a `SolidGrid` from a list of solid tile positions.
|
|
@@ -161,15 +237,6 @@ declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: num
|
|
|
161
237
|
declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: number, dy: number, halfW: number, halfH: number, out: Vec2): Vec2;
|
|
162
238
|
//#endregion
|
|
163
239
|
//#region src/spatial-index.d.ts
|
|
164
|
-
/** Item stored in the spatial index. */
|
|
165
|
-
interface SpatialEntry<T> {
|
|
166
|
-
readonly item: T;
|
|
167
|
-
/** Bounding rect (minX, minY, maxX, maxY). */
|
|
168
|
-
minX: number;
|
|
169
|
-
minY: number;
|
|
170
|
-
maxX: number;
|
|
171
|
-
maxY: number;
|
|
172
|
-
}
|
|
173
240
|
/**
|
|
174
241
|
* Uniform spatial-hash broadphase.
|
|
175
242
|
* `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.
|
|
@@ -201,5 +268,5 @@ declare class SpatialIndex<T> {
|
|
|
201
268
|
private _unlink;
|
|
202
269
|
}
|
|
203
270
|
//#endregion
|
|
204
|
-
export { AABB, type AABBShape, CircleShape, type CircleShape as CircleShapeType, type Collider, type CollisionPair, CollisionWorld, type RaycastHit, type RaycastTarget, type Shape, type SolidGrid, type SolidTile,
|
|
271
|
+
export { AABB, type AABBShape, CircleShape, type CircleShape as CircleShapeType, type Collider, type CollisionPair, CollisionWorld, type Overlap, type RaycastHit, type RaycastTarget, type Shape, type SolidGrid, type SolidTile, SpatialIndex, buildSolidGrid, createCollider, createOverlap, isBlockedAABB, moveAndSlideInto, overlap, overlapInto, rayVsShapeInto, raycast, shapeBounds, shapeBoundsInto, shapeOverlap };
|
|
205
272
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/shape.ts","../src/collider.ts","../src/
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/shape.ts","../src/collider.ts","../src/raycast.ts","../src/collision-world.ts","../src/solid-grid.ts","../src/spatial-index.ts"],"mappings":";;;;UAGiB;WACN;;WAEA;;WAEA;;;UAIM;WACN;WACA;;;KAIC,QAAQ,YAAY;;iBAGhB,KAAK,eAAe,iBAAiB;;iBAKrC,YAAY,iBAAiB;;iBAK7B,YAAY,OAAO,OAAO,WAAW,YAAY;;iBAajD,gBAAgB,OAAO,OAAO,WAAW,WAAW,KAAK,OAAO;;iBAgBhE,aACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA;;;;;;;;;;;;;;;;;UA2Fe;;EAEf;EACA;;EAEA;;;iBAIc,iBAAiB;iBAIjB,YACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,YACA,KAAK,UACJ;;iBA0Ba,QACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,aACC;;;;UChNc;;EAEf,OAAO;;EAEP;;EAEA;;EAEA;;;iBAIc,eAAe,OAAO,OAAO,OAAO,QAAQ,KAAK,sBAAsB;;;;UCXtE;;EAEf;;EAEA;;EAEA,OAAO;;EAEP,QAAQ;;;UAIO;EACf;EACA,OAAO;EACP;EACA;;;;;;;;;;;iBAYc,QACd,YACA,YACA,YACA,YACA,iBACA,kBAAkB,kBACjB;;;;;;;;;;iBAkKa,eACd,YACA,YACA,YACA,YACA,iBACA,gBACA,OAAO,OACP,WACA,WACA,KAAK,aACJ;;;;UC1Mc;WACN;WACA;;;;;;cAuCE;WACF,kBAAgB,OAAA;WAChB,gBAAc,OAAA;WACd,gBAAc,OAAA;WACd,eAAa,OAAA;UAEd;UACA;;;;;UAKA;UACA;UACA;;UAEA;;UAEA;;EAGR,IACE,gBACA,OAAO,OACP,WACA,WACA,qBACA,gBACA;;;;;;EAkBF,YAAY,gBAAgB,UAAU,UAAU,WAAW;;EAK3D,OAAO;;EAyBP,YAAY,gBAAgB,WAAW;;MAUnC;;MAKA;;EAKJ;;EAiEA,sBAAsB,gBAAgB,KAAK,OAAO;;EAQlD,kBAAkB,iBAAiB;;EAOnC;;UASQ;UACA;UACA;;;;;;;;;;;;;;;;;;;;;;;;;EA+BR,QACE,YACA,YACA,YACA,YACA,iBACA,gBACC;;UAyDK;;;;;;;;;UCtUO;WACN;WACA;;;;;;;UAQM;WACN;WACA;WACA;WACA,OAAO;;;;;;;iBAQF,eACd,eACA,gBACA,kBACA,gBAAgB,cACf;;;;;;iBAmBa,cACd,MAAM,WACN,WACA,WACA,eACA;;;;;;;iBA0Bc,iBACd,MAAM,WACN,WACA,WACA,YACA,YACA,eACA,eACA,KAAK,OACJ;;;;;;;;;;cC9BU,aAAa;UAChB;UACA;UACA;UACA;;MAGJ;;EAKJ,OAAO,MAAM,GAAG,QAAQ;;EAexB,OAAO,MAAM;;EAgBb,OAAO,MAAM,GAAG,QAAQ;;;;;EAgBxB,MAAM,QAAQ,MAAM,SAAS,MAAM;;EAgCnC;UAOQ;UAmBA"}
|