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