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