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