@flare-engine/physics 0.2.0 → 0.3.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/dist/index.mjs CHANGED
@@ -1,428 +1,445 @@
1
- // src/shape.ts
1
+ import { Signal } from "@flare-engine/events";
2
+ //#region src/collider.ts
3
+ /** Create a collider with default settings. */
4
+ function createCollider(shape, opts) {
5
+ return {
6
+ shape,
7
+ isTrigger: opts?.isTrigger ?? false,
8
+ layer: opts?.layer ?? 1,
9
+ mask: opts?.mask ?? 4294967295
10
+ };
11
+ }
12
+ //#endregion
13
+ //#region src/shape.ts
14
+ /** Create an AABB shape from width and height. */
2
15
  function AABB(width, height) {
3
- return { type: "aabb", hw: width * 0.5, hh: height * 0.5 };
16
+ return {
17
+ type: "aabb",
18
+ hw: width * .5,
19
+ hh: height * .5
20
+ };
4
21
  }
22
+ /** Create a circle shape from radius. */
5
23
  function CircleShape(radius) {
6
- return { type: "circle", radius };
24
+ return {
25
+ type: "circle",
26
+ radius
27
+ };
7
28
  }
29
+ /** Compute the bounding rect of a shape at a given position. */
8
30
  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
- };
31
+ if (shape.type === "aabb") return {
32
+ x: x - shape.hw,
33
+ y: y - shape.hh,
34
+ width: shape.hw * 2,
35
+ height: shape.hh * 2
36
+ };
37
+ return {
38
+ x: x - shape.radius,
39
+ y: y - shape.radius,
40
+ width: shape.radius * 2,
41
+ height: shape.radius * 2
42
+ };
18
43
  }
44
+ /** Zero-alloc variant — writes the shape bounds into the provided output rect. */
19
45
  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;
46
+ if (shape.type === "aabb") {
47
+ out.x = x - shape.hw;
48
+ out.y = y - shape.hh;
49
+ out.width = shape.hw * 2;
50
+ out.height = shape.hh * 2;
51
+ return out;
52
+ }
53
+ out.x = x - shape.radius;
54
+ out.y = y - shape.radius;
55
+ out.width = shape.radius * 2;
56
+ out.height = shape.radius * 2;
57
+ return out;
32
58
  }
59
+ /** Test overlap between two positioned shapes. Returns true if overlapping. */
33
60
  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
- );
61
+ if (shapeA.type === "aabb" && shapeB.type === "aabb") return aabbVsAabb(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh);
62
+ if (shapeA.type === "circle" && shapeB.type === "circle") return circleVsCircle(ax, ay, shapeA.radius, bx, by, shapeB.radius);
63
+ if (shapeA.type === "aabb" && shapeB.type === "circle") return aabbVsCircle(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius);
64
+ return aabbVsCircle(bx, by, shapeB.hw, shapeB.hh, ax, ay, shapeA.radius);
52
65
  }
53
66
  function aabbVsAabb(ax, ay, ahw, ahh, bx, by, bhw, bhh) {
54
- return Math.abs(ax - bx) < ahw + bhw && Math.abs(ay - by) < ahh + bhh;
67
+ return Math.abs(ax - bx) < ahw + bhw && Math.abs(ay - by) < ahh + bhh;
55
68
  }
56
69
  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;
70
+ const dx = bx - ax;
71
+ const dy = by - ay;
72
+ const radSum = ar + br;
73
+ return dx * dx + dy * dy < radSum * radSum;
61
74
  }
62
75
  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;
76
+ const left = ax - ahw;
77
+ const right = ax + ahw;
78
+ const top = ay - ahh;
79
+ const bottom = ay + ahh;
80
+ const closestX = cx < left ? left : cx > right ? right : cx;
81
+ const closestY = cy < top ? top : cy > bottom ? bottom : cy;
82
+ const dx = cx - closestX;
83
+ const dy = cy - closestY;
84
+ return dx * dx + dy * dy <= cr * cr;
72
85
  }
73
-
74
- // src/spatial-index.ts
86
+ //#endregion
87
+ //#region src/spatial-index.ts
88
+ /**
89
+ * Brute-force spatial index for broadphase collision.
90
+ * Suitable for < ~1000 dynamic entities. Upgrade to grid/rbush for more.
91
+ */
75
92
  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
- }
93
+ entries = [];
94
+ /** Number of items in the index. */
95
+ get size() {
96
+ return this.entries.length;
97
+ }
98
+ /** Insert an item with a bounding rect. */
99
+ insert(item, bounds) {
100
+ this.entries.push({
101
+ item,
102
+ minX: bounds.x,
103
+ minY: bounds.y,
104
+ maxX: bounds.x + bounds.width,
105
+ maxY: bounds.y + bounds.height
106
+ });
107
+ }
108
+ /** Remove an item. */
109
+ remove(item) {
110
+ const idx = this.entries.findIndex((e) => e.item === item);
111
+ if (idx === -1) return false;
112
+ const last = this.entries.pop();
113
+ if (idx < this.entries.length) this.entries[idx] = last;
114
+ return true;
115
+ }
116
+ /** Update the bounds for an existing item. */
117
+ update(item, bounds) {
118
+ const entry = this.entries.find((e) => e.item === item);
119
+ if (entry) {
120
+ entry.minX = bounds.x;
121
+ entry.minY = bounds.y;
122
+ entry.maxX = bounds.x + bounds.width;
123
+ entry.maxY = bounds.y + bounds.height;
124
+ }
125
+ }
126
+ /** Query all items whose bounds overlap the given rect. */
127
+ query(bounds, result) {
128
+ const out = result ?? [];
129
+ const qMinX = bounds.x;
130
+ const qMinY = bounds.y;
131
+ const qMaxX = bounds.x + bounds.width;
132
+ const qMaxY = bounds.y + bounds.height;
133
+ for (let i = 0; i < this.entries.length; i++) {
134
+ const e = this.entries[i];
135
+ if (e.maxX > qMinX && e.minX < qMaxX && e.maxY > qMinY && e.minY < qMaxY) out.push(e.item);
136
+ }
137
+ return out;
138
+ }
139
+ /** Remove all items. */
140
+ clear() {
141
+ this.entries.length = 0;
142
+ }
130
143
  };
131
-
132
- // src/collision-world.ts
133
- import { Signal } from "@flare-engine/events";
134
- var ENTITY_SHIFT = 2 ** 26;
144
+ //#endregion
145
+ //#region src/collision-world.ts
146
+ const ENTITY_SHIFT = 2 ** 26;
135
147
  function encodePair(a, b) {
136
- const lo = a < b ? a : b;
137
- const hi = a < b ? b : a;
138
- return lo * ENTITY_SHIFT + hi;
148
+ const lo = a < b ? a : b;
149
+ const hi = a < b ? b : a;
150
+ return lo * ENTITY_SHIFT + hi;
139
151
  }
140
152
  function decodeLo(key) {
141
- return Math.floor(key / ENTITY_SHIFT);
153
+ return Math.floor(key / ENTITY_SHIFT);
142
154
  }
143
155
  function decodeHi(key) {
144
- return key - Math.floor(key / ENTITY_SHIFT) * ENTITY_SHIFT;
156
+ return key - Math.floor(key / ENTITY_SHIFT) * ENTITY_SHIFT;
145
157
  }
158
+ /**
159
+ * AABB collision world. Manages entities with shapes, detects overlaps each step,
160
+ * and emits collision/trigger signals.
161
+ */
146
162
  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
- }
163
+ onCollisionStart = new Signal();
164
+ onCollisionEnd = new Signal();
165
+ onTriggerEnter = new Signal();
166
+ onTriggerExit = new Signal();
167
+ entries = /* @__PURE__ */ new Map();
168
+ spatial = new SpatialIndex();
169
+ /**
170
+ * Two pre-allocated pair buffers — swapped each step (no per-frame allocation).
171
+ * Keys are packed numbers via `encodePair(a, b)`.
172
+ */
173
+ pairBufA = /* @__PURE__ */ new Set();
174
+ pairBufB = /* @__PURE__ */ new Set();
175
+ activePairs = this.pairBufA;
176
+ /** Reusable query result buffer. */
177
+ queryBuf = [];
178
+ /** Reusable bounds rect — avoids per-entity Rect allocation in step(). */
179
+ boundsBuf = {
180
+ x: 0,
181
+ y: 0,
182
+ width: 0,
183
+ height: 0
184
+ };
185
+ /** Add an entity with a shape. */
186
+ add(entity, shape, x, y, isTrigger = false, layer = 1, mask = 4294967295) {
187
+ const entry = {
188
+ entity,
189
+ shape,
190
+ x,
191
+ y,
192
+ isTrigger,
193
+ layer,
194
+ mask
195
+ };
196
+ const exists = this.entries.has(entity);
197
+ this.entries.set(entity, entry);
198
+ shapeBoundsInto(shape, x, y, this.boundsBuf);
199
+ if (exists) this.spatial.update(entity, this.boundsBuf);
200
+ else this.spatial.insert(entity, this.boundsBuf);
201
+ }
202
+ /** Remove an entity from the world. Emits exit signals for any active pairs. */
203
+ remove(entity) {
204
+ const entry = this.entries.get(entity);
205
+ if (!entry) return;
206
+ this.entries.delete(entity);
207
+ this.spatial.remove(entity);
208
+ for (const key of this.activePairs) {
209
+ const lo = decodeLo(key);
210
+ const hi = decodeHi(key);
211
+ if (lo !== entity && hi !== entity) continue;
212
+ this.activePairs.delete(key);
213
+ const otherEntity = lo === entity ? hi : lo;
214
+ const otherEntry = this.entries.get(otherEntity);
215
+ const pair = {
216
+ entityA: lo,
217
+ entityB: hi
218
+ };
219
+ if (entry.isTrigger || otherEntry?.isTrigger) this.onTriggerExit.emit(pair);
220
+ else this.onCollisionEnd.emit(pair);
221
+ }
222
+ }
223
+ /** Update an entity's position. */
224
+ setPosition(entity, x, y) {
225
+ const entry = this.entries.get(entity);
226
+ if (!entry) return;
227
+ entry.x = x;
228
+ entry.y = y;
229
+ shapeBoundsInto(entry.shape, x, y, this.boundsBuf);
230
+ this.spatial.update(entity, this.boundsBuf);
231
+ }
232
+ /** Number of entities in the world. */
233
+ get size() {
234
+ return this.entries.size;
235
+ }
236
+ /** Number of broadphase records (one per live entity after add/remove). Diagnostic for broadphase growth. */
237
+ get broadphaseSize() {
238
+ return this.spatial.size;
239
+ }
240
+ /** Run one collision detection step. */
241
+ step() {
242
+ const newPairs = this.activePairs === this.pairBufA ? this.pairBufB : this.pairBufA;
243
+ newPairs.clear();
244
+ for (const [entityA, entryA] of this.entries) {
245
+ shapeBoundsInto(entryA.shape, entryA.x, entryA.y, this.boundsBuf);
246
+ this.queryBuf.length = 0;
247
+ this.spatial.query(this.boundsBuf, this.queryBuf);
248
+ for (let i = 0; i < this.queryBuf.length; i++) {
249
+ const entityB = this.queryBuf[i];
250
+ if (entityB <= entityA) continue;
251
+ const entryB = this.entries.get(entityB);
252
+ if (!entryB) continue;
253
+ if ((entryA.layer & entryB.mask) === 0 && (entryB.layer & entryA.mask) === 0) continue;
254
+ if (!shapeOverlap(entryA.shape, entryA.x, entryA.y, entryB.shape, entryB.x, entryB.y)) continue;
255
+ newPairs.add(encodePair(entityA, entityB));
256
+ }
257
+ }
258
+ for (const key of newPairs) if (!this.activePairs.has(key)) {
259
+ const lo = decodeLo(key);
260
+ const hi = decodeHi(key);
261
+ const a = this.entries.get(lo);
262
+ const b = this.entries.get(hi);
263
+ const pair = {
264
+ entityA: lo,
265
+ entityB: hi
266
+ };
267
+ if (a?.isTrigger || b?.isTrigger) this.onTriggerEnter.emit(pair);
268
+ else this.onCollisionStart.emit(pair);
269
+ }
270
+ for (const key of this.activePairs) if (!newPairs.has(key)) {
271
+ const lo = decodeLo(key);
272
+ const hi = decodeHi(key);
273
+ const a = this.entries.get(lo);
274
+ const b = this.entries.get(hi);
275
+ const pair = {
276
+ entityA: lo,
277
+ entityB: hi
278
+ };
279
+ if (a?.isTrigger || b?.isTrigger) this.onTriggerExit.emit(pair);
280
+ else this.onCollisionEnd.emit(pair);
281
+ }
282
+ this.activePairs = newPairs;
283
+ }
284
+ /** Remove all entities. */
285
+ clear() {
286
+ this.entries.clear();
287
+ this.spatial.clear();
288
+ this.pairBufA.clear();
289
+ this.pairBufB.clear();
290
+ this.activePairs = this.pairBufA;
291
+ }
263
292
  };
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
293
+ //#endregion
294
+ //#region src/raycast.ts
295
+ /**
296
+ * Cast a ray against a list of targets. Returns the closest hit, or null.
297
+ * @param ox Ray origin X
298
+ * @param oy Ray origin Y
299
+ * @param dx Ray direction X (should be normalized)
300
+ * @param dy Ray direction Y (should be normalized)
301
+ * @param maxDist Maximum raycast distance
302
+ * @param targets Array of entities to test against
303
+ */
276
304
  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;
305
+ let closest = null;
306
+ for (let i = 0; i < targets.length; i++) {
307
+ const t = targets[i];
308
+ const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);
309
+ if (hit && (closest === null || hit.distance < closest.distance)) closest = hit;
310
+ }
311
+ return closest;
286
312
  }
287
313
  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
- );
314
+ if (target.shape.type === "aabb") return rayVsAABB(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.hw, target.shape.hh);
315
+ return rayVsCircle(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.radius);
313
316
  }
314
317
  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
- };
318
+ const minX = cx - hw;
319
+ const maxX = cx + hw;
320
+ const minY = cy - hh;
321
+ const maxY = cy + hh;
322
+ const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
323
+ const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
324
+ const tx1 = (minX - ox) * invDx;
325
+ const tx2 = (maxX - ox) * invDx;
326
+ const ty1 = (minY - oy) * invDy;
327
+ const ty2 = (maxY - oy) * invDy;
328
+ const tMinX = Math.min(tx1, tx2);
329
+ const tMaxX = Math.max(tx1, tx2);
330
+ const tMinY = Math.min(ty1, ty2);
331
+ const tMaxY = Math.max(ty1, ty2);
332
+ const tMin = Math.max(tMinX, tMinY);
333
+ const tMax = Math.min(tMaxX, tMaxY);
334
+ if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;
335
+ const t = tMin >= 0 ? tMin : tMax;
336
+ if (t > maxDist || t < 0) return null;
337
+ const px = ox + dx * t;
338
+ const py = oy + dy * t;
339
+ let nx = 0;
340
+ let ny = 0;
341
+ if (tMinX > tMinY) nx = dx < 0 ? 1 : -1;
342
+ else ny = dy < 0 ? 1 : -1;
343
+ return {
344
+ entity,
345
+ distance: t,
346
+ point: {
347
+ x: px,
348
+ y: py
349
+ },
350
+ normal: {
351
+ x: nx,
352
+ y: ny
353
+ }
354
+ };
349
355
  }
350
356
  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
- };
357
+ const fx = ox - cx;
358
+ const fy = oy - cy;
359
+ const a = dx * dx + dy * dy;
360
+ const b = 2 * (fx * dx + fy * dy);
361
+ const c = fx * fx + fy * fy - radius * radius;
362
+ if (a < 1e-12) return null;
363
+ const discriminant = b * b - 4 * a * c;
364
+ if (discriminant < 0) return null;
365
+ const sqrtDisc = Math.sqrt(discriminant);
366
+ let t = (-b - sqrtDisc) / (2 * a);
367
+ if (t < 0) t = (-b + sqrtDisc) / (2 * a);
368
+ if (t < 0 || t > maxDist) return null;
369
+ const px = ox + dx * t;
370
+ const py = oy + dy * t;
371
+ const nx = px - cx;
372
+ const ny = py - cy;
373
+ const len = Math.sqrt(nx * nx + ny * ny);
374
+ return {
375
+ entity,
376
+ distance: t,
377
+ point: {
378
+ x: px,
379
+ y: py
380
+ },
381
+ normal: len > 0 ? {
382
+ x: nx / len,
383
+ y: ny / len
384
+ } : {
385
+ x: 0,
386
+ y: 0
387
+ }
388
+ };
376
389
  }
377
-
378
- // src/solid-grid.ts
390
+ //#endregion
391
+ //#region src/solid-grid.ts
392
+ /**
393
+ * Build a `SolidGrid` from a list of solid tile positions.
394
+ * Input is structurally typed (`readonly SolidTile[]`) so the caller does not
395
+ * need to import `@flare-engine/tilemap` — physics stays tilemap-free.
396
+ */
379
397
  function buildSolidGrid(width, height, tileSize, tiles) {
380
- const solid = /* @__PURE__ */ new Set();
381
- for (const t of tiles) {
382
- solid.add(t.row * width + t.col);
383
- }
384
- return { width, height, tileSize, solid };
398
+ const solid = /* @__PURE__ */ new Set();
399
+ for (const t of tiles) solid.add(t.row * width + t.col);
400
+ return {
401
+ width,
402
+ height,
403
+ tileSize,
404
+ solid
405
+ };
385
406
  }
386
- var EDGE = 1e-4;
407
+ /**
408
+ * Half-open boundary constant: the AABB is treated as [min, max) so a box
409
+ * whose max edge lands exactly on a tile boundary does NOT overlap that tile.
410
+ */
411
+ const EDGE = 1e-4;
412
+ /**
413
+ * Returns `true` when an axis-aligned box centered at (`x`, `y`) with
414
+ * half-extents (`halfW`, `halfH`) overlaps a solid tile or lies outside the
415
+ * map boundary. Uses half-open EDGE semantics on max edges.
416
+ */
387
417
  function isBlockedAABB(grid, x, y, halfW, halfH) {
388
- const ts = grid.tileSize;
389
- const minCol = Math.floor((x - halfW) / ts);
390
- const maxCol = Math.floor((x + halfW - EDGE) / ts);
391
- const minRow = Math.floor((y - halfH) / ts);
392
- const maxRow = Math.floor((y + halfH - EDGE) / ts);
393
- if (minCol < 0 || minRow < 0 || maxCol >= grid.width || maxRow >= grid.height) {
394
- return true;
395
- }
396
- for (let row = minRow; row <= maxRow; row++) {
397
- for (let col = minCol; col <= maxCol; col++) {
398
- if (grid.solid.has(row * grid.width + col)) {
399
- return true;
400
- }
401
- }
402
- }
403
- return false;
418
+ const ts = grid.tileSize;
419
+ const minCol = Math.floor((x - halfW) / ts);
420
+ const maxCol = Math.floor((x + halfW - EDGE) / ts);
421
+ const minRow = Math.floor((y - halfH) / ts);
422
+ const maxRow = Math.floor((y + halfH - EDGE) / ts);
423
+ if (minCol < 0 || minRow < 0 || maxCol >= grid.width || maxRow >= grid.height) return true;
424
+ for (let row = minRow; row <= maxRow; row++) for (let col = minCol; col <= maxCol; col++) if (grid.solid.has(row * grid.width + col)) return true;
425
+ return false;
404
426
  }
427
+ /**
428
+ * Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis
429
+ * is applied independently so a blocked diagonal input slides along the wall.
430
+ * The y-check uses the already-updated x position.
431
+ * Zero-alloc: writes result into the provided `out` Vec2 and returns it.
432
+ */
405
433
  function moveAndSlideInto(grid, x, y, dx, dy, halfW, halfH, out) {
406
- let nx = x;
407
- let ny = y;
408
- if (dx !== 0 && !isBlockedAABB(grid, nx + dx, ny, halfW, halfH)) nx += dx;
409
- if (dy !== 0 && !isBlockedAABB(grid, nx, ny + dy, halfW, halfH)) ny += dy;
410
- out.x = nx;
411
- out.y = ny;
412
- return out;
434
+ let nx = x;
435
+ let ny = y;
436
+ if (dx !== 0 && !isBlockedAABB(grid, nx + dx, ny, halfW, halfH)) nx += dx;
437
+ if (dy !== 0 && !isBlockedAABB(grid, nx, ny + dy, halfW, halfH)) ny += dy;
438
+ out.x = nx;
439
+ out.y = ny;
440
+ return out;
413
441
  }
414
- export {
415
- AABB,
416
- CircleShape,
417
- CollisionWorld,
418
- SpatialIndex,
419
- buildSolidGrid,
420
- createCollider,
421
- isBlockedAABB,
422
- moveAndSlideInto,
423
- raycast,
424
- shapeBounds,
425
- shapeBoundsInto,
426
- shapeOverlap
427
- };
442
+ //#endregion
443
+ export { AABB, CircleShape, CollisionWorld, SpatialIndex, buildSolidGrid, createCollider, isBlockedAABB, moveAndSlideInto, raycast, shapeBounds, shapeBoundsInto, shapeOverlap };
444
+
428
445
  //# sourceMappingURL=index.mjs.map