@flare-engine/physics 0.4.6 → 0.4.8

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/dist/index.d.mts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { Signal } from "@flare-engine/events";
2
2
  import { Rect, Vec2 } from "@flare-engine/math";
3
- //#region src/shape.d.ts
4
3
  /** AABB shape — axis-aligned bounding box. */
5
4
  interface AABBShape {
6
5
  readonly type: 'aabb';
@@ -10,22 +9,22 @@ interface AABBShape {
10
9
  readonly hh: number;
11
10
  }
12
11
  /** Circle shape. */
13
- interface CircleShape {
12
+ export interface CircleShape {
14
13
  readonly type: 'circle';
15
14
  readonly radius: number;
16
15
  }
17
16
  /** Union of supported physics shapes. */
18
17
  type Shape = AABBShape | CircleShape;
19
18
  /** Create an AABB shape from width and height. */
20
- declare function AABB(width: number, height: number): AABBShape;
19
+ export declare function AABB(width: number, height: number): AABBShape;
21
20
  /** Create a circle shape from radius. */
22
- declare function CircleShape(radius: number): CircleShape;
21
+ export declare function CircleShape(radius: number): CircleShape;
23
22
  /** Compute the bounding rect of a shape at a given position. */
24
- declare function shapeBounds(shape: Shape, x: number, y: number): Rect;
23
+ export declare function shapeBounds(shape: Shape, x: number, y: number): Rect;
25
24
  /** Zero-alloc variant — writes the shape bounds into the provided output rect. */
26
- declare function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect;
25
+ export declare function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect;
27
26
  /** Test overlap between two positioned shapes. Returns true if overlapping. */
28
- declare function shapeOverlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): boolean;
27
+ export declare function shapeOverlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): boolean;
29
28
  /**
30
29
  * Minimum translation vector: the shortest push that separates two overlapping
31
30
  * shapes.
@@ -50,12 +49,10 @@ interface Overlap {
50
49
  depth: number;
51
50
  }
52
51
  /** 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;
52
+ export declare function createOverlap(): Overlap;
53
+ export declare function overlapInto(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number, out: Overlap): Overlap | null;
55
54
  /** 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;
57
- //#endregion
58
- //#region src/collider.d.ts
55
+ export declare function overlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): Overlap | null;
59
56
  /** Collider component data attached to an ECS entity. */
60
57
  interface Collider {
61
58
  /** The collision shape. */
@@ -68,9 +65,7 @@ interface Collider {
68
65
  mask: number;
69
66
  }
70
67
  /** Create a collider with default settings. */
71
- declare function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider;
72
- //#endregion
73
- //#region src/raycast.d.ts
68
+ export declare function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider;
74
69
  /** Result of a raycast hit. */
75
70
  interface RaycastHit {
76
71
  /** The entity that was hit. */
@@ -98,7 +93,7 @@ interface RaycastTarget {
98
93
  * @param maxDist Maximum raycast distance
99
94
  * @param targets Array of entities to test against
100
95
  */
101
- declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
96
+ export declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
102
97
  /**
103
98
  * Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns
104
99
  * `null` without touching `out`.
@@ -108,9 +103,7 @@ declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist
108
103
  * closest. That is three objects per candidate per ray. This variant exists so
109
104
  * `CollisionWorld.raycast` can test a whole broadphase cell without allocating.
110
105
  */
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
113
- //#region src/collision-world.d.ts
106
+ 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;
114
107
  /** Collision pair data emitted by collision signals. */
115
108
  interface CollisionPair {
116
109
  readonly entityA: number;
@@ -120,7 +113,7 @@ interface CollisionPair {
120
113
  * AABB collision world. Manages entities with shapes, detects overlaps each step,
121
114
  * and emits collision/trigger signals.
122
115
  */
123
- declare class CollisionWorld {
116
+ export declare class CollisionWorld {
124
117
  readonly onCollisionStart: Signal<CollisionPair>;
125
118
  readonly onCollisionEnd: Signal<CollisionPair>;
126
119
  readonly onTriggerEnter: Signal<CollisionPair>;
@@ -194,8 +187,6 @@ declare class CollisionWorld {
194
187
  /** Scratch record handed to `rayVsShapeInto` per candidate. */
195
188
  private scratchHit;
196
189
  }
197
- //#endregion
198
- //#region src/solid-grid.d.ts
199
190
  /**
200
191
  * Minimal structural type for a solid tile position in grid coordinates.
201
192
  * `TileCollider[]` from `@flare-engine/tilemap` is structurally assignable here
@@ -221,22 +212,20 @@ interface SolidGrid {
221
212
  * Input is structurally typed (`readonly SolidTile[]`) so the caller does not
222
213
  * need to import `@flare-engine/tilemap` — physics stays tilemap-free.
223
214
  */
224
- declare function buildSolidGrid(width: number, height: number, tileSize: number, tiles: readonly SolidTile[]): SolidGrid;
215
+ export declare function buildSolidGrid(width: number, height: number, tileSize: number, tiles: readonly SolidTile[]): SolidGrid;
225
216
  /**
226
217
  * Returns `true` when an axis-aligned box centered at (`x`, `y`) with
227
218
  * half-extents (`halfW`, `halfH`) overlaps a solid tile or lies outside the
228
219
  * map boundary. Uses half-open EDGE semantics on max edges.
229
220
  */
230
- declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: number, halfH: number): boolean;
221
+ export declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: number, halfH: number): boolean;
231
222
  /**
232
223
  * Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis
233
224
  * is applied independently so a blocked diagonal input slides along the wall.
234
225
  * The y-check uses the already-updated x position.
235
226
  * Zero-alloc: writes result into the provided `out` Vec2 and returns it.
236
227
  */
237
- declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: number, dy: number, halfW: number, halfH: number, out: Vec2): Vec2;
238
- //#endregion
239
- //#region src/spatial-index.d.ts
228
+ export declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: number, dy: number, halfW: number, halfH: number, out: Vec2): Vec2;
240
229
  /**
241
230
  * Uniform spatial-hash broadphase.
242
231
  * `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.
@@ -244,7 +233,7 @@ declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: num
244
233
  * Zero-alloc in the query hot-path: duplicate suppression uses a monotone stamp counter,
245
234
  * not a per-call Set.
246
235
  */
247
- declare class SpatialIndex<T> {
236
+ export declare class SpatialIndex<T> {
248
237
  private _data;
249
238
  private _slots;
250
239
  private _cells;
@@ -267,6 +256,5 @@ declare class SpatialIndex<T> {
267
256
  private _link;
268
257
  private _unlink;
269
258
  }
270
- //#endregion
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 };
259
+ export type { AABBShape, CircleShape as CircleShapeType, Collider, CollisionPair, Overlap, RaycastHit, RaycastTarget, Shape, SolidGrid, SolidTile };
272
260
  //# sourceMappingURL=index.d.mts.map
@@ -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;;;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"}
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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 -1,6 +1,5 @@
1
1
  import { Rect, Vec2 } from "@flare-engine/math";
2
2
  import { Signal } from "@flare-engine/events";
3
- //#region src/shape.d.ts
4
3
  /** AABB shape — axis-aligned bounding box. */
5
4
  interface AABBShape {
6
5
  readonly type: 'aabb';
@@ -10,22 +9,22 @@ interface AABBShape {
10
9
  readonly hh: number;
11
10
  }
12
11
  /** Circle shape. */
13
- interface CircleShape {
12
+ export interface CircleShape {
14
13
  readonly type: 'circle';
15
14
  readonly radius: number;
16
15
  }
17
16
  /** Union of supported physics shapes. */
18
17
  type Shape = AABBShape | CircleShape;
19
18
  /** Create an AABB shape from width and height. */
20
- declare function AABB(width: number, height: number): AABBShape;
19
+ export declare function AABB(width: number, height: number): AABBShape;
21
20
  /** Create a circle shape from radius. */
22
- declare function CircleShape(radius: number): CircleShape;
21
+ export declare function CircleShape(radius: number): CircleShape;
23
22
  /** Compute the bounding rect of a shape at a given position. */
24
- declare function shapeBounds(shape: Shape, x: number, y: number): Rect;
23
+ export declare function shapeBounds(shape: Shape, x: number, y: number): Rect;
25
24
  /** Zero-alloc variant — writes the shape bounds into the provided output rect. */
26
- declare function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect;
25
+ export declare function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect;
27
26
  /** Test overlap between two positioned shapes. Returns true if overlapping. */
28
- declare function shapeOverlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): boolean;
27
+ export declare function shapeOverlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): boolean;
29
28
  /**
30
29
  * Minimum translation vector: the shortest push that separates two overlapping
31
30
  * shapes.
@@ -50,12 +49,10 @@ interface Overlap {
50
49
  depth: number;
51
50
  }
52
51
  /** 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;
52
+ export declare function createOverlap(): Overlap;
53
+ export declare function overlapInto(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number, out: Overlap): Overlap | null;
55
54
  /** 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;
57
- //#endregion
58
- //#region src/collider.d.ts
55
+ export declare function overlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): Overlap | null;
59
56
  /** Collider component data attached to an ECS entity. */
60
57
  interface Collider {
61
58
  /** The collision shape. */
@@ -68,9 +65,7 @@ interface Collider {
68
65
  mask: number;
69
66
  }
70
67
  /** Create a collider with default settings. */
71
- declare function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider;
72
- //#endregion
73
- //#region src/raycast.d.ts
68
+ export declare function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider;
74
69
  /** Result of a raycast hit. */
75
70
  interface RaycastHit {
76
71
  /** The entity that was hit. */
@@ -98,7 +93,7 @@ interface RaycastTarget {
98
93
  * @param maxDist Maximum raycast distance
99
94
  * @param targets Array of entities to test against
100
95
  */
101
- declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
96
+ export declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
102
97
  /**
103
98
  * Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns
104
99
  * `null` without touching `out`.
@@ -108,9 +103,7 @@ declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist
108
103
  * closest. That is three objects per candidate per ray. This variant exists so
109
104
  * `CollisionWorld.raycast` can test a whole broadphase cell without allocating.
110
105
  */
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
113
- //#region src/collision-world.d.ts
106
+ 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;
114
107
  /** Collision pair data emitted by collision signals. */
115
108
  interface CollisionPair {
116
109
  readonly entityA: number;
@@ -120,7 +113,7 @@ interface CollisionPair {
120
113
  * AABB collision world. Manages entities with shapes, detects overlaps each step,
121
114
  * and emits collision/trigger signals.
122
115
  */
123
- declare class CollisionWorld {
116
+ export declare class CollisionWorld {
124
117
  readonly onCollisionStart: Signal<CollisionPair>;
125
118
  readonly onCollisionEnd: Signal<CollisionPair>;
126
119
  readonly onTriggerEnter: Signal<CollisionPair>;
@@ -194,8 +187,6 @@ declare class CollisionWorld {
194
187
  /** Scratch record handed to `rayVsShapeInto` per candidate. */
195
188
  private scratchHit;
196
189
  }
197
- //#endregion
198
- //#region src/solid-grid.d.ts
199
190
  /**
200
191
  * Minimal structural type for a solid tile position in grid coordinates.
201
192
  * `TileCollider[]` from `@flare-engine/tilemap` is structurally assignable here
@@ -221,22 +212,20 @@ interface SolidGrid {
221
212
  * Input is structurally typed (`readonly SolidTile[]`) so the caller does not
222
213
  * need to import `@flare-engine/tilemap` — physics stays tilemap-free.
223
214
  */
224
- declare function buildSolidGrid(width: number, height: number, tileSize: number, tiles: readonly SolidTile[]): SolidGrid;
215
+ export declare function buildSolidGrid(width: number, height: number, tileSize: number, tiles: readonly SolidTile[]): SolidGrid;
225
216
  /**
226
217
  * Returns `true` when an axis-aligned box centered at (`x`, `y`) with
227
218
  * half-extents (`halfW`, `halfH`) overlaps a solid tile or lies outside the
228
219
  * map boundary. Uses half-open EDGE semantics on max edges.
229
220
  */
230
- declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: number, halfH: number): boolean;
221
+ export declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: number, halfH: number): boolean;
231
222
  /**
232
223
  * Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis
233
224
  * is applied independently so a blocked diagonal input slides along the wall.
234
225
  * The y-check uses the already-updated x position.
235
226
  * Zero-alloc: writes result into the provided `out` Vec2 and returns it.
236
227
  */
237
- declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: number, dy: number, halfW: number, halfH: number, out: Vec2): Vec2;
238
- //#endregion
239
- //#region src/spatial-index.d.ts
228
+ export declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: number, dy: number, halfW: number, halfH: number, out: Vec2): Vec2;
240
229
  /**
241
230
  * Uniform spatial-hash broadphase.
242
231
  * `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.
@@ -244,7 +233,7 @@ declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: num
244
233
  * Zero-alloc in the query hot-path: duplicate suppression uses a monotone stamp counter,
245
234
  * not a per-call Set.
246
235
  */
247
- declare class SpatialIndex<T> {
236
+ export declare class SpatialIndex<T> {
248
237
  private _data;
249
238
  private _slots;
250
239
  private _cells;
@@ -267,6 +256,5 @@ declare class SpatialIndex<T> {
267
256
  private _link;
268
257
  private _unlink;
269
258
  }
270
- //#endregion
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 };
259
+ export type { AABBShape, CircleShape as CircleShapeType, Collider, CollisionPair, Overlap, RaycastHit, RaycastTarget, Shape, SolidGrid, SolidTile };
272
260
  //# sourceMappingURL=index.d.ts.map
@@ -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;;;UAIM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BACA,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"}
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;;;;;;;;;;;wBAYc,QACd,YACA,YACA,YACA,YACA,iBACA,kBAAkB,kBACjB;;;;;;;;;;wBAkKa,eACd,YACA,YACA,YACA,YACA,iBACA,gBACA,OAAO,OACP,WACA,WACA,KAAK,aACJ;;UC1Mc;WACN;WACA;;;;;;qBAuCE;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;;;;;;;wBAQF,eACd,eACA,gBACA,kBACA,gBAAgB,cACf;;;;;;wBAmBa,cACd,MAAM,WACN,WACA,WACA,eACA;;;;;;;wBA0Bc,iBACd,MAAM,WACN,WACA,WACA,YACA,YACA,eACA,eACA,KAAK,OACJ;;;;;;;;qBC9BU,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.js CHANGED
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  let _flare_engine_events = require("@flare-engine/events");
3
- //#region src/collider.ts
3
+ // src/collider.ts
4
4
  /** Create a collider with default settings. */
5
5
  function createCollider(shape, opts) {
6
6
  return {
@@ -10,8 +10,7 @@ function createCollider(shape, opts) {
10
10
  mask: opts?.mask ?? 4294967295
11
11
  };
12
12
  }
13
- //#endregion
14
- //#region src/raycast.ts
13
+ // src/raycast.ts
15
14
  /**
16
15
  * Cast a ray against a list of targets. Returns the closest hit, or null.
17
16
  * @param ox Ray origin X
@@ -175,8 +174,7 @@ function rayVsCircleInto(ox, oy, dx, dy, maxDist, entity, cx, cy, radius, out) {
175
174
  out.normal.y = len > 0 ? ny / len : 0;
176
175
  return out;
177
176
  }
178
- //#endregion
179
- //#region src/shape.ts
177
+ // src/shape.ts
180
178
  /** Create an AABB shape from width and height. */
181
179
  function AABB(width, height) {
182
180
  return {
@@ -353,8 +351,7 @@ function aabbCircleOverlap(ax, ay, ahw, ahh, cx, cy, cr, out, flip) {
353
351
  out.depth = depth + cr;
354
352
  return out;
355
353
  }
356
- //#endregion
357
- //#region src/spatial-index.ts
354
+ // src/spatial-index.ts
358
355
  const CELL = 64;
359
356
  const KEY_RANGE = 65536;
360
357
  const KEY_OFFSET = 32768;
@@ -368,7 +365,7 @@ function _cMax(v) {
368
365
  return Math.ceil(v / CELL) - 1;
369
366
  }
370
367
  const MIN_CELL = -32768;
371
- const MAX_CELL = KEY_OFFSET - 1;
368
+ const MAX_CELL = 32767;
372
369
  function _clampCell(c) {
373
370
  if (c > MAX_CELL) return MAX_CELL;
374
371
  if (c < MIN_CELL) return MIN_CELL;
@@ -494,8 +491,7 @@ var SpatialIndex = class {
494
491
  }
495
492
  }
496
493
  };
497
- //#endregion
498
- //#region src/collision-world.ts
494
+ // src/collision-world.ts
499
495
  const ENTITY_SHIFT = 2 ** 26;
500
496
  /** Padding applied to a ray's query AABB — see `CollisionWorld.raycast`. */
501
497
  const RAY_BOUNDS_PAD = 1e-6;
@@ -761,8 +757,7 @@ var CollisionWorld = class {
761
757
  }
762
758
  };
763
759
  };
764
- //#endregion
765
- //#region src/solid-grid.ts
760
+ // src/solid-grid.ts
766
761
  /**
767
762
  * Build a `SolidGrid` from a list of solid tile positions.
768
763
  * Input is structurally typed (`readonly SolidTile[]`) so the caller does not
@@ -813,7 +808,6 @@ function moveAndSlideInto(grid, x, y, dx, dy, halfW, halfH, out) {
813
808
  out.y = ny;
814
809
  return out;
815
810
  }
816
- //#endregion
817
811
  exports.AABB = AABB;
818
812
  exports.CircleShape = CircleShape;
819
813
  exports.CollisionWorld = CollisionWorld;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["Signal"],"sources":["../src/collider.ts","../src/raycast.ts","../src/shape.ts","../src/spatial-index.ts","../src/collision-world.ts","../src/solid-grid.ts"],"sourcesContent":["import type { Shape } from './shape.js';\n\n/** Collider component data attached to an ECS entity. */\nexport interface Collider {\n /** The collision shape. */\n shape: Shape;\n /** If true, generates trigger events but no physical response. */\n isTrigger: boolean;\n /** Collision layer bitmask — which layer this collider belongs to. */\n layer: number;\n /** Collision mask — which layers this collider can interact with. */\n mask: number;\n}\n\n/** Create a collider with default settings. */\nexport function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider {\n return {\n shape,\n isTrigger: opts?.isTrigger ?? false,\n layer: opts?.layer ?? 1,\n mask: opts?.mask ?? 0xffffffff,\n };\n}\n","import type { Vec2 } from '@flare-engine/math';\nimport type { Shape } from './shape.js';\n\n/** Result of a raycast hit. */\nexport interface RaycastHit {\n /** The entity that was hit. */\n entity: number;\n /** Distance from ray origin to hit point. */\n distance: number;\n /** World-space hit point. */\n point: Vec2;\n /** Surface normal at hit point. */\n normal: Vec2;\n}\n\n/** Entity entry for raycast — position + shape. */\nexport interface RaycastTarget {\n entity: number;\n shape: Shape;\n x: number;\n y: number;\n}\n\n/**\n * Cast a ray against a list of targets. Returns the closest hit, or null.\n * @param ox Ray origin X\n * @param oy Ray origin Y\n * @param dx Ray direction X (should be normalized)\n * @param dy Ray direction Y (should be normalized)\n * @param maxDist Maximum raycast distance\n * @param targets Array of entities to test against\n */\nexport function raycast(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n targets: readonly RaycastTarget[],\n): RaycastHit | null {\n let closest: RaycastHit | null = null;\n\n for (let i = 0; i < targets.length; i++) {\n const t = targets[i]!;\n const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);\n if (hit && (closest === null || hit.distance < closest.distance)) {\n closest = hit;\n }\n }\n\n return closest;\n}\n\nfunction rayVsShape(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n target: RaycastTarget,\n): RaycastHit | null {\n if (target.shape.type === 'aabb') {\n return rayVsAABB(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n target.entity,\n target.x,\n target.y,\n target.shape.hw,\n target.shape.hh,\n );\n }\n return rayVsCircle(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n target.entity,\n target.x,\n target.y,\n target.shape.radius,\n );\n}\n\nfunction rayVsAABB(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n hw: number,\n hh: number,\n): RaycastHit | null {\n const minX = cx - hw;\n const maxX = cx + hw;\n const minY = cy - hh;\n const maxY = cy + hh;\n\n const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;\n const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;\n\n const tx1 = (minX - ox) * invDx;\n const tx2 = (maxX - ox) * invDx;\n const ty1 = (minY - oy) * invDy;\n const ty2 = (maxY - oy) * invDy;\n\n const tMinX = Math.min(tx1, tx2);\n const tMaxX = Math.max(tx1, tx2);\n const tMinY = Math.min(ty1, ty2);\n const tMaxY = Math.max(ty1, ty2);\n\n const tMin = Math.max(tMinX, tMinY);\n const tMax = Math.min(tMaxX, tMaxY);\n\n if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;\n\n const t = tMin >= 0 ? tMin : tMax;\n if (t > maxDist || t < 0) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n\n // Compute normal\n let nx = 0;\n let ny = 0;\n if (tMinX > tMinY) {\n nx = dx < 0 ? 1 : -1;\n } else {\n ny = dy < 0 ? 1 : -1;\n }\n\n return {\n entity,\n distance: t,\n point: { x: px, y: py },\n normal: { x: nx, y: ny },\n };\n}\n\nfunction rayVsCircle(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n radius: number,\n): RaycastHit | null {\n const fx = ox - cx;\n const fy = oy - cy;\n\n const a = dx * dx + dy * dy;\n const b = 2 * (fx * dx + fy * dy);\n const c = fx * fx + fy * fy - radius * radius;\n\n if (a < 1e-12) return null; // Zero-length direction\n\n const discriminant = b * b - 4 * a * c;\n if (discriminant < 0) return null;\n\n const sqrtDisc = Math.sqrt(discriminant);\n let t = (-b - sqrtDisc) / (2 * a);\n\n // If entry point is behind, try exit point\n if (t < 0) {\n t = (-b + sqrtDisc) / (2 * a);\n }\n if (t < 0 || t > maxDist) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n const nx = px - cx;\n const ny = py - cy;\n const len = Math.sqrt(nx * nx + ny * ny);\n\n return {\n entity,\n distance: t,\n point: { x: px, y: py },\n normal: len > 0 ? { x: nx / len, y: ny / len } : { x: 0, y: 0 },\n };\n}\n\n/**\n * Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns\n * `null` without touching `out`.\n *\n * The allocating `raycast()` above builds a fresh `RaycastHit` — with two nested\n * `Vec2` objects — for **every candidate it tests**, then discards all but the\n * closest. That is three objects per candidate per ray. This variant exists so\n * `CollisionWorld.raycast` can test a whole broadphase cell without allocating.\n */\nexport function rayVsShapeInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n shape: Shape,\n x: number,\n y: number,\n out: RaycastHit,\n): RaycastHit | null {\n if (shape.type === 'aabb') {\n return rayVsAABBInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.hw, shape.hh, out);\n }\n return rayVsCircleInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.radius, out);\n}\n\nfunction rayVsAABBInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n bx: number,\n by: number,\n hw: number,\n hh: number,\n out: RaycastHit,\n): RaycastHit | null {\n const invDx = dx !== 0 ? 1 / dx : Number.POSITIVE_INFINITY;\n const invDy = dy !== 0 ? 1 / dy : Number.POSITIVE_INFINITY;\n\n const tx1 = (bx - hw - ox) * invDx;\n const tx2 = (bx + hw - ox) * invDx;\n const ty1 = (by - hh - oy) * invDy;\n const ty2 = (by + hh - oy) * invDy;\n\n const tMinX = Math.min(tx1, tx2);\n const tMaxX = Math.max(tx1, tx2);\n const tMinY = Math.min(ty1, ty2);\n const tMaxY = Math.max(ty1, ty2);\n\n const tMin = Math.max(tMinX, tMinY);\n const tMax = Math.min(tMaxX, tMaxY);\n if (tMax < 0 || tMin > tMax) return null;\n\n const t = tMin < 0 ? tMax : tMin;\n if (t > maxDist || t < 0) return null;\n\n out.entity = entity;\n out.distance = t;\n out.point.x = ox + dx * t;\n out.point.y = oy + dy * t;\n if (tMinX > tMinY) {\n out.normal.x = dx < 0 ? 1 : -1;\n out.normal.y = 0;\n } else {\n out.normal.x = 0;\n out.normal.y = dy < 0 ? 1 : -1;\n }\n return out;\n}\n\nfunction rayVsCircleInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n radius: number,\n out: RaycastHit,\n): RaycastHit | null {\n const fx = ox - cx;\n const fy = oy - cy;\n const a = dx * dx + dy * dy;\n if (a < 1e-12) return null;\n\n const b = 2 * (fx * dx + fy * dy);\n const c = fx * fx + fy * fy - radius * radius;\n const discriminant = b * b - 4 * a * c;\n if (discriminant < 0) return null;\n\n const sqrtDisc = Math.sqrt(discriminant);\n let t = (-b - sqrtDisc) / (2 * a);\n if (t < 0) t = (-b + sqrtDisc) / (2 * a);\n if (t < 0 || t > maxDist) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n const nx = px - cx;\n const ny = py - cy;\n const len = Math.sqrt(nx * nx + ny * ny);\n\n out.entity = entity;\n out.distance = t;\n out.point.x = px;\n out.point.y = py;\n out.normal.x = len > 0 ? nx / len : 0;\n out.normal.y = len > 0 ? ny / len : 0;\n return out;\n}\n","import type { Rect } from '@flare-engine/math';\n\n/** AABB shape — axis-aligned bounding box. */\nexport interface AABBShape {\n readonly type: 'aabb';\n /** Half-width. */\n readonly hw: number;\n /** Half-height. */\n readonly hh: number;\n}\n\n/** Circle shape. */\nexport interface CircleShape {\n readonly type: 'circle';\n readonly radius: number;\n}\n\n/** Union of supported physics shapes. */\nexport type Shape = AABBShape | CircleShape;\n\n/** Create an AABB shape from width and height. */\nexport function AABB(width: number, height: number): AABBShape {\n return { type: 'aabb', hw: width * 0.5, hh: height * 0.5 };\n}\n\n/** Create a circle shape from radius. */\nexport function CircleShape(radius: number): CircleShape {\n return { type: 'circle', radius };\n}\n\n/** Compute the bounding rect of a shape at a given position. */\nexport function shapeBounds(shape: Shape, x: number, y: number): Rect {\n if (shape.type === 'aabb') {\n return { x: x - shape.hw, y: y - shape.hh, width: shape.hw * 2, height: shape.hh * 2 };\n }\n return {\n x: x - shape.radius,\n y: y - shape.radius,\n width: shape.radius * 2,\n height: shape.radius * 2,\n };\n}\n\n/** Zero-alloc variant — writes the shape bounds into the provided output rect. */\nexport function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect {\n if (shape.type === 'aabb') {\n out.x = x - shape.hw;\n out.y = y - shape.hh;\n out.width = shape.hw * 2;\n out.height = shape.hh * 2;\n return out;\n }\n out.x = x - shape.radius;\n out.y = y - shape.radius;\n out.width = shape.radius * 2;\n out.height = shape.radius * 2;\n return out;\n}\n\n/** Test overlap between two positioned shapes. Returns true if overlapping. */\nexport function shapeOverlap(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n): boolean {\n if (shapeA.type === 'aabb' && shapeB.type === 'aabb') {\n return aabbVsAabb(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh);\n }\n if (shapeA.type === 'circle' && shapeB.type === 'circle') {\n return circleVsCircle(ax, ay, shapeA.radius, bx, by, shapeB.radius);\n }\n // Mixed: one AABB, one circle\n if (shapeA.type === 'aabb' && shapeB.type === 'circle') {\n return aabbVsCircle(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius);\n }\n return aabbVsCircle(\n bx,\n by,\n (shapeB as AABBShape).hw,\n (shapeB as AABBShape).hh,\n ax,\n ay,\n (shapeA as CircleShape).radius,\n );\n}\n\nfunction aabbVsAabb(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n bx: number,\n by: number,\n bhw: number,\n bhh: number,\n): boolean {\n return Math.abs(ax - bx) < ahw + bhw && Math.abs(ay - by) < ahh + bhh;\n}\n\nfunction circleVsCircle(\n ax: number,\n ay: number,\n ar: number,\n bx: number,\n by: number,\n br: number,\n): boolean {\n const dx = bx - ax;\n const dy = by - ay;\n const radSum = ar + br;\n return dx * dx + dy * dy < radSum * radSum;\n}\n\nfunction aabbVsCircle(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n cx: number,\n cy: number,\n cr: number,\n): boolean {\n const left = ax - ahw;\n const right = ax + ahw;\n const top = ay - ahh;\n const bottom = ay + ahh;\n const closestX = cx < left ? left : cx > right ? right : cx;\n const closestY = cy < top ? top : cy > bottom ? bottom : cy;\n const dx = cx - closestX;\n const dy = cy - closestY;\n const distSq = dx * dx + dy * dy;\n // Strict, like aabbVsAabb and circleVsCircle: exactly-touching shapes do not\n // collide. A centre on or inside the box collides when the radius is positive;\n // a zero radius counts only strictly inside, as a zero-size box does.\n if (distSq > 0) return distSq < cr * cr;\n return cr > 0 || (cx > left && cx < right && cy > top && cy < bottom);\n}\n\n/**\n * Minimum translation vector: the shortest push that separates two overlapping\n * shapes.\n *\n * `shapeOverlap` answers *whether* two shapes collide and nothing more, which\n * leaves a detection-only consumer with no way to resolve the collision. This is\n * that missing step: `out.x`/`out.y` are a unit axis and `out.depth` is how far\n * along it `b` must move to just stop touching `a`.\n *\n * Returns `null` when the shapes are not overlapping, leaving `out` untouched so\n * a caller may reuse one buffer across many pairs.\n *\n * The degenerate concentric case (identical centres) has no meaningful axis;\n * `(1, 0)` is returned so the caller still gets a usable separation instead of\n * `NaN` from normalising a zero vector.\n */\nexport interface Overlap {\n /** Unit separation axis, pointing from `a` toward `b`. */\n x: number;\n y: number;\n /** Penetration depth along that axis. */\n depth: number;\n}\n\n/** A fresh, zeroed overlap record. Allocate once outside the loop and reuse. */\nexport function createOverlap(): Overlap {\n return { x: 0, y: 0, depth: 0 };\n}\n\nexport function overlapInto(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n out: Overlap,\n): Overlap | null {\n if (shapeA.type === 'aabb' && shapeB.type === 'aabb') {\n return aabbAabbOverlap(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh, out);\n }\n if (shapeA.type === 'circle' && shapeB.type === 'circle') {\n return circleCircleOverlap(ax, ay, shapeA.radius, bx, by, shapeB.radius, out);\n }\n if (shapeA.type === 'aabb' && shapeB.type === 'circle') {\n return aabbCircleOverlap(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius, out, false);\n }\n // Circle vs AABB — solve as AABB vs circle, then flip the axis so it still\n // points from `a` to `b`.\n return aabbCircleOverlap(\n bx,\n by,\n (shapeB as AABBShape).hw,\n (shapeB as AABBShape).hh,\n ax,\n ay,\n (shapeA as CircleShape).radius,\n out,\n true,\n );\n}\n\n/** Minimum translation vector. Allocates — see `overlapInto` for the hot path. */\nexport function overlap(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n): Overlap | null {\n return overlapInto(shapeA, ax, ay, shapeB, bx, by, createOverlap());\n}\n\nfunction aabbAabbOverlap(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n bx: number,\n by: number,\n bhw: number,\n bhh: number,\n out: Overlap,\n): Overlap | null {\n const dx = bx - ax;\n const dy = by - ay;\n const overlapX = ahw + bhw - Math.abs(dx);\n if (overlapX <= 0) return null;\n const overlapY = ahh + bhh - Math.abs(dy);\n if (overlapY <= 0) return null;\n\n // The minimum translation is along whichever axis is least penetrated.\n if (overlapX < overlapY) {\n out.x = dx < 0 ? -1 : 1;\n out.y = 0;\n out.depth = overlapX;\n } else {\n out.x = 0;\n out.y = dy < 0 ? -1 : 1;\n out.depth = overlapY;\n }\n return out;\n}\n\nfunction circleCircleOverlap(\n ax: number,\n ay: number,\n ar: number,\n bx: number,\n by: number,\n br: number,\n out: Overlap,\n): Overlap | null {\n const dx = bx - ax;\n const dy = by - ay;\n const radSum = ar + br;\n const distSq = dx * dx + dy * dy;\n if (distSq >= radSum * radSum) return null;\n\n const dist = Math.sqrt(distSq);\n if (dist === 0) {\n // Concentric: no meaningful axis, so pick one rather than divide by zero.\n out.x = 1;\n out.y = 0;\n out.depth = radSum;\n return out;\n }\n out.x = dx / dist;\n out.y = dy / dist;\n out.depth = radSum - dist;\n return out;\n}\n\nfunction aabbCircleOverlap(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n cx: number,\n cy: number,\n cr: number,\n out: Overlap,\n flip: boolean,\n): Overlap | null {\n const left = ax - ahw;\n const right = ax + ahw;\n const top = ay - ahh;\n const bottom = ay + ahh;\n const closestX = cx < left ? left : cx > right ? right : cx;\n const closestY = cy < top ? top : cy > bottom ? bottom : cy;\n const dx = cx - closestX;\n const dy = cy - closestY;\n const distSq = dx * dx + dy * dy;\n\n if (distSq > 0) {\n // Centre outside the box: separate along the closest-point normal.\n if (distSq >= cr * cr) return null;\n const dist = Math.sqrt(distSq);\n const nx = dx / dist;\n const ny = dy / dist;\n out.x = flip ? -nx : nx;\n out.y = flip ? -ny : ny;\n out.depth = cr - dist;\n return out;\n }\n\n // Centre inside the box: push out through the nearest face.\n const toLeft = cx - left;\n const toRight = right - cx;\n const toTop = cy - top;\n const toBottom = bottom - cy;\n let nx = -1;\n let ny = 0;\n let depth = toLeft;\n if (toRight < depth) {\n nx = 1;\n ny = 0;\n depth = toRight;\n }\n if (toTop < depth) {\n nx = 0;\n ny = -1;\n depth = toTop;\n }\n if (toBottom < depth) {\n nx = 0;\n ny = 1;\n depth = toBottom;\n }\n // Zero radius on the boundary is exactly touching, not overlapping.\n if (depth + cr <= 0) return null;\n out.x = flip ? -nx : nx;\n out.y = flip ? -ny : ny;\n out.depth = depth + cr;\n return out;\n}\n","import type { Rect } from '@flare-engine/math';\n\n// Internal entry: bounding rect (minX, minY, maxX, maxY) plus a per-query stamp\n// for zero-alloc dedup.\ninterface _Entry<T> {\n readonly item: T;\n minX: number;\n minY: number;\n maxX: number;\n maxY: number;\n _stamp: number;\n}\n\n// Uniform grid cell size in world units. Tuned for typical game-object sizes (16–64 px).\nconst CELL = 64;\n\n// Packed cell-key encoding. Supports cell coords in [-32768, 32767],\n// i.e. world coordinates ±2 097 152. All keys fit within Number.MAX_SAFE_INTEGER.\nconst KEY_RANGE = 65536; // 2^16\nconst KEY_OFFSET = 32768; // 2^15\n\nfunction _key(cx: number, cy: number): number {\n return (cx + KEY_OFFSET) * KEY_RANGE + (cy + KEY_OFFSET);\n}\n\n// Inclusive min cell that contains world coordinate v.\nfunction _cMin(v: number): number {\n return Math.floor(v / CELL);\n}\n\n// Inclusive max cell that contains world coordinate v (exclusive-end: a coord\n// falling exactly on a cell boundary is NOT part of the adjacent cell — matches\n// the strict-inequality overlap semantics of query()).\nfunction _cMax(v: number): number {\n return Math.ceil(v / CELL) - 1;\n}\n\n// Supported cell-coordinate range for the packed key above.\nconst MIN_CELL = -KEY_OFFSET;\nconst MAX_CELL = KEY_OFFSET - 1;\n\n// Bounds a derived cell coordinate to the packed-key range. Without this, a\n// non-finite world coordinate (±Infinity — an unbounded raycast maxDist, or an\n// entity parked at Infinity) makes cxMin/cxMax/cyMin/cyMax infinite, and\n// `for (cx = cxMin; cx <= cxMax; cx++)` never exits: once cx itself is\n// Infinity, `cx <= Infinity` stays true and `cx++` is a no-op, so the loop\n// spins forever pushing/reading the same bucket (unbounded memory growth on\n// the link side). NaN is left untouched: every comparison against NaN is\n// false, so the loop already runs zero times, which is the existing (safe)\n// behaviour for a degenerate direction like `0 * Infinity`.\nfunction _clampCell(c: number): number {\n if (c > MAX_CELL) return MAX_CELL;\n if (c < MIN_CELL) return MIN_CELL;\n return c;\n}\n\n/**\n * Uniform spatial-hash broadphase.\n * `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.\n * `query` walks only the cells overlapping the given rect; result order is unspecified.\n * Zero-alloc in the query hot-path: duplicate suppression uses a monotone stamp counter,\n * not a per-call Set.\n */\nexport class SpatialIndex<T> {\n private _data: _Entry<T>[] = [];\n private _slots = new Map<T, number>(); // item → dense-array slot\n private _cells = new Map<number, _Entry<T>[]>(); // packed cell key → bucket\n private _gen = 0; // monotone query-generation counter\n\n /** Number of items in the index. */\n get size(): number {\n return this._data.length;\n }\n\n /** Insert an item with a bounding rect. */\n insert(item: T, bounds: Rect): void {\n const entry: _Entry<T> = {\n item,\n minX: bounds.x,\n minY: bounds.y,\n maxX: bounds.x + bounds.width,\n maxY: bounds.y + bounds.height,\n _stamp: 0,\n };\n this._slots.set(item, this._data.length);\n this._data.push(entry);\n this._link(entry);\n }\n\n /** Remove an item. Returns false when the item was not present. */\n remove(item: T): boolean {\n const slot = this._slots.get(item);\n if (slot === undefined) return false;\n const entry = this._data[slot]!;\n this._unlink(entry);\n this._slots.delete(item);\n // Swap-remove: plug the hole with the last element.\n const last = this._data.pop()!;\n if (slot < this._data.length) {\n this._data[slot] = last;\n this._slots.set(last.item, slot);\n }\n return true;\n }\n\n /** Update the bounds for an existing item. No-op when the item is absent. */\n update(item: T, bounds: Rect): void {\n const slot = this._slots.get(item);\n if (slot === undefined) return;\n const entry = this._data[slot]!;\n this._unlink(entry);\n entry.minX = bounds.x;\n entry.minY = bounds.y;\n entry.maxX = bounds.x + bounds.width;\n entry.maxY = bounds.y + bounds.height;\n this._link(entry);\n }\n\n /**\n * Query all items whose bounds overlap the given rect.\n * Result order is unspecified. Appends to `result` when provided and returns it.\n */\n query(bounds: Rect, result?: T[]): T[] {\n const out = result ?? [];\n const gen = ++this._gen;\n\n const qMinX = bounds.x;\n const qMinY = bounds.y;\n const qMaxX = bounds.x + bounds.width;\n const qMaxY = bounds.y + bounds.height;\n\n const cxMin = _clampCell(_cMin(qMinX));\n const cxMax = _clampCell(_cMax(qMaxX));\n const cyMin = _clampCell(_cMin(qMinY));\n const cyMax = _clampCell(_cMax(qMaxY));\n\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const bucket = this._cells.get(_key(cx, cy));\n if (bucket === undefined) continue;\n for (let i = 0; i < bucket.length; i++) {\n const e = bucket[i]!;\n if (e._stamp === gen) continue; // already emitted this query\n e._stamp = gen;\n if (e.maxX > qMinX && e.minX < qMaxX && e.maxY > qMinY && e.minY < qMaxY) {\n out.push(e.item);\n }\n }\n }\n }\n return out;\n }\n\n /** Remove all items. */\n clear(): void {\n this._data.length = 0;\n this._slots.clear();\n this._cells.clear();\n }\n\n // Register entry in all grid cells it overlaps.\n private _link(entry: _Entry<T>): void {\n const cxMin = _clampCell(_cMin(entry.minX));\n const cxMax = _clampCell(_cMax(entry.maxX));\n const cyMin = _clampCell(_cMin(entry.minY));\n const cyMax = _clampCell(_cMax(entry.maxY));\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const k = _key(cx, cy);\n let b = this._cells.get(k);\n if (b === undefined) {\n b = [];\n this._cells.set(k, b);\n }\n b.push(entry);\n }\n }\n }\n\n // Remove entry from all grid cells it currently occupies.\n private _unlink(entry: _Entry<T>): void {\n const cxMin = _clampCell(_cMin(entry.minX));\n const cxMax = _clampCell(_cMax(entry.maxX));\n const cyMin = _clampCell(_cMin(entry.minY));\n const cyMax = _clampCell(_cMax(entry.maxY));\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const b = this._cells.get(_key(cx, cy));\n if (b === undefined) continue;\n const i = b.indexOf(entry);\n if (i !== -1) {\n // Swap-remove within the bucket.\n const last = b.pop()!;\n if (i < b.length) b[i] = last;\n }\n }\n }\n }\n}\n","import { Signal } from '@flare-engine/events';\nimport type { Rect } from '@flare-engine/math';\nimport type { Collider } from './collider.js';\nimport type { RaycastHit } from './raycast.js';\nimport { rayVsShapeInto } from './raycast.js';\nimport type { Shape } from './shape.js';\nimport { shapeBoundsInto, shapeOverlap } from './shape.js';\nimport { SpatialIndex } from './spatial-index.js';\n\n/** Collision pair data emitted by collision signals. */\nexport interface CollisionPair {\n readonly entityA: number;\n readonly entityB: number;\n}\n\n/** Per-entity collision data stored in the world. */\ninterface CollisionEntry {\n entity: number;\n shape: Shape;\n x: number;\n y: number;\n isTrigger: boolean;\n layer: number;\n mask: number;\n}\n\n// Packed pair-key layout: `lo * ENTITY_SHIFT + hi`, lo = min(a,b), hi = max(a,b).\n// 2^26 fits within Number.MAX_SAFE_INTEGER for 26+26 bits → max entity id ~67M.\nconst ENTITY_SHIFT = 2 ** 26;\n\n/** Padding applied to a ray's query AABB — see `CollisionWorld.raycast`. */\nconst RAY_BOUNDS_PAD = 1e-6;\n\nfunction encodePair(a: number, b: number): number {\n const lo = a < b ? a : b;\n const hi = a < b ? b : a;\n return lo * ENTITY_SHIFT + hi;\n}\n\nfunction decodeLo(key: number): number {\n return Math.floor(key / ENTITY_SHIFT);\n}\n\nfunction decodeHi(key: number): number {\n return key - Math.floor(key / ENTITY_SHIFT) * ENTITY_SHIFT;\n}\n\n/**\n * AABB collision world. Manages entities with shapes, detects overlaps each step,\n * and emits collision/trigger signals.\n */\nexport class CollisionWorld {\n readonly onCollisionStart = new Signal<CollisionPair>();\n readonly onCollisionEnd = new Signal<CollisionPair>();\n readonly onTriggerEnter = new Signal<CollisionPair>();\n readonly onTriggerExit = new Signal<CollisionPair>();\n\n private entries = new Map<number, CollisionEntry>();\n private spatial = new SpatialIndex<number>();\n /**\n * Two pre-allocated pair buffers — swapped each step (no per-frame allocation).\n * Keys are packed numbers via `encodePair(a, b)`.\n */\n private pairBufA = new Set<number>();\n private pairBufB = new Set<number>();\n private activePairs: Set<number> = this.pairBufA;\n /** Reusable query result buffer. */\n private queryBuf: number[] = [];\n /** Reusable bounds rect — avoids per-entity Rect allocation in step(). */\n private boundsBuf: Rect = { x: 0, y: 0, width: 0, height: 0 };\n\n /** Add an entity with a shape. Positional twin of `addCollider`. */\n add(\n entity: number,\n shape: Shape,\n x: number,\n y: number,\n isTrigger = false,\n layer = 1,\n mask = 0xffffffff,\n ): void {\n const entry: CollisionEntry = { entity, shape, x, y, isTrigger, layer, mask };\n const exists = this.entries.has(entity);\n this.entries.set(entity, entry);\n shapeBoundsInto(shape, x, y, this.boundsBuf);\n if (exists) {\n this.spatial.update(entity, this.boundsBuf);\n } else {\n this.spatial.insert(entity, this.boundsBuf);\n }\n }\n\n /**\n * Add an entity from a `Collider`. Its `shape`, `isTrigger`, `layer` and `mask`\n * are copied at add time — later edits to the collider do not reach the world;\n * re-add to change them. Re-adding an existing entity replaces its data.\n */\n addCollider(entity: number, collider: Collider, x: number, y: number): void {\n this.add(entity, collider.shape, x, y, collider.isTrigger, collider.layer, collider.mask);\n }\n\n /** Remove an entity from the world. Emits exit signals for any active pairs. */\n remove(entity: number): void {\n const entry = this.entries.get(entity);\n if (!entry) return;\n\n this.entries.delete(entity);\n this.spatial.remove(entity);\n\n // Fire exit signals for all active pairs involving this entity\n for (const key of this.activePairs) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n if (lo !== entity && hi !== entity) continue;\n this.activePairs.delete(key);\n const otherEntity = lo === entity ? hi : lo;\n const otherEntry = this.entries.get(otherEntity);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (entry.isTrigger || otherEntry?.isTrigger) {\n this.onTriggerExit.emit(pair);\n } else {\n this.onCollisionEnd.emit(pair);\n }\n }\n }\n\n /** Update an entity's position. */\n setPosition(entity: number, x: number, y: number): void {\n const entry = this.entries.get(entity);\n if (!entry) return;\n entry.x = x;\n entry.y = y;\n shapeBoundsInto(entry.shape, x, y, this.boundsBuf);\n this.spatial.update(entity, this.boundsBuf);\n }\n\n /** Number of entities in the world. */\n get size(): number {\n return this.entries.size;\n }\n\n /** Number of broadphase records (one per live entity after add/remove). Diagnostic for broadphase growth. */\n get broadphaseSize(): number {\n return this.spatial.size;\n }\n\n /** Run one collision detection step. */\n step(): void {\n const newPairs = this.activePairs === this.pairBufA ? this.pairBufB : this.pairBufA;\n newPairs.clear();\n\n for (const entryA of this.entries.values()) {\n const entityA = entryA.entity;\n shapeBoundsInto(entryA.shape, entryA.x, entryA.y, this.boundsBuf);\n this.queryBuf.length = 0;\n this.spatial.query(this.boundsBuf, this.queryBuf);\n\n for (let i = 0; i < this.queryBuf.length; i++) {\n const entityB = this.queryBuf[i]!;\n if (entityB <= entityA) continue; // Avoid self and duplicates\n\n const entryB = this.entries.get(entityB);\n if (!entryB) continue;\n\n // Layer/mask filtering\n if ((entryA.layer & entryB.mask) === 0 && (entryB.layer & entryA.mask) === 0) continue;\n\n // Narrowphase\n if (!shapeOverlap(entryA.shape, entryA.x, entryA.y, entryB.shape, entryB.x, entryB.y)) {\n continue;\n }\n\n newPairs.add(encodePair(entityA, entityB));\n }\n }\n\n // Enter/start events — pair objects are allocated here only on state transitions.\n for (const key of newPairs) {\n if (!this.activePairs.has(key)) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n const a = this.entries.get(lo);\n const b = this.entries.get(hi);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (a?.isTrigger || b?.isTrigger) {\n this.onTriggerEnter.emit(pair);\n } else {\n this.onCollisionStart.emit(pair);\n }\n }\n }\n\n // Exit/end events\n for (const key of this.activePairs) {\n if (!newPairs.has(key)) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n const a = this.entries.get(lo);\n const b = this.entries.get(hi);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (a?.isTrigger || b?.isTrigger) {\n this.onTriggerExit.emit(pair);\n } else {\n this.onCollisionEnd.emit(pair);\n }\n }\n }\n\n this.activePairs = newPairs;\n }\n\n /** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */\n getColliderBoundsInto(entity: number, out: Rect): Rect | null {\n const entry = this.entries.get(entity);\n if (!entry) return null;\n shapeBoundsInto(entry.shape, entry.x, entry.y, out);\n return out;\n }\n\n /** Allocating convenience wrapper — returns a fresh Rect or null when entity is absent. */\n getColliderBounds(entity: number): Rect | null {\n const entry = this.entries.get(entity);\n if (!entry) return null;\n return shapeBoundsInto(entry.shape, entry.x, entry.y, { x: 0, y: 0, width: 0, height: 0 });\n }\n\n /** Remove all entities. */\n clear(): void {\n this.entries.clear();\n this.spatial.clear();\n this.pairBufA.clear();\n this.pairBufB.clear();\n this.activePairs = this.pairBufA;\n }\n\n /** Reused across raycasts — see `raycast`. */\n private raycastBounds: Rect = { x: 0, y: 0, width: 0, height: 0 };\n private raycastCandidates: number[] = [];\n private raycastHit: RaycastHit = {\n entity: -1,\n distance: 0,\n point: { x: 0, y: 0 },\n normal: { x: 0, y: 0 },\n };\n\n /**\n * Cast a ray against the colliders in this world.\n *\n * The standalone `raycast()` export tests a caller-supplied target array, so\n * it neither sees the world's colliders nor benefits from the broadphase — an\n * O(n) scan sitting beside an O(1) index. This walks only the cells the ray's\n * bounding box touches.\n *\n * `mask` is a layer bitmask filtered against each collider's `layer`, using the\n * bits colliders already carry; it defaults to every layer. Triggers are\n * included — a ray asking \"what is over there\" wants them.\n *\n * The returned record is **reused between calls**: read it, or copy what you\n * need, before casting again. That is what keeps a per-frame ray allocation-\n * free, and it is why the standalone helper's fresh-object-per-candidate cost\n * was worth removing.\n *\n * `maxDist` must be finite — this requires a bounded ray, not the\n * `Number.POSITIVE_INFINITY` \"unbounded ray\" idiom some engines accept. The\n * broadphase below is a uniform grid: an infinite `maxDist` derives an\n * infinite cell range and never terminates the walk. Pass a large finite\n * bound instead (e.g. the map's diagonal) to cast \"until it leaves the\n * populated area\". A non-finite `maxDist` returns `null`.\n */\n raycast(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n mask = 0xffffffff,\n ): RaycastHit | null {\n if (!Number.isFinite(maxDist)) return null;\n\n // Bounding box of the whole ray — the broadphase is rect-based, so this is\n // a conservative prefilter, not the intersection test itself.\n const ex = ox + dx * maxDist;\n const ey = oy + dy * maxDist;\n const minX = ox < ex ? ox : ex;\n const minY = oy < ey ? oy : ey;\n // Padded so the rect always has non-zero extent on both axes. An axis-\n // aligned ray is otherwise degenerate — a horizontal ray has height 0 — and\n // SpatialIndex derives its cell range with an exclusive end (`_cMax` is\n // `ceil(v / CELL) - 1`), so a zero-height rect yields cyMin 0 > cyMax -1 and\n // the cell loop never runs. The broadphase is a conservative prefilter\n // anyway, so over-including by a hair costs nothing: the exact ray test\n // below rejects the extras.\n this.raycastBounds.x = minX - RAY_BOUNDS_PAD;\n this.raycastBounds.y = minY - RAY_BOUNDS_PAD;\n this.raycastBounds.width = Math.abs(ex - ox) + RAY_BOUNDS_PAD * 2;\n this.raycastBounds.height = Math.abs(ey - oy) + RAY_BOUNDS_PAD * 2;\n\n this.raycastCandidates.length = 0;\n this.spatial.query(this.raycastBounds, this.raycastCandidates);\n\n let bestDist = Number.POSITIVE_INFINITY;\n let found = false;\n for (let i = 0; i < this.raycastCandidates.length; i++) {\n const entry = this.entries.get(this.raycastCandidates[i]!);\n if (entry === undefined) continue;\n if ((entry.layer & mask) === 0) continue;\n const hit = rayVsShapeInto(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n entry.entity,\n entry.shape,\n entry.x,\n entry.y,\n this.scratchHit,\n );\n if (hit !== null && hit.distance < bestDist) {\n bestDist = hit.distance;\n found = true;\n this.raycastHit.entity = hit.entity;\n this.raycastHit.distance = hit.distance;\n this.raycastHit.point.x = hit.point.x;\n this.raycastHit.point.y = hit.point.y;\n this.raycastHit.normal.x = hit.normal.x;\n this.raycastHit.normal.y = hit.normal.y;\n }\n }\n return found ? this.raycastHit : null;\n }\n\n /** Scratch record handed to `rayVsShapeInto` per candidate. */\n private scratchHit: RaycastHit = {\n entity: -1,\n distance: 0,\n point: { x: 0, y: 0 },\n normal: { x: 0, y: 0 },\n };\n}\n","import type { Vec2 } from '@flare-engine/math';\n\n/**\n * Minimal structural type for a solid tile position in grid coordinates.\n * `TileCollider[]` from `@flare-engine/tilemap` is structurally assignable here\n * so callers can pass `extractColliders(map)` directly — no tilemap dep needed.\n */\nexport interface SolidTile {\n readonly row: number;\n readonly col: number;\n}\n\n/**\n * Precomputed solid-tile lookup used by `isBlockedAABB` and `moveAndSlideInto`.\n * `solid` has `width * height` cells indexed `row * width + col`; non-zero means\n * solid. Build once per map load via `buildSolidGrid`.\n */\nexport interface SolidGrid {\n readonly width: number;\n readonly height: number;\n readonly tileSize: number;\n readonly solid: Uint8Array;\n}\n\n/**\n * Build a `SolidGrid` from a list of solid tile positions.\n * Input is structurally typed (`readonly SolidTile[]`) so the caller does not\n * need to import `@flare-engine/tilemap` — physics stays tilemap-free.\n */\nexport function buildSolidGrid(\n width: number,\n height: number,\n tileSize: number,\n tiles: readonly SolidTile[],\n): SolidGrid {\n const solid = new Uint8Array(width * height);\n for (const t of tiles) {\n solid[t.row * width + t.col] = 1;\n }\n return { width, height, tileSize, solid };\n}\n\n/**\n * Half-open boundary constant: the AABB is treated as [min, max) so a box\n * whose max edge lands exactly on a tile boundary does NOT overlap that tile.\n */\nconst EDGE = 1e-4;\n\n/**\n * Returns `true` when an axis-aligned box centered at (`x`, `y`) with\n * half-extents (`halfW`, `halfH`) overlaps a solid tile or lies outside the\n * map boundary. Uses half-open EDGE semantics on max edges.\n */\nexport function isBlockedAABB(\n grid: SolidGrid,\n x: number,\n y: number,\n halfW: number,\n halfH: number,\n): boolean {\n const ts = grid.tileSize;\n const minCol = Math.floor((x - halfW) / ts);\n const maxCol = Math.floor((x + halfW - EDGE) / ts);\n const minRow = Math.floor((y - halfH) / ts);\n const maxRow = Math.floor((y + halfH - EDGE) / ts);\n if (minCol < 0 || minRow < 0 || maxCol >= grid.width || maxRow >= grid.height) {\n return true;\n }\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n if (grid.solid[row * grid.width + col] !== 0) {\n return true;\n }\n }\n }\n return false;\n}\n\n/**\n * Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis\n * is applied independently so a blocked diagonal input slides along the wall.\n * The y-check uses the already-updated x position.\n * Zero-alloc: writes result into the provided `out` Vec2 and returns it.\n */\nexport function moveAndSlideInto(\n grid: SolidGrid,\n x: number,\n y: number,\n dx: number,\n dy: number,\n halfW: number,\n halfH: number,\n out: Vec2,\n): Vec2 {\n let nx = x;\n let ny = y;\n if (dx !== 0 && !isBlockedAABB(grid, nx + dx, ny, halfW, halfH)) nx += dx;\n if (dy !== 0 && !isBlockedAABB(grid, nx, ny + dy, halfW, halfH)) ny += dy;\n out.x = nx;\n out.y = ny;\n return out;\n}\n"],"mappings":";;;;AAeA,SAAgB,eAAe,OAAc,MAAmD;CAC9F,OAAO;EACL;EACA,WAAW,MAAM,aAAa;EAC9B,OAAO,MAAM,SAAS;EACtB,MAAM,MAAM,QAAQ;CACtB;AACF;;;;;;;;;;;;ACUA,SAAgB,QACd,IACA,IACA,IACA,IACA,SACA,SACmB;CACnB,IAAI,UAA6B;CAEjC,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,MAAM,IAAI,QAAQ;EAClB,MAAM,MAAM,WAAW,IAAI,IAAI,IAAI,IAAI,SAAS,CAAC;EACjD,IAAI,QAAQ,YAAY,QAAQ,IAAI,WAAW,QAAQ,WAC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CACR,OAAO;AACT"}
1
+ {"version":3,"file":"index.js","names":["Signal"],"sources":["../src/collider.ts","../src/raycast.ts","../src/shape.ts","../src/spatial-index.ts","../src/collision-world.ts","../src/solid-grid.ts"],"sourcesContent":["import type { Shape } from './shape.js';\n\n/** Collider component data attached to an ECS entity. */\nexport interface Collider {\n /** The collision shape. */\n shape: Shape;\n /** If true, generates trigger events but no physical response. */\n isTrigger: boolean;\n /** Collision layer bitmask — which layer this collider belongs to. */\n layer: number;\n /** Collision mask — which layers this collider can interact with. */\n mask: number;\n}\n\n/** Create a collider with default settings. */\nexport function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider {\n return {\n shape,\n isTrigger: opts?.isTrigger ?? false,\n layer: opts?.layer ?? 1,\n mask: opts?.mask ?? 0xffffffff,\n };\n}\n","import type { Vec2 } from '@flare-engine/math';\nimport type { Shape } from './shape.js';\n\n/** Result of a raycast hit. */\nexport interface RaycastHit {\n /** The entity that was hit. */\n entity: number;\n /** Distance from ray origin to hit point. */\n distance: number;\n /** World-space hit point. */\n point: Vec2;\n /** Surface normal at hit point. */\n normal: Vec2;\n}\n\n/** Entity entry for raycast — position + shape. */\nexport interface RaycastTarget {\n entity: number;\n shape: Shape;\n x: number;\n y: number;\n}\n\n/**\n * Cast a ray against a list of targets. Returns the closest hit, or null.\n * @param ox Ray origin X\n * @param oy Ray origin Y\n * @param dx Ray direction X (should be normalized)\n * @param dy Ray direction Y (should be normalized)\n * @param maxDist Maximum raycast distance\n * @param targets Array of entities to test against\n */\nexport function raycast(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n targets: readonly RaycastTarget[],\n): RaycastHit | null {\n let closest: RaycastHit | null = null;\n\n for (let i = 0; i < targets.length; i++) {\n const t = targets[i]!;\n const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);\n if (hit && (closest === null || hit.distance < closest.distance)) {\n closest = hit;\n }\n }\n\n return closest;\n}\n\nfunction rayVsShape(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n target: RaycastTarget,\n): RaycastHit | null {\n if (target.shape.type === 'aabb') {\n return rayVsAABB(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n target.entity,\n target.x,\n target.y,\n target.shape.hw,\n target.shape.hh,\n );\n }\n return rayVsCircle(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n target.entity,\n target.x,\n target.y,\n target.shape.radius,\n );\n}\n\nfunction rayVsAABB(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n hw: number,\n hh: number,\n): RaycastHit | null {\n const minX = cx - hw;\n const maxX = cx + hw;\n const minY = cy - hh;\n const maxY = cy + hh;\n\n const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;\n const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;\n\n const tx1 = (minX - ox) * invDx;\n const tx2 = (maxX - ox) * invDx;\n const ty1 = (minY - oy) * invDy;\n const ty2 = (maxY - oy) * invDy;\n\n const tMinX = Math.min(tx1, tx2);\n const tMaxX = Math.max(tx1, tx2);\n const tMinY = Math.min(ty1, ty2);\n const tMaxY = Math.max(ty1, ty2);\n\n const tMin = Math.max(tMinX, tMinY);\n const tMax = Math.min(tMaxX, tMaxY);\n\n if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;\n\n const t = tMin >= 0 ? tMin : tMax;\n if (t > maxDist || t < 0) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n\n // Compute normal\n let nx = 0;\n let ny = 0;\n if (tMinX > tMinY) {\n nx = dx < 0 ? 1 : -1;\n } else {\n ny = dy < 0 ? 1 : -1;\n }\n\n return {\n entity,\n distance: t,\n point: { x: px, y: py },\n normal: { x: nx, y: ny },\n };\n}\n\nfunction rayVsCircle(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n radius: number,\n): RaycastHit | null {\n const fx = ox - cx;\n const fy = oy - cy;\n\n const a = dx * dx + dy * dy;\n const b = 2 * (fx * dx + fy * dy);\n const c = fx * fx + fy * fy - radius * radius;\n\n if (a < 1e-12) return null; // Zero-length direction\n\n const discriminant = b * b - 4 * a * c;\n if (discriminant < 0) return null;\n\n const sqrtDisc = Math.sqrt(discriminant);\n let t = (-b - sqrtDisc) / (2 * a);\n\n // If entry point is behind, try exit point\n if (t < 0) {\n t = (-b + sqrtDisc) / (2 * a);\n }\n if (t < 0 || t > maxDist) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n const nx = px - cx;\n const ny = py - cy;\n const len = Math.sqrt(nx * nx + ny * ny);\n\n return {\n entity,\n distance: t,\n point: { x: px, y: py },\n normal: len > 0 ? { x: nx / len, y: ny / len } : { x: 0, y: 0 },\n };\n}\n\n/**\n * Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns\n * `null` without touching `out`.\n *\n * The allocating `raycast()` above builds a fresh `RaycastHit` — with two nested\n * `Vec2` objects — for **every candidate it tests**, then discards all but the\n * closest. That is three objects per candidate per ray. This variant exists so\n * `CollisionWorld.raycast` can test a whole broadphase cell without allocating.\n */\nexport function rayVsShapeInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n shape: Shape,\n x: number,\n y: number,\n out: RaycastHit,\n): RaycastHit | null {\n if (shape.type === 'aabb') {\n return rayVsAABBInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.hw, shape.hh, out);\n }\n return rayVsCircleInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.radius, out);\n}\n\nfunction rayVsAABBInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n bx: number,\n by: number,\n hw: number,\n hh: number,\n out: RaycastHit,\n): RaycastHit | null {\n const invDx = dx !== 0 ? 1 / dx : Number.POSITIVE_INFINITY;\n const invDy = dy !== 0 ? 1 / dy : Number.POSITIVE_INFINITY;\n\n const tx1 = (bx - hw - ox) * invDx;\n const tx2 = (bx + hw - ox) * invDx;\n const ty1 = (by - hh - oy) * invDy;\n const ty2 = (by + hh - oy) * invDy;\n\n const tMinX = Math.min(tx1, tx2);\n const tMaxX = Math.max(tx1, tx2);\n const tMinY = Math.min(ty1, ty2);\n const tMaxY = Math.max(ty1, ty2);\n\n const tMin = Math.max(tMinX, tMinY);\n const tMax = Math.min(tMaxX, tMaxY);\n if (tMax < 0 || tMin > tMax) return null;\n\n const t = tMin < 0 ? tMax : tMin;\n if (t > maxDist || t < 0) return null;\n\n out.entity = entity;\n out.distance = t;\n out.point.x = ox + dx * t;\n out.point.y = oy + dy * t;\n if (tMinX > tMinY) {\n out.normal.x = dx < 0 ? 1 : -1;\n out.normal.y = 0;\n } else {\n out.normal.x = 0;\n out.normal.y = dy < 0 ? 1 : -1;\n }\n return out;\n}\n\nfunction rayVsCircleInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n radius: number,\n out: RaycastHit,\n): RaycastHit | null {\n const fx = ox - cx;\n const fy = oy - cy;\n const a = dx * dx + dy * dy;\n if (a < 1e-12) return null;\n\n const b = 2 * (fx * dx + fy * dy);\n const c = fx * fx + fy * fy - radius * radius;\n const discriminant = b * b - 4 * a * c;\n if (discriminant < 0) return null;\n\n const sqrtDisc = Math.sqrt(discriminant);\n let t = (-b - sqrtDisc) / (2 * a);\n if (t < 0) t = (-b + sqrtDisc) / (2 * a);\n if (t < 0 || t > maxDist) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n const nx = px - cx;\n const ny = py - cy;\n const len = Math.sqrt(nx * nx + ny * ny);\n\n out.entity = entity;\n out.distance = t;\n out.point.x = px;\n out.point.y = py;\n out.normal.x = len > 0 ? nx / len : 0;\n out.normal.y = len > 0 ? ny / len : 0;\n return out;\n}\n","import type { Rect } from '@flare-engine/math';\n\n/** AABB shape — axis-aligned bounding box. */\nexport interface AABBShape {\n readonly type: 'aabb';\n /** Half-width. */\n readonly hw: number;\n /** Half-height. */\n readonly hh: number;\n}\n\n/** Circle shape. */\nexport interface CircleShape {\n readonly type: 'circle';\n readonly radius: number;\n}\n\n/** Union of supported physics shapes. */\nexport type Shape = AABBShape | CircleShape;\n\n/** Create an AABB shape from width and height. */\nexport function AABB(width: number, height: number): AABBShape {\n return { type: 'aabb', hw: width * 0.5, hh: height * 0.5 };\n}\n\n/** Create a circle shape from radius. */\nexport function CircleShape(radius: number): CircleShape {\n return { type: 'circle', radius };\n}\n\n/** Compute the bounding rect of a shape at a given position. */\nexport function shapeBounds(shape: Shape, x: number, y: number): Rect {\n if (shape.type === 'aabb') {\n return { x: x - shape.hw, y: y - shape.hh, width: shape.hw * 2, height: shape.hh * 2 };\n }\n return {\n x: x - shape.radius,\n y: y - shape.radius,\n width: shape.radius * 2,\n height: shape.radius * 2,\n };\n}\n\n/** Zero-alloc variant — writes the shape bounds into the provided output rect. */\nexport function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect {\n if (shape.type === 'aabb') {\n out.x = x - shape.hw;\n out.y = y - shape.hh;\n out.width = shape.hw * 2;\n out.height = shape.hh * 2;\n return out;\n }\n out.x = x - shape.radius;\n out.y = y - shape.radius;\n out.width = shape.radius * 2;\n out.height = shape.radius * 2;\n return out;\n}\n\n/** Test overlap between two positioned shapes. Returns true if overlapping. */\nexport function shapeOverlap(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n): boolean {\n if (shapeA.type === 'aabb' && shapeB.type === 'aabb') {\n return aabbVsAabb(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh);\n }\n if (shapeA.type === 'circle' && shapeB.type === 'circle') {\n return circleVsCircle(ax, ay, shapeA.radius, bx, by, shapeB.radius);\n }\n // Mixed: one AABB, one circle\n if (shapeA.type === 'aabb' && shapeB.type === 'circle') {\n return aabbVsCircle(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius);\n }\n return aabbVsCircle(\n bx,\n by,\n (shapeB as AABBShape).hw,\n (shapeB as AABBShape).hh,\n ax,\n ay,\n (shapeA as CircleShape).radius,\n );\n}\n\nfunction aabbVsAabb(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n bx: number,\n by: number,\n bhw: number,\n bhh: number,\n): boolean {\n return Math.abs(ax - bx) < ahw + bhw && Math.abs(ay - by) < ahh + bhh;\n}\n\nfunction circleVsCircle(\n ax: number,\n ay: number,\n ar: number,\n bx: number,\n by: number,\n br: number,\n): boolean {\n const dx = bx - ax;\n const dy = by - ay;\n const radSum = ar + br;\n return dx * dx + dy * dy < radSum * radSum;\n}\n\nfunction aabbVsCircle(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n cx: number,\n cy: number,\n cr: number,\n): boolean {\n const left = ax - ahw;\n const right = ax + ahw;\n const top = ay - ahh;\n const bottom = ay + ahh;\n const closestX = cx < left ? left : cx > right ? right : cx;\n const closestY = cy < top ? top : cy > bottom ? bottom : cy;\n const dx = cx - closestX;\n const dy = cy - closestY;\n const distSq = dx * dx + dy * dy;\n // Strict, like aabbVsAabb and circleVsCircle: exactly-touching shapes do not\n // collide. A centre on or inside the box collides when the radius is positive;\n // a zero radius counts only strictly inside, as a zero-size box does.\n if (distSq > 0) return distSq < cr * cr;\n return cr > 0 || (cx > left && cx < right && cy > top && cy < bottom);\n}\n\n/**\n * Minimum translation vector: the shortest push that separates two overlapping\n * shapes.\n *\n * `shapeOverlap` answers *whether* two shapes collide and nothing more, which\n * leaves a detection-only consumer with no way to resolve the collision. This is\n * that missing step: `out.x`/`out.y` are a unit axis and `out.depth` is how far\n * along it `b` must move to just stop touching `a`.\n *\n * Returns `null` when the shapes are not overlapping, leaving `out` untouched so\n * a caller may reuse one buffer across many pairs.\n *\n * The degenerate concentric case (identical centres) has no meaningful axis;\n * `(1, 0)` is returned so the caller still gets a usable separation instead of\n * `NaN` from normalising a zero vector.\n */\nexport interface Overlap {\n /** Unit separation axis, pointing from `a` toward `b`. */\n x: number;\n y: number;\n /** Penetration depth along that axis. */\n depth: number;\n}\n\n/** A fresh, zeroed overlap record. Allocate once outside the loop and reuse. */\nexport function createOverlap(): Overlap {\n return { x: 0, y: 0, depth: 0 };\n}\n\nexport function overlapInto(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n out: Overlap,\n): Overlap | null {\n if (shapeA.type === 'aabb' && shapeB.type === 'aabb') {\n return aabbAabbOverlap(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh, out);\n }\n if (shapeA.type === 'circle' && shapeB.type === 'circle') {\n return circleCircleOverlap(ax, ay, shapeA.radius, bx, by, shapeB.radius, out);\n }\n if (shapeA.type === 'aabb' && shapeB.type === 'circle') {\n return aabbCircleOverlap(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius, out, false);\n }\n // Circle vs AABB — solve as AABB vs circle, then flip the axis so it still\n // points from `a` to `b`.\n return aabbCircleOverlap(\n bx,\n by,\n (shapeB as AABBShape).hw,\n (shapeB as AABBShape).hh,\n ax,\n ay,\n (shapeA as CircleShape).radius,\n out,\n true,\n );\n}\n\n/** Minimum translation vector. Allocates — see `overlapInto` for the hot path. */\nexport function overlap(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n): Overlap | null {\n return overlapInto(shapeA, ax, ay, shapeB, bx, by, createOverlap());\n}\n\nfunction aabbAabbOverlap(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n bx: number,\n by: number,\n bhw: number,\n bhh: number,\n out: Overlap,\n): Overlap | null {\n const dx = bx - ax;\n const dy = by - ay;\n const overlapX = ahw + bhw - Math.abs(dx);\n if (overlapX <= 0) return null;\n const overlapY = ahh + bhh - Math.abs(dy);\n if (overlapY <= 0) return null;\n\n // The minimum translation is along whichever axis is least penetrated.\n if (overlapX < overlapY) {\n out.x = dx < 0 ? -1 : 1;\n out.y = 0;\n out.depth = overlapX;\n } else {\n out.x = 0;\n out.y = dy < 0 ? -1 : 1;\n out.depth = overlapY;\n }\n return out;\n}\n\nfunction circleCircleOverlap(\n ax: number,\n ay: number,\n ar: number,\n bx: number,\n by: number,\n br: number,\n out: Overlap,\n): Overlap | null {\n const dx = bx - ax;\n const dy = by - ay;\n const radSum = ar + br;\n const distSq = dx * dx + dy * dy;\n if (distSq >= radSum * radSum) return null;\n\n const dist = Math.sqrt(distSq);\n if (dist === 0) {\n // Concentric: no meaningful axis, so pick one rather than divide by zero.\n out.x = 1;\n out.y = 0;\n out.depth = radSum;\n return out;\n }\n out.x = dx / dist;\n out.y = dy / dist;\n out.depth = radSum - dist;\n return out;\n}\n\nfunction aabbCircleOverlap(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n cx: number,\n cy: number,\n cr: number,\n out: Overlap,\n flip: boolean,\n): Overlap | null {\n const left = ax - ahw;\n const right = ax + ahw;\n const top = ay - ahh;\n const bottom = ay + ahh;\n const closestX = cx < left ? left : cx > right ? right : cx;\n const closestY = cy < top ? top : cy > bottom ? bottom : cy;\n const dx = cx - closestX;\n const dy = cy - closestY;\n const distSq = dx * dx + dy * dy;\n\n if (distSq > 0) {\n // Centre outside the box: separate along the closest-point normal.\n if (distSq >= cr * cr) return null;\n const dist = Math.sqrt(distSq);\n const nx = dx / dist;\n const ny = dy / dist;\n out.x = flip ? -nx : nx;\n out.y = flip ? -ny : ny;\n out.depth = cr - dist;\n return out;\n }\n\n // Centre inside the box: push out through the nearest face.\n const toLeft = cx - left;\n const toRight = right - cx;\n const toTop = cy - top;\n const toBottom = bottom - cy;\n let nx = -1;\n let ny = 0;\n let depth = toLeft;\n if (toRight < depth) {\n nx = 1;\n ny = 0;\n depth = toRight;\n }\n if (toTop < depth) {\n nx = 0;\n ny = -1;\n depth = toTop;\n }\n if (toBottom < depth) {\n nx = 0;\n ny = 1;\n depth = toBottom;\n }\n // Zero radius on the boundary is exactly touching, not overlapping.\n if (depth + cr <= 0) return null;\n out.x = flip ? -nx : nx;\n out.y = flip ? -ny : ny;\n out.depth = depth + cr;\n return out;\n}\n","import type { Rect } from '@flare-engine/math';\n\n// Internal entry: bounding rect (minX, minY, maxX, maxY) plus a per-query stamp\n// for zero-alloc dedup.\ninterface _Entry<T> {\n readonly item: T;\n minX: number;\n minY: number;\n maxX: number;\n maxY: number;\n _stamp: number;\n}\n\n// Uniform grid cell size in world units. Tuned for typical game-object sizes (16–64 px).\nconst CELL = 64;\n\n// Packed cell-key encoding. Supports cell coords in [-32768, 32767],\n// i.e. world coordinates ±2 097 152. All keys fit within Number.MAX_SAFE_INTEGER.\nconst KEY_RANGE = 65536; // 2^16\nconst KEY_OFFSET = 32768; // 2^15\n\nfunction _key(cx: number, cy: number): number {\n return (cx + KEY_OFFSET) * KEY_RANGE + (cy + KEY_OFFSET);\n}\n\n// Inclusive min cell that contains world coordinate v.\nfunction _cMin(v: number): number {\n return Math.floor(v / CELL);\n}\n\n// Inclusive max cell that contains world coordinate v (exclusive-end: a coord\n// falling exactly on a cell boundary is NOT part of the adjacent cell — matches\n// the strict-inequality overlap semantics of query()).\nfunction _cMax(v: number): number {\n return Math.ceil(v / CELL) - 1;\n}\n\n// Supported cell-coordinate range for the packed key above.\nconst MIN_CELL = -KEY_OFFSET;\nconst MAX_CELL = KEY_OFFSET - 1;\n\n// Bounds a derived cell coordinate to the packed-key range. Without this, a\n// non-finite world coordinate (±Infinity — an unbounded raycast maxDist, or an\n// entity parked at Infinity) makes cxMin/cxMax/cyMin/cyMax infinite, and\n// `for (cx = cxMin; cx <= cxMax; cx++)` never exits: once cx itself is\n// Infinity, `cx <= Infinity` stays true and `cx++` is a no-op, so the loop\n// spins forever pushing/reading the same bucket (unbounded memory growth on\n// the link side). NaN is left untouched: every comparison against NaN is\n// false, so the loop already runs zero times, which is the existing (safe)\n// behaviour for a degenerate direction like `0 * Infinity`.\nfunction _clampCell(c: number): number {\n if (c > MAX_CELL) return MAX_CELL;\n if (c < MIN_CELL) return MIN_CELL;\n return c;\n}\n\n/**\n * Uniform spatial-hash broadphase.\n * `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.\n * `query` walks only the cells overlapping the given rect; result order is unspecified.\n * Zero-alloc in the query hot-path: duplicate suppression uses a monotone stamp counter,\n * not a per-call Set.\n */\nexport class SpatialIndex<T> {\n private _data: _Entry<T>[] = [];\n private _slots = new Map<T, number>(); // item → dense-array slot\n private _cells = new Map<number, _Entry<T>[]>(); // packed cell key → bucket\n private _gen = 0; // monotone query-generation counter\n\n /** Number of items in the index. */\n get size(): number {\n return this._data.length;\n }\n\n /** Insert an item with a bounding rect. */\n insert(item: T, bounds: Rect): void {\n const entry: _Entry<T> = {\n item,\n minX: bounds.x,\n minY: bounds.y,\n maxX: bounds.x + bounds.width,\n maxY: bounds.y + bounds.height,\n _stamp: 0,\n };\n this._slots.set(item, this._data.length);\n this._data.push(entry);\n this._link(entry);\n }\n\n /** Remove an item. Returns false when the item was not present. */\n remove(item: T): boolean {\n const slot = this._slots.get(item);\n if (slot === undefined) return false;\n const entry = this._data[slot]!;\n this._unlink(entry);\n this._slots.delete(item);\n // Swap-remove: plug the hole with the last element.\n const last = this._data.pop()!;\n if (slot < this._data.length) {\n this._data[slot] = last;\n this._slots.set(last.item, slot);\n }\n return true;\n }\n\n /** Update the bounds for an existing item. No-op when the item is absent. */\n update(item: T, bounds: Rect): void {\n const slot = this._slots.get(item);\n if (slot === undefined) return;\n const entry = this._data[slot]!;\n this._unlink(entry);\n entry.minX = bounds.x;\n entry.minY = bounds.y;\n entry.maxX = bounds.x + bounds.width;\n entry.maxY = bounds.y + bounds.height;\n this._link(entry);\n }\n\n /**\n * Query all items whose bounds overlap the given rect.\n * Result order is unspecified. Appends to `result` when provided and returns it.\n */\n query(bounds: Rect, result?: T[]): T[] {\n const out = result ?? [];\n const gen = ++this._gen;\n\n const qMinX = bounds.x;\n const qMinY = bounds.y;\n const qMaxX = bounds.x + bounds.width;\n const qMaxY = bounds.y + bounds.height;\n\n const cxMin = _clampCell(_cMin(qMinX));\n const cxMax = _clampCell(_cMax(qMaxX));\n const cyMin = _clampCell(_cMin(qMinY));\n const cyMax = _clampCell(_cMax(qMaxY));\n\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const bucket = this._cells.get(_key(cx, cy));\n if (bucket === undefined) continue;\n for (let i = 0; i < bucket.length; i++) {\n const e = bucket[i]!;\n if (e._stamp === gen) continue; // already emitted this query\n e._stamp = gen;\n if (e.maxX > qMinX && e.minX < qMaxX && e.maxY > qMinY && e.minY < qMaxY) {\n out.push(e.item);\n }\n }\n }\n }\n return out;\n }\n\n /** Remove all items. */\n clear(): void {\n this._data.length = 0;\n this._slots.clear();\n this._cells.clear();\n }\n\n // Register entry in all grid cells it overlaps.\n private _link(entry: _Entry<T>): void {\n const cxMin = _clampCell(_cMin(entry.minX));\n const cxMax = _clampCell(_cMax(entry.maxX));\n const cyMin = _clampCell(_cMin(entry.minY));\n const cyMax = _clampCell(_cMax(entry.maxY));\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const k = _key(cx, cy);\n let b = this._cells.get(k);\n if (b === undefined) {\n b = [];\n this._cells.set(k, b);\n }\n b.push(entry);\n }\n }\n }\n\n // Remove entry from all grid cells it currently occupies.\n private _unlink(entry: _Entry<T>): void {\n const cxMin = _clampCell(_cMin(entry.minX));\n const cxMax = _clampCell(_cMax(entry.maxX));\n const cyMin = _clampCell(_cMin(entry.minY));\n const cyMax = _clampCell(_cMax(entry.maxY));\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const b = this._cells.get(_key(cx, cy));\n if (b === undefined) continue;\n const i = b.indexOf(entry);\n if (i !== -1) {\n // Swap-remove within the bucket.\n const last = b.pop()!;\n if (i < b.length) b[i] = last;\n }\n }\n }\n }\n}\n","import { Signal } from '@flare-engine/events';\nimport type { Rect } from '@flare-engine/math';\nimport type { Collider } from './collider.js';\nimport type { RaycastHit } from './raycast.js';\nimport { rayVsShapeInto } from './raycast.js';\nimport type { Shape } from './shape.js';\nimport { shapeBoundsInto, shapeOverlap } from './shape.js';\nimport { SpatialIndex } from './spatial-index.js';\n\n/** Collision pair data emitted by collision signals. */\nexport interface CollisionPair {\n readonly entityA: number;\n readonly entityB: number;\n}\n\n/** Per-entity collision data stored in the world. */\ninterface CollisionEntry {\n entity: number;\n shape: Shape;\n x: number;\n y: number;\n isTrigger: boolean;\n layer: number;\n mask: number;\n}\n\n// Packed pair-key layout: `lo * ENTITY_SHIFT + hi`, lo = min(a,b), hi = max(a,b).\n// 2^26 fits within Number.MAX_SAFE_INTEGER for 26+26 bits → max entity id ~67M.\nconst ENTITY_SHIFT = 2 ** 26;\n\n/** Padding applied to a ray's query AABB — see `CollisionWorld.raycast`. */\nconst RAY_BOUNDS_PAD = 1e-6;\n\nfunction encodePair(a: number, b: number): number {\n const lo = a < b ? a : b;\n const hi = a < b ? b : a;\n return lo * ENTITY_SHIFT + hi;\n}\n\nfunction decodeLo(key: number): number {\n return Math.floor(key / ENTITY_SHIFT);\n}\n\nfunction decodeHi(key: number): number {\n return key - Math.floor(key / ENTITY_SHIFT) * ENTITY_SHIFT;\n}\n\n/**\n * AABB collision world. Manages entities with shapes, detects overlaps each step,\n * and emits collision/trigger signals.\n */\nexport class CollisionWorld {\n readonly onCollisionStart = new Signal<CollisionPair>();\n readonly onCollisionEnd = new Signal<CollisionPair>();\n readonly onTriggerEnter = new Signal<CollisionPair>();\n readonly onTriggerExit = new Signal<CollisionPair>();\n\n private entries = new Map<number, CollisionEntry>();\n private spatial = new SpatialIndex<number>();\n /**\n * Two pre-allocated pair buffers — swapped each step (no per-frame allocation).\n * Keys are packed numbers via `encodePair(a, b)`.\n */\n private pairBufA = new Set<number>();\n private pairBufB = new Set<number>();\n private activePairs: Set<number> = this.pairBufA;\n /** Reusable query result buffer. */\n private queryBuf: number[] = [];\n /** Reusable bounds rect — avoids per-entity Rect allocation in step(). */\n private boundsBuf: Rect = { x: 0, y: 0, width: 0, height: 0 };\n\n /** Add an entity with a shape. Positional twin of `addCollider`. */\n add(\n entity: number,\n shape: Shape,\n x: number,\n y: number,\n isTrigger = false,\n layer = 1,\n mask = 0xffffffff,\n ): void {\n const entry: CollisionEntry = { entity, shape, x, y, isTrigger, layer, mask };\n const exists = this.entries.has(entity);\n this.entries.set(entity, entry);\n shapeBoundsInto(shape, x, y, this.boundsBuf);\n if (exists) {\n this.spatial.update(entity, this.boundsBuf);\n } else {\n this.spatial.insert(entity, this.boundsBuf);\n }\n }\n\n /**\n * Add an entity from a `Collider`. Its `shape`, `isTrigger`, `layer` and `mask`\n * are copied at add time — later edits to the collider do not reach the world;\n * re-add to change them. Re-adding an existing entity replaces its data.\n */\n addCollider(entity: number, collider: Collider, x: number, y: number): void {\n this.add(entity, collider.shape, x, y, collider.isTrigger, collider.layer, collider.mask);\n }\n\n /** Remove an entity from the world. Emits exit signals for any active pairs. */\n remove(entity: number): void {\n const entry = this.entries.get(entity);\n if (!entry) return;\n\n this.entries.delete(entity);\n this.spatial.remove(entity);\n\n // Fire exit signals for all active pairs involving this entity\n for (const key of this.activePairs) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n if (lo !== entity && hi !== entity) continue;\n this.activePairs.delete(key);\n const otherEntity = lo === entity ? hi : lo;\n const otherEntry = this.entries.get(otherEntity);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (entry.isTrigger || otherEntry?.isTrigger) {\n this.onTriggerExit.emit(pair);\n } else {\n this.onCollisionEnd.emit(pair);\n }\n }\n }\n\n /** Update an entity's position. */\n setPosition(entity: number, x: number, y: number): void {\n const entry = this.entries.get(entity);\n if (!entry) return;\n entry.x = x;\n entry.y = y;\n shapeBoundsInto(entry.shape, x, y, this.boundsBuf);\n this.spatial.update(entity, this.boundsBuf);\n }\n\n /** Number of entities in the world. */\n get size(): number {\n return this.entries.size;\n }\n\n /** Number of broadphase records (one per live entity after add/remove). Diagnostic for broadphase growth. */\n get broadphaseSize(): number {\n return this.spatial.size;\n }\n\n /** Run one collision detection step. */\n step(): void {\n const newPairs = this.activePairs === this.pairBufA ? this.pairBufB : this.pairBufA;\n newPairs.clear();\n\n for (const entryA of this.entries.values()) {\n const entityA = entryA.entity;\n shapeBoundsInto(entryA.shape, entryA.x, entryA.y, this.boundsBuf);\n this.queryBuf.length = 0;\n this.spatial.query(this.boundsBuf, this.queryBuf);\n\n for (let i = 0; i < this.queryBuf.length; i++) {\n const entityB = this.queryBuf[i]!;\n if (entityB <= entityA) continue; // Avoid self and duplicates\n\n const entryB = this.entries.get(entityB);\n if (!entryB) continue;\n\n // Layer/mask filtering\n if ((entryA.layer & entryB.mask) === 0 && (entryB.layer & entryA.mask) === 0) continue;\n\n // Narrowphase\n if (!shapeOverlap(entryA.shape, entryA.x, entryA.y, entryB.shape, entryB.x, entryB.y)) {\n continue;\n }\n\n newPairs.add(encodePair(entityA, entityB));\n }\n }\n\n // Enter/start events — pair objects are allocated here only on state transitions.\n for (const key of newPairs) {\n if (!this.activePairs.has(key)) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n const a = this.entries.get(lo);\n const b = this.entries.get(hi);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (a?.isTrigger || b?.isTrigger) {\n this.onTriggerEnter.emit(pair);\n } else {\n this.onCollisionStart.emit(pair);\n }\n }\n }\n\n // Exit/end events\n for (const key of this.activePairs) {\n if (!newPairs.has(key)) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n const a = this.entries.get(lo);\n const b = this.entries.get(hi);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (a?.isTrigger || b?.isTrigger) {\n this.onTriggerExit.emit(pair);\n } else {\n this.onCollisionEnd.emit(pair);\n }\n }\n }\n\n this.activePairs = newPairs;\n }\n\n /** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */\n getColliderBoundsInto(entity: number, out: Rect): Rect | null {\n const entry = this.entries.get(entity);\n if (!entry) return null;\n shapeBoundsInto(entry.shape, entry.x, entry.y, out);\n return out;\n }\n\n /** Allocating convenience wrapper — returns a fresh Rect or null when entity is absent. */\n getColliderBounds(entity: number): Rect | null {\n const entry = this.entries.get(entity);\n if (!entry) return null;\n return shapeBoundsInto(entry.shape, entry.x, entry.y, { x: 0, y: 0, width: 0, height: 0 });\n }\n\n /** Remove all entities. */\n clear(): void {\n this.entries.clear();\n this.spatial.clear();\n this.pairBufA.clear();\n this.pairBufB.clear();\n this.activePairs = this.pairBufA;\n }\n\n /** Reused across raycasts — see `raycast`. */\n private raycastBounds: Rect = { x: 0, y: 0, width: 0, height: 0 };\n private raycastCandidates: number[] = [];\n private raycastHit: RaycastHit = {\n entity: -1,\n distance: 0,\n point: { x: 0, y: 0 },\n normal: { x: 0, y: 0 },\n };\n\n /**\n * Cast a ray against the colliders in this world.\n *\n * The standalone `raycast()` export tests a caller-supplied target array, so\n * it neither sees the world's colliders nor benefits from the broadphase — an\n * O(n) scan sitting beside an O(1) index. This walks only the cells the ray's\n * bounding box touches.\n *\n * `mask` is a layer bitmask filtered against each collider's `layer`, using the\n * bits colliders already carry; it defaults to every layer. Triggers are\n * included — a ray asking \"what is over there\" wants them.\n *\n * The returned record is **reused between calls**: read it, or copy what you\n * need, before casting again. That is what keeps a per-frame ray allocation-\n * free, and it is why the standalone helper's fresh-object-per-candidate cost\n * was worth removing.\n *\n * `maxDist` must be finite — this requires a bounded ray, not the\n * `Number.POSITIVE_INFINITY` \"unbounded ray\" idiom some engines accept. The\n * broadphase below is a uniform grid: an infinite `maxDist` derives an\n * infinite cell range and never terminates the walk. Pass a large finite\n * bound instead (e.g. the map's diagonal) to cast \"until it leaves the\n * populated area\". A non-finite `maxDist` returns `null`.\n */\n raycast(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n mask = 0xffffffff,\n ): RaycastHit | null {\n if (!Number.isFinite(maxDist)) return null;\n\n // Bounding box of the whole ray — the broadphase is rect-based, so this is\n // a conservative prefilter, not the intersection test itself.\n const ex = ox + dx * maxDist;\n const ey = oy + dy * maxDist;\n const minX = ox < ex ? ox : ex;\n const minY = oy < ey ? oy : ey;\n // Padded so the rect always has non-zero extent on both axes. An axis-\n // aligned ray is otherwise degenerate — a horizontal ray has height 0 — and\n // SpatialIndex derives its cell range with an exclusive end (`_cMax` is\n // `ceil(v / CELL) - 1`), so a zero-height rect yields cyMin 0 > cyMax -1 and\n // the cell loop never runs. The broadphase is a conservative prefilter\n // anyway, so over-including by a hair costs nothing: the exact ray test\n // below rejects the extras.\n this.raycastBounds.x = minX - RAY_BOUNDS_PAD;\n this.raycastBounds.y = minY - RAY_BOUNDS_PAD;\n this.raycastBounds.width = Math.abs(ex - ox) + RAY_BOUNDS_PAD * 2;\n this.raycastBounds.height = Math.abs(ey - oy) + RAY_BOUNDS_PAD * 2;\n\n this.raycastCandidates.length = 0;\n this.spatial.query(this.raycastBounds, this.raycastCandidates);\n\n let bestDist = Number.POSITIVE_INFINITY;\n let found = false;\n for (let i = 0; i < this.raycastCandidates.length; i++) {\n const entry = this.entries.get(this.raycastCandidates[i]!);\n if (entry === undefined) continue;\n if ((entry.layer & mask) === 0) continue;\n const hit = rayVsShapeInto(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n entry.entity,\n entry.shape,\n entry.x,\n entry.y,\n this.scratchHit,\n );\n if (hit !== null && hit.distance < bestDist) {\n bestDist = hit.distance;\n found = true;\n this.raycastHit.entity = hit.entity;\n this.raycastHit.distance = hit.distance;\n this.raycastHit.point.x = hit.point.x;\n this.raycastHit.point.y = hit.point.y;\n this.raycastHit.normal.x = hit.normal.x;\n this.raycastHit.normal.y = hit.normal.y;\n }\n }\n return found ? this.raycastHit : null;\n }\n\n /** Scratch record handed to `rayVsShapeInto` per candidate. */\n private scratchHit: RaycastHit = {\n entity: -1,\n distance: 0,\n point: { x: 0, y: 0 },\n normal: { x: 0, y: 0 },\n };\n}\n","import type { Vec2 } from '@flare-engine/math';\n\n/**\n * Minimal structural type for a solid tile position in grid coordinates.\n * `TileCollider[]` from `@flare-engine/tilemap` is structurally assignable here\n * so callers can pass `extractColliders(map)` directly — no tilemap dep needed.\n */\nexport interface SolidTile {\n readonly row: number;\n readonly col: number;\n}\n\n/**\n * Precomputed solid-tile lookup used by `isBlockedAABB` and `moveAndSlideInto`.\n * `solid` has `width * height` cells indexed `row * width + col`; non-zero means\n * solid. Build once per map load via `buildSolidGrid`.\n */\nexport interface SolidGrid {\n readonly width: number;\n readonly height: number;\n readonly tileSize: number;\n readonly solid: Uint8Array;\n}\n\n/**\n * Build a `SolidGrid` from a list of solid tile positions.\n * Input is structurally typed (`readonly SolidTile[]`) so the caller does not\n * need to import `@flare-engine/tilemap` — physics stays tilemap-free.\n */\nexport function buildSolidGrid(\n width: number,\n height: number,\n tileSize: number,\n tiles: readonly SolidTile[],\n): SolidGrid {\n const solid = new Uint8Array(width * height);\n for (const t of tiles) {\n solid[t.row * width + t.col] = 1;\n }\n return { width, height, tileSize, solid };\n}\n\n/**\n * Half-open boundary constant: the AABB is treated as [min, max) so a box\n * whose max edge lands exactly on a tile boundary does NOT overlap that tile.\n */\nconst EDGE = 1e-4;\n\n/**\n * Returns `true` when an axis-aligned box centered at (`x`, `y`) with\n * half-extents (`halfW`, `halfH`) overlaps a solid tile or lies outside the\n * map boundary. Uses half-open EDGE semantics on max edges.\n */\nexport function isBlockedAABB(\n grid: SolidGrid,\n x: number,\n y: number,\n halfW: number,\n halfH: number,\n): boolean {\n const ts = grid.tileSize;\n const minCol = Math.floor((x - halfW) / ts);\n const maxCol = Math.floor((x + halfW - EDGE) / ts);\n const minRow = Math.floor((y - halfH) / ts);\n const maxRow = Math.floor((y + halfH - EDGE) / ts);\n if (minCol < 0 || minRow < 0 || maxCol >= grid.width || maxRow >= grid.height) {\n return true;\n }\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n if (grid.solid[row * grid.width + col] !== 0) {\n return true;\n }\n }\n }\n return false;\n}\n\n/**\n * Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis\n * is applied independently so a blocked diagonal input slides along the wall.\n * The y-check uses the already-updated x position.\n * Zero-alloc: writes result into the provided `out` Vec2 and returns it.\n */\nexport function moveAndSlideInto(\n grid: SolidGrid,\n x: number,\n y: number,\n dx: number,\n dy: number,\n halfW: number,\n halfH: number,\n out: Vec2,\n): Vec2 {\n let nx = x;\n let ny = y;\n if (dx !== 0 && !isBlockedAABB(grid, nx + dx, ny, halfW, halfH)) nx += dx;\n if (dy !== 0 && !isBlockedAABB(grid, nx, ny + dy, halfW, halfH)) ny += dy;\n out.x = nx;\n out.y = ny;\n return out;\n}\n"],"mappings":";;;;AAeA,SAAgB,eAAe,OAAc,MAAmD;CAC9F,OAAO;EACL;EACA,WAAW,MAAM,aAAa;EAC9B,OAAO,MAAM,SAAS;EACtB,MAAM,MAAM,QAAQ;CACtB;AACF;;;;;;;;;;;ACUA,SAAgB,QACd,IACA,IACA,IACA,IACA,SACA,SACmB;CACnB,IAAI,UAA6B;CAEjC,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,MAAM,IAAI,QAAQ;EAClB,MAAM,MAAM,WAAW,IAAI,IAAI,IAAI,IAAI,SAAS,CAAC;EACjD,IAAI,QAAQ,YAAY,QAAQ,IAAI,WAAW,QAAQ,WACrD,UAAU;CAEd;CAEA,OAAO;AACT;AAEA,SAAS,WACP,IACA,IACA,IACA,IACA,SACA,QACmB;CACnB,IAAI,OAAO,MAAM,SAAS,QACxB,OAAO,UACL,IACA,IACA,IACA,IACA,SACA,OAAO,QACP,OAAO,GACP,OAAO,GACP,OAAO,MAAM,IACb,OAAO,MAAM,EACf;CAEF,OAAO,YACL,IACA,IACA,IACA,IACA,SACA,OAAO,QACP,OAAO,GACP,OAAO,GACP,OAAO,MAAM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AAQ,EAAE,OAAO;CAEjC,OAAO;EAAE;EAAO;EAAQ;EAAU;CAAM;AAC1C;;;;;AAMA,MAAM,OAAO;;;;;;AAOb,SAAgB,cACd,MACA,GACA,GACA,OACA,OACS;CACT,MAAM,KAAK,KAAK;CAChB,MAAM,SAAS,KAAK,OAAO,IAAI,SAAS,EAAE;CAC1C,MAAM,SAAS,KAAK,OAAO,IAAI,QAAQ,QAAQ,EAAE;CACjD,MAAM,SAAS,KAAK,OAAO,IAAI,SAAS,EAAE;CAC1C,MAAM,SAAS,KAAK,OAAO,IAAI,QAAQ,QAAQ,EAAE;CACjD,IAAI,SAAS,KAAK,SAAS,KAAK,UAAU,KAAK,SAAS,UAAU,KAAK,QACrE,OAAO;CAET,KAAK,IAAI,MAAM,QAAQ,OAAO,QAAQ,OACpC,KAAK,IAAI,MAAM,QAAQ,OAAO,QAAQ,OACpC,IAAI,KAAK,MAAM,MAAM,KAAK,QAAQ,SAAS,GACzC,OAAO;CAIb,OAAO;AACT;;;;;;;AAQA,SAAgB,iBACd,MACA,GACA,GACA,IACA,IACA,OACA,OACA,KACM;CACN,IAAI,KAAK;CACT,IAAI,KAAK;CACT,IAAI,OAAO,KAAK,CAAC,cAAc,MAAM,KAAK,IAAI,IAAI,OAAO,KAAK,GAAG,MAAM;CACvE,IAAI,OAAO,KAAK,CAAC,cAAc,MAAM,IAAI,KAAK,IAAI,OAAO,KAAK,GAAG,MAAM;CACvE,IAAI,IAAI;CACR,IAAI,IAAI;CACR,OAAO;AACT"}
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Signal } from "@flare-engine/events";
2
- //#region src/collider.ts
2
+ // src/collider.ts
3
3
  /** Create a collider with default settings. */
4
4
  function createCollider(shape, opts) {
5
5
  return {
@@ -9,8 +9,7 @@ function createCollider(shape, opts) {
9
9
  mask: opts?.mask ?? 4294967295
10
10
  };
11
11
  }
12
- //#endregion
13
- //#region src/raycast.ts
12
+ // src/raycast.ts
14
13
  /**
15
14
  * Cast a ray against a list of targets. Returns the closest hit, or null.
16
15
  * @param ox Ray origin X
@@ -174,8 +173,7 @@ function rayVsCircleInto(ox, oy, dx, dy, maxDist, entity, cx, cy, radius, out) {
174
173
  out.normal.y = len > 0 ? ny / len : 0;
175
174
  return out;
176
175
  }
177
- //#endregion
178
- //#region src/shape.ts
176
+ // src/shape.ts
179
177
  /** Create an AABB shape from width and height. */
180
178
  function AABB(width, height) {
181
179
  return {
@@ -352,8 +350,7 @@ function aabbCircleOverlap(ax, ay, ahw, ahh, cx, cy, cr, out, flip) {
352
350
  out.depth = depth + cr;
353
351
  return out;
354
352
  }
355
- //#endregion
356
- //#region src/spatial-index.ts
353
+ // src/spatial-index.ts
357
354
  const CELL = 64;
358
355
  const KEY_RANGE = 65536;
359
356
  const KEY_OFFSET = 32768;
@@ -367,7 +364,7 @@ function _cMax(v) {
367
364
  return Math.ceil(v / CELL) - 1;
368
365
  }
369
366
  const MIN_CELL = -32768;
370
- const MAX_CELL = KEY_OFFSET - 1;
367
+ const MAX_CELL = 32767;
371
368
  function _clampCell(c) {
372
369
  if (c > MAX_CELL) return MAX_CELL;
373
370
  if (c < MIN_CELL) return MIN_CELL;
@@ -493,8 +490,7 @@ var SpatialIndex = class {
493
490
  }
494
491
  }
495
492
  };
496
- //#endregion
497
- //#region src/collision-world.ts
493
+ // src/collision-world.ts
498
494
  const ENTITY_SHIFT = 2 ** 26;
499
495
  /** Padding applied to a ray's query AABB — see `CollisionWorld.raycast`. */
500
496
  const RAY_BOUNDS_PAD = 1e-6;
@@ -760,8 +756,7 @@ var CollisionWorld = class {
760
756
  }
761
757
  };
762
758
  };
763
- //#endregion
764
- //#region src/solid-grid.ts
759
+ // src/solid-grid.ts
765
760
  /**
766
761
  * Build a `SolidGrid` from a list of solid tile positions.
767
762
  * Input is structurally typed (`readonly SolidTile[]`) so the caller does not
@@ -812,7 +807,6 @@ function moveAndSlideInto(grid, x, y, dx, dy, halfW, halfH, out) {
812
807
  out.y = ny;
813
808
  return out;
814
809
  }
815
- //#endregion
816
810
  export { AABB, CircleShape, CollisionWorld, SpatialIndex, buildSolidGrid, createCollider, createOverlap, isBlockedAABB, moveAndSlideInto, overlap, overlapInto, rayVsShapeInto, raycast, shapeBounds, shapeBoundsInto, shapeOverlap };
817
811
 
818
812
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/collider.ts","../src/raycast.ts","../src/shape.ts","../src/spatial-index.ts","../src/collision-world.ts","../src/solid-grid.ts"],"sourcesContent":["import type { Shape } from './shape.js';\n\n/** Collider component data attached to an ECS entity. */\nexport interface Collider {\n /** The collision shape. */\n shape: Shape;\n /** If true, generates trigger events but no physical response. */\n isTrigger: boolean;\n /** Collision layer bitmask — which layer this collider belongs to. */\n layer: number;\n /** Collision mask — which layers this collider can interact with. */\n mask: number;\n}\n\n/** Create a collider with default settings. */\nexport function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider {\n return {\n shape,\n isTrigger: opts?.isTrigger ?? false,\n layer: opts?.layer ?? 1,\n mask: opts?.mask ?? 0xffffffff,\n };\n}\n","import type { Vec2 } from '@flare-engine/math';\nimport type { Shape } from './shape.js';\n\n/** Result of a raycast hit. */\nexport interface RaycastHit {\n /** The entity that was hit. */\n entity: number;\n /** Distance from ray origin to hit point. */\n distance: number;\n /** World-space hit point. */\n point: Vec2;\n /** Surface normal at hit point. */\n normal: Vec2;\n}\n\n/** Entity entry for raycast — position + shape. */\nexport interface RaycastTarget {\n entity: number;\n shape: Shape;\n x: number;\n y: number;\n}\n\n/**\n * Cast a ray against a list of targets. Returns the closest hit, or null.\n * @param ox Ray origin X\n * @param oy Ray origin Y\n * @param dx Ray direction X (should be normalized)\n * @param dy Ray direction Y (should be normalized)\n * @param maxDist Maximum raycast distance\n * @param targets Array of entities to test against\n */\nexport function raycast(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n targets: readonly RaycastTarget[],\n): RaycastHit | null {\n let closest: RaycastHit | null = null;\n\n for (let i = 0; i < targets.length; i++) {\n const t = targets[i]!;\n const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);\n if (hit && (closest === null || hit.distance < closest.distance)) {\n closest = hit;\n }\n }\n\n return closest;\n}\n\nfunction rayVsShape(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n target: RaycastTarget,\n): RaycastHit | null {\n if (target.shape.type === 'aabb') {\n return rayVsAABB(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n target.entity,\n target.x,\n target.y,\n target.shape.hw,\n target.shape.hh,\n );\n }\n return rayVsCircle(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n target.entity,\n target.x,\n target.y,\n target.shape.radius,\n );\n}\n\nfunction rayVsAABB(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n hw: number,\n hh: number,\n): RaycastHit | null {\n const minX = cx - hw;\n const maxX = cx + hw;\n const minY = cy - hh;\n const maxY = cy + hh;\n\n const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;\n const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;\n\n const tx1 = (minX - ox) * invDx;\n const tx2 = (maxX - ox) * invDx;\n const ty1 = (minY - oy) * invDy;\n const ty2 = (maxY - oy) * invDy;\n\n const tMinX = Math.min(tx1, tx2);\n const tMaxX = Math.max(tx1, tx2);\n const tMinY = Math.min(ty1, ty2);\n const tMaxY = Math.max(ty1, ty2);\n\n const tMin = Math.max(tMinX, tMinY);\n const tMax = Math.min(tMaxX, tMaxY);\n\n if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;\n\n const t = tMin >= 0 ? tMin : tMax;\n if (t > maxDist || t < 0) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n\n // Compute normal\n let nx = 0;\n let ny = 0;\n if (tMinX > tMinY) {\n nx = dx < 0 ? 1 : -1;\n } else {\n ny = dy < 0 ? 1 : -1;\n }\n\n return {\n entity,\n distance: t,\n point: { x: px, y: py },\n normal: { x: nx, y: ny },\n };\n}\n\nfunction rayVsCircle(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n radius: number,\n): RaycastHit | null {\n const fx = ox - cx;\n const fy = oy - cy;\n\n const a = dx * dx + dy * dy;\n const b = 2 * (fx * dx + fy * dy);\n const c = fx * fx + fy * fy - radius * radius;\n\n if (a < 1e-12) return null; // Zero-length direction\n\n const discriminant = b * b - 4 * a * c;\n if (discriminant < 0) return null;\n\n const sqrtDisc = Math.sqrt(discriminant);\n let t = (-b - sqrtDisc) / (2 * a);\n\n // If entry point is behind, try exit point\n if (t < 0) {\n t = (-b + sqrtDisc) / (2 * a);\n }\n if (t < 0 || t > maxDist) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n const nx = px - cx;\n const ny = py - cy;\n const len = Math.sqrt(nx * nx + ny * ny);\n\n return {\n entity,\n distance: t,\n point: { x: px, y: py },\n normal: len > 0 ? { x: nx / len, y: ny / len } : { x: 0, y: 0 },\n };\n}\n\n/**\n * Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns\n * `null` without touching `out`.\n *\n * The allocating `raycast()` above builds a fresh `RaycastHit` — with two nested\n * `Vec2` objects — for **every candidate it tests**, then discards all but the\n * closest. That is three objects per candidate per ray. This variant exists so\n * `CollisionWorld.raycast` can test a whole broadphase cell without allocating.\n */\nexport function rayVsShapeInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n shape: Shape,\n x: number,\n y: number,\n out: RaycastHit,\n): RaycastHit | null {\n if (shape.type === 'aabb') {\n return rayVsAABBInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.hw, shape.hh, out);\n }\n return rayVsCircleInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.radius, out);\n}\n\nfunction rayVsAABBInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n bx: number,\n by: number,\n hw: number,\n hh: number,\n out: RaycastHit,\n): RaycastHit | null {\n const invDx = dx !== 0 ? 1 / dx : Number.POSITIVE_INFINITY;\n const invDy = dy !== 0 ? 1 / dy : Number.POSITIVE_INFINITY;\n\n const tx1 = (bx - hw - ox) * invDx;\n const tx2 = (bx + hw - ox) * invDx;\n const ty1 = (by - hh - oy) * invDy;\n const ty2 = (by + hh - oy) * invDy;\n\n const tMinX = Math.min(tx1, tx2);\n const tMaxX = Math.max(tx1, tx2);\n const tMinY = Math.min(ty1, ty2);\n const tMaxY = Math.max(ty1, ty2);\n\n const tMin = Math.max(tMinX, tMinY);\n const tMax = Math.min(tMaxX, tMaxY);\n if (tMax < 0 || tMin > tMax) return null;\n\n const t = tMin < 0 ? tMax : tMin;\n if (t > maxDist || t < 0) return null;\n\n out.entity = entity;\n out.distance = t;\n out.point.x = ox + dx * t;\n out.point.y = oy + dy * t;\n if (tMinX > tMinY) {\n out.normal.x = dx < 0 ? 1 : -1;\n out.normal.y = 0;\n } else {\n out.normal.x = 0;\n out.normal.y = dy < 0 ? 1 : -1;\n }\n return out;\n}\n\nfunction rayVsCircleInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n radius: number,\n out: RaycastHit,\n): RaycastHit | null {\n const fx = ox - cx;\n const fy = oy - cy;\n const a = dx * dx + dy * dy;\n if (a < 1e-12) return null;\n\n const b = 2 * (fx * dx + fy * dy);\n const c = fx * fx + fy * fy - radius * radius;\n const discriminant = b * b - 4 * a * c;\n if (discriminant < 0) return null;\n\n const sqrtDisc = Math.sqrt(discriminant);\n let t = (-b - sqrtDisc) / (2 * a);\n if (t < 0) t = (-b + sqrtDisc) / (2 * a);\n if (t < 0 || t > maxDist) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n const nx = px - cx;\n const ny = py - cy;\n const len = Math.sqrt(nx * nx + ny * ny);\n\n out.entity = entity;\n out.distance = t;\n out.point.x = px;\n out.point.y = py;\n out.normal.x = len > 0 ? nx / len : 0;\n out.normal.y = len > 0 ? ny / len : 0;\n return out;\n}\n","import type { Rect } from '@flare-engine/math';\n\n/** AABB shape — axis-aligned bounding box. */\nexport interface AABBShape {\n readonly type: 'aabb';\n /** Half-width. */\n readonly hw: number;\n /** Half-height. */\n readonly hh: number;\n}\n\n/** Circle shape. */\nexport interface CircleShape {\n readonly type: 'circle';\n readonly radius: number;\n}\n\n/** Union of supported physics shapes. */\nexport type Shape = AABBShape | CircleShape;\n\n/** Create an AABB shape from width and height. */\nexport function AABB(width: number, height: number): AABBShape {\n return { type: 'aabb', hw: width * 0.5, hh: height * 0.5 };\n}\n\n/** Create a circle shape from radius. */\nexport function CircleShape(radius: number): CircleShape {\n return { type: 'circle', radius };\n}\n\n/** Compute the bounding rect of a shape at a given position. */\nexport function shapeBounds(shape: Shape, x: number, y: number): Rect {\n if (shape.type === 'aabb') {\n return { x: x - shape.hw, y: y - shape.hh, width: shape.hw * 2, height: shape.hh * 2 };\n }\n return {\n x: x - shape.radius,\n y: y - shape.radius,\n width: shape.radius * 2,\n height: shape.radius * 2,\n };\n}\n\n/** Zero-alloc variant — writes the shape bounds into the provided output rect. */\nexport function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect {\n if (shape.type === 'aabb') {\n out.x = x - shape.hw;\n out.y = y - shape.hh;\n out.width = shape.hw * 2;\n out.height = shape.hh * 2;\n return out;\n }\n out.x = x - shape.radius;\n out.y = y - shape.radius;\n out.width = shape.radius * 2;\n out.height = shape.radius * 2;\n return out;\n}\n\n/** Test overlap between two positioned shapes. Returns true if overlapping. */\nexport function shapeOverlap(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n): boolean {\n if (shapeA.type === 'aabb' && shapeB.type === 'aabb') {\n return aabbVsAabb(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh);\n }\n if (shapeA.type === 'circle' && shapeB.type === 'circle') {\n return circleVsCircle(ax, ay, shapeA.radius, bx, by, shapeB.radius);\n }\n // Mixed: one AABB, one circle\n if (shapeA.type === 'aabb' && shapeB.type === 'circle') {\n return aabbVsCircle(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius);\n }\n return aabbVsCircle(\n bx,\n by,\n (shapeB as AABBShape).hw,\n (shapeB as AABBShape).hh,\n ax,\n ay,\n (shapeA as CircleShape).radius,\n );\n}\n\nfunction aabbVsAabb(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n bx: number,\n by: number,\n bhw: number,\n bhh: number,\n): boolean {\n return Math.abs(ax - bx) < ahw + bhw && Math.abs(ay - by) < ahh + bhh;\n}\n\nfunction circleVsCircle(\n ax: number,\n ay: number,\n ar: number,\n bx: number,\n by: number,\n br: number,\n): boolean {\n const dx = bx - ax;\n const dy = by - ay;\n const radSum = ar + br;\n return dx * dx + dy * dy < radSum * radSum;\n}\n\nfunction aabbVsCircle(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n cx: number,\n cy: number,\n cr: number,\n): boolean {\n const left = ax - ahw;\n const right = ax + ahw;\n const top = ay - ahh;\n const bottom = ay + ahh;\n const closestX = cx < left ? left : cx > right ? right : cx;\n const closestY = cy < top ? top : cy > bottom ? bottom : cy;\n const dx = cx - closestX;\n const dy = cy - closestY;\n const distSq = dx * dx + dy * dy;\n // Strict, like aabbVsAabb and circleVsCircle: exactly-touching shapes do not\n // collide. A centre on or inside the box collides when the radius is positive;\n // a zero radius counts only strictly inside, as a zero-size box does.\n if (distSq > 0) return distSq < cr * cr;\n return cr > 0 || (cx > left && cx < right && cy > top && cy < bottom);\n}\n\n/**\n * Minimum translation vector: the shortest push that separates two overlapping\n * shapes.\n *\n * `shapeOverlap` answers *whether* two shapes collide and nothing more, which\n * leaves a detection-only consumer with no way to resolve the collision. This is\n * that missing step: `out.x`/`out.y` are a unit axis and `out.depth` is how far\n * along it `b` must move to just stop touching `a`.\n *\n * Returns `null` when the shapes are not overlapping, leaving `out` untouched so\n * a caller may reuse one buffer across many pairs.\n *\n * The degenerate concentric case (identical centres) has no meaningful axis;\n * `(1, 0)` is returned so the caller still gets a usable separation instead of\n * `NaN` from normalising a zero vector.\n */\nexport interface Overlap {\n /** Unit separation axis, pointing from `a` toward `b`. */\n x: number;\n y: number;\n /** Penetration depth along that axis. */\n depth: number;\n}\n\n/** A fresh, zeroed overlap record. Allocate once outside the loop and reuse. */\nexport function createOverlap(): Overlap {\n return { x: 0, y: 0, depth: 0 };\n}\n\nexport function overlapInto(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n out: Overlap,\n): Overlap | null {\n if (shapeA.type === 'aabb' && shapeB.type === 'aabb') {\n return aabbAabbOverlap(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh, out);\n }\n if (shapeA.type === 'circle' && shapeB.type === 'circle') {\n return circleCircleOverlap(ax, ay, shapeA.radius, bx, by, shapeB.radius, out);\n }\n if (shapeA.type === 'aabb' && shapeB.type === 'circle') {\n return aabbCircleOverlap(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius, out, false);\n }\n // Circle vs AABB — solve as AABB vs circle, then flip the axis so it still\n // points from `a` to `b`.\n return aabbCircleOverlap(\n bx,\n by,\n (shapeB as AABBShape).hw,\n (shapeB as AABBShape).hh,\n ax,\n ay,\n (shapeA as CircleShape).radius,\n out,\n true,\n );\n}\n\n/** Minimum translation vector. Allocates — see `overlapInto` for the hot path. */\nexport function overlap(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n): Overlap | null {\n return overlapInto(shapeA, ax, ay, shapeB, bx, by, createOverlap());\n}\n\nfunction aabbAabbOverlap(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n bx: number,\n by: number,\n bhw: number,\n bhh: number,\n out: Overlap,\n): Overlap | null {\n const dx = bx - ax;\n const dy = by - ay;\n const overlapX = ahw + bhw - Math.abs(dx);\n if (overlapX <= 0) return null;\n const overlapY = ahh + bhh - Math.abs(dy);\n if (overlapY <= 0) return null;\n\n // The minimum translation is along whichever axis is least penetrated.\n if (overlapX < overlapY) {\n out.x = dx < 0 ? -1 : 1;\n out.y = 0;\n out.depth = overlapX;\n } else {\n out.x = 0;\n out.y = dy < 0 ? -1 : 1;\n out.depth = overlapY;\n }\n return out;\n}\n\nfunction circleCircleOverlap(\n ax: number,\n ay: number,\n ar: number,\n bx: number,\n by: number,\n br: number,\n out: Overlap,\n): Overlap | null {\n const dx = bx - ax;\n const dy = by - ay;\n const radSum = ar + br;\n const distSq = dx * dx + dy * dy;\n if (distSq >= radSum * radSum) return null;\n\n const dist = Math.sqrt(distSq);\n if (dist === 0) {\n // Concentric: no meaningful axis, so pick one rather than divide by zero.\n out.x = 1;\n out.y = 0;\n out.depth = radSum;\n return out;\n }\n out.x = dx / dist;\n out.y = dy / dist;\n out.depth = radSum - dist;\n return out;\n}\n\nfunction aabbCircleOverlap(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n cx: number,\n cy: number,\n cr: number,\n out: Overlap,\n flip: boolean,\n): Overlap | null {\n const left = ax - ahw;\n const right = ax + ahw;\n const top = ay - ahh;\n const bottom = ay + ahh;\n const closestX = cx < left ? left : cx > right ? right : cx;\n const closestY = cy < top ? top : cy > bottom ? bottom : cy;\n const dx = cx - closestX;\n const dy = cy - closestY;\n const distSq = dx * dx + dy * dy;\n\n if (distSq > 0) {\n // Centre outside the box: separate along the closest-point normal.\n if (distSq >= cr * cr) return null;\n const dist = Math.sqrt(distSq);\n const nx = dx / dist;\n const ny = dy / dist;\n out.x = flip ? -nx : nx;\n out.y = flip ? -ny : ny;\n out.depth = cr - dist;\n return out;\n }\n\n // Centre inside the box: push out through the nearest face.\n const toLeft = cx - left;\n const toRight = right - cx;\n const toTop = cy - top;\n const toBottom = bottom - cy;\n let nx = -1;\n let ny = 0;\n let depth = toLeft;\n if (toRight < depth) {\n nx = 1;\n ny = 0;\n depth = toRight;\n }\n if (toTop < depth) {\n nx = 0;\n ny = -1;\n depth = toTop;\n }\n if (toBottom < depth) {\n nx = 0;\n ny = 1;\n depth = toBottom;\n }\n // Zero radius on the boundary is exactly touching, not overlapping.\n if (depth + cr <= 0) return null;\n out.x = flip ? -nx : nx;\n out.y = flip ? -ny : ny;\n out.depth = depth + cr;\n return out;\n}\n","import type { Rect } from '@flare-engine/math';\n\n// Internal entry: bounding rect (minX, minY, maxX, maxY) plus a per-query stamp\n// for zero-alloc dedup.\ninterface _Entry<T> {\n readonly item: T;\n minX: number;\n minY: number;\n maxX: number;\n maxY: number;\n _stamp: number;\n}\n\n// Uniform grid cell size in world units. Tuned for typical game-object sizes (16–64 px).\nconst CELL = 64;\n\n// Packed cell-key encoding. Supports cell coords in [-32768, 32767],\n// i.e. world coordinates ±2 097 152. All keys fit within Number.MAX_SAFE_INTEGER.\nconst KEY_RANGE = 65536; // 2^16\nconst KEY_OFFSET = 32768; // 2^15\n\nfunction _key(cx: number, cy: number): number {\n return (cx + KEY_OFFSET) * KEY_RANGE + (cy + KEY_OFFSET);\n}\n\n// Inclusive min cell that contains world coordinate v.\nfunction _cMin(v: number): number {\n return Math.floor(v / CELL);\n}\n\n// Inclusive max cell that contains world coordinate v (exclusive-end: a coord\n// falling exactly on a cell boundary is NOT part of the adjacent cell — matches\n// the strict-inequality overlap semantics of query()).\nfunction _cMax(v: number): number {\n return Math.ceil(v / CELL) - 1;\n}\n\n// Supported cell-coordinate range for the packed key above.\nconst MIN_CELL = -KEY_OFFSET;\nconst MAX_CELL = KEY_OFFSET - 1;\n\n// Bounds a derived cell coordinate to the packed-key range. Without this, a\n// non-finite world coordinate (±Infinity — an unbounded raycast maxDist, or an\n// entity parked at Infinity) makes cxMin/cxMax/cyMin/cyMax infinite, and\n// `for (cx = cxMin; cx <= cxMax; cx++)` never exits: once cx itself is\n// Infinity, `cx <= Infinity` stays true and `cx++` is a no-op, so the loop\n// spins forever pushing/reading the same bucket (unbounded memory growth on\n// the link side). NaN is left untouched: every comparison against NaN is\n// false, so the loop already runs zero times, which is the existing (safe)\n// behaviour for a degenerate direction like `0 * Infinity`.\nfunction _clampCell(c: number): number {\n if (c > MAX_CELL) return MAX_CELL;\n if (c < MIN_CELL) return MIN_CELL;\n return c;\n}\n\n/**\n * Uniform spatial-hash broadphase.\n * `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.\n * `query` walks only the cells overlapping the given rect; result order is unspecified.\n * Zero-alloc in the query hot-path: duplicate suppression uses a monotone stamp counter,\n * not a per-call Set.\n */\nexport class SpatialIndex<T> {\n private _data: _Entry<T>[] = [];\n private _slots = new Map<T, number>(); // item → dense-array slot\n private _cells = new Map<number, _Entry<T>[]>(); // packed cell key → bucket\n private _gen = 0; // monotone query-generation counter\n\n /** Number of items in the index. */\n get size(): number {\n return this._data.length;\n }\n\n /** Insert an item with a bounding rect. */\n insert(item: T, bounds: Rect): void {\n const entry: _Entry<T> = {\n item,\n minX: bounds.x,\n minY: bounds.y,\n maxX: bounds.x + bounds.width,\n maxY: bounds.y + bounds.height,\n _stamp: 0,\n };\n this._slots.set(item, this._data.length);\n this._data.push(entry);\n this._link(entry);\n }\n\n /** Remove an item. Returns false when the item was not present. */\n remove(item: T): boolean {\n const slot = this._slots.get(item);\n if (slot === undefined) return false;\n const entry = this._data[slot]!;\n this._unlink(entry);\n this._slots.delete(item);\n // Swap-remove: plug the hole with the last element.\n const last = this._data.pop()!;\n if (slot < this._data.length) {\n this._data[slot] = last;\n this._slots.set(last.item, slot);\n }\n return true;\n }\n\n /** Update the bounds for an existing item. No-op when the item is absent. */\n update(item: T, bounds: Rect): void {\n const slot = this._slots.get(item);\n if (slot === undefined) return;\n const entry = this._data[slot]!;\n this._unlink(entry);\n entry.minX = bounds.x;\n entry.minY = bounds.y;\n entry.maxX = bounds.x + bounds.width;\n entry.maxY = bounds.y + bounds.height;\n this._link(entry);\n }\n\n /**\n * Query all items whose bounds overlap the given rect.\n * Result order is unspecified. Appends to `result` when provided and returns it.\n */\n query(bounds: Rect, result?: T[]): T[] {\n const out = result ?? [];\n const gen = ++this._gen;\n\n const qMinX = bounds.x;\n const qMinY = bounds.y;\n const qMaxX = bounds.x + bounds.width;\n const qMaxY = bounds.y + bounds.height;\n\n const cxMin = _clampCell(_cMin(qMinX));\n const cxMax = _clampCell(_cMax(qMaxX));\n const cyMin = _clampCell(_cMin(qMinY));\n const cyMax = _clampCell(_cMax(qMaxY));\n\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const bucket = this._cells.get(_key(cx, cy));\n if (bucket === undefined) continue;\n for (let i = 0; i < bucket.length; i++) {\n const e = bucket[i]!;\n if (e._stamp === gen) continue; // already emitted this query\n e._stamp = gen;\n if (e.maxX > qMinX && e.minX < qMaxX && e.maxY > qMinY && e.minY < qMaxY) {\n out.push(e.item);\n }\n }\n }\n }\n return out;\n }\n\n /** Remove all items. */\n clear(): void {\n this._data.length = 0;\n this._slots.clear();\n this._cells.clear();\n }\n\n // Register entry in all grid cells it overlaps.\n private _link(entry: _Entry<T>): void {\n const cxMin = _clampCell(_cMin(entry.minX));\n const cxMax = _clampCell(_cMax(entry.maxX));\n const cyMin = _clampCell(_cMin(entry.minY));\n const cyMax = _clampCell(_cMax(entry.maxY));\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const k = _key(cx, cy);\n let b = this._cells.get(k);\n if (b === undefined) {\n b = [];\n this._cells.set(k, b);\n }\n b.push(entry);\n }\n }\n }\n\n // Remove entry from all grid cells it currently occupies.\n private _unlink(entry: _Entry<T>): void {\n const cxMin = _clampCell(_cMin(entry.minX));\n const cxMax = _clampCell(_cMax(entry.maxX));\n const cyMin = _clampCell(_cMin(entry.minY));\n const cyMax = _clampCell(_cMax(entry.maxY));\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const b = this._cells.get(_key(cx, cy));\n if (b === undefined) continue;\n const i = b.indexOf(entry);\n if (i !== -1) {\n // Swap-remove within the bucket.\n const last = b.pop()!;\n if (i < b.length) b[i] = last;\n }\n }\n }\n }\n}\n","import { Signal } from '@flare-engine/events';\nimport type { Rect } from '@flare-engine/math';\nimport type { Collider } from './collider.js';\nimport type { RaycastHit } from './raycast.js';\nimport { rayVsShapeInto } from './raycast.js';\nimport type { Shape } from './shape.js';\nimport { shapeBoundsInto, shapeOverlap } from './shape.js';\nimport { SpatialIndex } from './spatial-index.js';\n\n/** Collision pair data emitted by collision signals. */\nexport interface CollisionPair {\n readonly entityA: number;\n readonly entityB: number;\n}\n\n/** Per-entity collision data stored in the world. */\ninterface CollisionEntry {\n entity: number;\n shape: Shape;\n x: number;\n y: number;\n isTrigger: boolean;\n layer: number;\n mask: number;\n}\n\n// Packed pair-key layout: `lo * ENTITY_SHIFT + hi`, lo = min(a,b), hi = max(a,b).\n// 2^26 fits within Number.MAX_SAFE_INTEGER for 26+26 bits → max entity id ~67M.\nconst ENTITY_SHIFT = 2 ** 26;\n\n/** Padding applied to a ray's query AABB — see `CollisionWorld.raycast`. */\nconst RAY_BOUNDS_PAD = 1e-6;\n\nfunction encodePair(a: number, b: number): number {\n const lo = a < b ? a : b;\n const hi = a < b ? b : a;\n return lo * ENTITY_SHIFT + hi;\n}\n\nfunction decodeLo(key: number): number {\n return Math.floor(key / ENTITY_SHIFT);\n}\n\nfunction decodeHi(key: number): number {\n return key - Math.floor(key / ENTITY_SHIFT) * ENTITY_SHIFT;\n}\n\n/**\n * AABB collision world. Manages entities with shapes, detects overlaps each step,\n * and emits collision/trigger signals.\n */\nexport class CollisionWorld {\n readonly onCollisionStart = new Signal<CollisionPair>();\n readonly onCollisionEnd = new Signal<CollisionPair>();\n readonly onTriggerEnter = new Signal<CollisionPair>();\n readonly onTriggerExit = new Signal<CollisionPair>();\n\n private entries = new Map<number, CollisionEntry>();\n private spatial = new SpatialIndex<number>();\n /**\n * Two pre-allocated pair buffers — swapped each step (no per-frame allocation).\n * Keys are packed numbers via `encodePair(a, b)`.\n */\n private pairBufA = new Set<number>();\n private pairBufB = new Set<number>();\n private activePairs: Set<number> = this.pairBufA;\n /** Reusable query result buffer. */\n private queryBuf: number[] = [];\n /** Reusable bounds rect — avoids per-entity Rect allocation in step(). */\n private boundsBuf: Rect = { x: 0, y: 0, width: 0, height: 0 };\n\n /** Add an entity with a shape. Positional twin of `addCollider`. */\n add(\n entity: number,\n shape: Shape,\n x: number,\n y: number,\n isTrigger = false,\n layer = 1,\n mask = 0xffffffff,\n ): void {\n const entry: CollisionEntry = { entity, shape, x, y, isTrigger, layer, mask };\n const exists = this.entries.has(entity);\n this.entries.set(entity, entry);\n shapeBoundsInto(shape, x, y, this.boundsBuf);\n if (exists) {\n this.spatial.update(entity, this.boundsBuf);\n } else {\n this.spatial.insert(entity, this.boundsBuf);\n }\n }\n\n /**\n * Add an entity from a `Collider`. Its `shape`, `isTrigger`, `layer` and `mask`\n * are copied at add time — later edits to the collider do not reach the world;\n * re-add to change them. Re-adding an existing entity replaces its data.\n */\n addCollider(entity: number, collider: Collider, x: number, y: number): void {\n this.add(entity, collider.shape, x, y, collider.isTrigger, collider.layer, collider.mask);\n }\n\n /** Remove an entity from the world. Emits exit signals for any active pairs. */\n remove(entity: number): void {\n const entry = this.entries.get(entity);\n if (!entry) return;\n\n this.entries.delete(entity);\n this.spatial.remove(entity);\n\n // Fire exit signals for all active pairs involving this entity\n for (const key of this.activePairs) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n if (lo !== entity && hi !== entity) continue;\n this.activePairs.delete(key);\n const otherEntity = lo === entity ? hi : lo;\n const otherEntry = this.entries.get(otherEntity);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (entry.isTrigger || otherEntry?.isTrigger) {\n this.onTriggerExit.emit(pair);\n } else {\n this.onCollisionEnd.emit(pair);\n }\n }\n }\n\n /** Update an entity's position. */\n setPosition(entity: number, x: number, y: number): void {\n const entry = this.entries.get(entity);\n if (!entry) return;\n entry.x = x;\n entry.y = y;\n shapeBoundsInto(entry.shape, x, y, this.boundsBuf);\n this.spatial.update(entity, this.boundsBuf);\n }\n\n /** Number of entities in the world. */\n get size(): number {\n return this.entries.size;\n }\n\n /** Number of broadphase records (one per live entity after add/remove). Diagnostic for broadphase growth. */\n get broadphaseSize(): number {\n return this.spatial.size;\n }\n\n /** Run one collision detection step. */\n step(): void {\n const newPairs = this.activePairs === this.pairBufA ? this.pairBufB : this.pairBufA;\n newPairs.clear();\n\n for (const entryA of this.entries.values()) {\n const entityA = entryA.entity;\n shapeBoundsInto(entryA.shape, entryA.x, entryA.y, this.boundsBuf);\n this.queryBuf.length = 0;\n this.spatial.query(this.boundsBuf, this.queryBuf);\n\n for (let i = 0; i < this.queryBuf.length; i++) {\n const entityB = this.queryBuf[i]!;\n if (entityB <= entityA) continue; // Avoid self and duplicates\n\n const entryB = this.entries.get(entityB);\n if (!entryB) continue;\n\n // Layer/mask filtering\n if ((entryA.layer & entryB.mask) === 0 && (entryB.layer & entryA.mask) === 0) continue;\n\n // Narrowphase\n if (!shapeOverlap(entryA.shape, entryA.x, entryA.y, entryB.shape, entryB.x, entryB.y)) {\n continue;\n }\n\n newPairs.add(encodePair(entityA, entityB));\n }\n }\n\n // Enter/start events — pair objects are allocated here only on state transitions.\n for (const key of newPairs) {\n if (!this.activePairs.has(key)) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n const a = this.entries.get(lo);\n const b = this.entries.get(hi);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (a?.isTrigger || b?.isTrigger) {\n this.onTriggerEnter.emit(pair);\n } else {\n this.onCollisionStart.emit(pair);\n }\n }\n }\n\n // Exit/end events\n for (const key of this.activePairs) {\n if (!newPairs.has(key)) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n const a = this.entries.get(lo);\n const b = this.entries.get(hi);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (a?.isTrigger || b?.isTrigger) {\n this.onTriggerExit.emit(pair);\n } else {\n this.onCollisionEnd.emit(pair);\n }\n }\n }\n\n this.activePairs = newPairs;\n }\n\n /** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */\n getColliderBoundsInto(entity: number, out: Rect): Rect | null {\n const entry = this.entries.get(entity);\n if (!entry) return null;\n shapeBoundsInto(entry.shape, entry.x, entry.y, out);\n return out;\n }\n\n /** Allocating convenience wrapper — returns a fresh Rect or null when entity is absent. */\n getColliderBounds(entity: number): Rect | null {\n const entry = this.entries.get(entity);\n if (!entry) return null;\n return shapeBoundsInto(entry.shape, entry.x, entry.y, { x: 0, y: 0, width: 0, height: 0 });\n }\n\n /** Remove all entities. */\n clear(): void {\n this.entries.clear();\n this.spatial.clear();\n this.pairBufA.clear();\n this.pairBufB.clear();\n this.activePairs = this.pairBufA;\n }\n\n /** Reused across raycasts — see `raycast`. */\n private raycastBounds: Rect = { x: 0, y: 0, width: 0, height: 0 };\n private raycastCandidates: number[] = [];\n private raycastHit: RaycastHit = {\n entity: -1,\n distance: 0,\n point: { x: 0, y: 0 },\n normal: { x: 0, y: 0 },\n };\n\n /**\n * Cast a ray against the colliders in this world.\n *\n * The standalone `raycast()` export tests a caller-supplied target array, so\n * it neither sees the world's colliders nor benefits from the broadphase — an\n * O(n) scan sitting beside an O(1) index. This walks only the cells the ray's\n * bounding box touches.\n *\n * `mask` is a layer bitmask filtered against each collider's `layer`, using the\n * bits colliders already carry; it defaults to every layer. Triggers are\n * included — a ray asking \"what is over there\" wants them.\n *\n * The returned record is **reused between calls**: read it, or copy what you\n * need, before casting again. That is what keeps a per-frame ray allocation-\n * free, and it is why the standalone helper's fresh-object-per-candidate cost\n * was worth removing.\n *\n * `maxDist` must be finite — this requires a bounded ray, not the\n * `Number.POSITIVE_INFINITY` \"unbounded ray\" idiom some engines accept. The\n * broadphase below is a uniform grid: an infinite `maxDist` derives an\n * infinite cell range and never terminates the walk. Pass a large finite\n * bound instead (e.g. the map's diagonal) to cast \"until it leaves the\n * populated area\". A non-finite `maxDist` returns `null`.\n */\n raycast(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n mask = 0xffffffff,\n ): RaycastHit | null {\n if (!Number.isFinite(maxDist)) return null;\n\n // Bounding box of the whole ray — the broadphase is rect-based, so this is\n // a conservative prefilter, not the intersection test itself.\n const ex = ox + dx * maxDist;\n const ey = oy + dy * maxDist;\n const minX = ox < ex ? ox : ex;\n const minY = oy < ey ? oy : ey;\n // Padded so the rect always has non-zero extent on both axes. An axis-\n // aligned ray is otherwise degenerate — a horizontal ray has height 0 — and\n // SpatialIndex derives its cell range with an exclusive end (`_cMax` is\n // `ceil(v / CELL) - 1`), so a zero-height rect yields cyMin 0 > cyMax -1 and\n // the cell loop never runs. The broadphase is a conservative prefilter\n // anyway, so over-including by a hair costs nothing: the exact ray test\n // below rejects the extras.\n this.raycastBounds.x = minX - RAY_BOUNDS_PAD;\n this.raycastBounds.y = minY - RAY_BOUNDS_PAD;\n this.raycastBounds.width = Math.abs(ex - ox) + RAY_BOUNDS_PAD * 2;\n this.raycastBounds.height = Math.abs(ey - oy) + RAY_BOUNDS_PAD * 2;\n\n this.raycastCandidates.length = 0;\n this.spatial.query(this.raycastBounds, this.raycastCandidates);\n\n let bestDist = Number.POSITIVE_INFINITY;\n let found = false;\n for (let i = 0; i < this.raycastCandidates.length; i++) {\n const entry = this.entries.get(this.raycastCandidates[i]!);\n if (entry === undefined) continue;\n if ((entry.layer & mask) === 0) continue;\n const hit = rayVsShapeInto(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n entry.entity,\n entry.shape,\n entry.x,\n entry.y,\n this.scratchHit,\n );\n if (hit !== null && hit.distance < bestDist) {\n bestDist = hit.distance;\n found = true;\n this.raycastHit.entity = hit.entity;\n this.raycastHit.distance = hit.distance;\n this.raycastHit.point.x = hit.point.x;\n this.raycastHit.point.y = hit.point.y;\n this.raycastHit.normal.x = hit.normal.x;\n this.raycastHit.normal.y = hit.normal.y;\n }\n }\n return found ? this.raycastHit : null;\n }\n\n /** Scratch record handed to `rayVsShapeInto` per candidate. */\n private scratchHit: RaycastHit = {\n entity: -1,\n distance: 0,\n point: { x: 0, y: 0 },\n normal: { x: 0, y: 0 },\n };\n}\n","import type { Vec2 } from '@flare-engine/math';\n\n/**\n * Minimal structural type for a solid tile position in grid coordinates.\n * `TileCollider[]` from `@flare-engine/tilemap` is structurally assignable here\n * so callers can pass `extractColliders(map)` directly — no tilemap dep needed.\n */\nexport interface SolidTile {\n readonly row: number;\n readonly col: number;\n}\n\n/**\n * Precomputed solid-tile lookup used by `isBlockedAABB` and `moveAndSlideInto`.\n * `solid` has `width * height` cells indexed `row * width + col`; non-zero means\n * solid. Build once per map load via `buildSolidGrid`.\n */\nexport interface SolidGrid {\n readonly width: number;\n readonly height: number;\n readonly tileSize: number;\n readonly solid: Uint8Array;\n}\n\n/**\n * Build a `SolidGrid` from a list of solid tile positions.\n * Input is structurally typed (`readonly SolidTile[]`) so the caller does not\n * need to import `@flare-engine/tilemap` — physics stays tilemap-free.\n */\nexport function buildSolidGrid(\n width: number,\n height: number,\n tileSize: number,\n tiles: readonly SolidTile[],\n): SolidGrid {\n const solid = new Uint8Array(width * height);\n for (const t of tiles) {\n solid[t.row * width + t.col] = 1;\n }\n return { width, height, tileSize, solid };\n}\n\n/**\n * Half-open boundary constant: the AABB is treated as [min, max) so a box\n * whose max edge lands exactly on a tile boundary does NOT overlap that tile.\n */\nconst EDGE = 1e-4;\n\n/**\n * Returns `true` when an axis-aligned box centered at (`x`, `y`) with\n * half-extents (`halfW`, `halfH`) overlaps a solid tile or lies outside the\n * map boundary. Uses half-open EDGE semantics on max edges.\n */\nexport function isBlockedAABB(\n grid: SolidGrid,\n x: number,\n y: number,\n halfW: number,\n halfH: number,\n): boolean {\n const ts = grid.tileSize;\n const minCol = Math.floor((x - halfW) / ts);\n const maxCol = Math.floor((x + halfW - EDGE) / ts);\n const minRow = Math.floor((y - halfH) / ts);\n const maxRow = Math.floor((y + halfH - EDGE) / ts);\n if (minCol < 0 || minRow < 0 || maxCol >= grid.width || maxRow >= grid.height) {\n return true;\n }\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n if (grid.solid[row * grid.width + col] !== 0) {\n return true;\n }\n }\n }\n return false;\n}\n\n/**\n * Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis\n * is applied independently so a blocked diagonal input slides along the wall.\n * The y-check uses the already-updated x position.\n * Zero-alloc: writes result into the provided `out` Vec2 and returns it.\n */\nexport function moveAndSlideInto(\n grid: SolidGrid,\n x: number,\n y: number,\n dx: number,\n dy: number,\n halfW: number,\n halfH: number,\n out: Vec2,\n): Vec2 {\n let nx = x;\n let ny = y;\n if (dx !== 0 && !isBlockedAABB(grid, nx + dx, ny, halfW, halfH)) nx += dx;\n if (dy !== 0 && !isBlockedAABB(grid, nx, ny + dy, halfW, halfH)) ny += dy;\n out.x = nx;\n out.y = ny;\n return out;\n}\n"],"mappings":";;;AAeA,SAAgB,eAAe,OAAc,MAAmD;CAC9F,OAAO;EACL;EACA,WAAW,MAAM,aAAa;EAC9B,OAAO,MAAM,SAAS;EACtB,MAAM,MAAM,QAAQ;CACtB;AACF;;;;;;;;;;;;ACUA,SAAgB,QACd,IACA,IACA,IACA,IACA,SACA,SACmB;CACnB,IAAI,UAA6B;CAEjC,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,MAAM,IAAI,QAAQ;EAClB,MAAM,MAAM,WAAW,IAAI,IAAI,IAAI,IAAI,SAAS,CAAC;EACjD,IAAI,QAAQ,YAAY,QAAQ,IAAI,WAAW,QAAQ,WACrD,UAAU;CAEd;CAEA,OAAO;AACT;AAEA,SAAS,WACP,IACA,IACA,IACA,IACA,SACA,QACmB;CACnB,IAAI,OAAO,M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GAAG;GAAG,GAAG;EAAE;CACvB;AACF;;;;;;;;ACtTA,SAAgB,eACd,OACA,QACA,UACA,OACW;CACX,MAAM,QAAQ,IAAI,WAAW,QAAQ,MAAM;CAC3C,KAAK,MAAM,KAAK,OACd,MAAM,EAAE,MAAM,QAAQ,EAAE,OAAO;CAEjC,OAAO;EAAE;EAAO;EAAQ;EAAU;CAAM;AAC1C;;;;;AAMA,MAAM,OAAO;;;;;;AAOb,SAAgB,cACd,MACA,GACA,GACA,OACA,OACS;CACT,MAAM,KAAK,KAAK;CAChB,MAAM,SAAS,KAAK,OAAO,IAAI,SAAS,EAAE;CAC1C,MAAM,SAAS,KAAK,OAAO,IAAI,QAAQ,QAAQ,EAAE;CACjD,MAAM,SAAS,KAAK,OAAO,IAAI,SAAS,EAAE;CAC1C,MAAM,SAAS,KAAK,OAAO,IAAI,QAAQ,QAAQ,EAAE;CACjD,IAAI,SAAS,KAAK,SAAS,KAAK,UAAU,KAAK,SAAS,UAAU,KAAK,QACrE,OAAO;CAET,KAAK,IAAI,MAAM,QAAQ,OAAO,QAAQ,OACpC,KAAK,IAAI,MAAM,QAAQ,OAAO,QAAQ,OACpC,IAAI,KAAK,MAAM,MAAM,KAAK,QAAQ,SAAS,GACzC,OAAO;CAIb,OAAO;AACT;;;;;;;AAQA,SAAgB,iBACd,MACA,GACA,GACA,IACA,IACA,OACA,OACA,KACM;CACN,IAAI,KAAK;CACT,IAAI,KAAK;CACT,IAAI,OAAO,KAAK,CAAC,cAAc,MAAM,KAAK,IAAI,IAAI,OAAO,KAAK,GAAG,MAAM;CACvE,IAAI,OAAO,KAAK,CAAC,cAAc,MAAM,IAAI,KAAK,IAAI,OAAO,KAAK,GAAG,MAAM;CACvE,IAAI,IAAI;CACR,IAAI,IAAI;CACR,OAAO;AACT"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/collider.ts","../src/raycast.ts","../src/shape.ts","../src/spatial-index.ts","../src/collision-world.ts","../src/solid-grid.ts"],"sourcesContent":["import type { Shape } from './shape.js';\n\n/** Collider component data attached to an ECS entity. */\nexport interface Collider {\n /** The collision shape. */\n shape: Shape;\n /** If true, generates trigger events but no physical response. */\n isTrigger: boolean;\n /** Collision layer bitmask — which layer this collider belongs to. */\n layer: number;\n /** Collision mask — which layers this collider can interact with. */\n mask: number;\n}\n\n/** Create a collider with default settings. */\nexport function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider {\n return {\n shape,\n isTrigger: opts?.isTrigger ?? false,\n layer: opts?.layer ?? 1,\n mask: opts?.mask ?? 0xffffffff,\n };\n}\n","import type { Vec2 } from '@flare-engine/math';\nimport type { Shape } from './shape.js';\n\n/** Result of a raycast hit. */\nexport interface RaycastHit {\n /** The entity that was hit. */\n entity: number;\n /** Distance from ray origin to hit point. */\n distance: number;\n /** World-space hit point. */\n point: Vec2;\n /** Surface normal at hit point. */\n normal: Vec2;\n}\n\n/** Entity entry for raycast — position + shape. */\nexport interface RaycastTarget {\n entity: number;\n shape: Shape;\n x: number;\n y: number;\n}\n\n/**\n * Cast a ray against a list of targets. Returns the closest hit, or null.\n * @param ox Ray origin X\n * @param oy Ray origin Y\n * @param dx Ray direction X (should be normalized)\n * @param dy Ray direction Y (should be normalized)\n * @param maxDist Maximum raycast distance\n * @param targets Array of entities to test against\n */\nexport function raycast(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n targets: readonly RaycastTarget[],\n): RaycastHit | null {\n let closest: RaycastHit | null = null;\n\n for (let i = 0; i < targets.length; i++) {\n const t = targets[i]!;\n const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);\n if (hit && (closest === null || hit.distance < closest.distance)) {\n closest = hit;\n }\n }\n\n return closest;\n}\n\nfunction rayVsShape(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n target: RaycastTarget,\n): RaycastHit | null {\n if (target.shape.type === 'aabb') {\n return rayVsAABB(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n target.entity,\n target.x,\n target.y,\n target.shape.hw,\n target.shape.hh,\n );\n }\n return rayVsCircle(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n target.entity,\n target.x,\n target.y,\n target.shape.radius,\n );\n}\n\nfunction rayVsAABB(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n hw: number,\n hh: number,\n): RaycastHit | null {\n const minX = cx - hw;\n const maxX = cx + hw;\n const minY = cy - hh;\n const maxY = cy + hh;\n\n const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;\n const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;\n\n const tx1 = (minX - ox) * invDx;\n const tx2 = (maxX - ox) * invDx;\n const ty1 = (minY - oy) * invDy;\n const ty2 = (maxY - oy) * invDy;\n\n const tMinX = Math.min(tx1, tx2);\n const tMaxX = Math.max(tx1, tx2);\n const tMinY = Math.min(ty1, ty2);\n const tMaxY = Math.max(ty1, ty2);\n\n const tMin = Math.max(tMinX, tMinY);\n const tMax = Math.min(tMaxX, tMaxY);\n\n if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;\n\n const t = tMin >= 0 ? tMin : tMax;\n if (t > maxDist || t < 0) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n\n // Compute normal\n let nx = 0;\n let ny = 0;\n if (tMinX > tMinY) {\n nx = dx < 0 ? 1 : -1;\n } else {\n ny = dy < 0 ? 1 : -1;\n }\n\n return {\n entity,\n distance: t,\n point: { x: px, y: py },\n normal: { x: nx, y: ny },\n };\n}\n\nfunction rayVsCircle(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n radius: number,\n): RaycastHit | null {\n const fx = ox - cx;\n const fy = oy - cy;\n\n const a = dx * dx + dy * dy;\n const b = 2 * (fx * dx + fy * dy);\n const c = fx * fx + fy * fy - radius * radius;\n\n if (a < 1e-12) return null; // Zero-length direction\n\n const discriminant = b * b - 4 * a * c;\n if (discriminant < 0) return null;\n\n const sqrtDisc = Math.sqrt(discriminant);\n let t = (-b - sqrtDisc) / (2 * a);\n\n // If entry point is behind, try exit point\n if (t < 0) {\n t = (-b + sqrtDisc) / (2 * a);\n }\n if (t < 0 || t > maxDist) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n const nx = px - cx;\n const ny = py - cy;\n const len = Math.sqrt(nx * nx + ny * ny);\n\n return {\n entity,\n distance: t,\n point: { x: px, y: py },\n normal: len > 0 ? { x: nx / len, y: ny / len } : { x: 0, y: 0 },\n };\n}\n\n/**\n * Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns\n * `null` without touching `out`.\n *\n * The allocating `raycast()` above builds a fresh `RaycastHit` — with two nested\n * `Vec2` objects — for **every candidate it tests**, then discards all but the\n * closest. That is three objects per candidate per ray. This variant exists so\n * `CollisionWorld.raycast` can test a whole broadphase cell without allocating.\n */\nexport function rayVsShapeInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n shape: Shape,\n x: number,\n y: number,\n out: RaycastHit,\n): RaycastHit | null {\n if (shape.type === 'aabb') {\n return rayVsAABBInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.hw, shape.hh, out);\n }\n return rayVsCircleInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.radius, out);\n}\n\nfunction rayVsAABBInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n bx: number,\n by: number,\n hw: number,\n hh: number,\n out: RaycastHit,\n): RaycastHit | null {\n const invDx = dx !== 0 ? 1 / dx : Number.POSITIVE_INFINITY;\n const invDy = dy !== 0 ? 1 / dy : Number.POSITIVE_INFINITY;\n\n const tx1 = (bx - hw - ox) * invDx;\n const tx2 = (bx + hw - ox) * invDx;\n const ty1 = (by - hh - oy) * invDy;\n const ty2 = (by + hh - oy) * invDy;\n\n const tMinX = Math.min(tx1, tx2);\n const tMaxX = Math.max(tx1, tx2);\n const tMinY = Math.min(ty1, ty2);\n const tMaxY = Math.max(ty1, ty2);\n\n const tMin = Math.max(tMinX, tMinY);\n const tMax = Math.min(tMaxX, tMaxY);\n if (tMax < 0 || tMin > tMax) return null;\n\n const t = tMin < 0 ? tMax : tMin;\n if (t > maxDist || t < 0) return null;\n\n out.entity = entity;\n out.distance = t;\n out.point.x = ox + dx * t;\n out.point.y = oy + dy * t;\n if (tMinX > tMinY) {\n out.normal.x = dx < 0 ? 1 : -1;\n out.normal.y = 0;\n } else {\n out.normal.x = 0;\n out.normal.y = dy < 0 ? 1 : -1;\n }\n return out;\n}\n\nfunction rayVsCircleInto(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n entity: number,\n cx: number,\n cy: number,\n radius: number,\n out: RaycastHit,\n): RaycastHit | null {\n const fx = ox - cx;\n const fy = oy - cy;\n const a = dx * dx + dy * dy;\n if (a < 1e-12) return null;\n\n const b = 2 * (fx * dx + fy * dy);\n const c = fx * fx + fy * fy - radius * radius;\n const discriminant = b * b - 4 * a * c;\n if (discriminant < 0) return null;\n\n const sqrtDisc = Math.sqrt(discriminant);\n let t = (-b - sqrtDisc) / (2 * a);\n if (t < 0) t = (-b + sqrtDisc) / (2 * a);\n if (t < 0 || t > maxDist) return null;\n\n const px = ox + dx * t;\n const py = oy + dy * t;\n const nx = px - cx;\n const ny = py - cy;\n const len = Math.sqrt(nx * nx + ny * ny);\n\n out.entity = entity;\n out.distance = t;\n out.point.x = px;\n out.point.y = py;\n out.normal.x = len > 0 ? nx / len : 0;\n out.normal.y = len > 0 ? ny / len : 0;\n return out;\n}\n","import type { Rect } from '@flare-engine/math';\n\n/** AABB shape — axis-aligned bounding box. */\nexport interface AABBShape {\n readonly type: 'aabb';\n /** Half-width. */\n readonly hw: number;\n /** Half-height. */\n readonly hh: number;\n}\n\n/** Circle shape. */\nexport interface CircleShape {\n readonly type: 'circle';\n readonly radius: number;\n}\n\n/** Union of supported physics shapes. */\nexport type Shape = AABBShape | CircleShape;\n\n/** Create an AABB shape from width and height. */\nexport function AABB(width: number, height: number): AABBShape {\n return { type: 'aabb', hw: width * 0.5, hh: height * 0.5 };\n}\n\n/** Create a circle shape from radius. */\nexport function CircleShape(radius: number): CircleShape {\n return { type: 'circle', radius };\n}\n\n/** Compute the bounding rect of a shape at a given position. */\nexport function shapeBounds(shape: Shape, x: number, y: number): Rect {\n if (shape.type === 'aabb') {\n return { x: x - shape.hw, y: y - shape.hh, width: shape.hw * 2, height: shape.hh * 2 };\n }\n return {\n x: x - shape.radius,\n y: y - shape.radius,\n width: shape.radius * 2,\n height: shape.radius * 2,\n };\n}\n\n/** Zero-alloc variant — writes the shape bounds into the provided output rect. */\nexport function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect {\n if (shape.type === 'aabb') {\n out.x = x - shape.hw;\n out.y = y - shape.hh;\n out.width = shape.hw * 2;\n out.height = shape.hh * 2;\n return out;\n }\n out.x = x - shape.radius;\n out.y = y - shape.radius;\n out.width = shape.radius * 2;\n out.height = shape.radius * 2;\n return out;\n}\n\n/** Test overlap between two positioned shapes. Returns true if overlapping. */\nexport function shapeOverlap(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n): boolean {\n if (shapeA.type === 'aabb' && shapeB.type === 'aabb') {\n return aabbVsAabb(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh);\n }\n if (shapeA.type === 'circle' && shapeB.type === 'circle') {\n return circleVsCircle(ax, ay, shapeA.radius, bx, by, shapeB.radius);\n }\n // Mixed: one AABB, one circle\n if (shapeA.type === 'aabb' && shapeB.type === 'circle') {\n return aabbVsCircle(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius);\n }\n return aabbVsCircle(\n bx,\n by,\n (shapeB as AABBShape).hw,\n (shapeB as AABBShape).hh,\n ax,\n ay,\n (shapeA as CircleShape).radius,\n );\n}\n\nfunction aabbVsAabb(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n bx: number,\n by: number,\n bhw: number,\n bhh: number,\n): boolean {\n return Math.abs(ax - bx) < ahw + bhw && Math.abs(ay - by) < ahh + bhh;\n}\n\nfunction circleVsCircle(\n ax: number,\n ay: number,\n ar: number,\n bx: number,\n by: number,\n br: number,\n): boolean {\n const dx = bx - ax;\n const dy = by - ay;\n const radSum = ar + br;\n return dx * dx + dy * dy < radSum * radSum;\n}\n\nfunction aabbVsCircle(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n cx: number,\n cy: number,\n cr: number,\n): boolean {\n const left = ax - ahw;\n const right = ax + ahw;\n const top = ay - ahh;\n const bottom = ay + ahh;\n const closestX = cx < left ? left : cx > right ? right : cx;\n const closestY = cy < top ? top : cy > bottom ? bottom : cy;\n const dx = cx - closestX;\n const dy = cy - closestY;\n const distSq = dx * dx + dy * dy;\n // Strict, like aabbVsAabb and circleVsCircle: exactly-touching shapes do not\n // collide. A centre on or inside the box collides when the radius is positive;\n // a zero radius counts only strictly inside, as a zero-size box does.\n if (distSq > 0) return distSq < cr * cr;\n return cr > 0 || (cx > left && cx < right && cy > top && cy < bottom);\n}\n\n/**\n * Minimum translation vector: the shortest push that separates two overlapping\n * shapes.\n *\n * `shapeOverlap` answers *whether* two shapes collide and nothing more, which\n * leaves a detection-only consumer with no way to resolve the collision. This is\n * that missing step: `out.x`/`out.y` are a unit axis and `out.depth` is how far\n * along it `b` must move to just stop touching `a`.\n *\n * Returns `null` when the shapes are not overlapping, leaving `out` untouched so\n * a caller may reuse one buffer across many pairs.\n *\n * The degenerate concentric case (identical centres) has no meaningful axis;\n * `(1, 0)` is returned so the caller still gets a usable separation instead of\n * `NaN` from normalising a zero vector.\n */\nexport interface Overlap {\n /** Unit separation axis, pointing from `a` toward `b`. */\n x: number;\n y: number;\n /** Penetration depth along that axis. */\n depth: number;\n}\n\n/** A fresh, zeroed overlap record. Allocate once outside the loop and reuse. */\nexport function createOverlap(): Overlap {\n return { x: 0, y: 0, depth: 0 };\n}\n\nexport function overlapInto(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n out: Overlap,\n): Overlap | null {\n if (shapeA.type === 'aabb' && shapeB.type === 'aabb') {\n return aabbAabbOverlap(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh, out);\n }\n if (shapeA.type === 'circle' && shapeB.type === 'circle') {\n return circleCircleOverlap(ax, ay, shapeA.radius, bx, by, shapeB.radius, out);\n }\n if (shapeA.type === 'aabb' && shapeB.type === 'circle') {\n return aabbCircleOverlap(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius, out, false);\n }\n // Circle vs AABB — solve as AABB vs circle, then flip the axis so it still\n // points from `a` to `b`.\n return aabbCircleOverlap(\n bx,\n by,\n (shapeB as AABBShape).hw,\n (shapeB as AABBShape).hh,\n ax,\n ay,\n (shapeA as CircleShape).radius,\n out,\n true,\n );\n}\n\n/** Minimum translation vector. Allocates — see `overlapInto` for the hot path. */\nexport function overlap(\n shapeA: Shape,\n ax: number,\n ay: number,\n shapeB: Shape,\n bx: number,\n by: number,\n): Overlap | null {\n return overlapInto(shapeA, ax, ay, shapeB, bx, by, createOverlap());\n}\n\nfunction aabbAabbOverlap(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n bx: number,\n by: number,\n bhw: number,\n bhh: number,\n out: Overlap,\n): Overlap | null {\n const dx = bx - ax;\n const dy = by - ay;\n const overlapX = ahw + bhw - Math.abs(dx);\n if (overlapX <= 0) return null;\n const overlapY = ahh + bhh - Math.abs(dy);\n if (overlapY <= 0) return null;\n\n // The minimum translation is along whichever axis is least penetrated.\n if (overlapX < overlapY) {\n out.x = dx < 0 ? -1 : 1;\n out.y = 0;\n out.depth = overlapX;\n } else {\n out.x = 0;\n out.y = dy < 0 ? -1 : 1;\n out.depth = overlapY;\n }\n return out;\n}\n\nfunction circleCircleOverlap(\n ax: number,\n ay: number,\n ar: number,\n bx: number,\n by: number,\n br: number,\n out: Overlap,\n): Overlap | null {\n const dx = bx - ax;\n const dy = by - ay;\n const radSum = ar + br;\n const distSq = dx * dx + dy * dy;\n if (distSq >= radSum * radSum) return null;\n\n const dist = Math.sqrt(distSq);\n if (dist === 0) {\n // Concentric: no meaningful axis, so pick one rather than divide by zero.\n out.x = 1;\n out.y = 0;\n out.depth = radSum;\n return out;\n }\n out.x = dx / dist;\n out.y = dy / dist;\n out.depth = radSum - dist;\n return out;\n}\n\nfunction aabbCircleOverlap(\n ax: number,\n ay: number,\n ahw: number,\n ahh: number,\n cx: number,\n cy: number,\n cr: number,\n out: Overlap,\n flip: boolean,\n): Overlap | null {\n const left = ax - ahw;\n const right = ax + ahw;\n const top = ay - ahh;\n const bottom = ay + ahh;\n const closestX = cx < left ? left : cx > right ? right : cx;\n const closestY = cy < top ? top : cy > bottom ? bottom : cy;\n const dx = cx - closestX;\n const dy = cy - closestY;\n const distSq = dx * dx + dy * dy;\n\n if (distSq > 0) {\n // Centre outside the box: separate along the closest-point normal.\n if (distSq >= cr * cr) return null;\n const dist = Math.sqrt(distSq);\n const nx = dx / dist;\n const ny = dy / dist;\n out.x = flip ? -nx : nx;\n out.y = flip ? -ny : ny;\n out.depth = cr - dist;\n return out;\n }\n\n // Centre inside the box: push out through the nearest face.\n const toLeft = cx - left;\n const toRight = right - cx;\n const toTop = cy - top;\n const toBottom = bottom - cy;\n let nx = -1;\n let ny = 0;\n let depth = toLeft;\n if (toRight < depth) {\n nx = 1;\n ny = 0;\n depth = toRight;\n }\n if (toTop < depth) {\n nx = 0;\n ny = -1;\n depth = toTop;\n }\n if (toBottom < depth) {\n nx = 0;\n ny = 1;\n depth = toBottom;\n }\n // Zero radius on the boundary is exactly touching, not overlapping.\n if (depth + cr <= 0) return null;\n out.x = flip ? -nx : nx;\n out.y = flip ? -ny : ny;\n out.depth = depth + cr;\n return out;\n}\n","import type { Rect } from '@flare-engine/math';\n\n// Internal entry: bounding rect (minX, minY, maxX, maxY) plus a per-query stamp\n// for zero-alloc dedup.\ninterface _Entry<T> {\n readonly item: T;\n minX: number;\n minY: number;\n maxX: number;\n maxY: number;\n _stamp: number;\n}\n\n// Uniform grid cell size in world units. Tuned for typical game-object sizes (16–64 px).\nconst CELL = 64;\n\n// Packed cell-key encoding. Supports cell coords in [-32768, 32767],\n// i.e. world coordinates ±2 097 152. All keys fit within Number.MAX_SAFE_INTEGER.\nconst KEY_RANGE = 65536; // 2^16\nconst KEY_OFFSET = 32768; // 2^15\n\nfunction _key(cx: number, cy: number): number {\n return (cx + KEY_OFFSET) * KEY_RANGE + (cy + KEY_OFFSET);\n}\n\n// Inclusive min cell that contains world coordinate v.\nfunction _cMin(v: number): number {\n return Math.floor(v / CELL);\n}\n\n// Inclusive max cell that contains world coordinate v (exclusive-end: a coord\n// falling exactly on a cell boundary is NOT part of the adjacent cell — matches\n// the strict-inequality overlap semantics of query()).\nfunction _cMax(v: number): number {\n return Math.ceil(v / CELL) - 1;\n}\n\n// Supported cell-coordinate range for the packed key above.\nconst MIN_CELL = -KEY_OFFSET;\nconst MAX_CELL = KEY_OFFSET - 1;\n\n// Bounds a derived cell coordinate to the packed-key range. Without this, a\n// non-finite world coordinate (±Infinity — an unbounded raycast maxDist, or an\n// entity parked at Infinity) makes cxMin/cxMax/cyMin/cyMax infinite, and\n// `for (cx = cxMin; cx <= cxMax; cx++)` never exits: once cx itself is\n// Infinity, `cx <= Infinity` stays true and `cx++` is a no-op, so the loop\n// spins forever pushing/reading the same bucket (unbounded memory growth on\n// the link side). NaN is left untouched: every comparison against NaN is\n// false, so the loop already runs zero times, which is the existing (safe)\n// behaviour for a degenerate direction like `0 * Infinity`.\nfunction _clampCell(c: number): number {\n if (c > MAX_CELL) return MAX_CELL;\n if (c < MIN_CELL) return MIN_CELL;\n return c;\n}\n\n/**\n * Uniform spatial-hash broadphase.\n * `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.\n * `query` walks only the cells overlapping the given rect; result order is unspecified.\n * Zero-alloc in the query hot-path: duplicate suppression uses a monotone stamp counter,\n * not a per-call Set.\n */\nexport class SpatialIndex<T> {\n private _data: _Entry<T>[] = [];\n private _slots = new Map<T, number>(); // item → dense-array slot\n private _cells = new Map<number, _Entry<T>[]>(); // packed cell key → bucket\n private _gen = 0; // monotone query-generation counter\n\n /** Number of items in the index. */\n get size(): number {\n return this._data.length;\n }\n\n /** Insert an item with a bounding rect. */\n insert(item: T, bounds: Rect): void {\n const entry: _Entry<T> = {\n item,\n minX: bounds.x,\n minY: bounds.y,\n maxX: bounds.x + bounds.width,\n maxY: bounds.y + bounds.height,\n _stamp: 0,\n };\n this._slots.set(item, this._data.length);\n this._data.push(entry);\n this._link(entry);\n }\n\n /** Remove an item. Returns false when the item was not present. */\n remove(item: T): boolean {\n const slot = this._slots.get(item);\n if (slot === undefined) return false;\n const entry = this._data[slot]!;\n this._unlink(entry);\n this._slots.delete(item);\n // Swap-remove: plug the hole with the last element.\n const last = this._data.pop()!;\n if (slot < this._data.length) {\n this._data[slot] = last;\n this._slots.set(last.item, slot);\n }\n return true;\n }\n\n /** Update the bounds for an existing item. No-op when the item is absent. */\n update(item: T, bounds: Rect): void {\n const slot = this._slots.get(item);\n if (slot === undefined) return;\n const entry = this._data[slot]!;\n this._unlink(entry);\n entry.minX = bounds.x;\n entry.minY = bounds.y;\n entry.maxX = bounds.x + bounds.width;\n entry.maxY = bounds.y + bounds.height;\n this._link(entry);\n }\n\n /**\n * Query all items whose bounds overlap the given rect.\n * Result order is unspecified. Appends to `result` when provided and returns it.\n */\n query(bounds: Rect, result?: T[]): T[] {\n const out = result ?? [];\n const gen = ++this._gen;\n\n const qMinX = bounds.x;\n const qMinY = bounds.y;\n const qMaxX = bounds.x + bounds.width;\n const qMaxY = bounds.y + bounds.height;\n\n const cxMin = _clampCell(_cMin(qMinX));\n const cxMax = _clampCell(_cMax(qMaxX));\n const cyMin = _clampCell(_cMin(qMinY));\n const cyMax = _clampCell(_cMax(qMaxY));\n\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const bucket = this._cells.get(_key(cx, cy));\n if (bucket === undefined) continue;\n for (let i = 0; i < bucket.length; i++) {\n const e = bucket[i]!;\n if (e._stamp === gen) continue; // already emitted this query\n e._stamp = gen;\n if (e.maxX > qMinX && e.minX < qMaxX && e.maxY > qMinY && e.minY < qMaxY) {\n out.push(e.item);\n }\n }\n }\n }\n return out;\n }\n\n /** Remove all items. */\n clear(): void {\n this._data.length = 0;\n this._slots.clear();\n this._cells.clear();\n }\n\n // Register entry in all grid cells it overlaps.\n private _link(entry: _Entry<T>): void {\n const cxMin = _clampCell(_cMin(entry.minX));\n const cxMax = _clampCell(_cMax(entry.maxX));\n const cyMin = _clampCell(_cMin(entry.minY));\n const cyMax = _clampCell(_cMax(entry.maxY));\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const k = _key(cx, cy);\n let b = this._cells.get(k);\n if (b === undefined) {\n b = [];\n this._cells.set(k, b);\n }\n b.push(entry);\n }\n }\n }\n\n // Remove entry from all grid cells it currently occupies.\n private _unlink(entry: _Entry<T>): void {\n const cxMin = _clampCell(_cMin(entry.minX));\n const cxMax = _clampCell(_cMax(entry.maxX));\n const cyMin = _clampCell(_cMin(entry.minY));\n const cyMax = _clampCell(_cMax(entry.maxY));\n for (let cx = cxMin; cx <= cxMax; cx++) {\n for (let cy = cyMin; cy <= cyMax; cy++) {\n const b = this._cells.get(_key(cx, cy));\n if (b === undefined) continue;\n const i = b.indexOf(entry);\n if (i !== -1) {\n // Swap-remove within the bucket.\n const last = b.pop()!;\n if (i < b.length) b[i] = last;\n }\n }\n }\n }\n}\n","import { Signal } from '@flare-engine/events';\nimport type { Rect } from '@flare-engine/math';\nimport type { Collider } from './collider.js';\nimport type { RaycastHit } from './raycast.js';\nimport { rayVsShapeInto } from './raycast.js';\nimport type { Shape } from './shape.js';\nimport { shapeBoundsInto, shapeOverlap } from './shape.js';\nimport { SpatialIndex } from './spatial-index.js';\n\n/** Collision pair data emitted by collision signals. */\nexport interface CollisionPair {\n readonly entityA: number;\n readonly entityB: number;\n}\n\n/** Per-entity collision data stored in the world. */\ninterface CollisionEntry {\n entity: number;\n shape: Shape;\n x: number;\n y: number;\n isTrigger: boolean;\n layer: number;\n mask: number;\n}\n\n// Packed pair-key layout: `lo * ENTITY_SHIFT + hi`, lo = min(a,b), hi = max(a,b).\n// 2^26 fits within Number.MAX_SAFE_INTEGER for 26+26 bits → max entity id ~67M.\nconst ENTITY_SHIFT = 2 ** 26;\n\n/** Padding applied to a ray's query AABB — see `CollisionWorld.raycast`. */\nconst RAY_BOUNDS_PAD = 1e-6;\n\nfunction encodePair(a: number, b: number): number {\n const lo = a < b ? a : b;\n const hi = a < b ? b : a;\n return lo * ENTITY_SHIFT + hi;\n}\n\nfunction decodeLo(key: number): number {\n return Math.floor(key / ENTITY_SHIFT);\n}\n\nfunction decodeHi(key: number): number {\n return key - Math.floor(key / ENTITY_SHIFT) * ENTITY_SHIFT;\n}\n\n/**\n * AABB collision world. Manages entities with shapes, detects overlaps each step,\n * and emits collision/trigger signals.\n */\nexport class CollisionWorld {\n readonly onCollisionStart = new Signal<CollisionPair>();\n readonly onCollisionEnd = new Signal<CollisionPair>();\n readonly onTriggerEnter = new Signal<CollisionPair>();\n readonly onTriggerExit = new Signal<CollisionPair>();\n\n private entries = new Map<number, CollisionEntry>();\n private spatial = new SpatialIndex<number>();\n /**\n * Two pre-allocated pair buffers — swapped each step (no per-frame allocation).\n * Keys are packed numbers via `encodePair(a, b)`.\n */\n private pairBufA = new Set<number>();\n private pairBufB = new Set<number>();\n private activePairs: Set<number> = this.pairBufA;\n /** Reusable query result buffer. */\n private queryBuf: number[] = [];\n /** Reusable bounds rect — avoids per-entity Rect allocation in step(). */\n private boundsBuf: Rect = { x: 0, y: 0, width: 0, height: 0 };\n\n /** Add an entity with a shape. Positional twin of `addCollider`. */\n add(\n entity: number,\n shape: Shape,\n x: number,\n y: number,\n isTrigger = false,\n layer = 1,\n mask = 0xffffffff,\n ): void {\n const entry: CollisionEntry = { entity, shape, x, y, isTrigger, layer, mask };\n const exists = this.entries.has(entity);\n this.entries.set(entity, entry);\n shapeBoundsInto(shape, x, y, this.boundsBuf);\n if (exists) {\n this.spatial.update(entity, this.boundsBuf);\n } else {\n this.spatial.insert(entity, this.boundsBuf);\n }\n }\n\n /**\n * Add an entity from a `Collider`. Its `shape`, `isTrigger`, `layer` and `mask`\n * are copied at add time — later edits to the collider do not reach the world;\n * re-add to change them. Re-adding an existing entity replaces its data.\n */\n addCollider(entity: number, collider: Collider, x: number, y: number): void {\n this.add(entity, collider.shape, x, y, collider.isTrigger, collider.layer, collider.mask);\n }\n\n /** Remove an entity from the world. Emits exit signals for any active pairs. */\n remove(entity: number): void {\n const entry = this.entries.get(entity);\n if (!entry) return;\n\n this.entries.delete(entity);\n this.spatial.remove(entity);\n\n // Fire exit signals for all active pairs involving this entity\n for (const key of this.activePairs) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n if (lo !== entity && hi !== entity) continue;\n this.activePairs.delete(key);\n const otherEntity = lo === entity ? hi : lo;\n const otherEntry = this.entries.get(otherEntity);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (entry.isTrigger || otherEntry?.isTrigger) {\n this.onTriggerExit.emit(pair);\n } else {\n this.onCollisionEnd.emit(pair);\n }\n }\n }\n\n /** Update an entity's position. */\n setPosition(entity: number, x: number, y: number): void {\n const entry = this.entries.get(entity);\n if (!entry) return;\n entry.x = x;\n entry.y = y;\n shapeBoundsInto(entry.shape, x, y, this.boundsBuf);\n this.spatial.update(entity, this.boundsBuf);\n }\n\n /** Number of entities in the world. */\n get size(): number {\n return this.entries.size;\n }\n\n /** Number of broadphase records (one per live entity after add/remove). Diagnostic for broadphase growth. */\n get broadphaseSize(): number {\n return this.spatial.size;\n }\n\n /** Run one collision detection step. */\n step(): void {\n const newPairs = this.activePairs === this.pairBufA ? this.pairBufB : this.pairBufA;\n newPairs.clear();\n\n for (const entryA of this.entries.values()) {\n const entityA = entryA.entity;\n shapeBoundsInto(entryA.shape, entryA.x, entryA.y, this.boundsBuf);\n this.queryBuf.length = 0;\n this.spatial.query(this.boundsBuf, this.queryBuf);\n\n for (let i = 0; i < this.queryBuf.length; i++) {\n const entityB = this.queryBuf[i]!;\n if (entityB <= entityA) continue; // Avoid self and duplicates\n\n const entryB = this.entries.get(entityB);\n if (!entryB) continue;\n\n // Layer/mask filtering\n if ((entryA.layer & entryB.mask) === 0 && (entryB.layer & entryA.mask) === 0) continue;\n\n // Narrowphase\n if (!shapeOverlap(entryA.shape, entryA.x, entryA.y, entryB.shape, entryB.x, entryB.y)) {\n continue;\n }\n\n newPairs.add(encodePair(entityA, entityB));\n }\n }\n\n // Enter/start events — pair objects are allocated here only on state transitions.\n for (const key of newPairs) {\n if (!this.activePairs.has(key)) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n const a = this.entries.get(lo);\n const b = this.entries.get(hi);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (a?.isTrigger || b?.isTrigger) {\n this.onTriggerEnter.emit(pair);\n } else {\n this.onCollisionStart.emit(pair);\n }\n }\n }\n\n // Exit/end events\n for (const key of this.activePairs) {\n if (!newPairs.has(key)) {\n const lo = decodeLo(key);\n const hi = decodeHi(key);\n const a = this.entries.get(lo);\n const b = this.entries.get(hi);\n const pair: CollisionPair = { entityA: lo, entityB: hi };\n if (a?.isTrigger || b?.isTrigger) {\n this.onTriggerExit.emit(pair);\n } else {\n this.onCollisionEnd.emit(pair);\n }\n }\n }\n\n this.activePairs = newPairs;\n }\n\n /** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */\n getColliderBoundsInto(entity: number, out: Rect): Rect | null {\n const entry = this.entries.get(entity);\n if (!entry) return null;\n shapeBoundsInto(entry.shape, entry.x, entry.y, out);\n return out;\n }\n\n /** Allocating convenience wrapper — returns a fresh Rect or null when entity is absent. */\n getColliderBounds(entity: number): Rect | null {\n const entry = this.entries.get(entity);\n if (!entry) return null;\n return shapeBoundsInto(entry.shape, entry.x, entry.y, { x: 0, y: 0, width: 0, height: 0 });\n }\n\n /** Remove all entities. */\n clear(): void {\n this.entries.clear();\n this.spatial.clear();\n this.pairBufA.clear();\n this.pairBufB.clear();\n this.activePairs = this.pairBufA;\n }\n\n /** Reused across raycasts — see `raycast`. */\n private raycastBounds: Rect = { x: 0, y: 0, width: 0, height: 0 };\n private raycastCandidates: number[] = [];\n private raycastHit: RaycastHit = {\n entity: -1,\n distance: 0,\n point: { x: 0, y: 0 },\n normal: { x: 0, y: 0 },\n };\n\n /**\n * Cast a ray against the colliders in this world.\n *\n * The standalone `raycast()` export tests a caller-supplied target array, so\n * it neither sees the world's colliders nor benefits from the broadphase — an\n * O(n) scan sitting beside an O(1) index. This walks only the cells the ray's\n * bounding box touches.\n *\n * `mask` is a layer bitmask filtered against each collider's `layer`, using the\n * bits colliders already carry; it defaults to every layer. Triggers are\n * included — a ray asking \"what is over there\" wants them.\n *\n * The returned record is **reused between calls**: read it, or copy what you\n * need, before casting again. That is what keeps a per-frame ray allocation-\n * free, and it is why the standalone helper's fresh-object-per-candidate cost\n * was worth removing.\n *\n * `maxDist` must be finite — this requires a bounded ray, not the\n * `Number.POSITIVE_INFINITY` \"unbounded ray\" idiom some engines accept. The\n * broadphase below is a uniform grid: an infinite `maxDist` derives an\n * infinite cell range and never terminates the walk. Pass a large finite\n * bound instead (e.g. the map's diagonal) to cast \"until it leaves the\n * populated area\". A non-finite `maxDist` returns `null`.\n */\n raycast(\n ox: number,\n oy: number,\n dx: number,\n dy: number,\n maxDist: number,\n mask = 0xffffffff,\n ): RaycastHit | null {\n if (!Number.isFinite(maxDist)) return null;\n\n // Bounding box of the whole ray — the broadphase is rect-based, so this is\n // a conservative prefilter, not the intersection test itself.\n const ex = ox + dx * maxDist;\n const ey = oy + dy * maxDist;\n const minX = ox < ex ? ox : ex;\n const minY = oy < ey ? oy : ey;\n // Padded so the rect always has non-zero extent on both axes. An axis-\n // aligned ray is otherwise degenerate — a horizontal ray has height 0 — and\n // SpatialIndex derives its cell range with an exclusive end (`_cMax` is\n // `ceil(v / CELL) - 1`), so a zero-height rect yields cyMin 0 > cyMax -1 and\n // the cell loop never runs. The broadphase is a conservative prefilter\n // anyway, so over-including by a hair costs nothing: the exact ray test\n // below rejects the extras.\n this.raycastBounds.x = minX - RAY_BOUNDS_PAD;\n this.raycastBounds.y = minY - RAY_BOUNDS_PAD;\n this.raycastBounds.width = Math.abs(ex - ox) + RAY_BOUNDS_PAD * 2;\n this.raycastBounds.height = Math.abs(ey - oy) + RAY_BOUNDS_PAD * 2;\n\n this.raycastCandidates.length = 0;\n this.spatial.query(this.raycastBounds, this.raycastCandidates);\n\n let bestDist = Number.POSITIVE_INFINITY;\n let found = false;\n for (let i = 0; i < this.raycastCandidates.length; i++) {\n const entry = this.entries.get(this.raycastCandidates[i]!);\n if (entry === undefined) continue;\n if ((entry.layer & mask) === 0) continue;\n const hit = rayVsShapeInto(\n ox,\n oy,\n dx,\n dy,\n maxDist,\n entry.entity,\n entry.shape,\n entry.x,\n entry.y,\n this.scratchHit,\n );\n if (hit !== null && hit.distance < bestDist) {\n bestDist = hit.distance;\n found = true;\n this.raycastHit.entity = hit.entity;\n this.raycastHit.distance = hit.distance;\n this.raycastHit.point.x = hit.point.x;\n this.raycastHit.point.y = hit.point.y;\n this.raycastHit.normal.x = hit.normal.x;\n this.raycastHit.normal.y = hit.normal.y;\n }\n }\n return found ? this.raycastHit : null;\n }\n\n /** Scratch record handed to `rayVsShapeInto` per candidate. */\n private scratchHit: RaycastHit = {\n entity: -1,\n distance: 0,\n point: { x: 0, y: 0 },\n normal: { x: 0, y: 0 },\n };\n}\n","import type { Vec2 } from '@flare-engine/math';\n\n/**\n * Minimal structural type for a solid tile position in grid coordinates.\n * `TileCollider[]` from `@flare-engine/tilemap` is structurally assignable here\n * so callers can pass `extractColliders(map)` directly — no tilemap dep needed.\n */\nexport interface SolidTile {\n readonly row: number;\n readonly col: number;\n}\n\n/**\n * Precomputed solid-tile lookup used by `isBlockedAABB` and `moveAndSlideInto`.\n * `solid` has `width * height` cells indexed `row * width + col`; non-zero means\n * solid. Build once per map load via `buildSolidGrid`.\n */\nexport interface SolidGrid {\n readonly width: number;\n readonly height: number;\n readonly tileSize: number;\n readonly solid: Uint8Array;\n}\n\n/**\n * Build a `SolidGrid` from a list of solid tile positions.\n * Input is structurally typed (`readonly SolidTile[]`) so the caller does not\n * need to import `@flare-engine/tilemap` — physics stays tilemap-free.\n */\nexport function buildSolidGrid(\n width: number,\n height: number,\n tileSize: number,\n tiles: readonly SolidTile[],\n): SolidGrid {\n const solid = new Uint8Array(width * height);\n for (const t of tiles) {\n solid[t.row * width + t.col] = 1;\n }\n return { width, height, tileSize, solid };\n}\n\n/**\n * Half-open boundary constant: the AABB is treated as [min, max) so a box\n * whose max edge lands exactly on a tile boundary does NOT overlap that tile.\n */\nconst EDGE = 1e-4;\n\n/**\n * Returns `true` when an axis-aligned box centered at (`x`, `y`) with\n * half-extents (`halfW`, `halfH`) overlaps a solid tile or lies outside the\n * map boundary. Uses half-open EDGE semantics on max edges.\n */\nexport function isBlockedAABB(\n grid: SolidGrid,\n x: number,\n y: number,\n halfW: number,\n halfH: number,\n): boolean {\n const ts = grid.tileSize;\n const minCol = Math.floor((x - halfW) / ts);\n const maxCol = Math.floor((x + halfW - EDGE) / ts);\n const minRow = Math.floor((y - halfH) / ts);\n const maxRow = Math.floor((y + halfH - EDGE) / ts);\n if (minCol < 0 || minRow < 0 || maxCol >= grid.width || maxRow >= grid.height) {\n return true;\n }\n for (let row = minRow; row <= maxRow; row++) {\n for (let col = minCol; col <= maxCol; col++) {\n if (grid.solid[row * grid.width + col] !== 0) {\n return true;\n }\n }\n }\n return false;\n}\n\n/**\n * Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis\n * is applied independently so a blocked diagonal input slides along the wall.\n * The y-check uses the already-updated x position.\n * Zero-alloc: writes result into the provided `out` Vec2 and returns it.\n */\nexport function moveAndSlideInto(\n grid: SolidGrid,\n x: number,\n y: number,\n dx: number,\n dy: number,\n halfW: number,\n halfH: number,\n out: Vec2,\n): Vec2 {\n let nx = x;\n let ny = y;\n if (dx !== 0 && !isBlockedAABB(grid, nx + dx, ny, halfW, halfH)) nx += dx;\n if (dy !== 0 && !isBlockedAABB(grid, nx, ny + dy, halfW, halfH)) ny += dy;\n out.x = nx;\n out.y = ny;\n return out;\n}\n"],"mappings":";;;AAeA,SAAgB,eAAe,OAAc,MAAmD;CAC9F,OAAO;EACL;EACA,WAAW,MAAM,aAAa;EAC9B,OAAO,MAAM,SAAS;EACtB,MAAM,MAAM,QAAQ;CACtB;AACF;;;;;;;;;;;ACUA,SAAgB,QACd,IACA,IACA,IACA,IACA,SACA,SACmB;CACnB,IAAI,UAA6B;CAEjC,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,MAAM,IAAI,QAAQ;EAClB,MAAM,MAAM,WAAW,IAAI,IAAI,IAAI,IAAI,SAAS,CAAC;EACjD,IAAI,QAAQ,YAAY,QAAQ,IAAI,WAAW,QAAQ,WACrD,UAAU;CAEd;CAEA,OAAO;AACT;AAEA,SAAS,WACP,IACA,IACA,IACA,IACA,SACA,QACmB;CACnB,IAAI,OAAO,MAAM,SAAS,QACxB,OAAO,UACL,IACA,IACA,IACA,IACA,SACA,OAAO,QACP,OAAO,GACP,OAAO,GACP,OAAO,MAAM,IACb,OAAO,MAAM,EACf;CAEF,OAAO,YACL,IACA,IACA,IACA,IACA,SACA,OAAO,QACP,OAAO,GACP,OAAO,GACP,OAAO,MAAM,MACf;AACF;AAEA,SAAS,UACP,IACA,IACA,IACA,IACA,SACA,QACA,IACA,IACA,IACA,IACmB;CACnB,MAAM,OAAO,KAAK;CAClB,MAAM,OAAO,KAAK;CAClB,MAAM,OAA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S,KAAK,OAAO,IAAI,SAAS,EAAE;CAC1C,MAAM,SAAS,KAAK,OAAO,IAAI,QAAQ,QAAQ,EAAE;CACjD,MAAM,SAAS,KAAK,OAAO,IAAI,SAAS,EAAE;CAC1C,MAAM,SAAS,KAAK,OAAO,IAAI,QAAQ,QAAQ,EAAE;CACjD,IAAI,SAAS,KAAK,SAAS,KAAK,UAAU,KAAK,SAAS,UAAU,KAAK,QACrE,OAAO;CAET,KAAK,IAAI,MAAM,QAAQ,OAAO,QAAQ,OACpC,KAAK,IAAI,MAAM,QAAQ,OAAO,QAAQ,OACpC,IAAI,KAAK,MAAM,MAAM,KAAK,QAAQ,SAAS,GACzC,OAAO;CAIb,OAAO;AACT;;;;;;;AAQA,SAAgB,iBACd,MACA,GACA,GACA,IACA,IACA,OACA,OACA,KACM;CACN,IAAI,KAAK;CACT,IAAI,KAAK;CACT,IAAI,OAAO,KAAK,CAAC,cAAc,MAAM,KAAK,IAAI,IAAI,OAAO,KAAK,GAAG,MAAM;CACvE,IAAI,OAAO,KAAK,CAAC,cAAc,MAAM,IAAI,KAAK,IAAI,OAAO,KAAK,GAAG,MAAM;CACvE,IAAI,IAAI;CACR,IAAI,IAAI;CACR,OAAO;AACT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flare-engine/physics",
3
- "version": "0.4.6",
3
+ "version": "0.4.8",
4
4
  "description": "AABB collision, spatial index, raycasting",
5
5
  "keywords": ["flare-engine", "physics", "collision", "aabb", "raycast"],
6
6
  "license": "Apache-2.0",
@@ -37,8 +37,8 @@
37
37
  "typecheck": "tsc --noEmit"
38
38
  },
39
39
  "dependencies": {
40
- "@flare-engine/events": "^0.4.6",
41
- "@flare-engine/math": "^0.4.6"
40
+ "@flare-engine/events": "^0.4.8",
41
+ "@flare-engine/math": "^0.4.8"
42
42
  },
43
43
  "engines": {
44
44
  "node": ">=20"