@flare-engine/physics 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Grzegorz Otto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # @flare-engine/physics
2
+
3
+ Built-in AABB / Circle collision detection — `CollisionWorld` with enter/exit signals, `SpatialIndex` brute-force broadphase tuned for <1000 dynamic entities, `raycast` for line-vs-shape queries. Trigger / layer / mask filtering. The planck.js Tier-2 integration is deferred until benchmarks justify it.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ bun add @flare-engine/physics
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { CollisionWorld, AABB, createCollider } from '@flare-engine/physics';
15
+
16
+ const world = new CollisionWorld();
17
+ world.onCollisionStart.add((pair) => console.log(pair.a, pair.b));
18
+
19
+ const colliderA = createCollider({ shape: AABB(32, 32), layer: 1, mask: 2 });
20
+ world.add(entityA, colliderA.shape, x, y, false, colliderA.layer, colliderA.mask);
21
+ world.step();
22
+ ```
23
+
24
+ See [API spec](../../docs/design/api-specs.md#flare-enginephysics--4kb-aabb) and [skill reference](../../.github/skills/flare-physics/SKILL.md).
25
+
26
+ ## License
27
+
28
+ MIT
@@ -0,0 +1,143 @@
1
+ import { Rect, Vec2 } from '@flare-engine/math';
2
+ import { Signal } from '@flare-engine/events';
3
+
4
+ /** AABB shape — axis-aligned bounding box. */
5
+ interface AABBShape {
6
+ readonly type: 'aabb';
7
+ /** Half-width. */
8
+ readonly hw: number;
9
+ /** Half-height. */
10
+ readonly hh: number;
11
+ }
12
+ /** Union of supported physics shapes. */
13
+ type Shape = AABBShape | CircleShape;
14
+ /** Create an AABB shape from width and height. */
15
+ declare function AABB(width: number, height: number): AABBShape;
16
+ /** Circle shape. */
17
+ interface CircleShape {
18
+ readonly type: 'circle';
19
+ readonly radius: number;
20
+ }
21
+ /** Create a circle shape from radius. */
22
+ declare function CircleShape(radius: number): CircleShape;
23
+ /** Compute the bounding rect of a shape at a given position. */
24
+ declare function shapeBounds(shape: Shape, x: number, y: number): Rect;
25
+ /** 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;
27
+ /** 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;
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;
57
+ }
58
+
59
+ /** Collision pair data emitted by collision signals. */
60
+ interface CollisionPair {
61
+ readonly entityA: number;
62
+ readonly entityB: number;
63
+ }
64
+ /**
65
+ * AABB collision world. Manages entities with shapes, detects overlaps each step,
66
+ * and emits collision/trigger signals.
67
+ */
68
+ 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;
98
+ }
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
+
114
+ /** Result of a raycast hit. */
115
+ 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;
124
+ }
125
+ /** Entity entry for raycast — position + shape. */
126
+ interface RaycastTarget {
127
+ entity: number;
128
+ shape: Shape;
129
+ x: number;
130
+ y: number;
131
+ }
132
+ /**
133
+ * Cast a ray against a list of targets. Returns the closest hit, or null.
134
+ * @param ox Ray origin X
135
+ * @param oy Ray origin Y
136
+ * @param dx Ray direction X (should be normalized)
137
+ * @param dy Ray direction Y (should be normalized)
138
+ * @param maxDist Maximum raycast distance
139
+ * @param targets Array of entities to test against
140
+ */
141
+ declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
142
+
143
+ export { AABB, type AABBShape, CircleShape, CircleShape as CircleShapeType, type Collider, type CollisionPair, CollisionWorld, type RaycastHit, type RaycastTarget, type Shape, type SpatialEntry, SpatialIndex, createCollider, raycast, shapeBounds, shapeBoundsInto, shapeOverlap };
@@ -0,0 +1,143 @@
1
+ import { Rect, Vec2 } from '@flare-engine/math';
2
+ import { Signal } from '@flare-engine/events';
3
+
4
+ /** AABB shape — axis-aligned bounding box. */
5
+ interface AABBShape {
6
+ readonly type: 'aabb';
7
+ /** Half-width. */
8
+ readonly hw: number;
9
+ /** Half-height. */
10
+ readonly hh: number;
11
+ }
12
+ /** Union of supported physics shapes. */
13
+ type Shape = AABBShape | CircleShape;
14
+ /** Create an AABB shape from width and height. */
15
+ declare function AABB(width: number, height: number): AABBShape;
16
+ /** Circle shape. */
17
+ interface CircleShape {
18
+ readonly type: 'circle';
19
+ readonly radius: number;
20
+ }
21
+ /** Create a circle shape from radius. */
22
+ declare function CircleShape(radius: number): CircleShape;
23
+ /** Compute the bounding rect of a shape at a given position. */
24
+ declare function shapeBounds(shape: Shape, x: number, y: number): Rect;
25
+ /** 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;
27
+ /** 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;
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;
57
+ }
58
+
59
+ /** Collision pair data emitted by collision signals. */
60
+ interface CollisionPair {
61
+ readonly entityA: number;
62
+ readonly entityB: number;
63
+ }
64
+ /**
65
+ * AABB collision world. Manages entities with shapes, detects overlaps each step,
66
+ * and emits collision/trigger signals.
67
+ */
68
+ 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;
98
+ }
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
+
114
+ /** Result of a raycast hit. */
115
+ 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;
124
+ }
125
+ /** Entity entry for raycast — position + shape. */
126
+ interface RaycastTarget {
127
+ entity: number;
128
+ shape: Shape;
129
+ x: number;
130
+ y: number;
131
+ }
132
+ /**
133
+ * Cast a ray against a list of targets. Returns the closest hit, or null.
134
+ * @param ox Ray origin X
135
+ * @param oy Ray origin Y
136
+ * @param dx Ray direction X (should be normalized)
137
+ * @param dy Ray direction Y (should be normalized)
138
+ * @param maxDist Maximum raycast distance
139
+ * @param targets Array of entities to test against
140
+ */
141
+ declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
142
+
143
+ export { AABB, type AABBShape, CircleShape, CircleShape as CircleShapeType, type Collider, type CollisionPair, CollisionWorld, type RaycastHit, type RaycastTarget, type Shape, type SpatialEntry, SpatialIndex, createCollider, raycast, shapeBounds, shapeBoundsInto, shapeOverlap };
package/dist/index.js ADDED
@@ -0,0 +1,423 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AABB: () => AABB,
24
+ CircleShape: () => CircleShape,
25
+ CollisionWorld: () => CollisionWorld,
26
+ SpatialIndex: () => SpatialIndex,
27
+ createCollider: () => createCollider,
28
+ raycast: () => raycast,
29
+ shapeBounds: () => shapeBounds,
30
+ shapeBoundsInto: () => shapeBoundsInto,
31
+ shapeOverlap: () => shapeOverlap
32
+ });
33
+ module.exports = __toCommonJS(index_exports);
34
+
35
+ // src/shape.ts
36
+ function AABB(width, height) {
37
+ return { type: "aabb", hw: width * 0.5, hh: height * 0.5 };
38
+ }
39
+ function CircleShape(radius) {
40
+ return { type: "circle", radius };
41
+ }
42
+ function shapeBounds(shape, x, y) {
43
+ if (shape.type === "aabb") {
44
+ return { x: x - shape.hw, y: y - shape.hh, width: shape.hw * 2, height: shape.hh * 2 };
45
+ }
46
+ return {
47
+ x: x - shape.radius,
48
+ y: y - shape.radius,
49
+ width: shape.radius * 2,
50
+ height: shape.radius * 2
51
+ };
52
+ }
53
+ function shapeBoundsInto(shape, x, y, out) {
54
+ if (shape.type === "aabb") {
55
+ out.x = x - shape.hw;
56
+ out.y = y - shape.hh;
57
+ out.width = shape.hw * 2;
58
+ out.height = shape.hh * 2;
59
+ return out;
60
+ }
61
+ out.x = x - shape.radius;
62
+ out.y = y - shape.radius;
63
+ out.width = shape.radius * 2;
64
+ out.height = shape.radius * 2;
65
+ return out;
66
+ }
67
+ function shapeOverlap(shapeA, ax, ay, shapeB, bx, by) {
68
+ if (shapeA.type === "aabb" && shapeB.type === "aabb") {
69
+ return aabbVsAabb(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh);
70
+ }
71
+ if (shapeA.type === "circle" && shapeB.type === "circle") {
72
+ return circleVsCircle(ax, ay, shapeA.radius, bx, by, shapeB.radius);
73
+ }
74
+ if (shapeA.type === "aabb" && shapeB.type === "circle") {
75
+ return aabbVsCircle(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius);
76
+ }
77
+ return aabbVsCircle(
78
+ bx,
79
+ by,
80
+ shapeB.hw,
81
+ shapeB.hh,
82
+ ax,
83
+ ay,
84
+ shapeA.radius
85
+ );
86
+ }
87
+ function aabbVsAabb(ax, ay, ahw, ahh, bx, by, bhw, bhh) {
88
+ return Math.abs(ax - bx) < ahw + bhw && Math.abs(ay - by) < ahh + bhh;
89
+ }
90
+ function circleVsCircle(ax, ay, ar, bx, by, br) {
91
+ const dx = bx - ax;
92
+ const dy = by - ay;
93
+ const radSum = ar + br;
94
+ return dx * dx + dy * dy < radSum * radSum;
95
+ }
96
+ function aabbVsCircle(ax, ay, ahw, ahh, cx, cy, cr) {
97
+ const left = ax - ahw;
98
+ const right = ax + ahw;
99
+ const top = ay - ahh;
100
+ const bottom = ay + ahh;
101
+ const closestX = cx < left ? left : cx > right ? right : cx;
102
+ const closestY = cy < top ? top : cy > bottom ? bottom : cy;
103
+ const dx = cx - closestX;
104
+ const dy = cy - closestY;
105
+ return dx * dx + dy * dy <= cr * cr;
106
+ }
107
+
108
+ // src/spatial-index.ts
109
+ var SpatialIndex = class {
110
+ entries = [];
111
+ /** Number of items in the index. */
112
+ get size() {
113
+ return this.entries.length;
114
+ }
115
+ /** Insert an item with a bounding rect. */
116
+ insert(item, bounds) {
117
+ this.entries.push({
118
+ item,
119
+ minX: bounds.x,
120
+ minY: bounds.y,
121
+ maxX: bounds.x + bounds.width,
122
+ maxY: bounds.y + bounds.height
123
+ });
124
+ }
125
+ /** Remove an item. */
126
+ remove(item) {
127
+ const idx = this.entries.findIndex((e) => e.item === item);
128
+ if (idx === -1) return false;
129
+ const last = this.entries.pop();
130
+ if (idx < this.entries.length) {
131
+ this.entries[idx] = last;
132
+ }
133
+ return true;
134
+ }
135
+ /** Update the bounds for an existing item. */
136
+ update(item, bounds) {
137
+ const entry = this.entries.find((e) => e.item === item);
138
+ if (entry) {
139
+ entry.minX = bounds.x;
140
+ entry.minY = bounds.y;
141
+ entry.maxX = bounds.x + bounds.width;
142
+ entry.maxY = bounds.y + bounds.height;
143
+ }
144
+ }
145
+ /** Query all items whose bounds overlap the given rect. */
146
+ query(bounds, result) {
147
+ const out = result ?? [];
148
+ const qMinX = bounds.x;
149
+ const qMinY = bounds.y;
150
+ const qMaxX = bounds.x + bounds.width;
151
+ const qMaxY = bounds.y + bounds.height;
152
+ for (let i = 0; i < this.entries.length; i++) {
153
+ const e = this.entries[i];
154
+ if (e.maxX > qMinX && e.minX < qMaxX && e.maxY > qMinY && e.minY < qMaxY) {
155
+ out.push(e.item);
156
+ }
157
+ }
158
+ return out;
159
+ }
160
+ /** Remove all items. */
161
+ clear() {
162
+ this.entries.length = 0;
163
+ }
164
+ };
165
+
166
+ // src/collision-world.ts
167
+ var import_events = require("@flare-engine/events");
168
+ var ENTITY_SHIFT = 2 ** 26;
169
+ function encodePair(a, b) {
170
+ const lo = a < b ? a : b;
171
+ const hi = a < b ? b : a;
172
+ return lo * ENTITY_SHIFT + hi;
173
+ }
174
+ function decodeLo(key) {
175
+ return Math.floor(key / ENTITY_SHIFT);
176
+ }
177
+ function decodeHi(key) {
178
+ return key - Math.floor(key / ENTITY_SHIFT) * ENTITY_SHIFT;
179
+ }
180
+ var CollisionWorld = class {
181
+ onCollisionStart = new import_events.Signal();
182
+ onCollisionEnd = new import_events.Signal();
183
+ onTriggerEnter = new import_events.Signal();
184
+ onTriggerExit = new import_events.Signal();
185
+ entries = /* @__PURE__ */ new Map();
186
+ spatial = new SpatialIndex();
187
+ /**
188
+ * Two pre-allocated pair buffers — swapped each step (no per-frame allocation).
189
+ * Keys are packed numbers via `encodePair(a, b)`.
190
+ */
191
+ pairBufA = /* @__PURE__ */ new Set();
192
+ pairBufB = /* @__PURE__ */ new Set();
193
+ activePairs = this.pairBufA;
194
+ /** Reusable query result buffer. */
195
+ queryBuf = [];
196
+ /** Reusable bounds rect — avoids per-entity Rect allocation in step(). */
197
+ boundsBuf = { x: 0, y: 0, width: 0, height: 0 };
198
+ /** Add an entity with a shape. */
199
+ add(entity, shape, x, y, isTrigger = false, layer = 1, mask = 4294967295) {
200
+ const entry = { entity, shape, x, y, isTrigger, layer, mask };
201
+ this.entries.set(entity, entry);
202
+ shapeBoundsInto(shape, x, y, this.boundsBuf);
203
+ this.spatial.insert(entity, this.boundsBuf);
204
+ }
205
+ /** Remove an entity from the world. Emits exit signals for any active pairs. */
206
+ remove(entity) {
207
+ const entry = this.entries.get(entity);
208
+ if (!entry) return;
209
+ this.entries.delete(entity);
210
+ this.spatial.remove(entity);
211
+ for (const key of this.activePairs) {
212
+ const lo = decodeLo(key);
213
+ const hi = decodeHi(key);
214
+ if (lo !== entity && hi !== entity) continue;
215
+ this.activePairs.delete(key);
216
+ const otherEntity = lo === entity ? hi : lo;
217
+ const otherEntry = this.entries.get(otherEntity);
218
+ const pair = { entityA: lo, entityB: hi };
219
+ if (entry.isTrigger || otherEntry?.isTrigger) {
220
+ this.onTriggerExit.emit(pair);
221
+ } else {
222
+ this.onCollisionEnd.emit(pair);
223
+ }
224
+ }
225
+ }
226
+ /** Update an entity's position. */
227
+ setPosition(entity, x, y) {
228
+ const entry = this.entries.get(entity);
229
+ if (!entry) return;
230
+ entry.x = x;
231
+ entry.y = y;
232
+ shapeBoundsInto(entry.shape, x, y, this.boundsBuf);
233
+ this.spatial.update(entity, this.boundsBuf);
234
+ }
235
+ /** Number of entities in the world. */
236
+ get size() {
237
+ return this.entries.size;
238
+ }
239
+ /** Run one collision detection step. */
240
+ step() {
241
+ const newPairs = this.activePairs === this.pairBufA ? this.pairBufB : this.pairBufA;
242
+ newPairs.clear();
243
+ for (const [entityA, entryA] of this.entries) {
244
+ shapeBoundsInto(entryA.shape, entryA.x, entryA.y, this.boundsBuf);
245
+ this.queryBuf.length = 0;
246
+ this.spatial.query(this.boundsBuf, this.queryBuf);
247
+ for (let i = 0; i < this.queryBuf.length; i++) {
248
+ const entityB = this.queryBuf[i];
249
+ if (entityB <= entityA) continue;
250
+ const entryB = this.entries.get(entityB);
251
+ if (!entryB) continue;
252
+ if ((entryA.layer & entryB.mask) === 0 && (entryB.layer & entryA.mask) === 0) continue;
253
+ if (!shapeOverlap(entryA.shape, entryA.x, entryA.y, entryB.shape, entryB.x, entryB.y)) {
254
+ continue;
255
+ }
256
+ newPairs.add(encodePair(entityA, entityB));
257
+ }
258
+ }
259
+ for (const key of newPairs) {
260
+ if (!this.activePairs.has(key)) {
261
+ const lo = decodeLo(key);
262
+ const hi = decodeHi(key);
263
+ const a = this.entries.get(lo);
264
+ const b = this.entries.get(hi);
265
+ const pair = { entityA: lo, entityB: hi };
266
+ if (a?.isTrigger || b?.isTrigger) {
267
+ this.onTriggerEnter.emit(pair);
268
+ } else {
269
+ this.onCollisionStart.emit(pair);
270
+ }
271
+ }
272
+ }
273
+ for (const key of this.activePairs) {
274
+ if (!newPairs.has(key)) {
275
+ const lo = decodeLo(key);
276
+ const hi = decodeHi(key);
277
+ const a = this.entries.get(lo);
278
+ const b = this.entries.get(hi);
279
+ const pair = { entityA: lo, entityB: hi };
280
+ if (a?.isTrigger || b?.isTrigger) {
281
+ this.onTriggerExit.emit(pair);
282
+ } else {
283
+ this.onCollisionEnd.emit(pair);
284
+ }
285
+ }
286
+ }
287
+ this.activePairs = newPairs;
288
+ }
289
+ /** Remove all entities. */
290
+ clear() {
291
+ this.entries.clear();
292
+ this.spatial.clear();
293
+ this.pairBufA.clear();
294
+ this.pairBufB.clear();
295
+ this.activePairs = this.pairBufA;
296
+ }
297
+ };
298
+
299
+ // src/collider.ts
300
+ function createCollider(shape, opts) {
301
+ return {
302
+ shape,
303
+ isTrigger: opts?.isTrigger ?? false,
304
+ layer: opts?.layer ?? 1,
305
+ mask: opts?.mask ?? 4294967295
306
+ };
307
+ }
308
+
309
+ // src/raycast.ts
310
+ function raycast(ox, oy, dx, dy, maxDist, targets) {
311
+ let closest = null;
312
+ for (let i = 0; i < targets.length; i++) {
313
+ const t = targets[i];
314
+ const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);
315
+ if (hit && (closest === null || hit.distance < closest.distance)) {
316
+ closest = hit;
317
+ }
318
+ }
319
+ return closest;
320
+ }
321
+ function rayVsShape(ox, oy, dx, dy, maxDist, target) {
322
+ if (target.shape.type === "aabb") {
323
+ return rayVsAABB(
324
+ ox,
325
+ oy,
326
+ dx,
327
+ dy,
328
+ maxDist,
329
+ target.entity,
330
+ target.x,
331
+ target.y,
332
+ target.shape.hw,
333
+ target.shape.hh
334
+ );
335
+ }
336
+ return rayVsCircle(
337
+ ox,
338
+ oy,
339
+ dx,
340
+ dy,
341
+ maxDist,
342
+ target.entity,
343
+ target.x,
344
+ target.y,
345
+ target.shape.radius
346
+ );
347
+ }
348
+ function rayVsAABB(ox, oy, dx, dy, maxDist, entity, cx, cy, hw, hh) {
349
+ const minX = cx - hw;
350
+ const maxX = cx + hw;
351
+ const minY = cy - hh;
352
+ const maxY = cy + hh;
353
+ const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
354
+ const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
355
+ const tx1 = (minX - ox) * invDx;
356
+ const tx2 = (maxX - ox) * invDx;
357
+ const ty1 = (minY - oy) * invDy;
358
+ const ty2 = (maxY - oy) * invDy;
359
+ const tMinX = Math.min(tx1, tx2);
360
+ const tMaxX = Math.max(tx1, tx2);
361
+ const tMinY = Math.min(ty1, ty2);
362
+ const tMaxY = Math.max(ty1, ty2);
363
+ const tMin = Math.max(tMinX, tMinY);
364
+ const tMax = Math.min(tMaxX, tMaxY);
365
+ if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;
366
+ const t = tMin >= 0 ? tMin : tMax;
367
+ if (t > maxDist || t < 0) return null;
368
+ const px = ox + dx * t;
369
+ const py = oy + dy * t;
370
+ let nx = 0;
371
+ let ny = 0;
372
+ if (tMinX > tMinY) {
373
+ nx = dx < 0 ? 1 : -1;
374
+ } else {
375
+ ny = dy < 0 ? 1 : -1;
376
+ }
377
+ return {
378
+ entity,
379
+ distance: t,
380
+ point: { x: px, y: py },
381
+ normal: { x: nx, y: ny }
382
+ };
383
+ }
384
+ function rayVsCircle(ox, oy, dx, dy, maxDist, entity, cx, cy, radius) {
385
+ const fx = ox - cx;
386
+ const fy = oy - cy;
387
+ const a = dx * dx + dy * dy;
388
+ const b = 2 * (fx * dx + fy * dy);
389
+ const c = fx * fx + fy * fy - radius * radius;
390
+ if (a < 1e-12) return null;
391
+ const discriminant = b * b - 4 * a * c;
392
+ if (discriminant < 0) return null;
393
+ const sqrtDisc = Math.sqrt(discriminant);
394
+ let t = (-b - sqrtDisc) / (2 * a);
395
+ if (t < 0) {
396
+ t = (-b + sqrtDisc) / (2 * a);
397
+ }
398
+ if (t < 0 || t > maxDist) return null;
399
+ const px = ox + dx * t;
400
+ const py = oy + dy * t;
401
+ const nx = px - cx;
402
+ const ny = py - cy;
403
+ const len = Math.sqrt(nx * nx + ny * ny);
404
+ return {
405
+ entity,
406
+ distance: t,
407
+ point: { x: px, y: py },
408
+ normal: len > 0 ? { x: nx / len, y: ny / len } : { x: 0, y: 0 }
409
+ };
410
+ }
411
+ // Annotate the CommonJS export names for ESM import in node:
412
+ 0 && (module.exports = {
413
+ AABB,
414
+ CircleShape,
415
+ CollisionWorld,
416
+ SpatialIndex,
417
+ createCollider,
418
+ raycast,
419
+ shapeBounds,
420
+ shapeBoundsInto,
421
+ shapeOverlap
422
+ });
423
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/shape.ts","../src/spatial-index.ts","../src/collision-world.ts","../src/collider.ts","../src/raycast.ts"],"sourcesContent":["export {\n type AABBShape,\n type CircleShape as CircleShapeType,\n type Shape,\n AABB,\n CircleShape,\n shapeBounds,\n shapeBoundsInto,\n shapeOverlap,\n} from './shape.js';\n\nexport { type SpatialEntry, SpatialIndex } from './spatial-index.js';\n\nexport { type CollisionPair, CollisionWorld } from './collision-world.js';\n\nexport { type Collider, createCollider } from './collider.js';\n\nexport { type RaycastHit, type RaycastTarget, raycast } from './raycast.js';\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 return dx * dx + dy * dy <= cr * cr;\n}\n","import type { Rect } from '@flare-engine/math';\n\n/** Item stored in the spatial index. */\nexport interface SpatialEntry<T> {\n readonly item: T;\n /** Bounding rect (minX, minY, width, height). */\n minX: number;\n minY: number;\n maxX: number;\n maxY: number;\n}\n\n/**\n * Brute-force spatial index for broadphase collision.\n * Suitable for < ~1000 dynamic entities. Upgrade to grid/rbush for more.\n */\nexport class SpatialIndex<T> {\n private entries: SpatialEntry<T>[] = [];\n\n /** Number of items in the index. */\n get size(): number {\n return this.entries.length;\n }\n\n /** Insert an item with a bounding rect. */\n insert(item: T, bounds: Rect): void {\n this.entries.push({\n item,\n minX: bounds.x,\n minY: bounds.y,\n maxX: bounds.x + bounds.width,\n maxY: bounds.y + bounds.height,\n });\n }\n\n /** Remove an item. */\n remove(item: T): boolean {\n const idx = this.entries.findIndex((e) => e.item === item);\n if (idx === -1) return false;\n // Swap-remove for O(1) removal\n const last = this.entries.pop()!;\n if (idx < this.entries.length) {\n this.entries[idx] = last;\n }\n return true;\n }\n\n /** Update the bounds for an existing item. */\n update(item: T, bounds: Rect): void {\n const entry = this.entries.find((e) => e.item === item);\n if (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 }\n }\n\n /** Query all items whose bounds overlap the given rect. */\n query(bounds: Rect, result?: T[]): T[] {\n const out = result ?? [];\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 for (let i = 0; i < this.entries.length; i++) {\n const e = this.entries[i]!;\n if (e.maxX > qMinX && e.minX < qMaxX && e.maxY > qMinY && e.minY < qMaxY) {\n out.push(e.item);\n }\n }\n return out;\n }\n\n /** Remove all items. */\n clear(): void {\n this.entries.length = 0;\n }\n}\n","import { Signal } from '@flare-engine/events';\nimport type { Rect } from '@flare-engine/math';\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\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. */\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 this.entries.set(entity, entry);\n shapeBoundsInto(shape, x, y, this.boundsBuf);\n this.spatial.insert(entity, this.boundsBuf);\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 /** 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 [entityA, entryA] of this.entries) {\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 /** 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","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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqBO,SAAS,KAAK,OAAe,QAA2B;AAC7D,SAAO,EAAE,MAAM,QAAQ,IAAI,QAAQ,KAAK,IAAI,SAAS,IAAI;AAC3D;AAGO,SAAS,YAAY,QAA6B;AACvD,SAAO,EAAE,MAAM,UAAU,OAAO;AAClC;AAGO,SAAS,YAAY,OAAc,GAAW,GAAiB;AACpE,MAAI,MAAM,SAAS,QAAQ;AACzB,WAAO,EAAE,GAAG,IAAI,MAAM,IAAI,GAAG,IAAI,MAAM,IAAI,OAAO,MAAM,KAAK,GAAG,QAAQ,MAAM,KAAK,EAAE;AAAA,EACvF;AACA,SAAO;AAAA,IACL,GAAG,IAAI,MAAM;AAAA,IACb,GAAG,IAAI,MAAM;AAAA,IACb,OAAO,MAAM,SAAS;AAAA,IACtB,QAAQ,MAAM,SAAS;AAAA,EACzB;AACF;AAGO,SAAS,gBAAgB,OAAc,GAAW,GAAW,KAAiB;AACnF,MAAI,MAAM,SAAS,QAAQ;AACzB,QAAI,IAAI,IAAI,MAAM;AAClB,QAAI,IAAI,IAAI,MAAM;AACl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,EACpB;AAEA,SAAO;AAAA,IACL;AAAA,IACA,UAAU;AAAA,IACV,OAAO,EAAE,GAAG,IAAI,GAAG,GAAG;AAAA,IACtB,QAAQ,EAAE,GAAG,IAAI,GAAG,GAAG;AAAA,EACzB;AACF;AAEA,SAAS,YACP,IACA,IACA,IACA,IACA,SACA,QACA,IACA,IACA,QACmB;AACnB,QAAM,KAAK,KAAK;AAChB,QAAM,KAAK,KAAK;AAEhB,QAAM,IAAI,KAAK,KAAK,KAAK;AACzB,QAAM,IAAI,KAAK,KAAK,KAAK,KAAK;AAC9B,QAAM,IAAI,KAAK,KAAK,KAAK,KAAK,SAAS;AAEvC,MAAI,IAAI,MAAO,QAAO;AAEtB,QAAM,eAAe,IAAI,IAAI,IAAI,IAAI;AACrC,MAAI,eAAe,EAAG,QAAO;AAE7B,QAAM,WAAW,KAAK,KAAK,YAAY;AACvC,MAAI,KAAK,CAAC,IAAI,aAAa,IAAI;AAG/B,MAAI,IAAI,GAAG;AACT,SAAK,CAAC,IAAI,aAAa,IAAI;AAAA,EAC7B;AACA,MAAI,IAAI,KAAK,IAAI,QAAS,QAAO;AAEjC,QAAM,KAAK,KAAK,KAAK;AACrB,QAAM,KAAK,KAAK,KAAK;AACrB,QAAM,KAAK,KAAK;AAChB,QAAM,KAAK,KAAK;AAChB,QAAM,MAAM,KAAK,KAAK,KAAK,KAAK,KAAK,EAAE;AAEvC,SAAO;AAAA,IACL;AAAA,IACA,UAAU;AAAA,IACV,OAAO,EAAE,GAAG,IAAI,GAAG,GAAG;AAAA,IACtB,QAAQ,MAAM,IAAI,EAAE,GAAG,KAAK,KAAK,GAAG,KAAK,IAAI,IAAI,EAAE,GAAG,GAAG,GAAG,EAAE;AAAA,EAChE;AACF;","names":[]}
package/dist/index.mjs ADDED
@@ -0,0 +1,388 @@
1
+ // src/shape.ts
2
+ function AABB(width, height) {
3
+ return { type: "aabb", hw: width * 0.5, hh: height * 0.5 };
4
+ }
5
+ function CircleShape(radius) {
6
+ return { type: "circle", radius };
7
+ }
8
+ function shapeBounds(shape, x, y) {
9
+ if (shape.type === "aabb") {
10
+ return { x: x - shape.hw, y: y - shape.hh, width: shape.hw * 2, height: shape.hh * 2 };
11
+ }
12
+ return {
13
+ x: x - shape.radius,
14
+ y: y - shape.radius,
15
+ width: shape.radius * 2,
16
+ height: shape.radius * 2
17
+ };
18
+ }
19
+ function shapeBoundsInto(shape, x, y, out) {
20
+ if (shape.type === "aabb") {
21
+ out.x = x - shape.hw;
22
+ out.y = y - shape.hh;
23
+ out.width = shape.hw * 2;
24
+ out.height = shape.hh * 2;
25
+ return out;
26
+ }
27
+ out.x = x - shape.radius;
28
+ out.y = y - shape.radius;
29
+ out.width = shape.radius * 2;
30
+ out.height = shape.radius * 2;
31
+ return out;
32
+ }
33
+ function shapeOverlap(shapeA, ax, ay, shapeB, bx, by) {
34
+ if (shapeA.type === "aabb" && shapeB.type === "aabb") {
35
+ return aabbVsAabb(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh);
36
+ }
37
+ if (shapeA.type === "circle" && shapeB.type === "circle") {
38
+ return circleVsCircle(ax, ay, shapeA.radius, bx, by, shapeB.radius);
39
+ }
40
+ if (shapeA.type === "aabb" && shapeB.type === "circle") {
41
+ return aabbVsCircle(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius);
42
+ }
43
+ return aabbVsCircle(
44
+ bx,
45
+ by,
46
+ shapeB.hw,
47
+ shapeB.hh,
48
+ ax,
49
+ ay,
50
+ shapeA.radius
51
+ );
52
+ }
53
+ function aabbVsAabb(ax, ay, ahw, ahh, bx, by, bhw, bhh) {
54
+ return Math.abs(ax - bx) < ahw + bhw && Math.abs(ay - by) < ahh + bhh;
55
+ }
56
+ function circleVsCircle(ax, ay, ar, bx, by, br) {
57
+ const dx = bx - ax;
58
+ const dy = by - ay;
59
+ const radSum = ar + br;
60
+ return dx * dx + dy * dy < radSum * radSum;
61
+ }
62
+ function aabbVsCircle(ax, ay, ahw, ahh, cx, cy, cr) {
63
+ const left = ax - ahw;
64
+ const right = ax + ahw;
65
+ const top = ay - ahh;
66
+ const bottom = ay + ahh;
67
+ const closestX = cx < left ? left : cx > right ? right : cx;
68
+ const closestY = cy < top ? top : cy > bottom ? bottom : cy;
69
+ const dx = cx - closestX;
70
+ const dy = cy - closestY;
71
+ return dx * dx + dy * dy <= cr * cr;
72
+ }
73
+
74
+ // src/spatial-index.ts
75
+ var SpatialIndex = class {
76
+ entries = [];
77
+ /** Number of items in the index. */
78
+ get size() {
79
+ return this.entries.length;
80
+ }
81
+ /** Insert an item with a bounding rect. */
82
+ insert(item, bounds) {
83
+ this.entries.push({
84
+ item,
85
+ minX: bounds.x,
86
+ minY: bounds.y,
87
+ maxX: bounds.x + bounds.width,
88
+ maxY: bounds.y + bounds.height
89
+ });
90
+ }
91
+ /** Remove an item. */
92
+ remove(item) {
93
+ const idx = this.entries.findIndex((e) => e.item === item);
94
+ if (idx === -1) return false;
95
+ const last = this.entries.pop();
96
+ if (idx < this.entries.length) {
97
+ this.entries[idx] = last;
98
+ }
99
+ return true;
100
+ }
101
+ /** Update the bounds for an existing item. */
102
+ update(item, bounds) {
103
+ const entry = this.entries.find((e) => e.item === item);
104
+ if (entry) {
105
+ entry.minX = bounds.x;
106
+ entry.minY = bounds.y;
107
+ entry.maxX = bounds.x + bounds.width;
108
+ entry.maxY = bounds.y + bounds.height;
109
+ }
110
+ }
111
+ /** Query all items whose bounds overlap the given rect. */
112
+ query(bounds, result) {
113
+ const out = result ?? [];
114
+ const qMinX = bounds.x;
115
+ const qMinY = bounds.y;
116
+ const qMaxX = bounds.x + bounds.width;
117
+ const qMaxY = bounds.y + bounds.height;
118
+ for (let i = 0; i < this.entries.length; i++) {
119
+ const e = this.entries[i];
120
+ if (e.maxX > qMinX && e.minX < qMaxX && e.maxY > qMinY && e.minY < qMaxY) {
121
+ out.push(e.item);
122
+ }
123
+ }
124
+ return out;
125
+ }
126
+ /** Remove all items. */
127
+ clear() {
128
+ this.entries.length = 0;
129
+ }
130
+ };
131
+
132
+ // src/collision-world.ts
133
+ import { Signal } from "@flare-engine/events";
134
+ var ENTITY_SHIFT = 2 ** 26;
135
+ function encodePair(a, b) {
136
+ const lo = a < b ? a : b;
137
+ const hi = a < b ? b : a;
138
+ return lo * ENTITY_SHIFT + hi;
139
+ }
140
+ function decodeLo(key) {
141
+ return Math.floor(key / ENTITY_SHIFT);
142
+ }
143
+ function decodeHi(key) {
144
+ return key - Math.floor(key / ENTITY_SHIFT) * ENTITY_SHIFT;
145
+ }
146
+ var CollisionWorld = class {
147
+ onCollisionStart = new Signal();
148
+ onCollisionEnd = new Signal();
149
+ onTriggerEnter = new Signal();
150
+ onTriggerExit = new Signal();
151
+ entries = /* @__PURE__ */ new Map();
152
+ spatial = new SpatialIndex();
153
+ /**
154
+ * Two pre-allocated pair buffers — swapped each step (no per-frame allocation).
155
+ * Keys are packed numbers via `encodePair(a, b)`.
156
+ */
157
+ pairBufA = /* @__PURE__ */ new Set();
158
+ pairBufB = /* @__PURE__ */ new Set();
159
+ activePairs = this.pairBufA;
160
+ /** Reusable query result buffer. */
161
+ queryBuf = [];
162
+ /** Reusable bounds rect — avoids per-entity Rect allocation in step(). */
163
+ boundsBuf = { x: 0, y: 0, width: 0, height: 0 };
164
+ /** Add an entity with a shape. */
165
+ add(entity, shape, x, y, isTrigger = false, layer = 1, mask = 4294967295) {
166
+ const entry = { entity, shape, x, y, isTrigger, layer, mask };
167
+ this.entries.set(entity, entry);
168
+ shapeBoundsInto(shape, x, y, this.boundsBuf);
169
+ this.spatial.insert(entity, this.boundsBuf);
170
+ }
171
+ /** Remove an entity from the world. Emits exit signals for any active pairs. */
172
+ remove(entity) {
173
+ const entry = this.entries.get(entity);
174
+ if (!entry) return;
175
+ this.entries.delete(entity);
176
+ this.spatial.remove(entity);
177
+ for (const key of this.activePairs) {
178
+ const lo = decodeLo(key);
179
+ const hi = decodeHi(key);
180
+ if (lo !== entity && hi !== entity) continue;
181
+ this.activePairs.delete(key);
182
+ const otherEntity = lo === entity ? hi : lo;
183
+ const otherEntry = this.entries.get(otherEntity);
184
+ const pair = { entityA: lo, entityB: hi };
185
+ if (entry.isTrigger || otherEntry?.isTrigger) {
186
+ this.onTriggerExit.emit(pair);
187
+ } else {
188
+ this.onCollisionEnd.emit(pair);
189
+ }
190
+ }
191
+ }
192
+ /** Update an entity's position. */
193
+ setPosition(entity, x, y) {
194
+ const entry = this.entries.get(entity);
195
+ if (!entry) return;
196
+ entry.x = x;
197
+ entry.y = y;
198
+ shapeBoundsInto(entry.shape, x, y, this.boundsBuf);
199
+ this.spatial.update(entity, this.boundsBuf);
200
+ }
201
+ /** Number of entities in the world. */
202
+ get size() {
203
+ return this.entries.size;
204
+ }
205
+ /** Run one collision detection step. */
206
+ step() {
207
+ const newPairs = this.activePairs === this.pairBufA ? this.pairBufB : this.pairBufA;
208
+ newPairs.clear();
209
+ for (const [entityA, entryA] of this.entries) {
210
+ shapeBoundsInto(entryA.shape, entryA.x, entryA.y, this.boundsBuf);
211
+ this.queryBuf.length = 0;
212
+ this.spatial.query(this.boundsBuf, this.queryBuf);
213
+ for (let i = 0; i < this.queryBuf.length; i++) {
214
+ const entityB = this.queryBuf[i];
215
+ if (entityB <= entityA) continue;
216
+ const entryB = this.entries.get(entityB);
217
+ if (!entryB) continue;
218
+ if ((entryA.layer & entryB.mask) === 0 && (entryB.layer & entryA.mask) === 0) continue;
219
+ if (!shapeOverlap(entryA.shape, entryA.x, entryA.y, entryB.shape, entryB.x, entryB.y)) {
220
+ continue;
221
+ }
222
+ newPairs.add(encodePair(entityA, entityB));
223
+ }
224
+ }
225
+ for (const key of newPairs) {
226
+ if (!this.activePairs.has(key)) {
227
+ const lo = decodeLo(key);
228
+ const hi = decodeHi(key);
229
+ const a = this.entries.get(lo);
230
+ const b = this.entries.get(hi);
231
+ const pair = { entityA: lo, entityB: hi };
232
+ if (a?.isTrigger || b?.isTrigger) {
233
+ this.onTriggerEnter.emit(pair);
234
+ } else {
235
+ this.onCollisionStart.emit(pair);
236
+ }
237
+ }
238
+ }
239
+ for (const key of this.activePairs) {
240
+ if (!newPairs.has(key)) {
241
+ const lo = decodeLo(key);
242
+ const hi = decodeHi(key);
243
+ const a = this.entries.get(lo);
244
+ const b = this.entries.get(hi);
245
+ const pair = { entityA: lo, entityB: hi };
246
+ if (a?.isTrigger || b?.isTrigger) {
247
+ this.onTriggerExit.emit(pair);
248
+ } else {
249
+ this.onCollisionEnd.emit(pair);
250
+ }
251
+ }
252
+ }
253
+ this.activePairs = newPairs;
254
+ }
255
+ /** Remove all entities. */
256
+ clear() {
257
+ this.entries.clear();
258
+ this.spatial.clear();
259
+ this.pairBufA.clear();
260
+ this.pairBufB.clear();
261
+ this.activePairs = this.pairBufA;
262
+ }
263
+ };
264
+
265
+ // src/collider.ts
266
+ function createCollider(shape, opts) {
267
+ return {
268
+ shape,
269
+ isTrigger: opts?.isTrigger ?? false,
270
+ layer: opts?.layer ?? 1,
271
+ mask: opts?.mask ?? 4294967295
272
+ };
273
+ }
274
+
275
+ // src/raycast.ts
276
+ function raycast(ox, oy, dx, dy, maxDist, targets) {
277
+ let closest = null;
278
+ for (let i = 0; i < targets.length; i++) {
279
+ const t = targets[i];
280
+ const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);
281
+ if (hit && (closest === null || hit.distance < closest.distance)) {
282
+ closest = hit;
283
+ }
284
+ }
285
+ return closest;
286
+ }
287
+ function rayVsShape(ox, oy, dx, dy, maxDist, target) {
288
+ if (target.shape.type === "aabb") {
289
+ return rayVsAABB(
290
+ ox,
291
+ oy,
292
+ dx,
293
+ dy,
294
+ maxDist,
295
+ target.entity,
296
+ target.x,
297
+ target.y,
298
+ target.shape.hw,
299
+ target.shape.hh
300
+ );
301
+ }
302
+ return rayVsCircle(
303
+ ox,
304
+ oy,
305
+ dx,
306
+ dy,
307
+ maxDist,
308
+ target.entity,
309
+ target.x,
310
+ target.y,
311
+ target.shape.radius
312
+ );
313
+ }
314
+ function rayVsAABB(ox, oy, dx, dy, maxDist, entity, cx, cy, hw, hh) {
315
+ const minX = cx - hw;
316
+ const maxX = cx + hw;
317
+ const minY = cy - hh;
318
+ const maxY = cy + hh;
319
+ const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
320
+ const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
321
+ const tx1 = (minX - ox) * invDx;
322
+ const tx2 = (maxX - ox) * invDx;
323
+ const ty1 = (minY - oy) * invDy;
324
+ const ty2 = (maxY - oy) * invDy;
325
+ const tMinX = Math.min(tx1, tx2);
326
+ const tMaxX = Math.max(tx1, tx2);
327
+ const tMinY = Math.min(ty1, ty2);
328
+ const tMaxY = Math.max(ty1, ty2);
329
+ const tMin = Math.max(tMinX, tMinY);
330
+ const tMax = Math.min(tMaxX, tMaxY);
331
+ if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;
332
+ const t = tMin >= 0 ? tMin : tMax;
333
+ if (t > maxDist || t < 0) return null;
334
+ const px = ox + dx * t;
335
+ const py = oy + dy * t;
336
+ let nx = 0;
337
+ let ny = 0;
338
+ if (tMinX > tMinY) {
339
+ nx = dx < 0 ? 1 : -1;
340
+ } else {
341
+ ny = dy < 0 ? 1 : -1;
342
+ }
343
+ return {
344
+ entity,
345
+ distance: t,
346
+ point: { x: px, y: py },
347
+ normal: { x: nx, y: ny }
348
+ };
349
+ }
350
+ function rayVsCircle(ox, oy, dx, dy, maxDist, entity, cx, cy, radius) {
351
+ const fx = ox - cx;
352
+ const fy = oy - cy;
353
+ const a = dx * dx + dy * dy;
354
+ const b = 2 * (fx * dx + fy * dy);
355
+ const c = fx * fx + fy * fy - radius * radius;
356
+ if (a < 1e-12) return null;
357
+ const discriminant = b * b - 4 * a * c;
358
+ if (discriminant < 0) return null;
359
+ const sqrtDisc = Math.sqrt(discriminant);
360
+ let t = (-b - sqrtDisc) / (2 * a);
361
+ if (t < 0) {
362
+ t = (-b + sqrtDisc) / (2 * a);
363
+ }
364
+ if (t < 0 || t > maxDist) return null;
365
+ const px = ox + dx * t;
366
+ const py = oy + dy * t;
367
+ const nx = px - cx;
368
+ const ny = py - cy;
369
+ const len = Math.sqrt(nx * nx + ny * ny);
370
+ return {
371
+ entity,
372
+ distance: t,
373
+ point: { x: px, y: py },
374
+ normal: len > 0 ? { x: nx / len, y: ny / len } : { x: 0, y: 0 }
375
+ };
376
+ }
377
+ export {
378
+ AABB,
379
+ CircleShape,
380
+ CollisionWorld,
381
+ SpatialIndex,
382
+ createCollider,
383
+ raycast,
384
+ shapeBounds,
385
+ shapeBoundsInto,
386
+ shapeOverlap
387
+ };
388
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/shape.ts","../src/spatial-index.ts","../src/collision-world.ts","../src/collider.ts","../src/raycast.ts"],"sourcesContent":["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 return dx * dx + dy * dy <= cr * cr;\n}\n","import type { Rect } from '@flare-engine/math';\n\n/** Item stored in the spatial index. */\nexport interface SpatialEntry<T> {\n readonly item: T;\n /** Bounding rect (minX, minY, width, height). */\n minX: number;\n minY: number;\n maxX: number;\n maxY: number;\n}\n\n/**\n * Brute-force spatial index for broadphase collision.\n * Suitable for < ~1000 dynamic entities. Upgrade to grid/rbush for more.\n */\nexport class SpatialIndex<T> {\n private entries: SpatialEntry<T>[] = [];\n\n /** Number of items in the index. */\n get size(): number {\n return this.entries.length;\n }\n\n /** Insert an item with a bounding rect. */\n insert(item: T, bounds: Rect): void {\n this.entries.push({\n item,\n minX: bounds.x,\n minY: bounds.y,\n maxX: bounds.x + bounds.width,\n maxY: bounds.y + bounds.height,\n });\n }\n\n /** Remove an item. */\n remove(item: T): boolean {\n const idx = this.entries.findIndex((e) => e.item === item);\n if (idx === -1) return false;\n // Swap-remove for O(1) removal\n const last = this.entries.pop()!;\n if (idx < this.entries.length) {\n this.entries[idx] = last;\n }\n return true;\n }\n\n /** Update the bounds for an existing item. */\n update(item: T, bounds: Rect): void {\n const entry = this.entries.find((e) => e.item === item);\n if (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 }\n }\n\n /** Query all items whose bounds overlap the given rect. */\n query(bounds: Rect, result?: T[]): T[] {\n const out = result ?? [];\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 for (let i = 0; i < this.entries.length; i++) {\n const e = this.entries[i]!;\n if (e.maxX > qMinX && e.minX < qMaxX && e.maxY > qMinY && e.minY < qMaxY) {\n out.push(e.item);\n }\n }\n return out;\n }\n\n /** Remove all items. */\n clear(): void {\n this.entries.length = 0;\n }\n}\n","import { Signal } from '@flare-engine/events';\nimport type { Rect } from '@flare-engine/math';\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\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. */\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 this.entries.set(entity, entry);\n shapeBoundsInto(shape, x, y, this.boundsBuf);\n this.spatial.insert(entity, this.boundsBuf);\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 /** 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 [entityA, entryA] of this.entries) {\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 /** 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","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"],"mappings":";AAqBO,SAAS,KA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MAAI,IAAI,GAAG;AACT,SAAK,CAAC,IAAI,aAAa,IAAI;AAAA,EAC7B;AACA,MAAI,IAAI,KAAK,IAAI,QAAS,QAAO;AAEjC,QAAM,KAAK,KAAK,KAAK;AACrB,QAAM,KAAK,KAAK,KAAK;AACrB,QAAM,KAAK,KAAK;AAChB,QAAM,KAAK,KAAK;AAChB,QAAM,MAAM,KAAK,KAAK,KAAK,KAAK,KAAK,EAAE;AAEvC,SAAO;AAAA,IACL;AAAA,IACA,UAAU;AAAA,IACV,OAAO,EAAE,GAAG,IAAI,GAAG,GAAG;AAAA,IACtB,QAAQ,MAAM,IAAI,EAAE,GAAG,KAAK,KAAK,GAAG,KAAK,IAAI,IAAI,EAAE,GAAG,GAAG,GAAG,EAAE;AAAA,EAChE;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@flare-engine/physics",
3
+ "version": "0.1.0",
4
+ "description": "AABB collision, spatial index, raycasting",
5
+ "keywords": ["flare-engine", "physics", "collision", "aabb", "raycast"],
6
+ "license": "MIT",
7
+ "author": "Grzegorz Otto",
8
+ "homepage": "https://github.com/grzott/flare-engine#readme",
9
+ "bugs": {
10
+ "url": "https://github.com/grzott/flare-engine/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/grzott/flare-engine.git",
15
+ "directory": "packages/physics"
16
+ },
17
+ "main": "dist/index.js",
18
+ "module": "dist/index.mjs",
19
+ "types": "dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.mjs",
24
+ "require": "./dist/index.js"
25
+ }
26
+ },
27
+ "files": ["dist", "README.md", "LICENSE"],
28
+ "scripts": {
29
+ "build": "tsup",
30
+ "test": "bun test",
31
+ "lint": "biome check src/",
32
+ "typecheck": "tsc --noEmit"
33
+ },
34
+ "dependencies": {
35
+ "@flare-engine/events": "^0.1.0",
36
+ "@flare-engine/math": "^0.1.0"
37
+ },
38
+ "publishConfig": {
39
+ "access": "restricted",
40
+ "registry": "https://registry.npmjs.org/"
41
+ },
42
+ "sideEffects": false
43
+ }