@flare-engine/physics 0.3.0 → 0.4.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/README.md +48 -2
- package/dist/index.d.mts +132 -50
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +132 -50
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +495 -118
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +492 -119
- 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. */
|
|
@@ -82,40 +156,45 @@ declare class CollisionWorld {
|
|
|
82
156
|
get broadphaseSize(): number;
|
|
83
157
|
/** Run one collision detection step. */
|
|
84
158
|
step(): void;
|
|
159
|
+
/** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */
|
|
160
|
+
getColliderBoundsInto(entity: number, out: Rect): Rect | null;
|
|
161
|
+
/** Allocating convenience wrapper — returns a fresh Rect or null when entity is absent. */
|
|
162
|
+
getColliderBounds(entity: number): Rect | null;
|
|
85
163
|
/** Remove all entities. */
|
|
86
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;
|
|
87
196
|
}
|
|
88
197
|
//#endregion
|
|
89
|
-
//#region src/raycast.d.ts
|
|
90
|
-
/** Result of a raycast hit. */
|
|
91
|
-
interface RaycastHit {
|
|
92
|
-
/** The entity that was hit. */
|
|
93
|
-
entity: number;
|
|
94
|
-
/** Distance from ray origin to hit point. */
|
|
95
|
-
distance: number;
|
|
96
|
-
/** World-space hit point. */
|
|
97
|
-
point: Vec2;
|
|
98
|
-
/** Surface normal at hit point. */
|
|
99
|
-
normal: Vec2;
|
|
100
|
-
}
|
|
101
|
-
/** Entity entry for raycast — position + shape. */
|
|
102
|
-
interface RaycastTarget {
|
|
103
|
-
entity: number;
|
|
104
|
-
shape: Shape;
|
|
105
|
-
x: number;
|
|
106
|
-
y: number;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Cast a ray against a list of targets. Returns the closest hit, or null.
|
|
110
|
-
* @param ox Ray origin X
|
|
111
|
-
* @param oy Ray origin Y
|
|
112
|
-
* @param dx Ray direction X (should be normalized)
|
|
113
|
-
* @param dy Ray direction Y (should be normalized)
|
|
114
|
-
* @param maxDist Maximum raycast distance
|
|
115
|
-
* @param targets Array of entities to test against
|
|
116
|
-
*/
|
|
117
|
-
declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
|
|
118
|
-
//#endregion
|
|
119
198
|
//#region src/solid-grid.d.ts
|
|
120
199
|
/**
|
|
121
200
|
* Minimal structural type for a solid tile position in grid coordinates.
|
|
@@ -127,14 +206,15 @@ interface SolidTile {
|
|
|
127
206
|
readonly col: number;
|
|
128
207
|
}
|
|
129
208
|
/**
|
|
130
|
-
* Precomputed
|
|
131
|
-
*
|
|
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`.
|
|
132
212
|
*/
|
|
133
213
|
interface SolidGrid {
|
|
134
214
|
readonly width: number;
|
|
135
215
|
readonly height: number;
|
|
136
216
|
readonly tileSize: number;
|
|
137
|
-
readonly solid:
|
|
217
|
+
readonly solid: Uint8Array;
|
|
138
218
|
}
|
|
139
219
|
/**
|
|
140
220
|
* Build a `SolidGrid` from a list of solid tile positions.
|
|
@@ -157,34 +237,36 @@ declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: num
|
|
|
157
237
|
declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: number, dy: number, halfW: number, halfH: number, out: Vec2): Vec2;
|
|
158
238
|
//#endregion
|
|
159
239
|
//#region src/spatial-index.d.ts
|
|
160
|
-
/** Item stored in the spatial index. */
|
|
161
|
-
interface SpatialEntry<T> {
|
|
162
|
-
readonly item: T;
|
|
163
|
-
/** Bounding rect (minX, minY, width, height). */
|
|
164
|
-
minX: number;
|
|
165
|
-
minY: number;
|
|
166
|
-
maxX: number;
|
|
167
|
-
maxY: number;
|
|
168
|
-
}
|
|
169
240
|
/**
|
|
170
|
-
*
|
|
171
|
-
*
|
|
241
|
+
* Uniform spatial-hash broadphase.
|
|
242
|
+
* `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.
|
|
243
|
+
* `query` walks only the cells overlapping the given rect; result order is unspecified.
|
|
244
|
+
* Zero-alloc in the query hot-path: duplicate suppression uses a monotone stamp counter,
|
|
245
|
+
* not a per-call Set.
|
|
172
246
|
*/
|
|
173
247
|
declare class SpatialIndex<T> {
|
|
174
|
-
private
|
|
248
|
+
private _data;
|
|
249
|
+
private _slots;
|
|
250
|
+
private _cells;
|
|
251
|
+
private _gen;
|
|
175
252
|
/** Number of items in the index. */
|
|
176
253
|
get size(): number;
|
|
177
254
|
/** Insert an item with a bounding rect. */
|
|
178
255
|
insert(item: T, bounds: Rect): void;
|
|
179
|
-
/** Remove an item. */
|
|
256
|
+
/** Remove an item. Returns false when the item was not present. */
|
|
180
257
|
remove(item: T): boolean;
|
|
181
|
-
/** Update the bounds for an existing item. */
|
|
258
|
+
/** Update the bounds for an existing item. No-op when the item is absent. */
|
|
182
259
|
update(item: T, bounds: Rect): void;
|
|
183
|
-
/**
|
|
260
|
+
/**
|
|
261
|
+
* Query all items whose bounds overlap the given rect.
|
|
262
|
+
* Result order is unspecified. Appends to `result` when provided and returns it.
|
|
263
|
+
*/
|
|
184
264
|
query(bounds: Rect, result?: T[]): T[];
|
|
185
265
|
/** Remove all items. */
|
|
186
266
|
clear(): void;
|
|
267
|
+
private _link;
|
|
268
|
+
private _unlink;
|
|
187
269
|
}
|
|
188
270
|
//#endregion
|
|
189
|
-
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 };
|
|
190
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. */
|
|
@@ -82,40 +156,45 @@ declare class CollisionWorld {
|
|
|
82
156
|
get broadphaseSize(): number;
|
|
83
157
|
/** Run one collision detection step. */
|
|
84
158
|
step(): void;
|
|
159
|
+
/** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */
|
|
160
|
+
getColliderBoundsInto(entity: number, out: Rect): Rect | null;
|
|
161
|
+
/** Allocating convenience wrapper — returns a fresh Rect or null when entity is absent. */
|
|
162
|
+
getColliderBounds(entity: number): Rect | null;
|
|
85
163
|
/** Remove all entities. */
|
|
86
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;
|
|
87
196
|
}
|
|
88
197
|
//#endregion
|
|
89
|
-
//#region src/raycast.d.ts
|
|
90
|
-
/** Result of a raycast hit. */
|
|
91
|
-
interface RaycastHit {
|
|
92
|
-
/** The entity that was hit. */
|
|
93
|
-
entity: number;
|
|
94
|
-
/** Distance from ray origin to hit point. */
|
|
95
|
-
distance: number;
|
|
96
|
-
/** World-space hit point. */
|
|
97
|
-
point: Vec2;
|
|
98
|
-
/** Surface normal at hit point. */
|
|
99
|
-
normal: Vec2;
|
|
100
|
-
}
|
|
101
|
-
/** Entity entry for raycast — position + shape. */
|
|
102
|
-
interface RaycastTarget {
|
|
103
|
-
entity: number;
|
|
104
|
-
shape: Shape;
|
|
105
|
-
x: number;
|
|
106
|
-
y: number;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Cast a ray against a list of targets. Returns the closest hit, or null.
|
|
110
|
-
* @param ox Ray origin X
|
|
111
|
-
* @param oy Ray origin Y
|
|
112
|
-
* @param dx Ray direction X (should be normalized)
|
|
113
|
-
* @param dy Ray direction Y (should be normalized)
|
|
114
|
-
* @param maxDist Maximum raycast distance
|
|
115
|
-
* @param targets Array of entities to test against
|
|
116
|
-
*/
|
|
117
|
-
declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
|
|
118
|
-
//#endregion
|
|
119
198
|
//#region src/solid-grid.d.ts
|
|
120
199
|
/**
|
|
121
200
|
* Minimal structural type for a solid tile position in grid coordinates.
|
|
@@ -127,14 +206,15 @@ interface SolidTile {
|
|
|
127
206
|
readonly col: number;
|
|
128
207
|
}
|
|
129
208
|
/**
|
|
130
|
-
* Precomputed
|
|
131
|
-
*
|
|
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`.
|
|
132
212
|
*/
|
|
133
213
|
interface SolidGrid {
|
|
134
214
|
readonly width: number;
|
|
135
215
|
readonly height: number;
|
|
136
216
|
readonly tileSize: number;
|
|
137
|
-
readonly solid:
|
|
217
|
+
readonly solid: Uint8Array;
|
|
138
218
|
}
|
|
139
219
|
/**
|
|
140
220
|
* Build a `SolidGrid` from a list of solid tile positions.
|
|
@@ -157,34 +237,36 @@ declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: num
|
|
|
157
237
|
declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: number, dy: number, halfW: number, halfH: number, out: Vec2): Vec2;
|
|
158
238
|
//#endregion
|
|
159
239
|
//#region src/spatial-index.d.ts
|
|
160
|
-
/** Item stored in the spatial index. */
|
|
161
|
-
interface SpatialEntry<T> {
|
|
162
|
-
readonly item: T;
|
|
163
|
-
/** Bounding rect (minX, minY, width, height). */
|
|
164
|
-
minX: number;
|
|
165
|
-
minY: number;
|
|
166
|
-
maxX: number;
|
|
167
|
-
maxY: number;
|
|
168
|
-
}
|
|
169
240
|
/**
|
|
170
|
-
*
|
|
171
|
-
*
|
|
241
|
+
* Uniform spatial-hash broadphase.
|
|
242
|
+
* `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.
|
|
243
|
+
* `query` walks only the cells overlapping the given rect; result order is unspecified.
|
|
244
|
+
* Zero-alloc in the query hot-path: duplicate suppression uses a monotone stamp counter,
|
|
245
|
+
* not a per-call Set.
|
|
172
246
|
*/
|
|
173
247
|
declare class SpatialIndex<T> {
|
|
174
|
-
private
|
|
248
|
+
private _data;
|
|
249
|
+
private _slots;
|
|
250
|
+
private _cells;
|
|
251
|
+
private _gen;
|
|
175
252
|
/** Number of items in the index. */
|
|
176
253
|
get size(): number;
|
|
177
254
|
/** Insert an item with a bounding rect. */
|
|
178
255
|
insert(item: T, bounds: Rect): void;
|
|
179
|
-
/** Remove an item. */
|
|
256
|
+
/** Remove an item. Returns false when the item was not present. */
|
|
180
257
|
remove(item: T): boolean;
|
|
181
|
-
/** Update the bounds for an existing item. */
|
|
258
|
+
/** Update the bounds for an existing item. No-op when the item is absent. */
|
|
182
259
|
update(item: T, bounds: Rect): void;
|
|
183
|
-
/**
|
|
260
|
+
/**
|
|
261
|
+
* Query all items whose bounds overlap the given rect.
|
|
262
|
+
* Result order is unspecified. Appends to `result` when provided and returns it.
|
|
263
|
+
*/
|
|
184
264
|
query(bounds: Rect, result?: T[]): T[];
|
|
185
265
|
/** Remove all items. */
|
|
186
266
|
clear(): void;
|
|
267
|
+
private _link;
|
|
268
|
+
private _unlink;
|
|
187
269
|
}
|
|
188
270
|
//#endregion
|
|
189
|
-
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 };
|
|
190
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"}
|