@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/README.md +48 -2
- package/dist/index.d.mts +111 -44
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +111 -44
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +386 -104
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +383 -105
- 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,7 +246,111 @@ 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
|
|
@@ -97,6 +366,13 @@ function _cMin(v) {
|
|
|
97
366
|
function _cMax(v) {
|
|
98
367
|
return Math.ceil(v / CELL) - 1;
|
|
99
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
|
+
}
|
|
100
376
|
/**
|
|
101
377
|
* Uniform spatial-hash broadphase.
|
|
102
378
|
* `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.
|
|
@@ -164,10 +440,10 @@ var SpatialIndex = class {
|
|
|
164
440
|
const qMinY = bounds.y;
|
|
165
441
|
const qMaxX = bounds.x + bounds.width;
|
|
166
442
|
const qMaxY = bounds.y + bounds.height;
|
|
167
|
-
const cxMin = _cMin(qMinX);
|
|
168
|
-
const cxMax = _cMax(qMaxX);
|
|
169
|
-
const cyMin = _cMin(qMinY);
|
|
170
|
-
const cyMax = _cMax(qMaxY);
|
|
443
|
+
const cxMin = _clampCell(_cMin(qMinX));
|
|
444
|
+
const cxMax = _clampCell(_cMax(qMaxX));
|
|
445
|
+
const cyMin = _clampCell(_cMin(qMinY));
|
|
446
|
+
const cyMax = _clampCell(_cMax(qMaxY));
|
|
171
447
|
for (let cx = cxMin; cx <= cxMax; cx++) for (let cy = cyMin; cy <= cyMax; cy++) {
|
|
172
448
|
const bucket = this._cells.get(_key(cx, cy));
|
|
173
449
|
if (bucket === void 0) continue;
|
|
@@ -187,10 +463,10 @@ var SpatialIndex = class {
|
|
|
187
463
|
this._cells.clear();
|
|
188
464
|
}
|
|
189
465
|
_link(entry) {
|
|
190
|
-
const cxMin = _cMin(entry.minX);
|
|
191
|
-
const cxMax = _cMax(entry.maxX);
|
|
192
|
-
const cyMin = _cMin(entry.minY);
|
|
193
|
-
const cyMax = _cMax(entry.maxY);
|
|
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));
|
|
194
470
|
for (let cx = cxMin; cx <= cxMax; cx++) for (let cy = cyMin; cy <= cyMax; cy++) {
|
|
195
471
|
const k = _key(cx, cy);
|
|
196
472
|
let b = this._cells.get(k);
|
|
@@ -202,10 +478,10 @@ var SpatialIndex = class {
|
|
|
202
478
|
}
|
|
203
479
|
}
|
|
204
480
|
_unlink(entry) {
|
|
205
|
-
const cxMin = _cMin(entry.minX);
|
|
206
|
-
const cxMax = _cMax(entry.maxX);
|
|
207
|
-
const cyMin = _cMin(entry.minY);
|
|
208
|
-
const cyMax = _cMax(entry.maxY);
|
|
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));
|
|
209
485
|
for (let cx = cxMin; cx <= cxMax; cx++) for (let cy = cyMin; cy <= cyMax; cy++) {
|
|
210
486
|
const b = this._cells.get(_key(cx, cy));
|
|
211
487
|
if (b === void 0) continue;
|
|
@@ -220,6 +496,8 @@ var SpatialIndex = class {
|
|
|
220
496
|
//#endregion
|
|
221
497
|
//#region src/collision-world.ts
|
|
222
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;
|
|
223
501
|
function encodePair(a, b) {
|
|
224
502
|
const lo = a < b ? a : b;
|
|
225
503
|
const hi = a < b ? b : a;
|
|
@@ -258,7 +536,7 @@ var CollisionWorld = class {
|
|
|
258
536
|
width: 0,
|
|
259
537
|
height: 0
|
|
260
538
|
};
|
|
261
|
-
/** Add an entity with a shape. */
|
|
539
|
+
/** Add an entity with a shape. Positional twin of `addCollider`. */
|
|
262
540
|
add(entity, shape, x, y, isTrigger = false, layer = 1, mask = 4294967295) {
|
|
263
541
|
const entry = {
|
|
264
542
|
entity,
|
|
@@ -275,6 +553,14 @@ var CollisionWorld = class {
|
|
|
275
553
|
if (exists) this.spatial.update(entity, this.boundsBuf);
|
|
276
554
|
else this.spatial.insert(entity, this.boundsBuf);
|
|
277
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
|
+
}
|
|
278
564
|
/** Remove an entity from the world. Emits exit signals for any active pairs. */
|
|
279
565
|
remove(entity) {
|
|
280
566
|
const entry = this.entries.get(entity);
|
|
@@ -384,104 +670,96 @@ var CollisionWorld = class {
|
|
|
384
670
|
this.pairBufB.clear();
|
|
385
671
|
this.activePairs = this.pairBufA;
|
|
386
672
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
*/
|
|
399
|
-
function raycast(ox, oy, dx, dy, maxDist, targets) {
|
|
400
|
-
let closest = null;
|
|
401
|
-
for (let i = 0; i < targets.length; i++) {
|
|
402
|
-
const t = targets[i];
|
|
403
|
-
const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);
|
|
404
|
-
if (hit && (closest === null || hit.distance < closest.distance)) closest = hit;
|
|
405
|
-
}
|
|
406
|
-
return closest;
|
|
407
|
-
}
|
|
408
|
-
function rayVsShape(ox, oy, dx, dy, maxDist, target) {
|
|
409
|
-
if (target.shape.type === "aabb") return rayVsAABB(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.hw, target.shape.hh);
|
|
410
|
-
return rayVsCircle(ox, oy, dx, dy, maxDist, target.entity, target.x, target.y, target.shape.radius);
|
|
411
|
-
}
|
|
412
|
-
function rayVsAABB(ox, oy, dx, dy, maxDist, entity, cx, cy, hw, hh) {
|
|
413
|
-
const minX = cx - hw;
|
|
414
|
-
const maxX = cx + hw;
|
|
415
|
-
const minY = cy - hh;
|
|
416
|
-
const maxY = cy + hh;
|
|
417
|
-
const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
|
|
418
|
-
const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
|
|
419
|
-
const tx1 = (minX - ox) * invDx;
|
|
420
|
-
const tx2 = (maxX - ox) * invDx;
|
|
421
|
-
const ty1 = (minY - oy) * invDy;
|
|
422
|
-
const ty2 = (maxY - oy) * invDy;
|
|
423
|
-
const tMinX = Math.min(tx1, tx2);
|
|
424
|
-
const tMaxX = Math.max(tx1, tx2);
|
|
425
|
-
const tMinY = Math.min(ty1, ty2);
|
|
426
|
-
const tMaxY = Math.max(ty1, ty2);
|
|
427
|
-
const tMin = Math.max(tMinX, tMinY);
|
|
428
|
-
const tMax = Math.min(tMaxX, tMaxY);
|
|
429
|
-
if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;
|
|
430
|
-
const t = tMin >= 0 ? tMin : tMax;
|
|
431
|
-
if (t > maxDist || t < 0) return null;
|
|
432
|
-
const px = ox + dx * t;
|
|
433
|
-
const py = oy + dy * t;
|
|
434
|
-
let nx = 0;
|
|
435
|
-
let ny = 0;
|
|
436
|
-
if (tMinX > tMinY) nx = dx < 0 ? 1 : -1;
|
|
437
|
-
else ny = dy < 0 ? 1 : -1;
|
|
438
|
-
return {
|
|
439
|
-
entity,
|
|
440
|
-
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,
|
|
441
684
|
point: {
|
|
442
|
-
x:
|
|
443
|
-
y:
|
|
685
|
+
x: 0,
|
|
686
|
+
y: 0
|
|
444
687
|
},
|
|
445
688
|
normal: {
|
|
446
|
-
x:
|
|
447
|
-
y:
|
|
689
|
+
x: 0,
|
|
690
|
+
y: 0
|
|
448
691
|
}
|
|
449
692
|
};
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
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,
|
|
472
753
|
point: {
|
|
473
|
-
x:
|
|
474
|
-
y:
|
|
754
|
+
x: 0,
|
|
755
|
+
y: 0
|
|
475
756
|
},
|
|
476
|
-
normal:
|
|
477
|
-
x: nx / len,
|
|
478
|
-
y: ny / len
|
|
479
|
-
} : {
|
|
757
|
+
normal: {
|
|
480
758
|
x: 0,
|
|
481
759
|
y: 0
|
|
482
760
|
}
|
|
483
761
|
};
|
|
484
|
-
}
|
|
762
|
+
};
|
|
485
763
|
//#endregion
|
|
486
764
|
//#region src/solid-grid.ts
|
|
487
765
|
/**
|
|
@@ -490,8 +768,8 @@ function rayVsCircle(ox, oy, dx, dy, maxDist, entity, cx, cy, radius) {
|
|
|
490
768
|
* need to import `@flare-engine/tilemap` — physics stays tilemap-free.
|
|
491
769
|
*/
|
|
492
770
|
function buildSolidGrid(width, height, tileSize, tiles) {
|
|
493
|
-
const solid =
|
|
494
|
-
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;
|
|
495
773
|
return {
|
|
496
774
|
width,
|
|
497
775
|
height,
|
|
@@ -516,7 +794,7 @@ function isBlockedAABB(grid, x, y, halfW, halfH) {
|
|
|
516
794
|
const minRow = Math.floor((y - halfH) / ts);
|
|
517
795
|
const maxRow = Math.floor((y + halfH - EDGE) / ts);
|
|
518
796
|
if (minCol < 0 || minRow < 0 || maxCol >= grid.width || maxRow >= grid.height) return true;
|
|
519
|
-
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;
|
|
520
798
|
return false;
|
|
521
799
|
}
|
|
522
800
|
/**
|
|
@@ -535,6 +813,6 @@ function moveAndSlideInto(grid, x, y, dx, dy, halfW, halfH, out) {
|
|
|
535
813
|
return out;
|
|
536
814
|
}
|
|
537
815
|
//#endregion
|
|
538
|
-
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 };
|
|
539
817
|
|
|
540
818
|
//# sourceMappingURL=index.mjs.map
|