@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/dist/index.js CHANGED
@@ -1,466 +1,457 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
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
- return { type: "aabb", hw: width * 0.5, hh: height * 0.5 };
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
- return { type: "circle", radius };
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
- if (shape.type === "aabb") {
47
- return { x: x - shape.hw, y: y - shape.hh, width: shape.hw * 2, height: shape.hh * 2 };
48
- }
49
- return {
50
- x: x - shape.radius,
51
- y: y - shape.radius,
52
- width: shape.radius * 2,
53
- height: shape.radius * 2
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
- if (shape.type === "aabb") {
58
- out.x = x - shape.hw;
59
- out.y = y - shape.hh;
60
- out.width = shape.hw * 2;
61
- out.height = shape.hh * 2;
62
- return out;
63
- }
64
- out.x = x - shape.radius;
65
- out.y = y - shape.radius;
66
- out.width = shape.radius * 2;
67
- out.height = shape.radius * 2;
68
- return out;
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
- if (shapeA.type === "aabb" && shapeB.type === "aabb") {
72
- return aabbVsAabb(ax, ay, shapeA.hw, shapeA.hh, bx, by, shapeB.hw, shapeB.hh);
73
- }
74
- if (shapeA.type === "circle" && shapeB.type === "circle") {
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
- return Math.abs(ax - bx) < ahw + bhw && Math.abs(ay - by) < ahh + bhh;
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
- const dx = bx - ax;
95
- const dy = by - ay;
96
- const radSum = ar + br;
97
- return dx * dx + dy * dy < radSum * radSum;
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
- const left = ax - ahw;
101
- const right = ax + ahw;
102
- const top = ay - ahh;
103
- const bottom = ay + ahh;
104
- const closestX = cx < left ? left : cx > right ? right : cx;
105
- const closestY = cy < top ? top : cy > bottom ? bottom : cy;
106
- const dx = cx - closestX;
107
- const dy = cy - closestY;
108
- return dx * dx + dy * dy <= cr * cr;
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
- // src/spatial-index.ts
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
- entries = [];
114
- /** Number of items in the index. */
115
- get size() {
116
- return this.entries.length;
117
- }
118
- /** Insert an item with a bounding rect. */
119
- insert(item, bounds) {
120
- this.entries.push({
121
- item,
122
- minX: bounds.x,
123
- minY: bounds.y,
124
- maxX: bounds.x + bounds.width,
125
- maxY: bounds.y + bounds.height
126
- });
127
- }
128
- /** Remove an item. */
129
- remove(item) {
130
- const idx = this.entries.findIndex((e) => e.item === item);
131
- if (idx === -1) return false;
132
- const last = this.entries.pop();
133
- if (idx < this.entries.length) {
134
- this.entries[idx] = last;
135
- }
136
- return true;
137
- }
138
- /** Update the bounds for an existing item. */
139
- update(item, bounds) {
140
- const entry = this.entries.find((e) => e.item === item);
141
- if (entry) {
142
- entry.minX = bounds.x;
143
- entry.minY = bounds.y;
144
- entry.maxX = bounds.x + bounds.width;
145
- entry.maxY = bounds.y + bounds.height;
146
- }
147
- }
148
- /** Query all items whose bounds overlap the given rect. */
149
- query(bounds, result) {
150
- const out = result ?? [];
151
- const qMinX = bounds.x;
152
- const qMinY = bounds.y;
153
- const qMaxX = bounds.x + bounds.width;
154
- const qMaxY = bounds.y + bounds.height;
155
- for (let i = 0; i < this.entries.length; i++) {
156
- const e = this.entries[i];
157
- if (e.maxX > qMinX && e.minX < qMaxX && e.maxY > qMinY && e.minY < qMaxY) {
158
- out.push(e.item);
159
- }
160
- }
161
- return out;
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
- // src/collision-world.ts
170
- var import_events = require("@flare-engine/events");
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
- const lo = a < b ? a : b;
174
- const hi = a < b ? b : a;
175
- return lo * ENTITY_SHIFT + hi;
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
- return Math.floor(key / ENTITY_SHIFT);
154
+ return Math.floor(key / ENTITY_SHIFT);
179
155
  }
180
156
  function decodeHi(key) {
181
- return key - Math.floor(key / ENTITY_SHIFT) * ENTITY_SHIFT;
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
- onCollisionStart = new import_events.Signal();
185
- onCollisionEnd = new import_events.Signal();
186
- onTriggerEnter = new import_events.Signal();
187
- onTriggerExit = new import_events.Signal();
188
- entries = /* @__PURE__ */ new Map();
189
- spatial = new SpatialIndex();
190
- /**
191
- * Two pre-allocated pair buffers — swapped each step (no per-frame allocation).
192
- * Keys are packed numbers via `encodePair(a, b)`.
193
- */
194
- pairBufA = /* @__PURE__ */ new Set();
195
- pairBufB = /* @__PURE__ */ new Set();
196
- activePairs = this.pairBufA;
197
- /** Reusable query result buffer. */
198
- queryBuf = [];
199
- /** Reusable bounds rect — avoids per-entity Rect allocation in step(). */
200
- boundsBuf = { x: 0, y: 0, width: 0, height: 0 };
201
- /** Add an entity with a shape. */
202
- add(entity, shape, x, y, isTrigger = false, layer = 1, mask = 4294967295) {
203
- const entry = { entity, shape, x, y, isTrigger, layer, mask };
204
- this.entries.set(entity, entry);
205
- shapeBoundsInto(shape, x, y, this.boundsBuf);
206
- this.spatial.insert(entity, this.boundsBuf);
207
- }
208
- /** Remove an entity from the world. Emits exit signals for any active pairs. */
209
- remove(entity) {
210
- const entry = this.entries.get(entity);
211
- if (!entry) return;
212
- this.entries.delete(entity);
213
- this.spatial.remove(entity);
214
- for (const key of this.activePairs) {
215
- const lo = decodeLo(key);
216
- const hi = decodeHi(key);
217
- if (lo !== entity && hi !== entity) continue;
218
- this.activePairs.delete(key);
219
- const otherEntity = lo === entity ? hi : lo;
220
- const otherEntry = this.entries.get(otherEntity);
221
- const pair = { entityA: lo, entityB: hi };
222
- if (entry.isTrigger || otherEntry?.isTrigger) {
223
- this.onTriggerExit.emit(pair);
224
- } else {
225
- this.onCollisionEnd.emit(pair);
226
- }
227
- }
228
- }
229
- /** Update an entity's position. */
230
- setPosition(entity, x, y) {
231
- const entry = this.entries.get(entity);
232
- if (!entry) return;
233
- entry.x = x;
234
- entry.y = y;
235
- shapeBoundsInto(entry.shape, x, y, this.boundsBuf);
236
- this.spatial.update(entity, this.boundsBuf);
237
- }
238
- /** Number of entities in the world. */
239
- get size() {
240
- return this.entries.size;
241
- }
242
- /** Run one collision detection step. */
243
- step() {
244
- const newPairs = this.activePairs === this.pairBufA ? this.pairBufB : this.pairBufA;
245
- newPairs.clear();
246
- for (const [entityA, entryA] of this.entries) {
247
- shapeBoundsInto(entryA.shape, entryA.x, entryA.y, this.boundsBuf);
248
- this.queryBuf.length = 0;
249
- this.spatial.query(this.boundsBuf, this.queryBuf);
250
- for (let i = 0; i < this.queryBuf.length; i++) {
251
- const entityB = this.queryBuf[i];
252
- if (entityB <= entityA) continue;
253
- const entryB = this.entries.get(entityB);
254
- if (!entryB) continue;
255
- if ((entryA.layer & entryB.mask) === 0 && (entryB.layer & entryA.mask) === 0) continue;
256
- if (!shapeOverlap(entryA.shape, entryA.x, entryA.y, entryB.shape, entryB.x, entryB.y)) {
257
- continue;
258
- }
259
- newPairs.add(encodePair(entityA, entityB));
260
- }
261
- }
262
- for (const key of newPairs) {
263
- if (!this.activePairs.has(key)) {
264
- const lo = decodeLo(key);
265
- const hi = decodeHi(key);
266
- const a = this.entries.get(lo);
267
- const b = this.entries.get(hi);
268
- const pair = { entityA: lo, entityB: hi };
269
- if (a?.isTrigger || b?.isTrigger) {
270
- this.onTriggerEnter.emit(pair);
271
- } else {
272
- this.onCollisionStart.emit(pair);
273
- }
274
- }
275
- }
276
- for (const key of this.activePairs) {
277
- if (!newPairs.has(key)) {
278
- const lo = decodeLo(key);
279
- const hi = decodeHi(key);
280
- const a = this.entries.get(lo);
281
- const b = this.entries.get(hi);
282
- const pair = { entityA: lo, entityB: hi };
283
- if (a?.isTrigger || b?.isTrigger) {
284
- this.onTriggerExit.emit(pair);
285
- } else {
286
- this.onCollisionEnd.emit(pair);
287
- }
288
- }
289
- }
290
- this.activePairs = newPairs;
291
- }
292
- /** Remove all entities. */
293
- clear() {
294
- this.entries.clear();
295
- this.spatial.clear();
296
- this.pairBufA.clear();
297
- this.pairBufB.clear();
298
- this.activePairs = this.pairBufA;
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
- // src/collider.ts
303
- function createCollider(shape, opts) {
304
- return {
305
- shape,
306
- isTrigger: opts?.isTrigger ?? false,
307
- layer: opts?.layer ?? 1,
308
- mask: opts?.mask ?? 4294967295
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
- let closest = null;
315
- for (let i = 0; i < targets.length; i++) {
316
- const t = targets[i];
317
- const hit = rayVsShape(ox, oy, dx, dy, maxDist, t);
318
- if (hit && (closest === null || hit.distance < closest.distance)) {
319
- closest = hit;
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
- if (target.shape.type === "aabb") {
326
- return rayVsAABB(
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
- const minX = cx - hw;
353
- const maxX = cx + hw;
354
- const minY = cy - hh;
355
- const maxY = cy + hh;
356
- const invDx = dx !== 0 ? 1 / dx : dx >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
357
- const invDy = dy !== 0 ? 1 / dy : dy >= 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
358
- const tx1 = (minX - ox) * invDx;
359
- const tx2 = (maxX - ox) * invDx;
360
- const ty1 = (minY - oy) * invDy;
361
- const ty2 = (maxY - oy) * invDy;
362
- const tMinX = Math.min(tx1, tx2);
363
- const tMaxX = Math.max(tx1, tx2);
364
- const tMinY = Math.min(ty1, ty2);
365
- const tMaxY = Math.max(ty1, ty2);
366
- const tMin = Math.max(tMinX, tMinY);
367
- const tMax = Math.min(tMaxX, tMaxY);
368
- if (tMax < 0 || tMin > tMax || tMin > maxDist) return null;
369
- const t = tMin >= 0 ? tMin : tMax;
370
- if (t > maxDist || t < 0) return null;
371
- const px = ox + dx * t;
372
- const py = oy + dy * t;
373
- let nx = 0;
374
- let ny = 0;
375
- if (tMinX > tMinY) {
376
- nx = dx < 0 ? 1 : -1;
377
- } else {
378
- ny = dy < 0 ? 1 : -1;
379
- }
380
- return {
381
- entity,
382
- distance: t,
383
- point: { x: px, y: py },
384
- normal: { x: nx, y: ny }
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
- const fx = ox - cx;
389
- const fy = oy - cy;
390
- const a = dx * dx + dy * dy;
391
- const b = 2 * (fx * dx + fy * dy);
392
- const c = fx * fx + fy * fy - radius * radius;
393
- if (a < 1e-12) return null;
394
- const discriminant = b * b - 4 * a * c;
395
- if (discriminant < 0) return null;
396
- const sqrtDisc = Math.sqrt(discriminant);
397
- let t = (-b - sqrtDisc) / (2 * a);
398
- if (t < 0) {
399
- t = (-b + sqrtDisc) / (2 * a);
400
- }
401
- if (t < 0 || t > maxDist) return null;
402
- const px = ox + dx * t;
403
- const py = oy + dy * t;
404
- const nx = px - cx;
405
- const ny = py - cy;
406
- const len = Math.sqrt(nx * nx + ny * ny);
407
- return {
408
- entity,
409
- distance: t,
410
- point: { x: px, y: py },
411
- normal: len > 0 ? { x: nx / len, y: ny / len } : { x: 0, y: 0 }
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
- // src/solid-grid.ts
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
- const solid = /* @__PURE__ */ new Set();
418
- for (const t of tiles) {
419
- solid.add(t.row * width + t.col);
420
- }
421
- return { width, height, tileSize, solid };
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
- var EDGE = 1e-4;
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
- const ts = grid.tileSize;
426
- const minCol = Math.floor((x - halfW) / ts);
427
- const maxCol = Math.floor((x + halfW - EDGE) / ts);
428
- const minRow = Math.floor((y - halfH) / ts);
429
- const maxRow = Math.floor((y + halfH - EDGE) / ts);
430
- if (minCol < 0 || minRow < 0 || maxCol >= grid.width || maxRow >= grid.height) {
431
- return true;
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
- let nx = x;
444
- let ny = y;
445
- if (dx !== 0 && !isBlockedAABB(grid, nx + dx, ny, halfW, halfH)) nx += dx;
446
- if (dy !== 0 && !isBlockedAABB(grid, nx, ny + dy, halfW, halfH)) ny += dy;
447
- out.x = nx;
448
- out.y = ny;
449
- return out;
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
- // Annotate the CommonJS export names for ESM import in node:
452
- 0 && (module.exports = {
453
- AABB,
454
- CircleShape,
455
- CollisionWorld,
456
- SpatialIndex,
457
- buildSolidGrid,
458
- createCollider,
459
- isBlockedAABB,
460
- moveAndSlideInto,
461
- raycast,
462
- shapeBounds,
463
- shapeBoundsInto,
464
- shapeOverlap
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