@flare-engine/physics 0.3.1 → 0.4.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.js CHANGED
@@ -11,6 +11,171 @@ function createCollider(shape, opts) {
11
11
  };
12
12
  }
13
13
  //#endregion
14
+ //#region src/raycast.ts
15
+ /**
16
+ * Cast a ray against a list of targets. Returns the closest hit, or null.
17
+ * @param ox Ray origin X
18
+ * @param oy Ray origin Y
19
+ * @param dx Ray direction X (should be normalized)
20
+ * @param dy Ray direction Y (should be normalized)
21
+ * @param maxDist Maximum raycast distance
22
+ * @param targets Array of entities to test against
23
+ */
24
+ function raycast(ox, oy, dx, dy, maxDist, targets) {
25
+ let closest = null;
26
+ for (let i = 0; i < targets.length; i++) {
27
+ const t = targets[i];
28
+ const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);
29
+ if (hit && (closest === null || hit.distance < closest.distance)) closest = hit;
30
+ }
31
+ return closest;
32
+ }
33
+ function rayVsShape(ox, oy, dx, dy, maxDist, target) {
34
+ if (target.shape.type === "aabb") return rayVsAABB(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.hw, target.shape.hh);
35
+ return rayVsCircle(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.radius);
36
+ }
37
+ function rayVsAABB(ox, oy, dx, dy, maxDist, entity, cx, cy, hw, hh) {
38
+ const minX = cx - hw;
39
+ const maxX = cx + hw;
40
+ const minY = cy - hh;
41
+ const maxY = cy + hh;
42
+ const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
43
+ const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
44
+ const tx1 = (minX - ox) * invDx;
45
+ const tx2 = (maxX - ox) * invDx;
46
+ const ty1 = (minY - oy) * invDy;
47
+ const ty2 = (maxY - oy) * invDy;
48
+ const tMinX = Math.min(tx1, tx2);
49
+ const tMaxX = Math.max(tx1, tx2);
50
+ const tMinY = Math.min(ty1, ty2);
51
+ const tMaxY = Math.max(ty1, ty2);
52
+ const tMin = Math.max(tMinX, tMinY);
53
+ const tMax = Math.min(tMaxX, tMaxY);
54
+ if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;
55
+ const t = tMin >= 0 ? tMin : tMax;
56
+ if (t > maxDist || t < 0) return null;
57
+ const px = ox + dx * t;
58
+ const py = oy + dy * t;
59
+ let nx = 0;
60
+ let ny = 0;
61
+ if (tMinX > tMinY) nx = dx < 0 ? 1 : -1;
62
+ else ny = dy < 0 ? 1 : -1;
63
+ return {
64
+ entity,
65
+ distance: t,
66
+ point: {
67
+ x: px,
68
+ y: py
69
+ },
70
+ normal: {
71
+ x: nx,
72
+ y: ny
73
+ }
74
+ };
75
+ }
76
+ function rayVsCircle(ox, oy, dx, dy, maxDist, entity, cx, cy, radius) {
77
+ const fx = ox - cx;
78
+ const fy = oy - cy;
79
+ const a = dx * dx + dy * dy;
80
+ const b = 2 * (fx * dx + fy * dy);
81
+ const c = fx * fx + fy * fy - radius * radius;
82
+ if (a < 1e-12) return null;
83
+ const discriminant = b * b - 4 * a * c;
84
+ if (discriminant < 0) return null;
85
+ const sqrtDisc = Math.sqrt(discriminant);
86
+ let t = (-b - sqrtDisc) / (2 * a);
87
+ if (t < 0) t = (-b + sqrtDisc) / (2 * a);
88
+ if (t < 0 || t > maxDist) return null;
89
+ const px = ox + dx * t;
90
+ const py = oy + dy * t;
91
+ const nx = px - cx;
92
+ const ny = py - cy;
93
+ const len = Math.sqrt(nx * nx + ny * ny);
94
+ return {
95
+ entity,
96
+ distance: t,
97
+ point: {
98
+ x: px,
99
+ y: py
100
+ },
101
+ normal: len > 0 ? {
102
+ x: nx / len,
103
+ y: ny / len
104
+ } : {
105
+ x: 0,
106
+ y: 0
107
+ }
108
+ };
109
+ }
110
+ /**
111
+ * Zero-alloc ray-vs-shape test: writes into `out` and returns it, or returns
112
+ * `null` without touching `out`.
113
+ *
114
+ * The allocating `raycast()` above builds a fresh `RaycastHit` — with two nested
115
+ * `Vec2` objects — for **every candidate it tests**, then discards all but the
116
+ * closest. That is three objects per candidate per ray. This variant exists so
117
+ * `CollisionWorld.raycast` can test a whole broadphase cell without allocating.
118
+ */
119
+ function rayVsShapeInto(ox, oy, dx, dy, maxDist, entity, shape, x, y, out) {
120
+ if (shape.type === "aabb") return rayVsAABBInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.hw, shape.hh, out);
121
+ return rayVsCircleInto(ox, oy, dx, dy, maxDist, entity, x, y, shape.radius, out);
122
+ }
123
+ function rayVsAABBInto(ox, oy, dx, dy, maxDist, entity, bx, by, hw, hh, out) {
124
+ const invDx = dx !== 0 ? 1 / dx : Number.POSITIVE_INFINITY;
125
+ const invDy = dy !== 0 ? 1 / dy : Number.POSITIVE_INFINITY;
126
+ const tx1 = (bx - hw - ox) * invDx;
127
+ const tx2 = (bx + hw - ox) * invDx;
128
+ const ty1 = (by - hh - oy) * invDy;
129
+ const ty2 = (by + hh - oy) * invDy;
130
+ const tMinX = Math.min(tx1, tx2);
131
+ const tMaxX = Math.max(tx1, tx2);
132
+ const tMinY = Math.min(ty1, ty2);
133
+ const tMaxY = Math.max(ty1, ty2);
134
+ const tMin = Math.max(tMinX, tMinY);
135
+ const tMax = Math.min(tMaxX, tMaxY);
136
+ if (tMax < 0 || tMin > tMax) return null;
137
+ const t = tMin < 0 ? tMax : tMin;
138
+ if (t > maxDist || t < 0) return null;
139
+ out.entity = entity;
140
+ out.distance = t;
141
+ out.point.x = ox + dx * t;
142
+ out.point.y = oy + dy * t;
143
+ if (tMinX > tMinY) {
144
+ out.normal.x = dx < 0 ? 1 : -1;
145
+ out.normal.y = 0;
146
+ } else {
147
+ out.normal.x = 0;
148
+ out.normal.y = dy < 0 ? 1 : -1;
149
+ }
150
+ return out;
151
+ }
152
+ function rayVsCircleInto(ox, oy, dx, dy, maxDist, entity, cx, cy, radius, out) {
153
+ const fx = ox - cx;
154
+ const fy = oy - cy;
155
+ const a = dx * dx + dy * dy;
156
+ if (a < 1e-12) return null;
157
+ const b = 2 * (fx * dx + fy * dy);
158
+ const c = fx * fx + fy * fy - radius * radius;
159
+ const discriminant = b * b - 4 * a * c;
160
+ if (discriminant < 0) return null;
161
+ const sqrtDisc = Math.sqrt(discriminant);
162
+ let t = (-b - sqrtDisc) / (2 * a);
163
+ if (t < 0) t = (-b + sqrtDisc) / (2 * a);
164
+ if (t < 0 || t > maxDist) return null;
165
+ const px = ox + dx * t;
166
+ const py = oy + dy * t;
167
+ const nx = px - cx;
168
+ const ny = py - cy;
169
+ const len = Math.sqrt(nx * nx + ny * ny);
170
+ out.entity = entity;
171
+ out.distance = t;
172
+ out.point.x = px;
173
+ out.point.y = py;
174
+ out.normal.x = len > 0 ? nx / len : 0;
175
+ out.normal.y = len > 0 ? ny / len : 0;
176
+ return out;
177
+ }
178
+ //#endregion
14
179
  //#region src/shape.ts
15
180
  /** Create an AABB shape from width and height. */
16
181
  function AABB(width, height) {
@@ -82,7 +247,111 @@ function aabbVsCircle(ax, ay, ahw, ahh, cx, cy, cr) {
82
247
  const closestY = cy < top ? top : cy > bottom ? bottom : cy;
83
248
  const dx = cx - closestX;
84
249
  const dy = cy - closestY;
85
- return dx * dx + dy * dy <= cr * cr;
250
+ const distSq = dx * dx + dy * dy;
251
+ if (distSq > 0) return distSq < cr * cr;
252
+ return cr > 0 || cx > left && cx < right && cy > top && cy < bottom;
253
+ }
254
+ /** A fresh, zeroed overlap record. Allocate once outside the loop and reuse. */
255
+ function createOverlap() {
256
+ return {
257
+ x: 0,
258
+ y: 0,
259
+ depth: 0
260
+ };
261
+ }
262
+ function overlapInto(shapeA, ax, ay, shapeB, bx, by, out) {
263
+ if (shapeA.type === "aabb" && shapeB.type === "aabb") return aabbAabbOverlap(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh, out);
264
+ if (shapeA.type === "circle" && shapeB.type === "circle") return circleCircleOverlap(ax, ay, shapeA.radius, bx, by, shapeB.radius, out);
265
+ if (shapeA.type === "aabb" && shapeB.type === "circle") return aabbCircleOverlap(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.radius, out, false);
266
+ return aabbCircleOverlap(bx, by, shapeB.hw, shapeB.hh, ax, ay, shapeA.radius, out, true);
267
+ }
268
+ /** Minimum translation vector. Allocates — see `overlapInto` for the hot path. */
269
+ function overlap(shapeA, ax, ay, shapeB, bx, by) {
270
+ return overlapInto(shapeA, ax, ay, shapeB, bx, by, createOverlap());
271
+ }
272
+ function aabbAabbOverlap(ax, ay, ahw, ahh, bx, by, bhw, bhh, out) {
273
+ const dx = bx - ax;
274
+ const dy = by - ay;
275
+ const overlapX = ahw + bhw - Math.abs(dx);
276
+ if (overlapX <= 0) return null;
277
+ const overlapY = ahh + bhh - Math.abs(dy);
278
+ if (overlapY <= 0) return null;
279
+ if (overlapX < overlapY) {
280
+ out.x = dx < 0 ? -1 : 1;
281
+ out.y = 0;
282
+ out.depth = overlapX;
283
+ } else {
284
+ out.x = 0;
285
+ out.y = dy < 0 ? -1 : 1;
286
+ out.depth = overlapY;
287
+ }
288
+ return out;
289
+ }
290
+ function circleCircleOverlap(ax, ay, ar, bx, by, br, out) {
291
+ const dx = bx - ax;
292
+ const dy = by - ay;
293
+ const radSum = ar + br;
294
+ const distSq = dx * dx + dy * dy;
295
+ if (distSq >= radSum * radSum) return null;
296
+ const dist = Math.sqrt(distSq);
297
+ if (dist === 0) {
298
+ out.x = 1;
299
+ out.y = 0;
300
+ out.depth = radSum;
301
+ return out;
302
+ }
303
+ out.x = dx / dist;
304
+ out.y = dy / dist;
305
+ out.depth = radSum - dist;
306
+ return out;
307
+ }
308
+ function aabbCircleOverlap(ax, ay, ahw, ahh, cx, cy, cr, out, flip) {
309
+ const left = ax - ahw;
310
+ const right = ax + ahw;
311
+ const top = ay - ahh;
312
+ const bottom = ay + ahh;
313
+ const closestX = cx < left ? left : cx > right ? right : cx;
314
+ const closestY = cy < top ? top : cy > bottom ? bottom : cy;
315
+ const dx = cx - closestX;
316
+ const dy = cy - closestY;
317
+ const distSq = dx * dx + dy * dy;
318
+ if (distSq > 0) {
319
+ if (distSq >= cr * cr) return null;
320
+ const dist = Math.sqrt(distSq);
321
+ const nx = dx / dist;
322
+ const ny = dy / dist;
323
+ out.x = flip ? -nx : nx;
324
+ out.y = flip ? -ny : ny;
325
+ out.depth = cr - dist;
326
+ return out;
327
+ }
328
+ const toLeft = cx - left;
329
+ const toRight = right - cx;
330
+ const toTop = cy - top;
331
+ const toBottom = bottom - cy;
332
+ let nx = -1;
333
+ let ny = 0;
334
+ let depth = toLeft;
335
+ if (toRight < depth) {
336
+ nx = 1;
337
+ ny = 0;
338
+ depth = toRight;
339
+ }
340
+ if (toTop < depth) {
341
+ nx = 0;
342
+ ny = -1;
343
+ depth = toTop;
344
+ }
345
+ if (toBottom < depth) {
346
+ nx = 0;
347
+ ny = 1;
348
+ depth = toBottom;
349
+ }
350
+ if (depth + cr <= 0) return null;
351
+ out.x = flip ? -nx : nx;
352
+ out.y = flip ? -ny : ny;
353
+ out.depth = depth + cr;
354
+ return out;
86
355
  }
87
356
  //#endregion
88
357
  //#region src/spatial-index.ts
@@ -98,6 +367,13 @@ function _cMin(v) {
98
367
  function _cMax(v) {
99
368
  return Math.ceil(v / CELL) - 1;
100
369
  }
370
+ const MIN_CELL = -32768;
371
+ const MAX_CELL = KEY_OFFSET - 1;
372
+ function _clampCell(c) {
373
+ if (c > MAX_CELL) return MAX_CELL;
374
+ if (c < MIN_CELL) return MIN_CELL;
375
+ return c;
376
+ }
101
377
  /**
102
378
  * Uniform spatial-hash broadphase.
103
379
  * `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.
@@ -165,10 +441,10 @@ var SpatialIndex = class {
165
441
  const qMinY = bounds.y;
166
442
  const qMaxX = bounds.x + bounds.width;
167
443
  const qMaxY = bounds.y + bounds.height;
168
- const cxMin = _cMin(qMinX);
169
- const cxMax = _cMax(qMaxX);
170
- const cyMin = _cMin(qMinY);
171
- const cyMax = _cMax(qMaxY);
444
+ const cxMin = _clampCell(_cMin(qMinX));
445
+ const cxMax = _clampCell(_cMax(qMaxX));
446
+ const cyMin = _clampCell(_cMin(qMinY));
447
+ const cyMax = _clampCell(_cMax(qMaxY));
172
448
  for (let cx = cxMin; cx <= cxMax; cx++) for (let cy = cyMin; cy <= cyMax; cy++) {
173
449
  const bucket = this._cells.get(_key(cx, cy));
174
450
  if (bucket === void 0) continue;
@@ -188,10 +464,10 @@ var SpatialIndex = class {
188
464
  this._cells.clear();
189
465
  }
190
466
  _link(entry) {
191
- const cxMin = _cMin(entry.minX);
192
- const cxMax = _cMax(entry.maxX);
193
- const cyMin = _cMin(entry.minY);
194
- const cyMax = _cMax(entry.maxY);
467
+ const cxMin = _clampCell(_cMin(entry.minX));
468
+ const cxMax = _clampCell(_cMax(entry.maxX));
469
+ const cyMin = _clampCell(_cMin(entry.minY));
470
+ const cyMax = _clampCell(_cMax(entry.maxY));
195
471
  for (let cx = cxMin; cx <= cxMax; cx++) for (let cy = cyMin; cy <= cyMax; cy++) {
196
472
  const k = _key(cx, cy);
197
473
  let b = this._cells.get(k);
@@ -203,10 +479,10 @@ var SpatialIndex = class {
203
479
  }
204
480
  }
205
481
  _unlink(entry) {
206
- const cxMin = _cMin(entry.minX);
207
- const cxMax = _cMax(entry.maxX);
208
- const cyMin = _cMin(entry.minY);
209
- const cyMax = _cMax(entry.maxY);
482
+ const cxMin = _clampCell(_cMin(entry.minX));
483
+ const cxMax = _clampCell(_cMax(entry.maxX));
484
+ const cyMin = _clampCell(_cMin(entry.minY));
485
+ const cyMax = _clampCell(_cMax(entry.maxY));
210
486
  for (let cx = cxMin; cx <= cxMax; cx++) for (let cy = cyMin; cy <= cyMax; cy++) {
211
487
  const b = this._cells.get(_key(cx, cy));
212
488
  if (b === void 0) continue;
@@ -221,6 +497,8 @@ var SpatialIndex = class {
221
497
  //#endregion
222
498
  //#region src/collision-world.ts
223
499
  const ENTITY_SHIFT = 2 ** 26;
500
+ /** Padding applied to a ray's query AABB — see `CollisionWorld.raycast`. */
501
+ const RAY_BOUNDS_PAD = 1e-6;
224
502
  function encodePair(a, b) {
225
503
  const lo = a < b ? a : b;
226
504
  const hi = a < b ? b : a;
@@ -259,7 +537,7 @@ var CollisionWorld = class {
259
537
  width: 0,
260
538
  height: 0
261
539
  };
262
- /** Add an entity with a shape. */
540
+ /** Add an entity with a shape. Positional twin of `addCollider`. */
263
541
  add(entity, shape, x, y, isTrigger = false, layer = 1, mask = 4294967295) {
264
542
  const entry = {
265
543
  entity,
@@ -276,6 +554,14 @@ var CollisionWorld = class {
276
554
  if (exists) this.spatial.update(entity, this.boundsBuf);
277
555
  else this.spatial.insert(entity, this.boundsBuf);
278
556
  }
557
+ /**
558
+ * Add an entity from a `Collider`. Its `shape`, `isTrigger`, `layer` and `mask`
559
+ * are copied at add time — later edits to the collider do not reach the world;
560
+ * re-add to change them. Re-adding an existing entity replaces its data.
561
+ */
562
+ addCollider(entity, collider, x, y) {
563
+ this.add(entity, collider.shape, x, y, collider.isTrigger, collider.layer, collider.mask);
564
+ }
279
565
  /** Remove an entity from the world. Emits exit signals for any active pairs. */
280
566
  remove(entity) {
281
567
  const entry = this.entries.get(entity);
@@ -385,104 +671,96 @@ var CollisionWorld = class {
385
671
  this.pairBufB.clear();
386
672
  this.activePairs = this.pairBufA;
387
673
  }
388
- };
389
- //#endregion
390
- //#region src/raycast.ts
391
- /**
392
- * Cast a ray against a list of targets. Returns the closest hit, or null.
393
- * @param ox Ray origin X
394
- * @param oy Ray origin Y
395
- * @param dx Ray direction X (should be normalized)
396
- * @param dy Ray direction Y (should be normalized)
397
- * @param maxDist Maximum raycast distance
398
- * @param targets Array of entities to test against
399
- */
400
- function raycast(ox, oy, dx, dy, maxDist, targets) {
401
- let closest = null;
402
- for (let i = 0; i < targets.length; i++) {
403
- const t = targets[i];
404
- const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);
405
- if (hit && (closest === null || hit.distance < closest.distance)) closest = hit;
406
- }
407
- return closest;
408
- }
409
- function rayVsShape(ox, oy, dx, dy, maxDist, target) {
410
- if (target.shape.type === "aabb") return rayVsAABB(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.hw, target.shape.hh);
411
- return rayVsCircle(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.radius);
412
- }
413
- function rayVsAABB(ox, oy, dx, dy, maxDist, entity, cx, cy, hw, hh) {
414
- const minX = cx - hw;
415
- const maxX = cx + hw;
416
- const minY = cy - hh;
417
- const maxY = cy + hh;
418
- const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
419
- const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
420
- const tx1 = (minX - ox) * invDx;
421
- const tx2 = (maxX - ox) * invDx;
422
- const ty1 = (minY - oy) * invDy;
423
- const ty2 = (maxY - oy) * invDy;
424
- const tMinX = Math.min(tx1, tx2);
425
- const tMaxX = Math.max(tx1, tx2);
426
- const tMinY = Math.min(ty1, ty2);
427
- const tMaxY = Math.max(ty1, ty2);
428
- const tMin = Math.max(tMinX, tMinY);
429
- const tMax = Math.min(tMaxX, tMaxY);
430
- if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;
431
- const t = tMin >= 0 ? tMin : tMax;
432
- if (t > maxDist || t < 0) return null;
433
- const px = ox + dx * t;
434
- const py = oy + dy * t;
435
- let nx = 0;
436
- let ny = 0;
437
- if (tMinX > tMinY) nx = dx < 0 ? 1 : -1;
438
- else ny = dy < 0 ? 1 : -1;
439
- return {
440
- entity,
441
- distance: t,
674
+ /** Reused across raycasts — see `raycast`. */
675
+ raycastBounds = {
676
+ x: 0,
677
+ y: 0,
678
+ width: 0,
679
+ height: 0
680
+ };
681
+ raycastCandidates = [];
682
+ raycastHit = {
683
+ entity: -1,
684
+ distance: 0,
442
685
  point: {
443
- x: px,
444
- y: py
686
+ x: 0,
687
+ y: 0
445
688
  },
446
689
  normal: {
447
- x: nx,
448
- y: ny
690
+ x: 0,
691
+ y: 0
449
692
  }
450
693
  };
451
- }
452
- function rayVsCircle(ox, oy, dx, dy, maxDist, entity, cx, cy, radius) {
453
- const fx = ox - cx;
454
- const fy = oy - cy;
455
- const a = dx * dx + dy * dy;
456
- const b = 2 * (fx * dx + fy * dy);
457
- const c = fx * fx + fy * fy - radius * radius;
458
- if (a < 1e-12) return null;
459
- const discriminant = b * b - 4 * a * c;
460
- if (discriminant < 0) return null;
461
- const sqrtDisc = Math.sqrt(discriminant);
462
- let t = (-b - sqrtDisc) / (2 * a);
463
- if (t < 0) t = (-b + sqrtDisc) / (2 * a);
464
- if (t < 0 || t > maxDist) return null;
465
- const px = ox + dx * t;
466
- const py = oy + dy * t;
467
- const nx = px - cx;
468
- const ny = py - cy;
469
- const len = Math.sqrt(nx * nx + ny * ny);
470
- return {
471
- entity,
472
- distance: t,
694
+ /**
695
+ * Cast a ray against the colliders in this world.
696
+ *
697
+ * The standalone `raycast()` export tests a caller-supplied target array, so
698
+ * it neither sees the world's colliders nor benefits from the broadphase — an
699
+ * O(n) scan sitting beside an O(1) index. This walks only the cells the ray's
700
+ * bounding box touches.
701
+ *
702
+ * `mask` is a layer bitmask filtered against each collider's `layer`, using the
703
+ * bits colliders already carry; it defaults to every layer. Triggers are
704
+ * included — a ray asking "what is over there" wants them.
705
+ *
706
+ * The returned record is **reused between calls**: read it, or copy what you
707
+ * need, before casting again. That is what keeps a per-frame ray allocation-
708
+ * free, and it is why the standalone helper's fresh-object-per-candidate cost
709
+ * was worth removing.
710
+ *
711
+ * `maxDist` must be finite — this requires a bounded ray, not the
712
+ * `Number.POSITIVE_INFINITY` "unbounded ray" idiom some engines accept. The
713
+ * broadphase below is a uniform grid: an infinite `maxDist` derives an
714
+ * infinite cell range and never terminates the walk. Pass a large finite
715
+ * bound instead (e.g. the map's diagonal) to cast "until it leaves the
716
+ * populated area". A non-finite `maxDist` returns `null`.
717
+ */
718
+ raycast(ox, oy, dx, dy, maxDist, mask = 4294967295) {
719
+ if (!Number.isFinite(maxDist)) return null;
720
+ const ex = ox + dx * maxDist;
721
+ const ey = oy + dy * maxDist;
722
+ const minX = ox < ex ? ox : ex;
723
+ const minY = oy < ey ? oy : ey;
724
+ this.raycastBounds.x = minX - RAY_BOUNDS_PAD;
725
+ this.raycastBounds.y = minY - RAY_BOUNDS_PAD;
726
+ this.raycastBounds.width = Math.abs(ex - ox) + RAY_BOUNDS_PAD * 2;
727
+ this.raycastBounds.height = Math.abs(ey - oy) + RAY_BOUNDS_PAD * 2;
728
+ this.raycastCandidates.length = 0;
729
+ this.spatial.query(this.raycastBounds, this.raycastCandidates);
730
+ let bestDist = Number.POSITIVE_INFINITY;
731
+ let found = false;
732
+ for (let i = 0; i < this.raycastCandidates.length; i++) {
733
+ const entry = this.entries.get(this.raycastCandidates[i]);
734
+ if (entry === void 0) continue;
735
+ if ((entry.layer & mask) === 0) continue;
736
+ const hit = rayVsShapeInto(ox, oy, dx, dy, maxDist, entry.entity, entry.shape, entry.x, entry.y, this.scratchHit);
737
+ if (hit !== null && hit.distance < bestDist) {
738
+ bestDist = hit.distance;
739
+ found = true;
740
+ this.raycastHit.entity = hit.entity;
741
+ this.raycastHit.distance = hit.distance;
742
+ this.raycastHit.point.x = hit.point.x;
743
+ this.raycastHit.point.y = hit.point.y;
744
+ this.raycastHit.normal.x = hit.normal.x;
745
+ this.raycastHit.normal.y = hit.normal.y;
746
+ }
747
+ }
748
+ return found ? this.raycastHit : null;
749
+ }
750
+ /** Scratch record handed to `rayVsShapeInto` per candidate. */
751
+ scratchHit = {
752
+ entity: -1,
753
+ distance: 0,
473
754
  point: {
474
- x: px,
475
- y: py
755
+ x: 0,
756
+ y: 0
476
757
  },
477
- normal: len > 0 ? {
478
- x: nx / len,
479
- y: ny / len
480
- } : {
758
+ normal: {
481
759
  x: 0,
482
760
  y: 0
483
761
  }
484
762
  };
485
- }
763
+ };
486
764
  //#endregion
487
765
  //#region src/solid-grid.ts
488
766
  /**
@@ -491,8 +769,8 @@ function rayVsCircle(ox, oy, dx, dy, maxDist, entity, cx, cy, radius) {
491
769
  * need to import `@flare-engine/tilemap` — physics stays tilemap-free.
492
770
  */
493
771
  function buildSolidGrid(width, height, tileSize, tiles) {
494
- const solid = /* @__PURE__ */ new Set();
495
- for (const t of tiles) solid.add(t.row * width + t.col);
772
+ const solid = new Uint8Array(width * height);
773
+ for (const t of tiles) solid[t.row * width + t.col] = 1;
496
774
  return {
497
775
  width,
498
776
  height,
@@ -517,7 +795,7 @@ function isBlockedAABB(grid, x, y, halfW, halfH) {
517
795
  const minRow = Math.floor((y - halfH) / ts);
518
796
  const maxRow = Math.floor((y + halfH - EDGE) / ts);
519
797
  if (minCol < 0 || minRow < 0 || maxCol >= grid.width || maxRow >= grid.height) return true;
520
- for (let row = minRow; row <= maxRow; row++) for (let col = minCol; col <= maxCol; col++) if (grid.solid.has(row * grid.width + col)) return true;
798
+ for (let row = minRow; row <= maxRow; row++) for (let col = minCol; col <= maxCol; col++) if (grid.solid[row * grid.width + col] !== 0) return true;
521
799
  return false;
522
800
  }
523
801
  /**
@@ -542,8 +820,12 @@ exports.CollisionWorld = CollisionWorld;
542
820
  exports.SpatialIndex = SpatialIndex;
543
821
  exports.buildSolidGrid = buildSolidGrid;
544
822
  exports.createCollider = createCollider;
823
+ exports.createOverlap = createOverlap;
545
824
  exports.isBlockedAABB = isBlockedAABB;
546
825
  exports.moveAndSlideInto = moveAndSlideInto;
826
+ exports.overlap = overlap;
827
+ exports.overlapInto = overlapInto;
828
+ exports.rayVsShapeInto = rayVsShapeInto;
547
829
  exports.raycast = raycast;
548
830
  exports.shapeBounds = shapeBounds;
549
831
  exports.shapeBoundsInto = shapeBoundsInto;