@flare-engine/physics 0.2.1 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,23 +1,23 @@
1
- import { Rect, Vec2 } from '@flare-engine/math';
2
- import { Signal } from '@flare-engine/events';
3
-
1
+ import { Rect, Vec2 } from "@flare-engine/math";
2
+ import { Signal } from "@flare-engine/events";
3
+ //#region src/shape.d.ts
4
4
  /** AABB shape — axis-aligned bounding box. */
5
5
  interface AABBShape {
6
- readonly type: 'aabb';
7
- /** Half-width. */
8
- readonly hw: number;
9
- /** Half-height. */
10
- readonly hh: number;
6
+ readonly type: 'aabb';
7
+ /** Half-width. */
8
+ readonly hw: number;
9
+ /** Half-height. */
10
+ readonly hh: number;
11
+ }
12
+ /** Circle shape. */
13
+ interface CircleShape {
14
+ readonly type: 'circle';
15
+ readonly radius: number;
11
16
  }
12
17
  /** Union of supported physics shapes. */
13
18
  type Shape = AABBShape | CircleShape;
14
19
  /** Create an AABB shape from width and height. */
15
20
  declare function AABB(width: number, height: number): AABBShape;
16
- /** Circle shape. */
17
- interface CircleShape {
18
- readonly type: 'circle';
19
- readonly radius: number;
20
- }
21
21
  /** Create a circle shape from radius. */
22
22
  declare function CircleShape(radius: number): CircleShape;
23
23
  /** Compute the bounding rect of a shape at a given position. */
@@ -26,108 +26,88 @@ declare function shapeBounds(shape: Shape, x: number, y: number): Rect;
26
26
  declare function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect;
27
27
  /** Test overlap between two positioned shapes. Returns true if overlapping. */
28
28
  declare function shapeOverlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): boolean;
29
-
30
- /** Item stored in the spatial index. */
31
- interface SpatialEntry<T> {
32
- readonly item: T;
33
- /** Bounding rect (minX, minY, width, height). */
34
- minX: number;
35
- minY: number;
36
- maxX: number;
37
- maxY: number;
38
- }
39
- /**
40
- * Brute-force spatial index for broadphase collision.
41
- * Suitable for < ~1000 dynamic entities. Upgrade to grid/rbush for more.
42
- */
43
- declare class SpatialIndex<T> {
44
- private entries;
45
- /** Number of items in the index. */
46
- get size(): number;
47
- /** Insert an item with a bounding rect. */
48
- insert(item: T, bounds: Rect): void;
49
- /** Remove an item. */
50
- remove(item: T): boolean;
51
- /** Update the bounds for an existing item. */
52
- update(item: T, bounds: Rect): void;
53
- /** Query all items whose bounds overlap the given rect. */
54
- query(bounds: Rect, result?: T[]): T[];
55
- /** Remove all items. */
56
- clear(): void;
29
+ //#endregion
30
+ //#region src/collider.d.ts
31
+ /** Collider component data attached to an ECS entity. */
32
+ interface Collider {
33
+ /** The collision shape. */
34
+ shape: Shape;
35
+ /** If true, generates trigger events but no physical response. */
36
+ isTrigger: boolean;
37
+ /** Collision layer bitmask — which layer this collider belongs to. */
38
+ layer: number;
39
+ /** Collision mask — which layers this collider can interact with. */
40
+ mask: number;
57
41
  }
58
-
42
+ /** Create a collider with default settings. */
43
+ declare function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider;
44
+ //#endregion
45
+ //#region src/collision-world.d.ts
59
46
  /** Collision pair data emitted by collision signals. */
60
47
  interface CollisionPair {
61
- readonly entityA: number;
62
- readonly entityB: number;
48
+ readonly entityA: number;
49
+ readonly entityB: number;
63
50
  }
64
51
  /**
65
52
  * AABB collision world. Manages entities with shapes, detects overlaps each step,
66
53
  * and emits collision/trigger signals.
67
54
  */
68
55
  declare class CollisionWorld {
69
- readonly onCollisionStart: Signal<CollisionPair>;
70
- readonly onCollisionEnd: Signal<CollisionPair>;
71
- readonly onTriggerEnter: Signal<CollisionPair>;
72
- readonly onTriggerExit: Signal<CollisionPair>;
73
- private entries;
74
- private spatial;
75
- /**
76
- * Two pre-allocated pair buffers — swapped each step (no per-frame allocation).
77
- * Keys are packed numbers via `encodePair(a, b)`.
78
- */
79
- private pairBufA;
80
- private pairBufB;
81
- private activePairs;
82
- /** Reusable query result buffer. */
83
- private queryBuf;
84
- /** Reusable bounds rect — avoids per-entity Rect allocation in step(). */
85
- private boundsBuf;
86
- /** Add an entity with a shape. */
87
- add(entity: number, shape: Shape, x: number, y: number, isTrigger?: boolean, layer?: number, mask?: number): void;
88
- /** Remove an entity from the world. Emits exit signals for any active pairs. */
89
- remove(entity: number): void;
90
- /** Update an entity's position. */
91
- setPosition(entity: number, x: number, y: number): void;
92
- /** Number of entities in the world. */
93
- get size(): number;
94
- /** Run one collision detection step. */
95
- step(): void;
96
- /** Remove all entities. */
97
- clear(): void;
56
+ readonly onCollisionStart: Signal<CollisionPair>;
57
+ readonly onCollisionEnd: Signal<CollisionPair>;
58
+ readonly onTriggerEnter: Signal<CollisionPair>;
59
+ readonly onTriggerExit: Signal<CollisionPair>;
60
+ private entries;
61
+ private spatial;
62
+ /**
63
+ * Two pre-allocated pair buffers — swapped each step (no per-frame allocation).
64
+ * Keys are packed numbers via `encodePair(a, b)`.
65
+ */
66
+ private pairBufA;
67
+ private pairBufB;
68
+ private activePairs;
69
+ /** Reusable query result buffer. */
70
+ private queryBuf;
71
+ /** Reusable bounds rect — avoids per-entity Rect allocation in step(). */
72
+ private boundsBuf;
73
+ /** Add an entity with a shape. */
74
+ add(entity: number, shape: Shape, x: number, y: number, isTrigger?: boolean, layer?: number, mask?: number): void;
75
+ /** Remove an entity from the world. Emits exit signals for any active pairs. */
76
+ remove(entity: number): void;
77
+ /** Update an entity's position. */
78
+ setPosition(entity: number, x: number, y: number): void;
79
+ /** Number of entities in the world. */
80
+ get size(): number;
81
+ /** Number of broadphase records (one per live entity after add/remove). Diagnostic for broadphase growth. */
82
+ get broadphaseSize(): number;
83
+ /** Run one collision detection step. */
84
+ step(): void;
85
+ /** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */
86
+ getColliderBoundsInto(entity: number, out: Rect): Rect | null;
87
+ /** Allocating convenience wrapper — returns a fresh Rect or null when entity is absent. */
88
+ getColliderBounds(entity: number): Rect | null;
89
+ /** Remove all entities. */
90
+ clear(): void;
98
91
  }
99
-
100
- /** Collider component data attached to an ECS entity. */
101
- interface Collider {
102
- /** The collision shape. */
103
- shape: Shape;
104
- /** If true, generates trigger events but no physical response. */
105
- isTrigger: boolean;
106
- /** Collision layer bitmask — which layer this collider belongs to. */
107
- layer: number;
108
- /** Collision mask — which layers this collider can interact with. */
109
- mask: number;
110
- }
111
- /** Create a collider with default settings. */
112
- declare function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider;
113
-
92
+ //#endregion
93
+ //#region src/raycast.d.ts
114
94
  /** Result of a raycast hit. */
115
95
  interface RaycastHit {
116
- /** The entity that was hit. */
117
- entity: number;
118
- /** Distance from ray origin to hit point. */
119
- distance: number;
120
- /** World-space hit point. */
121
- point: Vec2;
122
- /** Surface normal at hit point. */
123
- normal: Vec2;
96
+ /** The entity that was hit. */
97
+ entity: number;
98
+ /** Distance from ray origin to hit point. */
99
+ distance: number;
100
+ /** World-space hit point. */
101
+ point: Vec2;
102
+ /** Surface normal at hit point. */
103
+ normal: Vec2;
124
104
  }
125
105
  /** Entity entry for raycast — position + shape. */
126
106
  interface RaycastTarget {
127
- entity: number;
128
- shape: Shape;
129
- x: number;
130
- y: number;
107
+ entity: number;
108
+ shape: Shape;
109
+ x: number;
110
+ y: number;
131
111
  }
132
112
  /**
133
113
  * Cast a ray against a list of targets. Returns the closest hit, or null.
@@ -139,25 +119,26 @@ interface RaycastTarget {
139
119
  * @param targets Array of entities to test against
140
120
  */
141
121
  declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
142
-
122
+ //#endregion
123
+ //#region src/solid-grid.d.ts
143
124
  /**
144
125
  * Minimal structural type for a solid tile position in grid coordinates.
145
126
  * `TileCollider[]` from `@flare-engine/tilemap` is structurally assignable here
146
127
  * so callers can pass `extractColliders(map)` directly — no tilemap dep needed.
147
128
  */
148
129
  interface SolidTile {
149
- readonly row: number;
150
- readonly col: number;
130
+ readonly row: number;
131
+ readonly col: number;
151
132
  }
152
133
  /**
153
134
  * Precomputed flat-index Set of solid tile positions used by `isBlockedAABB`
154
135
  * and `moveAndSlideInto`. Build once per map load via `buildSolidGrid`.
155
136
  */
156
137
  interface SolidGrid {
157
- readonly width: number;
158
- readonly height: number;
159
- readonly tileSize: number;
160
- readonly solid: Set<number>;
138
+ readonly width: number;
139
+ readonly height: number;
140
+ readonly tileSize: number;
141
+ readonly solid: Set<number>;
161
142
  }
162
143
  /**
163
144
  * Build a `SolidGrid` from a list of solid tile positions.
@@ -174,9 +155,51 @@ declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: num
174
155
  /**
175
156
  * Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis
176
157
  * is applied independently so a blocked diagonal input slides along the wall.
177
- * The y-check uses the already-updated x position (byte-identical to Berek).
158
+ * The y-check uses the already-updated x position.
178
159
  * Zero-alloc: writes result into the provided `out` Vec2 and returns it.
179
160
  */
180
161
  declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: number, dy: number, halfW: number, halfH: number, out: Vec2): Vec2;
181
-
182
- export { AABB, type AABBShape, CircleShape, CircleShape as CircleShapeType, type Collider, type CollisionPair, CollisionWorld, type RaycastHit, type RaycastTarget, type Shape, type SolidGrid, type SolidTile, type SpatialEntry, SpatialIndex, buildSolidGrid, createCollider, isBlockedAABB, moveAndSlideInto, raycast, shapeBounds, shapeBoundsInto, shapeOverlap };
162
+ //#endregion
163
+ //#region src/spatial-index.d.ts
164
+ /** Item stored in the spatial index. */
165
+ interface SpatialEntry<T> {
166
+ readonly item: T;
167
+ /** Bounding rect (minX, minY, maxX, maxY). */
168
+ minX: number;
169
+ minY: number;
170
+ maxX: number;
171
+ maxY: number;
172
+ }
173
+ /**
174
+ * Uniform spatial-hash broadphase.
175
+ * `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.
176
+ * `query` walks only the cells overlapping the given rect; result order is unspecified.
177
+ * Zero-alloc in the query hot-path: duplicate suppression uses a monotone stamp counter,
178
+ * not a per-call Set.
179
+ */
180
+ declare class SpatialIndex<T> {
181
+ private _data;
182
+ private _slots;
183
+ private _cells;
184
+ private _gen;
185
+ /** Number of items in the index. */
186
+ get size(): number;
187
+ /** Insert an item with a bounding rect. */
188
+ insert(item: T, bounds: Rect): void;
189
+ /** Remove an item. Returns false when the item was not present. */
190
+ remove(item: T): boolean;
191
+ /** Update the bounds for an existing item. No-op when the item is absent. */
192
+ update(item: T, bounds: Rect): void;
193
+ /**
194
+ * Query all items whose bounds overlap the given rect.
195
+ * Result order is unspecified. Appends to `result` when provided and returns it.
196
+ */
197
+ query(bounds: Rect, result?: T[]): T[];
198
+ /** Remove all items. */
199
+ clear(): void;
200
+ private _link;
201
+ private _unlink;
202
+ }
203
+ //#endregion
204
+ export { AABB, type AABBShape, CircleShape, type CircleShape as CircleShapeType, type Collider, type CollisionPair, CollisionWorld, type RaycastHit, type RaycastTarget, type Shape, type SolidGrid, type SolidTile, type SpatialEntry, SpatialIndex, buildSolidGrid, createCollider, isBlockedAABB, moveAndSlideInto, raycast, shapeBounds, shapeBoundsInto, shapeOverlap };
205
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/shape.ts","../src/collider.ts","../src/collision-world.ts","../src/raycast.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;;;;UC/De;;EAEf,OAAO;;EAEP;;EAEA;;EAEA;;;iBAIc,eAAe,OAAO,OAAO,OAAO,QA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