@flare-engine/physics 0.3.0 → 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/README.md +48 -2
- package/dist/index.d.mts +132 -50
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +132 -50
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +495 -118
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +492 -119
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
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,69 +247,258 @@ 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
|
-
|
|
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
|
|
358
|
+
const CELL = 64;
|
|
359
|
+
const KEY_RANGE = 65536;
|
|
360
|
+
const KEY_OFFSET = 32768;
|
|
361
|
+
function _key(cx, cy) {
|
|
362
|
+
return (cx + KEY_OFFSET) * KEY_RANGE + (cy + KEY_OFFSET);
|
|
363
|
+
}
|
|
364
|
+
function _cMin(v) {
|
|
365
|
+
return Math.floor(v / CELL);
|
|
366
|
+
}
|
|
367
|
+
function _cMax(v) {
|
|
368
|
+
return Math.ceil(v / CELL) - 1;
|
|
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
|
+
}
|
|
89
377
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
378
|
+
* Uniform spatial-hash broadphase.
|
|
379
|
+
* `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.
|
|
380
|
+
* `query` walks only the cells overlapping the given rect; result order is unspecified.
|
|
381
|
+
* Zero-alloc in the query hot-path: duplicate suppression uses a monotone stamp counter,
|
|
382
|
+
* not a per-call Set.
|
|
92
383
|
*/
|
|
93
384
|
var SpatialIndex = class {
|
|
94
|
-
|
|
385
|
+
_data = [];
|
|
386
|
+
_slots = /* @__PURE__ */ new Map();
|
|
387
|
+
_cells = /* @__PURE__ */ new Map();
|
|
388
|
+
_gen = 0;
|
|
95
389
|
/** Number of items in the index. */
|
|
96
390
|
get size() {
|
|
97
|
-
return this.
|
|
391
|
+
return this._data.length;
|
|
98
392
|
}
|
|
99
393
|
/** Insert an item with a bounding rect. */
|
|
100
394
|
insert(item, bounds) {
|
|
101
|
-
|
|
395
|
+
const entry = {
|
|
102
396
|
item,
|
|
103
397
|
minX: bounds.x,
|
|
104
398
|
minY: bounds.y,
|
|
105
399
|
maxX: bounds.x + bounds.width,
|
|
106
|
-
maxY: bounds.y + bounds.height
|
|
107
|
-
|
|
400
|
+
maxY: bounds.y + bounds.height,
|
|
401
|
+
_stamp: 0
|
|
402
|
+
};
|
|
403
|
+
this._slots.set(item, this._data.length);
|
|
404
|
+
this._data.push(entry);
|
|
405
|
+
this._link(entry);
|
|
108
406
|
}
|
|
109
|
-
/** Remove an item. */
|
|
407
|
+
/** Remove an item. Returns false when the item was not present. */
|
|
110
408
|
remove(item) {
|
|
111
|
-
const
|
|
112
|
-
if (
|
|
113
|
-
const
|
|
114
|
-
|
|
409
|
+
const slot = this._slots.get(item);
|
|
410
|
+
if (slot === void 0) return false;
|
|
411
|
+
const entry = this._data[slot];
|
|
412
|
+
this._unlink(entry);
|
|
413
|
+
this._slots.delete(item);
|
|
414
|
+
const last = this._data.pop();
|
|
415
|
+
if (slot < this._data.length) {
|
|
416
|
+
this._data[slot] = last;
|
|
417
|
+
this._slots.set(last.item, slot);
|
|
418
|
+
}
|
|
115
419
|
return true;
|
|
116
420
|
}
|
|
117
|
-
/** Update the bounds for an existing item. */
|
|
421
|
+
/** Update the bounds for an existing item. No-op when the item is absent. */
|
|
118
422
|
update(item, bounds) {
|
|
119
|
-
const
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
423
|
+
const slot = this._slots.get(item);
|
|
424
|
+
if (slot === void 0) return;
|
|
425
|
+
const entry = this._data[slot];
|
|
426
|
+
this._unlink(entry);
|
|
427
|
+
entry.minX = bounds.x;
|
|
428
|
+
entry.minY = bounds.y;
|
|
429
|
+
entry.maxX = bounds.x + bounds.width;
|
|
430
|
+
entry.maxY = bounds.y + bounds.height;
|
|
431
|
+
this._link(entry);
|
|
126
432
|
}
|
|
127
|
-
/**
|
|
433
|
+
/**
|
|
434
|
+
* Query all items whose bounds overlap the given rect.
|
|
435
|
+
* Result order is unspecified. Appends to `result` when provided and returns it.
|
|
436
|
+
*/
|
|
128
437
|
query(bounds, result) {
|
|
129
438
|
const out = result ?? [];
|
|
439
|
+
const gen = ++this._gen;
|
|
130
440
|
const qMinX = bounds.x;
|
|
131
441
|
const qMinY = bounds.y;
|
|
132
442
|
const qMaxX = bounds.x + bounds.width;
|
|
133
443
|
const qMaxY = bounds.y + bounds.height;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
444
|
+
const cxMin = _clampCell(_cMin(qMinX));
|
|
445
|
+
const cxMax = _clampCell(_cMax(qMaxX));
|
|
446
|
+
const cyMin = _clampCell(_cMin(qMinY));
|
|
447
|
+
const cyMax = _clampCell(_cMax(qMaxY));
|
|
448
|
+
for (let cx = cxMin; cx <= cxMax; cx++) for (let cy = cyMin; cy <= cyMax; cy++) {
|
|
449
|
+
const bucket = this._cells.get(_key(cx, cy));
|
|
450
|
+
if (bucket === void 0) continue;
|
|
451
|
+
for (let i = 0; i < bucket.length; i++) {
|
|
452
|
+
const e = bucket[i];
|
|
453
|
+
if (e._stamp === gen) continue;
|
|
454
|
+
e._stamp = gen;
|
|
455
|
+
if (e.maxX > qMinX && e.minX < qMaxX && e.maxY > qMinY && e.minY < qMaxY) out.push(e.item);
|
|
456
|
+
}
|
|
137
457
|
}
|
|
138
458
|
return out;
|
|
139
459
|
}
|
|
140
460
|
/** Remove all items. */
|
|
141
461
|
clear() {
|
|
142
|
-
this.
|
|
462
|
+
this._data.length = 0;
|
|
463
|
+
this._slots.clear();
|
|
464
|
+
this._cells.clear();
|
|
465
|
+
}
|
|
466
|
+
_link(entry) {
|
|
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));
|
|
471
|
+
for (let cx = cxMin; cx <= cxMax; cx++) for (let cy = cyMin; cy <= cyMax; cy++) {
|
|
472
|
+
const k = _key(cx, cy);
|
|
473
|
+
let b = this._cells.get(k);
|
|
474
|
+
if (b === void 0) {
|
|
475
|
+
b = [];
|
|
476
|
+
this._cells.set(k, b);
|
|
477
|
+
}
|
|
478
|
+
b.push(entry);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
_unlink(entry) {
|
|
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));
|
|
486
|
+
for (let cx = cxMin; cx <= cxMax; cx++) for (let cy = cyMin; cy <= cyMax; cy++) {
|
|
487
|
+
const b = this._cells.get(_key(cx, cy));
|
|
488
|
+
if (b === void 0) continue;
|
|
489
|
+
const i = b.indexOf(entry);
|
|
490
|
+
if (i !== -1) {
|
|
491
|
+
const last = b.pop();
|
|
492
|
+
if (i < b.length) b[i] = last;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
143
495
|
}
|
|
144
496
|
};
|
|
145
497
|
//#endregion
|
|
146
498
|
//#region src/collision-world.ts
|
|
147
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;
|
|
148
502
|
function encodePair(a, b) {
|
|
149
503
|
const lo = a < b ? a : b;
|
|
150
504
|
const hi = a < b ? b : a;
|
|
@@ -183,7 +537,7 @@ var CollisionWorld = class {
|
|
|
183
537
|
width: 0,
|
|
184
538
|
height: 0
|
|
185
539
|
};
|
|
186
|
-
/** Add an entity with a shape. */
|
|
540
|
+
/** Add an entity with a shape. Positional twin of `addCollider`. */
|
|
187
541
|
add(entity, shape, x, y, isTrigger = false, layer = 1, mask = 4294967295) {
|
|
188
542
|
const entry = {
|
|
189
543
|
entity,
|
|
@@ -200,6 +554,14 @@ var CollisionWorld = class {
|
|
|
200
554
|
if (exists) this.spatial.update(entity, this.boundsBuf);
|
|
201
555
|
else this.spatial.insert(entity, this.boundsBuf);
|
|
202
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
|
+
}
|
|
203
565
|
/** Remove an entity from the world. Emits exit signals for any active pairs. */
|
|
204
566
|
remove(entity) {
|
|
205
567
|
const entry = this.entries.get(entity);
|
|
@@ -242,7 +604,8 @@ var CollisionWorld = class {
|
|
|
242
604
|
step() {
|
|
243
605
|
const newPairs = this.activePairs === this.pairBufA ? this.pairBufB : this.pairBufA;
|
|
244
606
|
newPairs.clear();
|
|
245
|
-
for (const
|
|
607
|
+
for (const entryA of this.entries.values()) {
|
|
608
|
+
const entityA = entryA.entity;
|
|
246
609
|
shapeBoundsInto(entryA.shape, entryA.x, entryA.y, this.boundsBuf);
|
|
247
610
|
this.queryBuf.length = 0;
|
|
248
611
|
this.spatial.query(this.boundsBuf, this.queryBuf);
|
|
@@ -282,6 +645,24 @@ var CollisionWorld = class {
|
|
|
282
645
|
}
|
|
283
646
|
this.activePairs = newPairs;
|
|
284
647
|
}
|
|
648
|
+
/** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */
|
|
649
|
+
getColliderBoundsInto(entity, out) {
|
|
650
|
+
const entry = this.entries.get(entity);
|
|
651
|
+
if (!entry) return null;
|
|
652
|
+
shapeBoundsInto(entry.shape, entry.x, entry.y, out);
|
|
653
|
+
return out;
|
|
654
|
+
}
|
|
655
|
+
/** Allocating convenience wrapper — returns a fresh Rect or null when entity is absent. */
|
|
656
|
+
getColliderBounds(entity) {
|
|
657
|
+
const entry = this.entries.get(entity);
|
|
658
|
+
if (!entry) return null;
|
|
659
|
+
return shapeBoundsInto(entry.shape, entry.x, entry.y, {
|
|
660
|
+
x: 0,
|
|
661
|
+
y: 0,
|
|
662
|
+
width: 0,
|
|
663
|
+
height: 0
|
|
664
|
+
});
|
|
665
|
+
}
|
|
285
666
|
/** Remove all entities. */
|
|
286
667
|
clear() {
|
|
287
668
|
this.entries.clear();
|
|
@@ -290,104 +671,96 @@ var CollisionWorld = class {
|
|
|
290
671
|
this.pairBufB.clear();
|
|
291
672
|
this.activePairs = this.pairBufA;
|
|
292
673
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
*/
|
|
305
|
-
function raycast(ox, oy, dx, dy, maxDist, targets) {
|
|
306
|
-
let closest = null;
|
|
307
|
-
for (let i = 0; i < targets.length; i++) {
|
|
308
|
-
const t = targets[i];
|
|
309
|
-
const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);
|
|
310
|
-
if (hit && (closest === null || hit.distance < closest.distance)) closest = hit;
|
|
311
|
-
}
|
|
312
|
-
return closest;
|
|
313
|
-
}
|
|
314
|
-
function rayVsShape(ox, oy, dx, dy, maxDist, target) {
|
|
315
|
-
if (target.shape.type === "aabb") return rayVsAABB(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.hw, target.shape.hh);
|
|
316
|
-
return rayVsCircle(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.radius);
|
|
317
|
-
}
|
|
318
|
-
function rayVsAABB(ox, oy, dx, dy, maxDist, entity, cx, cy, hw, hh) {
|
|
319
|
-
const minX = cx - hw;
|
|
320
|
-
const maxX = cx + hw;
|
|
321
|
-
const minY = cy - hh;
|
|
322
|
-
const maxY = cy + hh;
|
|
323
|
-
const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
|
|
324
|
-
const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
|
|
325
|
-
const tx1 = (minX - ox) * invDx;
|
|
326
|
-
const tx2 = (maxX - ox) * invDx;
|
|
327
|
-
const ty1 = (minY - oy) * invDy;
|
|
328
|
-
const ty2 = (maxY - oy) * invDy;
|
|
329
|
-
const tMinX = Math.min(tx1, tx2);
|
|
330
|
-
const tMaxX = Math.max(tx1, tx2);
|
|
331
|
-
const tMinY = Math.min(ty1, ty2);
|
|
332
|
-
const tMaxY = Math.max(ty1, ty2);
|
|
333
|
-
const tMin = Math.max(tMinX, tMinY);
|
|
334
|
-
const tMax = Math.min(tMaxX, tMaxY);
|
|
335
|
-
if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;
|
|
336
|
-
const t = tMin >= 0 ? tMin : tMax;
|
|
337
|
-
if (t > maxDist || t < 0) return null;
|
|
338
|
-
const px = ox + dx * t;
|
|
339
|
-
const py = oy + dy * t;
|
|
340
|
-
let nx = 0;
|
|
341
|
-
let ny = 0;
|
|
342
|
-
if (tMinX > tMinY) nx = dx < 0 ? 1 : -1;
|
|
343
|
-
else ny = dy < 0 ? 1 : -1;
|
|
344
|
-
return {
|
|
345
|
-
entity,
|
|
346
|
-
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,
|
|
347
685
|
point: {
|
|
348
|
-
x:
|
|
349
|
-
y:
|
|
686
|
+
x: 0,
|
|
687
|
+
y: 0
|
|
350
688
|
},
|
|
351
689
|
normal: {
|
|
352
|
-
x:
|
|
353
|
-
y:
|
|
690
|
+
x: 0,
|
|
691
|
+
y: 0
|
|
354
692
|
}
|
|
355
693
|
};
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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,
|
|
378
754
|
point: {
|
|
379
|
-
x:
|
|
380
|
-
y:
|
|
755
|
+
x: 0,
|
|
756
|
+
y: 0
|
|
381
757
|
},
|
|
382
|
-
normal:
|
|
383
|
-
x: nx / len,
|
|
384
|
-
y: ny / len
|
|
385
|
-
} : {
|
|
758
|
+
normal: {
|
|
386
759
|
x: 0,
|
|
387
760
|
y: 0
|
|
388
761
|
}
|
|
389
762
|
};
|
|
390
|
-
}
|
|
763
|
+
};
|
|
391
764
|
//#endregion
|
|
392
765
|
//#region src/solid-grid.ts
|
|
393
766
|
/**
|
|
@@ -396,8 +769,8 @@ function rayVsCircle(ox, oy, dx, dy, maxDist, entity, cx, cy, radius) {
|
|
|
396
769
|
* need to import `@flare-engine/tilemap` — physics stays tilemap-free.
|
|
397
770
|
*/
|
|
398
771
|
function buildSolidGrid(width, height, tileSize, tiles) {
|
|
399
|
-
const solid =
|
|
400
|
-
for (const t of tiles) solid
|
|
772
|
+
const solid = new Uint8Array(width * height);
|
|
773
|
+
for (const t of tiles) solid[t.row * width + t.col] = 1;
|
|
401
774
|
return {
|
|
402
775
|
width,
|
|
403
776
|
height,
|
|
@@ -422,7 +795,7 @@ function isBlockedAABB(grid, x, y, halfW, halfH) {
|
|
|
422
795
|
const minRow = Math.floor((y - halfH) / ts);
|
|
423
796
|
const maxRow = Math.floor((y + halfH - EDGE) / ts);
|
|
424
797
|
if (minCol < 0 || minRow < 0 || maxCol >= grid.width || maxRow >= grid.height) return true;
|
|
425
|
-
for (let row = minRow; row <= maxRow; row++) for (let col = minCol; col <= maxCol; col++) if (grid.solid
|
|
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;
|
|
426
799
|
return false;
|
|
427
800
|
}
|
|
428
801
|
/**
|
|
@@ -447,8 +820,12 @@ exports.CollisionWorld = CollisionWorld;
|
|
|
447
820
|
exports.SpatialIndex = SpatialIndex;
|
|
448
821
|
exports.buildSolidGrid = buildSolidGrid;
|
|
449
822
|
exports.createCollider = createCollider;
|
|
823
|
+
exports.createOverlap = createOverlap;
|
|
450
824
|
exports.isBlockedAABB = isBlockedAABB;
|
|
451
825
|
exports.moveAndSlideInto = moveAndSlideInto;
|
|
826
|
+
exports.overlap = overlap;
|
|
827
|
+
exports.overlapInto = overlapInto;
|
|
828
|
+
exports.rayVsShapeInto = rayVsShapeInto;
|
|
452
829
|
exports.raycast = raycast;
|
|
453
830
|
exports.shapeBounds = shapeBounds;
|
|
454
831
|
exports.shapeBoundsInto = shapeBoundsInto;
|