@flare-engine/physics 0.4.11 → 0.4.13
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 +3 -3
- package/dist/index.d.mts +42 -8
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +42 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +143 -115
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +142 -116
- 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` 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.
|
|
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), with `isSolidAt` / `setSolidAt` to read or flip one cell at runtime. 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
|
|
|
@@ -69,8 +69,8 @@ by the `layer` bits colliders already carry (`mask` defaults to every layer). Th
|
|
|
69
69
|
is not in a world.
|
|
70
70
|
|
|
71
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
|
|
73
|
-
fresh hit (plus two nested `Vec2`s)
|
|
72
|
+
again. That is what keeps a per-frame ray allocation-free; the standalone helper by contrast returns
|
|
73
|
+
a fresh hit (plus two nested `Vec2`s) from each call that hits, which the caller may keep.
|
|
74
74
|
|
|
75
75
|
See [API spec](https://github.com/grzott/flare-engine/blob/main/docs/design/api-specs.md#flare-enginephysics--4kb-aabb).
|
|
76
76
|
|
package/dist/index.d.mts
CHANGED
|
@@ -86,6 +86,9 @@ interface RaycastTarget {
|
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
88
|
* Cast a ray against a list of targets. Returns the closest hit, or null.
|
|
89
|
+
*
|
|
90
|
+
* The returned hit is a fresh object — the caller may keep it; later calls do
|
|
91
|
+
* not overwrite it. Among targets at equal distance the first in `targets` wins.
|
|
89
92
|
* @param ox Ray origin X
|
|
90
93
|
* @param oy Ray origin Y
|
|
91
94
|
* @param dx Ray direction X (should be normalized)
|
|
@@ -98,10 +101,9 @@ export declare function raycast(ox: number, oy: number, dx: number, dy: number,
|
|
|
98
101
|
* Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns
|
|
99
102
|
* `null` without touching `out`.
|
|
100
103
|
*
|
|
101
|
-
*
|
|
102
|
-
* `
|
|
103
|
-
*
|
|
104
|
-
* `CollisionWorld.raycast` can test a whole broadphase cell without allocating.
|
|
104
|
+
* This is the single implementation of the ray math. `raycast()` above and
|
|
105
|
+
* `CollisionWorld.raycast` both run every candidate through it into a scratch
|
|
106
|
+
* record, so neither allocates per candidate tested.
|
|
105
107
|
*/
|
|
106
108
|
export 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;
|
|
107
109
|
/** Collision pair data emitted by collision signals. */
|
|
@@ -131,12 +133,21 @@ export declare class CollisionWorld {
|
|
|
131
133
|
private queryBuf;
|
|
132
134
|
/** Reusable bounds rect — avoids per-entity Rect allocation in step(). */
|
|
133
135
|
private boundsBuf;
|
|
134
|
-
/**
|
|
136
|
+
/**
|
|
137
|
+
* Add an entity with a shape. Positional twin of `addCollider`, and re-adding
|
|
138
|
+
* follows the same rules.
|
|
139
|
+
*/
|
|
135
140
|
add(entity: number, shape: Shape, x: number, y: number, isTrigger?: boolean, layer?: number, mask?: number): void;
|
|
136
141
|
/**
|
|
137
142
|
* Add an entity from a `Collider`. Its `shape`, `isTrigger`, `layer` and `mask`
|
|
138
143
|
* are copied at add time — later edits to the collider do not reach the world;
|
|
139
144
|
* re-add to change them. Re-adding an existing entity replaces its data.
|
|
145
|
+
*
|
|
146
|
+
* A re-add that flips `isTrigger` ends, during this call, every active pair
|
|
147
|
+
* whose kind that changes, with the exit signal of its old kind
|
|
148
|
+
* (`onTriggerExit` or `onCollisionEnd`). If the shapes still overlap, the next
|
|
149
|
+
* `step` starts the pair again with the enter signal of its new kind. A pair
|
|
150
|
+
* whose other side is a trigger stays a trigger pair and is left alone.
|
|
140
151
|
*/
|
|
141
152
|
addCollider(entity: number, collider: Collider, x: number, y: number): void;
|
|
142
153
|
/** Remove an entity from the world. Emits exit signals for any active pairs. */
|
|
@@ -149,6 +160,13 @@ export declare class CollisionWorld {
|
|
|
149
160
|
get broadphaseSize(): number;
|
|
150
161
|
/** Run one collision detection step. */
|
|
151
162
|
step(): void;
|
|
163
|
+
/**
|
|
164
|
+
* The one place that decides trigger vs collision for a pair and emits its
|
|
165
|
+
* enter or exit signal. `a` and `b` are the pair's two entries in either
|
|
166
|
+
* order. They are passed in rather than looked up because in `remove` the
|
|
167
|
+
* removed entity is already gone from `entries`.
|
|
168
|
+
*/
|
|
169
|
+
private emitPair;
|
|
152
170
|
/** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */
|
|
153
171
|
getColliderBoundsInto(entity: number, out: Rect): Rect | null;
|
|
154
172
|
/** Allocating convenience wrapper — returns a fresh Rect or null when entity is absent. */
|
|
@@ -173,8 +191,8 @@ export declare class CollisionWorld {
|
|
|
173
191
|
*
|
|
174
192
|
* The returned record is **reused between calls**: read it, or copy what you
|
|
175
193
|
* need, before casting again. That is what keeps a per-frame ray allocation-
|
|
176
|
-
* free
|
|
177
|
-
*
|
|
194
|
+
* free; the standalone helper by contrast returns a fresh hit from each call
|
|
195
|
+
* that hits, which the caller may keep.
|
|
178
196
|
*
|
|
179
197
|
* `maxDist` must be finite — this requires a bounded ray, not the
|
|
180
198
|
* `Number.POSITIVE_INFINITY` "unbounded ray" idiom some engines accept. The
|
|
@@ -233,6 +251,19 @@ export declare function buildSolidGrid(width: number, height: number, tileSize:
|
|
|
233
251
|
* box overlaps are tested. Uses half-open EDGE semantics on max edges.
|
|
234
252
|
*/
|
|
235
253
|
export declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: number, halfH: number): boolean;
|
|
254
|
+
/**
|
|
255
|
+
* Whether the cell at (`col`, `row`) is solid. Coordinates that name no cell
|
|
256
|
+
* (out of range, or not an integer) follow the grid's own `outside` rule, like
|
|
257
|
+
* `isBlockedAABB` at the map edge: solid on an `outside: 'solid'` grid, empty
|
|
258
|
+
* on an `'open'` one. Zero-alloc.
|
|
259
|
+
*/
|
|
260
|
+
export declare function isSolidAt(grid: SolidGrid, col: number, row: number): boolean;
|
|
261
|
+
/**
|
|
262
|
+
* Mark the cell at (`col`, `row`) solid or empty, for a door or a breakable
|
|
263
|
+
* wall. Returns `true` when the cell was written, `false` (and writes nothing)
|
|
264
|
+
* when the coordinates are out of range or not integers. Zero-alloc.
|
|
265
|
+
*/
|
|
266
|
+
export declare function setSolidAt(grid: SolidGrid, col: number, row: number, solid: boolean): boolean;
|
|
236
267
|
/**
|
|
237
268
|
* Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis
|
|
238
269
|
* is applied independently so a blocked diagonal input slides along the wall.
|
|
@@ -254,7 +285,10 @@ export declare class SpatialIndex<T> {
|
|
|
254
285
|
private _gen;
|
|
255
286
|
/** Number of items in the index. */
|
|
256
287
|
get size(): number;
|
|
257
|
-
/**
|
|
288
|
+
/**
|
|
289
|
+
* Insert an item with a bounding rect. Inserting an item already in the index
|
|
290
|
+
* replaces its bounds (upsert) — same as `update`.
|
|
291
|
+
*/
|
|
258
292
|
insert(item: T, bounds: Rect): void;
|
|
259
293
|
/** Remove an item. Returns false when the item was not present. */
|
|
260
294
|
remove(item: T): boolean;
|
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/raycast.ts","../src/collision-world.ts","../src/solid-grid.ts","../src/spatial-index.ts"],"mappings":";;;UAGiB;WACN;;WAEA;;WAEA;;;iBAIM;WACN;WACA;;;KAIC,QAAQ,YAAY;;wBAGhB,KAAK,eAAe,iBAAiB;;wBAKrC,YAAY,iBAAiB;;wBAK7B,YAAY,OAAO,OAAO,WAAW,YAAY;;wBAajD,gBAAgB,OAAO,OAAO,WAAW,WAAW,KAAK,OAAO;;wBAgBhE,aACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA;;;;;;;;;;;;;;;;;UA2Fe;;EAEf;EACA;;EAEA;;;wBAIc,iBAAiB;wBAIjB,YACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,YACA,KAAK,UACJ;;wBA0Ba,QACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,aACC;;UChNc;;EAEf,OAAO;;EAEP;;EAEA;;EAEA;;;wBAIc,eAAe,OAAO,OAAO,OAAO,QAAQ,KAAK,sBAAsB;;UCXtE;;EAEf;;EAEA;;EAEA,OAAO;;EAEP,QAAQ;;;UAIO;EACf;EACA,OAAO;EACP;EACA
|
|
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;;;iBAIM;WACN;WACA;;;KAIC,QAAQ,YAAY;;wBAGhB,KAAK,eAAe,iBAAiB;;wBAKrC,YAAY,iBAAiB;;wBAK7B,YAAY,OAAO,OAAO,WAAW,YAAY;;wBAajD,gBAAgB,OAAO,OAAO,WAAW,WAAW,KAAK,OAAO;;wBAgBhE,aACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA;;;;;;;;;;;;;;;;;UA2Fe;;EAEf;EACA;;EAEA;;;wBAIc,iBAAiB;wBAIjB,YACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,YACA,KAAK,UACJ;;wBA0Ba,QACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,aACC;;UChNc;;EAEf,OAAO;;EAEP;;EAEA;;EAEA;;;wBAIc,eAAe,OAAO,OAAO,OAAO,QAAQ,KAAK,sBAAsB;;UCXtE;;EAEf;;EAEA;;EAEA,OAAO;;EAEP,QAAQ;;;UAIO;EACf;EACA,OAAO;EACP;EACA;;;;;;;;;;;;;;wBAuBc,QACd,YACA,YACA,YACA,YACA,iBACA,kBAAkB,kBACjB;;;;;;;;;wBAqCa,eACd,YACA,YACA,YACA,YACA,iBACA,gBACA,OAAO,OACP,WACA,WACA,KAAK,aACJ;;UCxFc;WACN;WACA;;;;;;qBAuCE;WACF,kBAAgB,OAAA;WAChB,gBAAc,OAAA;WACd,gBAAc,OAAA;WACd,eAAa,OAAA;UAEd;UACA;;;;;UAKA;UACA;UACA;;UAEA;;UAEA;;;;;EAMR,IACE,gBACA,OAAO,OACP,WACA,WACA,qBACA,gBACA;;;;;;;;;;;;EAsCF,YAAY,gBAAgB,UAAU,UAAU,WAAW;;EAK3D,OAAO;;EAmBP,YAAY,gBAAgB,WAAW;;MAUnC;;MAKA;;EAKJ;;;;;;;UAwDQ;;EAmBR,sBAAsB,gBAAgB,KAAK,OAAO;;EAQlD,kBAAkB,iBAAiB;;EAOnC;;UASQ;UACA;UACA;;;;;;;;;;;;;;;;;;;;;;;;;EA+BR,QACE,YACA,YACA,YACA,YACA,iBACA,gBACC;;UAyDK;;;;;;;UCjWO;WACN;WACA;;;;;;;;KASC;UAEK;;WAEN,UAAU;;;;;;;UAQJ;WACN;WACA;WACA;WACA,OAAO;WACP,SAAS;;;;;;;wBAQJ,eACd,eACA,gBACA,kBACA,gBAAgB,aAChB,UAAU,mBACT;;;;;;;;wBAqBa,cACd,MAAM,WACN,WACA,WACA,eACA;;;;;;;wBA+Bc,UAAU,MAAM,WAAW,aAAa;;;;;;wBAUxC,WAAW,MAAM,WAAW,aAAa,aAAa;;;;;;;wBAuBtD,iBACd,MAAM,WACN,WACA,WACA,YACA,YACA,eACA,eACA,KAAK,OACJ;;;;;;;;qBCrFU,aAAa;UAChB;UACA;UACA;UACA;;MAGJ;;;;;EAQJ,OAAO,MAAM,GAAG,QAAQ;;EAmBxB,OAAO,MAAM;;EAgBb,OAAO,MAAM,GAAG,QAAQ;;;;;EAgBxB,MAAM,QAAQ,MAAM,SAAS,MAAM;;EAgCnC;UAOQ;UAmBA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -86,6 +86,9 @@ interface RaycastTarget {
|
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
88
|
* Cast a ray against a list of targets. Returns the closest hit, or null.
|
|
89
|
+
*
|
|
90
|
+
* The returned hit is a fresh object — the caller may keep it; later calls do
|
|
91
|
+
* not overwrite it. Among targets at equal distance the first in `targets` wins.
|
|
89
92
|
* @param ox Ray origin X
|
|
90
93
|
* @param oy Ray origin Y
|
|
91
94
|
* @param dx Ray direction X (should be normalized)
|
|
@@ -98,10 +101,9 @@ export declare function raycast(ox: number, oy: number, dx: number, dy: number,
|
|
|
98
101
|
* Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns
|
|
99
102
|
* `null` without touching `out`.
|
|
100
103
|
*
|
|
101
|
-
*
|
|
102
|
-
* `
|
|
103
|
-
*
|
|
104
|
-
* `CollisionWorld.raycast` can test a whole broadphase cell without allocating.
|
|
104
|
+
* This is the single implementation of the ray math. `raycast()` above and
|
|
105
|
+
* `CollisionWorld.raycast` both run every candidate through it into a scratch
|
|
106
|
+
* record, so neither allocates per candidate tested.
|
|
105
107
|
*/
|
|
106
108
|
export 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;
|
|
107
109
|
/** Collision pair data emitted by collision signals. */
|
|
@@ -131,12 +133,21 @@ export declare class CollisionWorld {
|
|
|
131
133
|
private queryBuf;
|
|
132
134
|
/** Reusable bounds rect — avoids per-entity Rect allocation in step(). */
|
|
133
135
|
private boundsBuf;
|
|
134
|
-
/**
|
|
136
|
+
/**
|
|
137
|
+
* Add an entity with a shape. Positional twin of `addCollider`, and re-adding
|
|
138
|
+
* follows the same rules.
|
|
139
|
+
*/
|
|
135
140
|
add(entity: number, shape: Shape, x: number, y: number, isTrigger?: boolean, layer?: number, mask?: number): void;
|
|
136
141
|
/**
|
|
137
142
|
* Add an entity from a `Collider`. Its `shape`, `isTrigger`, `layer` and `mask`
|
|
138
143
|
* are copied at add time — later edits to the collider do not reach the world;
|
|
139
144
|
* re-add to change them. Re-adding an existing entity replaces its data.
|
|
145
|
+
*
|
|
146
|
+
* A re-add that flips `isTrigger` ends, during this call, every active pair
|
|
147
|
+
* whose kind that changes, with the exit signal of its old kind
|
|
148
|
+
* (`onTriggerExit` or `onCollisionEnd`). If the shapes still overlap, the next
|
|
149
|
+
* `step` starts the pair again with the enter signal of its new kind. A pair
|
|
150
|
+
* whose other side is a trigger stays a trigger pair and is left alone.
|
|
140
151
|
*/
|
|
141
152
|
addCollider(entity: number, collider: Collider, x: number, y: number): void;
|
|
142
153
|
/** Remove an entity from the world. Emits exit signals for any active pairs. */
|
|
@@ -149,6 +160,13 @@ export declare class CollisionWorld {
|
|
|
149
160
|
get broadphaseSize(): number;
|
|
150
161
|
/** Run one collision detection step. */
|
|
151
162
|
step(): void;
|
|
163
|
+
/**
|
|
164
|
+
* The one place that decides trigger vs collision for a pair and emits its
|
|
165
|
+
* enter or exit signal. `a` and `b` are the pair's two entries in either
|
|
166
|
+
* order. They are passed in rather than looked up because in `remove` the
|
|
167
|
+
* removed entity is already gone from `entries`.
|
|
168
|
+
*/
|
|
169
|
+
private emitPair;
|
|
152
170
|
/** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */
|
|
153
171
|
getColliderBoundsInto(entity: number, out: Rect): Rect | null;
|
|
154
172
|
/** Allocating convenience wrapper — returns a fresh Rect or null when entity is absent. */
|
|
@@ -173,8 +191,8 @@ export declare class CollisionWorld {
|
|
|
173
191
|
*
|
|
174
192
|
* The returned record is **reused between calls**: read it, or copy what you
|
|
175
193
|
* need, before casting again. That is what keeps a per-frame ray allocation-
|
|
176
|
-
* free
|
|
177
|
-
*
|
|
194
|
+
* free; the standalone helper by contrast returns a fresh hit from each call
|
|
195
|
+
* that hits, which the caller may keep.
|
|
178
196
|
*
|
|
179
197
|
* `maxDist` must be finite — this requires a bounded ray, not the
|
|
180
198
|
* `Number.POSITIVE_INFINITY` "unbounded ray" idiom some engines accept. The
|
|
@@ -233,6 +251,19 @@ export declare function buildSolidGrid(width: number, height: number, tileSize:
|
|
|
233
251
|
* box overlaps are tested. Uses half-open EDGE semantics on max edges.
|
|
234
252
|
*/
|
|
235
253
|
export declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: number, halfH: number): boolean;
|
|
254
|
+
/**
|
|
255
|
+
* Whether the cell at (`col`, `row`) is solid. Coordinates that name no cell
|
|
256
|
+
* (out of range, or not an integer) follow the grid's own `outside` rule, like
|
|
257
|
+
* `isBlockedAABB` at the map edge: solid on an `outside: 'solid'` grid, empty
|
|
258
|
+
* on an `'open'` one. Zero-alloc.
|
|
259
|
+
*/
|
|
260
|
+
export declare function isSolidAt(grid: SolidGrid, col: number, row: number): boolean;
|
|
261
|
+
/**
|
|
262
|
+
* Mark the cell at (`col`, `row`) solid or empty, for a door or a breakable
|
|
263
|
+
* wall. Returns `true` when the cell was written, `false` (and writes nothing)
|
|
264
|
+
* when the coordinates are out of range or not integers. Zero-alloc.
|
|
265
|
+
*/
|
|
266
|
+
export declare function setSolidAt(grid: SolidGrid, col: number, row: number, solid: boolean): boolean;
|
|
236
267
|
/**
|
|
237
268
|
* Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis
|
|
238
269
|
* is applied independently so a blocked diagonal input slides along the wall.
|
|
@@ -254,7 +285,10 @@ export declare class SpatialIndex<T> {
|
|
|
254
285
|
private _gen;
|
|
255
286
|
/** Number of items in the index. */
|
|
256
287
|
get size(): number;
|
|
257
|
-
/**
|
|
288
|
+
/**
|
|
289
|
+
* Insert an item with a bounding rect. Inserting an item already in the index
|
|
290
|
+
* replaces its bounds (upsert) — same as `update`.
|
|
291
|
+
*/
|
|
258
292
|
insert(item: T, bounds: Rect): void;
|
|
259
293
|
/** Remove an item. Returns false when the item was not present. */
|
|
260
294
|
remove(item: T): boolean;
|
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/raycast.ts","../src/collision-world.ts","../src/solid-grid.ts","../src/spatial-index.ts"],"mappings":";;;UAGiB;WACN;;WAEA;;WAEA;;;iBAIM;WACN;WACA;;;KAIC,QAAQ,YAAY;;wBAGhB,KAAK,eAAe,iBAAiB;;wBAKrC,YAAY,iBAAiB;;wBAK7B,YAAY,OAAO,OAAO,WAAW,YAAY;;wBAajD,gBAAgB,OAAO,OAAO,WAAW,WAAW,KAAK,OAAO;;wBAgBhE,aACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA;;;;;;;;;;;;;;;;;UA2Fe;;EAEf;EACA;;EAEA;;;wBAIc,iBAAiB;wBAIjB,YACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,YACA,KAAK,UACJ;;wBA0Ba,QACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,aACC;;UChNc;;EAEf,OAAO;;EAEP;;EAEA;;EAEA;;;wBAIc,eAAe,OAAO,OAAO,OAAO,QAAQ,KAAK,sBAAsB;;UCXtE;;EAEf;;EAEA;;EAEA,OAAO;;EAEP,QAAQ;;;UAIO;EACf;EACA,OAAO;EACP;EACA
|
|
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;;;iBAIM;WACN;WACA;;;KAIC,QAAQ,YAAY;;wBAGhB,KAAK,eAAe,iBAAiB;;wBAKrC,YAAY,iBAAiB;;wBAK7B,YAAY,OAAO,OAAO,WAAW,YAAY;;wBAajD,gBAAgB,OAAO,OAAO,WAAW,WAAW,KAAK,OAAO;;wBAgBhE,aACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA;;;;;;;;;;;;;;;;;UA2Fe;;EAEf;EACA;;EAEA;;;wBAIc,iBAAiB;wBAIjB,YACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,YACA,KAAK,UACJ;;wBA0Ba,QACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA,aACC;;UChNc;;EAEf,OAAO;;EAEP;;EAEA;;EAEA;;;wBAIc,eAAe,OAAO,OAAO,OAAO,QAAQ,KAAK,sBAAsB;;UCXtE;;EAEf;;EAEA;;EAEA,OAAO;;EAEP,QAAQ;;;UAIO;EACf;EACA,OAAO;EACP;EACA;;;;;;;;;;;;;;wBAuBc,QACd,YACA,YACA,YACA,YACA,iBACA,kBAAkB,kBACjB;;;;;;;;;wBAqCa,eACd,YACA,YACA,YACA,YACA,iBACA,gBACA,OAAO,OACP,WACA,WACA,KAAK,aACJ;;UCxFc;WACN;WACA;;;;;;qBAuCE;WACF,kBAAgB,OAAA;WAChB,gBAAc,OAAA;WACd,gBAAc,OAAA;WACd,eAAa,OAAA;UAEd;UACA;;;;;UAKA;UACA;UACA;;UAEA;;UAEA;;;;;EAMR,IACE,gBACA,OAAO,OACP,WACA,WACA,qBACA,gBACA;;;;;;;;;;;;EAsCF,YAAY,gBAAgB,UAAU,UAAU,WAAW;;EAK3D,OAAO;;EAmBP,YAAY,gBAAgB,WAAW;;MAUnC;;MAKA;;EAKJ;;;;;;;UAwDQ;;EAmBR,sBAAsB,gBAAgB,KAAK,OAAO;;EAQlD,kBAAkB,iBAAiB;;EAOnC;;UASQ;UACA;UACA;;;;;;;;;;;;;;;;;;;;;;;;;EA+BR,QACE,YACA,YACA,YACA,YACA,iBACA,gBACC;;UAyDK;;;;;;;UCjWO;WACN;WACA;;;;;;;;KASC;UAEK;;WAEN,UAAU;;;;;;;UAQJ;WACN;WACA;WACA;WACA,OAAO;WACP,SAAS;;;;;;;wBAQJ,eACd,eACA,gBACA,kBACA,gBAAgB,aAChB,UAAU,mBACT;;;;;;;;wBAqBa,cACd,MAAM,WACN,WACA,WACA,eACA;;;;;;;wBA+Bc,UAAU,MAAM,WAAW,aAAa;;;;;;wBAUxC,WAAW,MAAM,WAAW,aAAa,aAAa;;;;;;;wBAuBtD,iBACd,MAAM,WACN,WACA,WACA,YACA,YACA,eACA,eACA,KAAK,OACJ;;;;;;;;qBCrFU,aAAa;UAChB;UACA;UACA;UACA;;MAGJ;;;;;EAQJ,OAAO,MAAM,GAAG,QAAQ;;EAmBxB,OAAO,MAAM;;EAgBb,OAAO,MAAM,GAAG,QAAQ;;;;;EAgBxB,MAAM,QAAQ,MAAM,SAAS,MAAM;;EAgCnC;UAOQ;UAmBA"}
|
package/dist/index.js
CHANGED
|
@@ -11,8 +11,24 @@ function createCollider(shape, opts) {
|
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
13
|
// src/raycast.ts
|
|
14
|
+
/** Scratch record `raycast()` hands to `rayVsShapeInto` per candidate. */
|
|
15
|
+
const scratchHit = {
|
|
16
|
+
entity: -1,
|
|
17
|
+
distance: 0,
|
|
18
|
+
point: {
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0
|
|
21
|
+
},
|
|
22
|
+
normal: {
|
|
23
|
+
x: 0,
|
|
24
|
+
y: 0
|
|
25
|
+
}
|
|
26
|
+
};
|
|
14
27
|
/**
|
|
15
28
|
* Cast a ray against a list of targets. Returns the closest hit, or null.
|
|
29
|
+
*
|
|
30
|
+
* The returned hit is a fresh object — the caller may keep it; later calls do
|
|
31
|
+
* not overwrite it. Among targets at equal distance the first in `targets` wins.
|
|
16
32
|
* @param ox Ray origin X
|
|
17
33
|
* @param oy Ray origin Y
|
|
18
34
|
* @param dx Ray direction X (should be normalized)
|
|
@@ -21,47 +37,30 @@ function createCollider(shape, opts) {
|
|
|
21
37
|
* @param targets Array of entities to test against
|
|
22
38
|
*/
|
|
23
39
|
function raycast(ox, oy, dx, dy, maxDist, targets) {
|
|
24
|
-
let
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
return closest;
|
|
31
|
-
}
|
|
32
|
-
function rayVsShape(ox, oy, dx, dy, maxDist, target) {
|
|
33
|
-
if (target.shape.type === "aabb") return rayVsAABB(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.hw, target.shape.hh);
|
|
34
|
-
return rayVsCircle(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.radius);
|
|
35
|
-
}
|
|
36
|
-
function rayVsAABB(ox, oy, dx, dy, maxDist, entity, cx, cy, hw, hh) {
|
|
37
|
-
const minX = cx - hw;
|
|
38
|
-
const maxX = cx + hw;
|
|
39
|
-
const minY = cy - hh;
|
|
40
|
-
const maxY = cy + hh;
|
|
41
|
-
const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
|
|
42
|
-
const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
|
|
43
|
-
const tx1 = (minX - ox) * invDx;
|
|
44
|
-
const tx2 = (maxX - ox) * invDx;
|
|
45
|
-
const ty1 = (minY - oy) * invDy;
|
|
46
|
-
const ty2 = (maxY - oy) * invDy;
|
|
47
|
-
const tMinX = Math.min(tx1, tx2);
|
|
48
|
-
const tMaxX = Math.max(tx1, tx2);
|
|
49
|
-
const tMinY = Math.min(ty1, ty2);
|
|
50
|
-
const tMaxY = Math.max(ty1, ty2);
|
|
51
|
-
const tMin = Math.max(tMinX, tMinY);
|
|
52
|
-
const tMax = Math.min(tMaxX, tMaxY);
|
|
53
|
-
if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;
|
|
54
|
-
const t = tMin >= 0 ? tMin : tMax;
|
|
55
|
-
if (t > maxDist || t < 0) return null;
|
|
56
|
-
const px = ox + dx * t;
|
|
57
|
-
const py = oy + dy * t;
|
|
40
|
+
let found = false;
|
|
41
|
+
let entity = -1;
|
|
42
|
+
let distance = 0;
|
|
43
|
+
let px = 0;
|
|
44
|
+
let py = 0;
|
|
58
45
|
let nx = 0;
|
|
59
46
|
let ny = 0;
|
|
60
|
-
|
|
61
|
-
|
|
47
|
+
for (let i = 0; i < targets.length; i++) {
|
|
48
|
+
const t = targets[i];
|
|
49
|
+
const hit = rayVsShapeInto(ox, oy, dx, dy, maxDist, t.entity, t.shape, t.x, t.y, scratchHit);
|
|
50
|
+
if (hit !== null && (!found || hit.distance < distance)) {
|
|
51
|
+
found = true;
|
|
52
|
+
entity = hit.entity;
|
|
53
|
+
distance = hit.distance;
|
|
54
|
+
px = hit.point.x;
|
|
55
|
+
py = hit.point.y;
|
|
56
|
+
nx = hit.normal.x;
|
|
57
|
+
ny = hit.normal.y;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (!found) return null;
|
|
62
61
|
return {
|
|
63
62
|
entity,
|
|
64
|
-
distance
|
|
63
|
+
distance,
|
|
65
64
|
point: {
|
|
66
65
|
x: px,
|
|
67
66
|
y: py
|
|
@@ -72,64 +71,42 @@ function rayVsAABB(ox, oy, dx, dy, maxDist, entity, cx, cy, hw, hh) {
|
|
|
72
71
|
}
|
|
73
72
|
};
|
|
74
73
|
}
|
|
75
|
-
function rayVsCircle(ox, oy, dx, dy, maxDist, entity, cx, cy, radius) {
|
|
76
|
-
const fx = ox - cx;
|
|
77
|
-
const fy = oy - cy;
|
|
78
|
-
const a = dx * dx + dy * dy;
|
|
79
|
-
const b = 2 * (fx * dx + fy * dy);
|
|
80
|
-
const c = fx * fx + fy * fy - radius * radius;
|
|
81
|
-
if (a < 1e-12) return null;
|
|
82
|
-
const discriminant = b * b - 4 * a * c;
|
|
83
|
-
if (discriminant < 0) return null;
|
|
84
|
-
const sqrtDisc = Math.sqrt(discriminant);
|
|
85
|
-
let t = (-b - sqrtDisc) / (2 * a);
|
|
86
|
-
if (t < 0) t = (-b + sqrtDisc) / (2 * a);
|
|
87
|
-
if (t < 0 || t > maxDist) return null;
|
|
88
|
-
const px = ox + dx * t;
|
|
89
|
-
const py = oy + dy * t;
|
|
90
|
-
const nx = px - cx;
|
|
91
|
-
const ny = py - cy;
|
|
92
|
-
const len = Math.sqrt(nx * nx + ny * ny);
|
|
93
|
-
return {
|
|
94
|
-
entity,
|
|
95
|
-
distance: t,
|
|
96
|
-
point: {
|
|
97
|
-
x: px,
|
|
98
|
-
y: py
|
|
99
|
-
},
|
|
100
|
-
normal: len > 0 ? {
|
|
101
|
-
x: nx / len,
|
|
102
|
-
y: ny / len
|
|
103
|
-
} : {
|
|
104
|
-
x: 0,
|
|
105
|
-
y: 0
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
74
|
/**
|
|
110
75
|
* Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns
|
|
111
76
|
* `null` without touching `out`.
|
|
112
77
|
*
|
|
113
|
-
*
|
|
114
|
-
* `
|
|
115
|
-
*
|
|
116
|
-
* `CollisionWorld.raycast` can test a whole broadphase cell without allocating.
|
|
78
|
+
* This is the single implementation of the ray math. `raycast()` above and
|
|
79
|
+
* `CollisionWorld.raycast` both run every candidate through it into a scratch
|
|
80
|
+
* record, so neither allocates per candidate tested.
|
|
117
81
|
*/
|
|
118
82
|
function rayVsShapeInto(ox, oy, dx, dy, maxDist, entity, shape, x, y, out) {
|
|
119
83
|
if (shape.type === "aabb") return rayVsAABBInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.hw, shape.hh, out);
|
|
120
84
|
return rayVsCircleInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.radius, out);
|
|
121
85
|
}
|
|
122
86
|
function rayVsAABBInto(ox, oy, dx, dy, maxDist, entity, bx, by, hw, hh, out) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
87
|
+
if (dx === 0 && dy === 0) return null;
|
|
88
|
+
let tMinX = Number.NEGATIVE_INFINITY;
|
|
89
|
+
let tMaxX = Number.POSITIVE_INFINITY;
|
|
90
|
+
if (dx === 0) {
|
|
91
|
+
if (ox < bx - hw || ox > bx + hw) return null;
|
|
92
|
+
} else {
|
|
93
|
+
const invDx = 1 / dx;
|
|
94
|
+
const tx1 = (bx - hw - ox) * invDx;
|
|
95
|
+
const tx2 = (bx + hw - ox) * invDx;
|
|
96
|
+
tMinX = Math.min(tx1, tx2);
|
|
97
|
+
tMaxX = Math.max(tx1, tx2);
|
|
98
|
+
}
|
|
99
|
+
let tMinY = Number.NEGATIVE_INFINITY;
|
|
100
|
+
let tMaxY = Number.POSITIVE_INFINITY;
|
|
101
|
+
if (dy === 0) {
|
|
102
|
+
if (oy < by - hh || oy > by + hh) return null;
|
|
103
|
+
} else {
|
|
104
|
+
const invDy = 1 / dy;
|
|
105
|
+
const ty1 = (by - hh - oy) * invDy;
|
|
106
|
+
const ty2 = (by + hh - oy) * invDy;
|
|
107
|
+
tMinY = Math.min(ty1, ty2);
|
|
108
|
+
tMaxY = Math.max(ty1, ty2);
|
|
109
|
+
}
|
|
133
110
|
const tMin = Math.max(tMinX, tMinY);
|
|
134
111
|
const tMax = Math.min(tMaxX, tMaxY);
|
|
135
112
|
if (tMax < 0 || tMin > tMax) return null;
|
|
@@ -387,8 +364,15 @@ var SpatialIndex = class {
|
|
|
387
364
|
get size() {
|
|
388
365
|
return this._data.length;
|
|
389
366
|
}
|
|
390
|
-
/**
|
|
367
|
+
/**
|
|
368
|
+
* Insert an item with a bounding rect. Inserting an item already in the index
|
|
369
|
+
* replaces its bounds (upsert) — same as `update`.
|
|
370
|
+
*/
|
|
391
371
|
insert(item, bounds) {
|
|
372
|
+
if (this._slots.has(item)) {
|
|
373
|
+
this.update(item, bounds);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
392
376
|
const entry = {
|
|
393
377
|
item,
|
|
394
378
|
minX: bounds.x,
|
|
@@ -533,7 +517,10 @@ var CollisionWorld = class {
|
|
|
533
517
|
width: 0,
|
|
534
518
|
height: 0
|
|
535
519
|
};
|
|
536
|
-
/**
|
|
520
|
+
/**
|
|
521
|
+
* Add an entity with a shape. Positional twin of `addCollider`, and re-adding
|
|
522
|
+
* follows the same rules.
|
|
523
|
+
*/
|
|
537
524
|
add(entity, shape, x, y, isTrigger = false, layer = 1, mask = 4294967295) {
|
|
538
525
|
const entry = {
|
|
539
526
|
entity,
|
|
@@ -544,16 +531,35 @@ var CollisionWorld = class {
|
|
|
544
531
|
layer,
|
|
545
532
|
mask
|
|
546
533
|
};
|
|
547
|
-
const
|
|
534
|
+
const previous = this.entries.get(entity);
|
|
548
535
|
this.entries.set(entity, entry);
|
|
549
536
|
shapeBoundsInto(shape, x, y, this.boundsBuf);
|
|
550
|
-
if (
|
|
551
|
-
|
|
537
|
+
if (previous === void 0) {
|
|
538
|
+
this.spatial.insert(entity, this.boundsBuf);
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
this.spatial.update(entity, this.boundsBuf);
|
|
542
|
+
if (previous.isTrigger === isTrigger) return;
|
|
543
|
+
for (const key of this.activePairs) {
|
|
544
|
+
const lo = decodeLo(key);
|
|
545
|
+
const hi = decodeHi(key);
|
|
546
|
+
if (lo !== entity && hi !== entity) continue;
|
|
547
|
+
const other = this.entries.get(lo === entity ? hi : lo);
|
|
548
|
+
if (other?.isTrigger) continue;
|
|
549
|
+
this.activePairs.delete(key);
|
|
550
|
+
this.emitPair(lo, hi, previous, other, false);
|
|
551
|
+
}
|
|
552
552
|
}
|
|
553
553
|
/**
|
|
554
554
|
* Add an entity from a `Collider`. Its `shape`, `isTrigger`, `layer` and `mask`
|
|
555
555
|
* are copied at add time — later edits to the collider do not reach the world;
|
|
556
556
|
* re-add to change them. Re-adding an existing entity replaces its data.
|
|
557
|
+
*
|
|
558
|
+
* A re-add that flips `isTrigger` ends, during this call, every active pair
|
|
559
|
+
* whose kind that changes, with the exit signal of its old kind
|
|
560
|
+
* (`onTriggerExit` or `onCollisionEnd`). If the shapes still overlap, the next
|
|
561
|
+
* `step` starts the pair again with the enter signal of its new kind. A pair
|
|
562
|
+
* whose other side is a trigger stays a trigger pair and is left alone.
|
|
557
563
|
*/
|
|
558
564
|
addCollider(entity, collider, x, y) {
|
|
559
565
|
this.add(entity, collider.shape, x, y, collider.isTrigger, collider.layer, collider.mask);
|
|
@@ -570,13 +576,7 @@ var CollisionWorld = class {
|
|
|
570
576
|
if (lo !== entity && hi !== entity) continue;
|
|
571
577
|
this.activePairs.delete(key);
|
|
572
578
|
const otherEntity = lo === entity ? hi : lo;
|
|
573
|
-
|
|
574
|
-
const pair = {
|
|
575
|
-
entityA: lo,
|
|
576
|
-
entityB: hi
|
|
577
|
-
};
|
|
578
|
-
if (entry.isTrigger || otherEntry?.isTrigger) this.onTriggerExit.emit(pair);
|
|
579
|
-
else this.onCollisionEnd.emit(pair);
|
|
579
|
+
this.emitPair(lo, hi, entry, this.entries.get(otherEntity), false);
|
|
580
580
|
}
|
|
581
581
|
}
|
|
582
582
|
/** Update an entity's position. */
|
|
@@ -618,29 +618,32 @@ var CollisionWorld = class {
|
|
|
618
618
|
for (const key of newPairs) if (!this.activePairs.has(key)) {
|
|
619
619
|
const lo = decodeLo(key);
|
|
620
620
|
const hi = decodeHi(key);
|
|
621
|
-
|
|
622
|
-
const b = this.entries.get(hi);
|
|
623
|
-
const pair = {
|
|
624
|
-
entityA: lo,
|
|
625
|
-
entityB: hi
|
|
626
|
-
};
|
|
627
|
-
if (a?.isTrigger || b?.isTrigger) this.onTriggerEnter.emit(pair);
|
|
628
|
-
else this.onCollisionStart.emit(pair);
|
|
621
|
+
this.emitPair(lo, hi, this.entries.get(lo), this.entries.get(hi), true);
|
|
629
622
|
}
|
|
630
623
|
for (const key of this.activePairs) if (!newPairs.has(key)) {
|
|
631
624
|
const lo = decodeLo(key);
|
|
632
625
|
const hi = decodeHi(key);
|
|
633
|
-
|
|
634
|
-
const b = this.entries.get(hi);
|
|
635
|
-
const pair = {
|
|
636
|
-
entityA: lo,
|
|
637
|
-
entityB: hi
|
|
638
|
-
};
|
|
639
|
-
if (a?.isTrigger || b?.isTrigger) this.onTriggerExit.emit(pair);
|
|
640
|
-
else this.onCollisionEnd.emit(pair);
|
|
626
|
+
this.emitPair(lo, hi, this.entries.get(lo), this.entries.get(hi), false);
|
|
641
627
|
}
|
|
642
628
|
this.activePairs = newPairs;
|
|
643
629
|
}
|
|
630
|
+
/**
|
|
631
|
+
* The one place that decides trigger vs collision for a pair and emits its
|
|
632
|
+
* enter or exit signal. `a` and `b` are the pair's two entries in either
|
|
633
|
+
* order. They are passed in rather than looked up because in `remove` the
|
|
634
|
+
* removed entity is already gone from `entries`.
|
|
635
|
+
*/
|
|
636
|
+
emitPair(lo, hi, a, b, entering) {
|
|
637
|
+
const pair = {
|
|
638
|
+
entityA: lo,
|
|
639
|
+
entityB: hi
|
|
640
|
+
};
|
|
641
|
+
if (a?.isTrigger || b?.isTrigger) {
|
|
642
|
+
if (entering) this.onTriggerEnter.emit(pair);
|
|
643
|
+
else this.onTriggerExit.emit(pair);
|
|
644
|
+
} else if (entering) this.onCollisionStart.emit(pair);
|
|
645
|
+
else this.onCollisionEnd.emit(pair);
|
|
646
|
+
}
|
|
644
647
|
/** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */
|
|
645
648
|
getColliderBoundsInto(entity, out) {
|
|
646
649
|
const entry = this.entries.get(entity);
|
|
@@ -701,8 +704,8 @@ var CollisionWorld = class {
|
|
|
701
704
|
*
|
|
702
705
|
* The returned record is **reused between calls**: read it, or copy what you
|
|
703
706
|
* need, before casting again. That is what keeps a per-frame ray allocation-
|
|
704
|
-
* free
|
|
705
|
-
*
|
|
707
|
+
* free; the standalone helper by contrast returns a fresh hit from each call
|
|
708
|
+
* that hits, which the caller may keep.
|
|
706
709
|
*
|
|
707
710
|
* `maxDist` must be finite — this requires a bounded ray, not the
|
|
708
711
|
* `Number.POSITIVE_INFINITY` "unbounded ray" idiom some engines accept. The
|
|
@@ -803,6 +806,29 @@ function isBlockedAABB(grid, x, y, halfW, halfH) {
|
|
|
803
806
|
return false;
|
|
804
807
|
}
|
|
805
808
|
/**
|
|
809
|
+
* Whether the cell at (`col`, `row`) is solid. Coordinates that name no cell
|
|
810
|
+
* (out of range, or not an integer) follow the grid's own `outside` rule, like
|
|
811
|
+
* `isBlockedAABB` at the map edge: solid on an `outside: 'solid'` grid, empty
|
|
812
|
+
* on an `'open'` one. Zero-alloc.
|
|
813
|
+
*/
|
|
814
|
+
function isSolidAt(grid, col, row) {
|
|
815
|
+
if (!isCell(grid, col, row)) return grid.outside === "solid";
|
|
816
|
+
return grid.solid[row * grid.width + col] !== 0;
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Mark the cell at (`col`, `row`) solid or empty, for a door or a breakable
|
|
820
|
+
* wall. Returns `true` when the cell was written, `false` (and writes nothing)
|
|
821
|
+
* when the coordinates are out of range or not integers. Zero-alloc.
|
|
822
|
+
*/
|
|
823
|
+
function setSolidAt(grid, col, row, solid) {
|
|
824
|
+
if (!isCell(grid, col, row)) return false;
|
|
825
|
+
grid.solid[row * grid.width + col] = solid ? 1 : 0;
|
|
826
|
+
return true;
|
|
827
|
+
}
|
|
828
|
+
function isCell(grid, col, row) {
|
|
829
|
+
return Number.isInteger(col) && Number.isInteger(row) && col >= 0 && col < grid.width && row >= 0 && row < grid.height;
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
806
832
|
* Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis
|
|
807
833
|
* is applied independently so a blocked diagonal input slides along the wall.
|
|
808
834
|
* The y-check uses the already-updated x position.
|
|
@@ -825,11 +851,13 @@ exports.buildSolidGrid = buildSolidGrid;
|
|
|
825
851
|
exports.createCollider = createCollider;
|
|
826
852
|
exports.createOverlap = createOverlap;
|
|
827
853
|
exports.isBlockedAABB = isBlockedAABB;
|
|
854
|
+
exports.isSolidAt = isSolidAt;
|
|
828
855
|
exports.moveAndSlideInto = moveAndSlideInto;
|
|
829
856
|
exports.overlap = overlap;
|
|
830
857
|
exports.overlapInto = overlapInto;
|
|
831
858
|
exports.rayVsShapeInto = rayVsShapeInto;
|
|
832
859
|
exports.raycast = raycast;
|
|
860
|
+
exports.setSolidAt = setSolidAt;
|
|
833
861
|
exports.shapeBounds = shapeBounds;
|
|
834
862
|
exports.shapeBoundsInto = shapeBoundsInto;
|
|
835
863
|
exports.shapeOverlap = shapeOverlap;
|