@daneren2005/shared-memory-physics 1.2.1 → 1.2.2

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,9 +1,9 @@
1
1
  import e from "flatbush";
2
- import { addAtomicFloat32 as t } from "@daneren2005/shared-memory-objects/utils/atomic-math";
3
- import { loadFloat32 as n, storeFloat32 as r } from "@daneren2005/shared-memory-objects/utils/float32-atomics";
4
- import { ComponentSystem as i } from "@daneren2005/shared-memory-ecs";
2
+ import { ComponentSystem as t, DEAD_INDEX as n } from "@daneren2005/shared-memory-ecs";
3
+ import { addAtomicFloat32 as r } from "@daneren2005/shared-memory-objects/utils/atomic-math";
4
+ import { loadFloat32 as i, storeFloat32 as a } from "@daneren2005/shared-memory-objects/utils/float32-atomics";
5
5
  //#region src/components/transform-component.ts
6
- var a = 0, o = 1, s = 2, c = 3, l = 4, u = 5, d = {
6
+ var o = 0, s = 1, c = 2, l = 3, u = 4, d = 5, f = {
7
7
  type: Float32Array,
8
8
  size: 5,
9
9
  loadProperties: [
@@ -64,7 +64,7 @@ var a = 0, o = 1, s = 2, c = 3, l = 4, u = 5, d = {
64
64
  y: e.y
65
65
  };
66
66
  }
67
- }, f = 0, p = 1, m = 2, h = {
67
+ }, p = 0, m = 1, h = 2, g = {
68
68
  type: Float32Array,
69
69
  size: 2,
70
70
  loadProperties: ["velocityX", "velocityY"],
@@ -92,19 +92,19 @@ var a = 0, o = 1, s = 2, c = 3, l = 4, u = 5, d = {
92
92
  velocityY: e.velocityY
93
93
  };
94
94
  }
95
- }, g = 1, _ = 2, v = 3, y = [
95
+ }, _ = 1, v = 2, y = 3, b = [
96
96
  1,
97
97
  2,
98
98
  3
99
- ], b = 1, x = 4294967295;
100
- function ee(e, t, n, r) {
99
+ ], x = 1, S = 4294967295;
100
+ function C(e, t, n, r) {
101
101
  return (t & n) !== 0 && (r & e) !== 0;
102
102
  }
103
- var te = 0, ne = 1, re = 2, ie = 3, ae = 4;
104
- function oe(e) {
103
+ var w = 0, T = 1, E = 2, D = 3, O = 4;
104
+ function k(e) {
105
105
  return e[3] !== 0;
106
106
  }
107
- var S = {
107
+ var A = {
108
108
  type: Uint32Array,
109
109
  size: 4,
110
110
  loadProperties: [
@@ -118,7 +118,7 @@ var S = {
118
118
  ],
119
119
  load(e, t, n) {
120
120
  let r = t.create([
121
- se(n),
121
+ ee(n),
122
122
  n.collideCategory ?? 1,
123
123
  n.collideMask ?? 4294967295,
124
124
  +!!n.sensor
@@ -152,15 +152,15 @@ var S = {
152
152
  };
153
153
  }
154
154
  };
155
- function se(e) {
155
+ function ee(e) {
156
156
  let t = e.shape;
157
157
  if (t === void 0) return e.radius === void 0 ? 1 : 2;
158
- if (!y.includes(t)) throw Error(`Unknown body shape: ${t}`);
158
+ if (!b.includes(t)) throw Error(`Unknown body shape: ${t}`);
159
159
  return t;
160
160
  }
161
161
  //#endregion
162
162
  //#region src/components/bounciness-component.ts
163
- var ce = 0, le = 1, C = {
163
+ var te = 0, ne = 1, re = {
164
164
  type: Float32Array,
165
165
  size: 1,
166
166
  loadProperties: ["bounciness"],
@@ -176,7 +176,7 @@ var ce = 0, le = 1, C = {
176
176
  }
177
177
  };
178
178
  }
179
- }, ue = 0, de = 1, fe = 2, pe = 3, me = 4, he = 5, ge = 6, _e = 7, ve = 8, w = {
179
+ }, ie = 0, ae = 1, oe = 2, se = 3, ce = 4, le = 5, ue = 6, de = 7, fe = 8, pe = {
180
180
  type: Float32Array,
181
181
  size: 8,
182
182
  loadProperties: ["interpolate"],
@@ -220,106 +220,153 @@ var ce = 0, le = 1, C = {
220
220
  };
221
221
  }
222
222
  };
223
- function ye(e) {
223
+ function me(e) {
224
224
  let { transform: t, interpolation: n } = e.components;
225
225
  !t || !n || (n.prevX = n.x = t.x, n.prevY = n.y = t.y);
226
226
  }
227
227
  //#endregion
228
228
  //#region src/components/registry.ts
229
- var be = {
230
- transform: d,
231
- velocity: h,
232
- body: S,
233
- bounciness: C,
234
- interpolation: w
229
+ var he = {
230
+ transform: f,
231
+ velocity: g,
232
+ body: A,
233
+ bounciness: re,
234
+ interpolation: pe
235
+ }, j = {
236
+ x: 0,
237
+ y: 0
238
+ }, M = {
239
+ x: 0,
240
+ y: 0
235
241
  };
236
- //#endregion
237
- //#region src/math/shapes.ts
238
- function T(e, t) {
242
+ function N(e, t) {
239
243
  return Math.max(0, (e - t) / 2);
240
244
  }
241
- function E(e, t, n) {
245
+ function P(e, t, n) {
242
246
  return e === 2 ? t / 2 : e === 3 ? n / 2 : 0;
243
247
  }
244
- function D(e, t, n) {
248
+ function F(e, t, n) {
245
249
  return e === 2 ? t <= 0 : e === 3 ? n <= 0 : t <= 0 || n <= 0;
246
250
  }
247
- function O(e, t, n, r) {
248
- return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.cos(r)) * T(t, n) + n / 2 : M(t, n, r);
251
+ function I(e, t, n, r) {
252
+ return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.cos(r)) * N(t, n) + n / 2 : Se(t, n, r);
249
253
  }
250
- function k(e, t, n, r) {
251
- return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.sin(r)) * T(t, n) + n / 2 : N(t, n, r);
254
+ function L(e, t, n, r) {
255
+ return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.sin(r)) * N(t, n) + n / 2 : Ce(t, n, r);
252
256
  }
253
- function A(e, t, n, r, i, a, o, s, c, l, u, d) {
254
- if (D(e, r, i) || D(o, l, u)) return !1;
257
+ function R(e, t, n, r, i, a, o, s, c, l, u, d) {
258
+ if (F(e, r, i) || F(o, l, u)) return !1;
255
259
  let f = e !== 1, p = o !== 1;
256
- if (!f && !p) return P(t, n, r, i, a, s, c, l, u, d);
257
- if (f && !p) return A(o, s, c, l, u, d, e, t, n, r, i, a);
258
- let m = j(o, l, u), h = Math.cos(d) * m, g = Math.sin(d) * m, _ = E(o, l, u);
259
- if (!f) return L(s - h, c - g, s + h, c + g, t, n, r, i, a) < _ * _;
260
- let v = j(e, r, i), y = Math.cos(a) * v, b = Math.sin(a) * v, x = E(e, r, i) + _;
261
- return I(t - y, n - b, t + y, n + b, s - h, c - g, s + h, c + g) < x * x;
262
- }
263
- function j(e, t, n) {
264
- return e === 3 ? T(t, n) : 0;
265
- }
266
- function M(e, t, n) {
260
+ if (!f && !p) return we(t, n, r, i, a, s, c, l, u, d);
261
+ if (f && !p) return R(o, s, c, l, u, d, e, t, n, r, i, a);
262
+ let m = z(o, l, u), h = Math.cos(d) * m, g = Math.sin(d) * m, _ = P(o, l, u);
263
+ if (!f) return U(s - h, c - g, s + h, c + g, t, n, r, i, a) < _ * _;
264
+ let v = z(e, r, i), y = Math.cos(a) * v, b = Math.sin(a) * v, x = P(e, r, i) + _;
265
+ return H(t - y, n - b, t + y, n + b, s - h, c - g, s + h, c + g) < x * x;
266
+ }
267
+ function z(e, t, n) {
268
+ return e === 3 ? N(t, n) : 0;
269
+ }
270
+ function ge(e, t, n, r, i, a, o, s, c, l, u, d, f) {
271
+ let p = e !== 1, m = o !== 1;
272
+ return !p && !m ? _e(t, n, r, i, a, s, c, l, u, d, f) : p && m ? ve(e, t, n, r, i, a, o, s, c, l, u, d, f) : p ? ye(s, c, l, u, d, e, t, n, r, i, a, f) : ye(t, n, r, i, a, o, s, c, l, u, d, f) ? (f.x = -f.x, f.y = -f.y, !0) : !1;
273
+ }
274
+ function _e(e, t, n, r, i, a, o, s, c, l, u) {
275
+ let d = Math.cos(i), f = Math.sin(i), p = Math.cos(l), m = Math.sin(l), h = n / 2, g = r / 2, _ = s / 2, v = c / 2, y = e - a, b = t - o, x = V(d, f, y, b, h, g, d, f, _, v, p, m);
276
+ u.x = d, u.y = f;
277
+ let S = V(-f, d, y, b, h, g, d, f, _, v, p, m);
278
+ return S < x && (x = S, u.x = -f, u.y = d), S = V(p, m, y, b, h, g, d, f, _, v, p, m), S < x && (x = S, u.x = p, u.y = m), S = V(-m, p, y, b, h, g, d, f, _, v, p, m), S < x && (u.x = -m, u.y = p), be(y, b, u), !0;
279
+ }
280
+ function ve(e, t, n, r, i, a, o, s, c, l, u, d, f) {
281
+ let p = z(e, r, i), m = Math.cos(a) * p, h = Math.sin(a) * p, g = z(o, l, u), _ = Math.cos(d) * g, v = Math.sin(d) * g, y = Math.sqrt(H(t - m, n - h, t + m, n + h, s - _, c - v, s + _, c + v, j));
282
+ return y > 0 ? (f.x = j.x / y, f.y = j.y / y, !0) : xe(t - s, n - c, f);
283
+ }
284
+ function ye(e, t, n, r, i, a, o, s, c, l, u, d) {
285
+ let f = z(a, c, l), p = Math.cos(u) * f, m = Math.sin(u) * f, h = Math.sqrt(U(o - p, s - m, o + p, s + m, e, t, n, r, i, j));
286
+ if (h > 0) return d.x = j.x / h, d.y = j.y / h, !0;
287
+ let g = Math.cos(i), _ = Math.sin(i), v = o - e, y = s - t, b = v * g + y * _, x = -v * _ + y * g, S = p * g + m * _, C = -p * _ + m * g, w = P(a, c, l), T = n / 2, E = r / 2, D = B(1, 0, b, x, S, C, w, T, E);
288
+ d.x = 1, d.y = 0;
289
+ let O = B(0, 1, b, x, S, C, w, T, E);
290
+ O < D && (D = O, d.x = 0, d.y = 1), xe(-C, S, M) && (O = B(M.x, M.y, b, x, S, C, w, T, E), O < D && (d.x = M.x, d.y = M.y)), be(b, x, d);
291
+ let k = d.x, A = d.y;
292
+ return d.x = k * g - A * _, d.y = k * _ + A * g, !0;
293
+ }
294
+ function B(e, t, n, r, i, a, o, s, c) {
295
+ return s * Math.abs(e) + c * Math.abs(t) + (Math.abs(i * e + a * t) + o) - Math.abs(n * e + r * t);
296
+ }
297
+ function be(e, t, n) {
298
+ e * n.x + t * n.y < 0 && (n.x = -n.x, n.y = -n.y);
299
+ }
300
+ function xe(e, t, n) {
301
+ let r = Math.sqrt(e * e + t * t);
302
+ return r === 0 ? !1 : (n.x = e / r, n.y = t / r, !0);
303
+ }
304
+ function Se(e, t, n) {
267
305
  return (Math.abs(Math.cos(n)) * e + Math.abs(Math.sin(n)) * t) / 2;
268
306
  }
269
- function N(e, t, n) {
307
+ function Ce(e, t, n) {
270
308
  return (Math.abs(Math.sin(n)) * e + Math.abs(Math.cos(n)) * t) / 2;
271
309
  }
272
- function P(e, t, n, r, i, a, o, s, c, l) {
310
+ function we(e, t, n, r, i, a, o, s, c, l) {
273
311
  if (n <= 0 || r <= 0 || s <= 0 || c <= 0) return !1;
274
312
  let u = Math.cos(i), d = Math.sin(i), f = Math.cos(l), p = Math.sin(l), m = n / 2, h = r / 2, g = s / 2, _ = c / 2, v = a - e, y = o - t;
275
- return !F(u, d, v, y, m, h, u, d, g, _, f, p) && !F(-d, u, v, y, m, h, u, d, g, _, f, p) && !F(f, p, v, y, m, h, u, d, g, _, f, p) && !F(-p, f, v, y, m, h, u, d, g, _, f, p);
313
+ return V(u, d, v, y, m, h, u, d, g, _, f, p) > 0 && V(-d, u, v, y, m, h, u, d, g, _, f, p) > 0 && V(f, p, v, y, m, h, u, d, g, _, f, p) > 0 && V(-p, f, v, y, m, h, u, d, g, _, f, p) > 0;
276
314
  }
277
- function F(e, t, n, r, i, a, o, s, c, l, u, d) {
278
- let f = i * Math.abs(o * e + s * t) + a * Math.abs(-s * e + o * t), p = c * Math.abs(u * e + d * t) + l * Math.abs(-d * e + u * t);
279
- return Math.abs(n * e + r * t) >= f + p;
315
+ function V(e, t, n, r, i, a, o, s, c, l, u, d) {
316
+ return i * Math.abs(o * e + s * t) + a * Math.abs(-s * e + o * t) + (c * Math.abs(u * e + d * t) + l * Math.abs(-d * e + u * t)) - Math.abs(n * e + r * t);
280
317
  }
281
- function I(e, t, n, r, i, a, o, s) {
282
- let c = n - e, l = r - t, u = o - i, d = s - a, f = e - i, p = t - a, m = c * c + l * l, h = u * u + d * d, g = u * f + d * p, _ = 0, v = 0;
283
- if (m <= 0 && h <= 0) return f * f + p * p;
284
- if (m <= 0) v = B(g / h);
318
+ function H(e, t, n, r, i, a, o, s, c) {
319
+ let l = n - e, u = r - t, d = o - i, f = s - a, p = e - i, m = t - a, h = l * l + u * u, g = d * d + f * f, _ = d * p + f * m, v = 0, y = 0;
320
+ if (h <= 0 && g <= 0) return c && (c.x = p, c.y = m), p * p + m * m;
321
+ if (h <= 0) y = q(_ / g);
285
322
  else {
286
- let e = c * f + l * p;
287
- if (h <= 0) _ = B(-e / m);
323
+ let e = l * p + u * m;
324
+ if (g <= 0) v = q(-e / h);
288
325
  else {
289
- let t = c * u + l * d, n = m * h - t * t;
290
- n !== 0 && (_ = B((t * g - e * h) / n)), v = (t * _ + g) / h, v < 0 ? (v = 0, _ = B(-e / m)) : v > 1 && (v = 1, _ = B((t - e) / m));
326
+ let t = l * d + u * f, n = h * g - t * t;
327
+ n !== 0 && (v = q((t * _ - e * g) / n)), y = (t * v + _) / g, y < 0 ? (y = 0, v = q(-e / h)) : y > 1 && (y = 1, v = q((t - e) / h));
291
328
  }
292
329
  }
293
- let y = e + c * _ - (i + u * v), b = t + l * _ - (a + d * v);
294
- return y * y + b * b;
295
- }
296
- function L(e, t, n, r, i, a, o, s, c) {
297
- let l = Math.cos(c), u = Math.sin(c), d = o / 2, f = s / 2, p = e - i, m = t - a, h = n - i, g = r - a, _ = p * l + m * u, v = -p * u + m * l, y = h * l + g * u, b = -h * u + g * l;
298
- return xe(_, v, y, b, d, f) ? Math.min(z(_, v, d, f), z(y, b, d, f), R(-d, -f, _, v, y, b), R(d, -f, _, v, y, b), R(d, f, _, v, y, b), R(-d, f, _, v, y, b)) : 0;
299
- }
300
- function R(e, t, n, r, i, a) {
301
- let o = i - n, s = a - r, c = o * o + s * s, l = c > 0 ? B(((e - n) * o + (t - r) * s) / c) : 0, u = e - (n + o * l), d = t - (r + s * l);
302
- return u * u + d * d;
303
- }
304
- function xe(e, t, n, r, i, a) {
330
+ let b = e + l * v - (i + d * y), x = t + u * v - (a + f * y);
331
+ return c && (c.x = b, c.y = x), b * b + x * x;
332
+ }
333
+ function U(e, t, n, r, i, a, o, s, c, l) {
334
+ let u = Math.cos(c), d = Math.sin(c), f = o / 2, p = s / 2, m = e - i, h = t - a, g = n - i, _ = r - a, v = m * u + h * d, y = -m * d + h * u, b = g * u + _ * d, x = -g * d + _ * u;
335
+ if (!Te(v, y, b, x, f, p)) return l && (l.x = 0, l.y = 0), 0;
336
+ if (!l) return Math.min(K(v, y, f, p), K(b, x, f, p), G(-f, -p, v, y, b, x), G(f, -p, v, y, b, x), G(f, p, v, y, b, x), G(-f, p, v, y, b, x));
337
+ let S = K(v, y, f, p, l);
338
+ S = W(S, K(b, x, f, p, M), M, l, !1), S = W(S, G(-f, -p, v, y, b, x, M), M, l, !0), S = W(S, G(f, -p, v, y, b, x, M), M, l, !0), S = W(S, G(f, p, v, y, b, x, M), M, l, !0), S = W(S, G(-f, p, v, y, b, x, M), M, l, !0);
339
+ let C = l.x, w = l.y;
340
+ return l.x = C * u - w * d, l.y = C * d + w * u, S;
341
+ }
342
+ function W(e, t, n, r, i) {
343
+ return t >= e ? e : (r.x = i ? -n.x : n.x, r.y = i ? -n.y : n.y, t);
344
+ }
345
+ function G(e, t, n, r, i, a, o) {
346
+ let s = i - n, c = a - r, l = s * s + c * c, u = l > 0 ? q(((e - n) * s + (t - r) * c) / l) : 0, d = e - (n + s * u), f = t - (r + c * u);
347
+ return o && (o.x = d, o.y = f), d * d + f * f;
348
+ }
349
+ function Te(e, t, n, r, i, a) {
305
350
  if (Math.max(e, n) < -i || Math.min(e, n) > i || Math.max(t, r) < -a || Math.min(t, r) > a) return !0;
306
351
  let o = -(r - t), s = n - e;
307
352
  return Math.abs(o * e + s * t) > i * Math.abs(o) + a * Math.abs(s);
308
353
  }
309
- function z(e, t, n, r) {
310
- let i = Math.abs(e) - n, a = Math.abs(t) - r, o = i > 0 ? i : 0, s = a > 0 ? a : 0;
311
- return o * o + s * s;
354
+ function K(e, t, n, r, i) {
355
+ let a = Math.abs(e) - n, o = Math.abs(t) - r, s = a > 0 ? a : 0, c = o > 0 ? o : 0;
356
+ return i && (i.x = e < 0 ? -s : s, i.y = t < 0 ? -c : c), s * s + c * c;
312
357
  }
313
- function B(e) {
358
+ function q(e) {
314
359
  return e < 0 ? 0 : e > 1 ? 1 : e;
315
360
  }
316
361
  //#endregion
317
362
  //#region src/systems/collision.ts
318
- var V = 1e-4, Se = 24, H = "collidable", U = [], W = {
363
+ var Ee = 1e-4, De = 24, Oe = "collidable", J = [], ke = 2 ** 26, Ae = {
319
364
  fraction: 1,
320
- blocking: U
321
- }, G = class {
365
+ blocking: J
366
+ }, je = class {
322
367
  buckets = [];
368
+ hasBounciness = !1;
369
+ resolved = /* @__PURE__ */ new Set();
323
370
  constructor(t, n) {
324
371
  let r = /* @__PURE__ */ new Map();
325
372
  for (let e of t) {
@@ -328,8 +375,10 @@ var V = 1e-4, Se = 24, H = "collidable", U = [], W = {
328
375
  let o = a[1];
329
376
  if (o === 0) continue;
330
377
  let s = a[0], c = i[2], l = i[3];
331
- if (D(s, c, l)) continue;
332
- let u = i[4], d = O(s, c, l, u), f = k(s, c, l, u), p = t.velocity, m = p ? Math.abs(p[0]) * n : 0, h = p ? Math.abs(p[1]) * n : 0, g = i[0], _ = i[1], v = r.get(o);
378
+ if (F(s, c, l)) continue;
379
+ let u = i[4], d = I(s, c, l, u), f = L(s, c, l, u);
380
+ t.bounciness && (this.hasBounciness = !0);
381
+ let p = t.velocity, m = p ? Math.abs(p[0]) * n : 0, h = p ? Math.abs(p[1]) * n : 0, g = i[0], _ = i[1], v = r.get(o);
333
382
  v || (v = {
334
383
  entries: [],
335
384
  bounds: []
@@ -348,55 +397,59 @@ var V = 1e-4, Se = 24, H = "collidable", U = [], W = {
348
397
  });
349
398
  }
350
399
  }
400
+ claimContact(e, t) {
401
+ let n = e < t ? e * ke + t : t * ke + e;
402
+ return this.resolved.has(n) ? !1 : (this.resolved.add(n), !0);
403
+ }
351
404
  forEachOverlapping(e, t) {
352
- let n = K(e);
405
+ let n = Y(e);
353
406
  if (!n) return;
354
407
  let { x: r, y: i, halfWidth: a, halfHeight: o } = n;
355
408
  this.forEachCandidate(n, r - a, i - o, r + a, i + o, (e) => {
356
- q(n, r, i, e) && t(e);
409
+ X(n, r, i, e) && t(e);
357
410
  });
358
411
  }
359
412
  sweep(e, t, n) {
360
- let r = Math.sqrt(t * t + n * n), i = K(e);
413
+ let r = Math.sqrt(t * t + n * n), i = Y(e);
361
414
  if (!i || r === 0) return {
362
415
  fraction: 1,
363
- blocking: U
416
+ blocking: J
364
417
  };
365
418
  let a = this.gatherCandidates(i, t, n);
366
419
  return a.length === 0 ? {
367
420
  fraction: 1,
368
- blocking: U
421
+ blocking: J
369
422
  } : this.refine(i, a, t, n, r);
370
423
  }
371
424
  resolveMove(e, t, n, r) {
372
- let i = Math.sqrt(t * t + n * n), a = K(e);
425
+ let i = Math.sqrt(t * t + n * n), a = Y(e);
373
426
  if (!a || i === 0) return {
374
427
  moveX: t,
375
428
  moveY: n,
376
- blocking: U
429
+ blocking: J
377
430
  };
378
431
  let o = this.gatherCandidates(a, t, n);
379
432
  if (o.length === 0) return {
380
433
  moveX: t,
381
434
  moveY: n,
382
- blocking: U
435
+ blocking: J
383
436
  };
384
437
  if (r && t !== 0 && n !== 0) {
385
438
  let e = this.gatherCandidates(a, t, 0), r = this.gatherCandidates(a, 0, n);
386
439
  if (e.length === 0 && r.length === 0) return Math.abs(t) >= Math.abs(n) ? {
387
440
  moveX: t,
388
441
  moveY: 0,
389
- blocking: U
442
+ blocking: J
390
443
  } : {
391
444
  moveX: 0,
392
445
  moveY: n,
393
- blocking: U
446
+ blocking: J
394
447
  };
395
- let i = e.length === 0 ? W : this.refine(a, e, t, 0, Math.abs(t)), o = r.length === 0 ? W : this.refine(a, r, 0, n, Math.abs(n));
448
+ let i = e.length === 0 ? Ae : this.refine(a, e, t, 0, Math.abs(t)), o = r.length === 0 ? Ae : this.refine(a, r, 0, n, Math.abs(n));
396
449
  return {
397
450
  moveX: t * i.fraction,
398
451
  moveY: n * o.fraction,
399
- blocking: Ce(i.blocking, o.blocking)
452
+ blocking: Me(i.blocking, o.blocking)
400
453
  };
401
454
  }
402
455
  let s = this.refine(a, o, t, n, i);
@@ -411,48 +464,48 @@ var V = 1e-4, Se = 24, H = "collidable", U = [], W = {
411
464
  if (e.sensor) return [];
412
465
  let s = r + t, c = i + n, l = [];
413
466
  return this.forEachCandidate(e, s - a, c - o, s + a, c + o, (t) => {
414
- q(e, s, c, t) && t.components.body[3] === 0 && (q(e, r, i, t) || l.push(t));
467
+ X(e, s, c, t) && t.components.body[3] === 0 && (X(e, r, i, t) || l.push(t));
415
468
  }), l;
416
469
  }
417
470
  refine(e, t, n, r, i) {
418
- let { x: a, y: o } = e, s = 0, c = 1, l = V / i;
419
- for (let i = 0; i < Se && c - s > l; i++) {
471
+ let { x: a, y: o } = e, s = 0, c = 1, l = Ee / i;
472
+ for (let i = 0; i < De && c - s > l; i++) {
420
473
  let i = (s + c) / 2;
421
474
  this.blockedAt(e, t, n, r, i) ? c = i : s = i;
422
475
  }
423
476
  let u = [], d = a + n * c, f = o + r * c;
424
- for (let n of t) q(e, d, f, n) && u.push(n);
477
+ for (let n of t) X(e, d, f, n) && u.push(n);
425
478
  return {
426
- fraction: s * i > V ? s : 0,
479
+ fraction: s * i > Ee ? s : 0,
427
480
  blocking: u
428
481
  };
429
482
  }
430
483
  blockedAt(e, t, n, r, i) {
431
484
  let a = e.x + n * i, o = e.y + r * i;
432
- for (let n of t) if (q(e, a, o, n)) return !0;
485
+ for (let n of t) if (X(e, a, o, n)) return !0;
433
486
  return !1;
434
487
  }
435
- forEachCandidate(e, t, n, r, i, a) {
436
- for (let o of this.buckets) if ((e.mask & o.category) !== 0) for (let s of o.index.search(t, n, r, i)) {
437
- let t = o.entries[s];
438
- t.entityId !== e.entityId && (t.components.body[2] & e.category) !== 0 && a(t);
488
+ forEachCandidate(e, t, r, i, a, o) {
489
+ for (let s of this.buckets) if ((e.mask & s.category) !== 0) for (let c of s.index.search(t, r, i, a)) {
490
+ let t = s.entries[c];
491
+ t.entityId !== e.entityId && (t.components.body[2] & e.category) !== 0 && t.components.entity?.[n] !== 1 && o(t);
439
492
  }
440
493
  }
441
494
  };
442
- function Ce(e, t) {
495
+ function Me(e, t) {
443
496
  if (e.length === 0) return t;
444
497
  if (t.length === 0) return e;
445
498
  let n = [...e];
446
499
  for (let e of t) n.some((t) => t.entityId === e.entityId) || n.push(e);
447
500
  return n;
448
501
  }
449
- function K(e) {
502
+ function Y(e) {
450
503
  let t = e.components.body;
451
504
  if (!t) return;
452
505
  let n = t[2];
453
506
  if (n === 0) return;
454
507
  let r = e.components.transform, i = t[0], a = r[2], o = r[3];
455
- if (D(i, a, o)) return;
508
+ if (F(i, a, o)) return;
456
509
  let s = r[4];
457
510
  return {
458
511
  entityId: e.entityId,
@@ -462,20 +515,20 @@ function K(e) {
462
515
  width: a,
463
516
  height: o,
464
517
  angle: s,
465
- halfWidth: O(i, a, o, s),
466
- halfHeight: k(i, a, o, s),
518
+ halfWidth: I(i, a, o, s),
519
+ halfHeight: L(i, a, o, s),
467
520
  category: t[1],
468
521
  mask: n,
469
522
  sensor: t[3] !== 0
470
523
  };
471
524
  }
472
- function q(e, t, n, r) {
525
+ function X(e, t, n, r) {
473
526
  let i = r.components.transform;
474
- return A(e.shape, t, n, e.width, e.height, e.angle, r.components.body[0], i[0], i[1], i[2], i[3], i[4]);
527
+ return R(e.shape, t, n, e.width, e.height, e.angle, r.components.body[0], i[0], i[1], i[2], i[3], i[4]);
475
528
  }
476
529
  //#endregion
477
530
  //#region src/systems/spatial-index.ts
478
- var we = class {
531
+ var Ne = class {
479
532
  entries = [];
480
533
  index;
481
534
  constructor(t) {
@@ -483,7 +536,7 @@ var we = class {
483
536
  for (let e of t) {
484
537
  let t = e.components, r = t.transform;
485
538
  if (!r) continue;
486
- let i = t.body, a = i ? i[0] : 1, o = r[2], s = r[3], c = r[4], l = r[0], u = r[1], d = O(a, o, s, c), f = k(a, o, s, c);
539
+ let i = t.body, a = i ? i[0] : 1, o = r[2], s = r[3], c = r[4], l = r[0], u = r[1], d = I(a, o, s, c), f = L(a, o, s, c);
487
540
  this.entries.push({
488
541
  entityId: e.entityId,
489
542
  components: t
@@ -515,96 +568,101 @@ var we = class {
515
568
  let o = this.entries;
516
569
  return (i ? a.neighbors(e, t, n, r, (e) => i(o[e])) : a.neighbors(e, t, n, r)).map((e) => o[e]);
517
570
  }
518
- }, J = {
519
- x: 0,
520
- y: 0
521
- }, Y = {
522
- x: 0,
523
- y: 0
524
- }, X = {
571
+ }, Z = {
525
572
  x: 0,
526
573
  y: 0
527
574
  };
528
- function Z(e, t) {
529
- let n = e.components.bounciness;
530
- if (!n || De(e.components.body) || De(t.components.body) || !Te(e, t, J)) return;
531
- let r = e.components.velocity, i = r[0], a = r[1], o = i * J.x + a * J.y;
532
- if (o >= 0) return;
533
- let s = (1 + n[0]) * o;
534
- r[0] = i - s * J.x, r[1] = a - s * J.y;
535
- }
536
- function Te(e, t, n) {
537
- let r = e.components.transform, i = t.components.transform, a = r[0] - i[0], o = r[1] - i[1], s = Ee(e.components.body), c = Ee(t.components.body);
538
- if (s === 2 && c === 2) {
539
- let e = Math.sqrt(a * a + o * o);
540
- return e === 0 ? !1 : (n.x = a / e, n.y = o / e, !0);
541
- }
542
- return Oe(r, s, Y), Oe(i, c, X), Y.x + X.x - Math.abs(a) < Y.y + X.y - Math.abs(o) ? (n.x = a < 0 ? -1 : 1, n.y = 0) : (n.x = 0, n.y = o < 0 ? -1 : 1), !0;
575
+ function Pe(e, t) {
576
+ !Ie(e) && !Ie(t) || Le(e, t) && Re(e, t, Z) && (Fe(e, Z.x, Z.y), Fe(t, -Z.x, -Z.y));
577
+ }
578
+ function Fe(e, t, n) {
579
+ let r = e.components.bounciness, i = e.components.velocity;
580
+ if (!r || !i) return;
581
+ let a = i[0], o = i[1], s = a * t + o * n;
582
+ if (s >= 0) return;
583
+ let c = (1 + r[0]) * s;
584
+ i[0] = a - c * t, i[1] = o - c * n;
585
+ }
586
+ function Ie(e) {
587
+ return e.components.bounciness !== void 0 && e.components.velocity !== void 0;
543
588
  }
544
- function Ee(e) {
589
+ function Le(e, t) {
590
+ return !Be(e.components.body) && !Be(t.components.body);
591
+ }
592
+ function Re(e, t, n) {
593
+ let r = e.components.transform, i = t.components.transform;
594
+ return ge(ze(e.components.body), r[0], r[1], r[2], r[3], r[4], ze(t.components.body), i[0], i[1], i[2], i[3], i[4], n);
595
+ }
596
+ function ze(e) {
545
597
  return e ? e[0] : 1;
546
598
  }
547
- function De(e) {
599
+ function Be(e) {
548
600
  return e !== void 0 && e[3] !== 0;
549
601
  }
550
- function Oe(e, t, n) {
551
- let r = e[2], i = e[3], a = e[4];
552
- n.x = O(t, r, i, a), n.y = k(t, r, i, a);
553
- }
554
602
  //#endregion
555
603
  //#region src/systems/physics-update.ts
556
- var ke = "position-updated";
557
- function Ae(e = {}) {
558
- let { onCollision: t, optional: n = [] } = e, r, i = Object.assign((e, n, i, a, o) => {
559
- let s = {
560
- entityId: n,
561
- components: i
562
- }, c = i.interpolation, l = e.elapsedTime / 1e3, u = i.velocity[0] * l, d = i.velocity[1] * l;
563
- if (Me(c, i.transform, e.elapsedTime), !r) {
564
- $(n, i.transform, u, d, o, e.reportMoves), Q(c, e.tick);
604
+ var Ve = "position-updated";
605
+ function He(e = {}) {
606
+ let { onCollision: t, optional: r = [] } = e, i, a = Object.assign((e, r, a, o, s) => {
607
+ if (a.entity?.[n] === 1) return;
608
+ let c = {
609
+ entityId: r,
610
+ components: a
611
+ }, l = a.interpolation, u = e.elapsedTime / 1e3, d = a.velocity[0] * u, f = a.velocity[1] * u;
612
+ Ge(l, a.transform, e.elapsedTime);
613
+ let p = i;
614
+ if (!p) {
615
+ $(r, a.transform, d, f, s, e.reportMoves), Q(l, e.tick);
565
616
  return;
566
617
  }
567
- let f = i.bounciness !== void 0, p = r.resolveMove(s, u, d, !f);
568
- if ($(n, i.transform, p.moveX, p.moveY, o, e.reportMoves), Q(c, e.tick), !(!f && !t)) {
569
- r.forEachOverlapping(s, (n) => {
570
- f && Z(s, n), t && t(e, s, n, a, o);
618
+ let m = a.bounciness !== void 0, h = p.resolveMove(c, d, f, !m);
619
+ if ($(r, a.transform, h.moveX, h.moveY, s, e.reportMoves), Q(l, e.tick), !(!p.hasBounciness && !t)) {
620
+ p.forEachOverlapping(c, (n) => {
621
+ p.claimContact(r, n.entityId) && Ue(e, c, n, o, s, t);
571
622
  });
572
- for (let n of p.blocking) f && Z(s, n), t && t(e, s, n, a, o);
623
+ for (let n of h.blocking) p.claimContact(r, n.entityId) && Ue(e, c, n, o, s, t);
573
624
  }
574
625
  }, { physics: {
575
626
  collision: !0,
576
- optional: n
627
+ optional: r
577
628
  } });
578
- return i.preRun = (e, t, n) => {
579
- r = new G(n.collidable ?? [], e.elapsedTime / 1e3);
580
- }, i;
629
+ return a.preRun = (e, t, n) => {
630
+ i = new je(n.collidable ?? [], e.elapsedTime / 1e3);
631
+ }, a;
581
632
  }
582
- var je = (e, t, n, r, i) => {
633
+ function Ue(e, t, n, r, i, a) {
634
+ Pe(t, n), a && a(e, t, n, r, i);
635
+ }
636
+ function We(e, t, n, r, i) {
583
637
  let a = e.elapsedTime / 1e3, o = n.interpolation;
584
- Me(o, n.transform, e.elapsedTime), $(t, n.transform, n.velocity[0] * a, n.velocity[1] * a, i, e.reportMoves), Q(o, e.tick);
585
- };
586
- function Me(e, t, n) {
638
+ Ge(o, n.transform, e.elapsedTime), $(t, n.transform, n.velocity[0] * a, n.velocity[1] * a, i, e.reportMoves), Q(o, e.tick);
639
+ }
640
+ function Ge(e, t, n) {
587
641
  e && (e[2] = t[0], e[3] = t[1], e[6] = n);
588
642
  }
589
643
  function Q(e, t) {
590
- e && r(e, 7, t);
644
+ e && a(e, 7, t);
591
645
  }
592
- function $(e, n, r, i, a, o) {
593
- r === 0 && i === 0 || (r !== 0 && t(n, 0, r, Infinity), i !== 0 && t(n, 1, i, Infinity), o !== !1 && a.emitSystemEvent(ke, e));
646
+ function $(e, t, n, i, a, o) {
647
+ n === 0 && i === 0 || (n !== 0 && r(t, 0, n, Infinity), i !== 0 && r(t, 1, i, Infinity), o !== !1 && a.emitSystemEvent(Ve, e));
594
648
  }
595
649
  //#endregion
596
650
  //#region src/systems/physics-system.ts
597
- var Ne = 50, Pe = class extends i {
651
+ var Ke = 50, qe = class extends t {
598
652
  tick = 0;
599
653
  reportMoves;
600
654
  constructor(e, t = {}) {
601
- let n = t.updateFunction ?? je, r = t.optional ?? n.physics?.optional ?? [], i = t.collision ?? n.physics?.collision ?? !1, a = [];
602
- i && !r.includes("body") && a.push("body"), i && !r.includes("bounciness") && a.push("bounciness"), r.includes("interpolation") || a.push("interpolation");
603
- let o = a.length ? [...a, ...r] : r, s = i ? { [H]: {
655
+ let n = t.updateFunction ?? We, r = t.optional ?? n.physics?.optional ?? [], i = t.collision ?? n.physics?.collision ?? !1, a = [];
656
+ i && !r.includes("body") && a.push("body"), i && !r.includes("bounciness") && a.push("bounciness"), r.includes("interpolation") || a.push("interpolation"), i && a.push("entity");
657
+ let o = a.length ? [...a, ...r] : r, s = r.includes("bounciness") ? r : ["bounciness", ...r], c = i ? { [Oe]: {
604
658
  required: ["transform", "body"],
605
- optional: ["velocity", ...r]
606
- } } : void 0, c = s || t.queries ? {
607
- ...s,
659
+ optional: [
660
+ "velocity",
661
+ "entity",
662
+ ...s
663
+ ]
664
+ } } : void 0, l = c || t.queries ? {
665
+ ...c,
608
666
  ...t.queries
609
667
  } : void 0;
610
668
  super(e, {
@@ -615,7 +673,7 @@ var Ne = 50, Pe = class extends i {
615
673
  optional: o,
616
674
  filter: t.filter,
617
675
  updateFunction: n,
618
- queries: c,
676
+ queries: l,
619
677
  forceMainThread: t.forceMainThread ?? !t.getWorker,
620
678
  getWorker: t.getWorker ?? (() => {
621
679
  throw Error("PhysicsSystem cannot start a worker: no getWorker was supplied");
@@ -625,23 +683,23 @@ var Ne = 50, Pe = class extends i {
625
683
  addDataToWorld(e) {
626
684
  e.tick = ++this.tick, e.reportMoves = this.reportMoves ?? this.listenerCount("position-updated") > 0;
627
685
  }
628
- }, Fe = (e, t, r) => {
629
- let i = r.interpolation, a = r.transform, o = n(i, 7), s = i[2], c = i[3], l = i[6], u = a[0], d = a[1], f = n(i, 7);
686
+ }, Je = (e, t, n) => {
687
+ let r = n.interpolation, a = n.transform, o = i(r, 7), s = r[2], c = r[3], l = r[6], u = a[0], d = a[1], f = i(r, 7);
630
688
  if (o !== f) return;
631
689
  if (l <= 0) {
632
- i[4] = 1, i[5] = f, i[0] = u, i[1] = d;
690
+ r[4] = 1, r[5] = f, r[0] = u, r[1] = d;
633
691
  return;
634
692
  }
635
- let p = i[4];
636
- f !== i[5] && (p = 0, i[5] = f), p += e.elapsedTime / l, p > 1 && (p = 1), i[4] = p, i[0] = s + (u - s) * p, i[1] = c + (d - c) * p;
637
- }, Ie = class extends i {
693
+ let p = r[4];
694
+ f !== r[5] && (p = 0, r[5] = f), p += e.elapsedTime / l, p > 1 && (p = 1), r[4] = p, r[0] = s + (u - s) * p, r[1] = c + (d - c) * p;
695
+ }, Ye = class extends t {
638
696
  constructor(e, t = {}) {
639
697
  super(e, {
640
698
  name: t.name ?? "InterpolationSystem",
641
699
  deltaBetweenRuns: 0,
642
700
  firstRun: t.firstRun,
643
701
  required: ["transform", "interpolation"],
644
- updateFunction: Fe,
702
+ updateFunction: Je,
645
703
  forceMainThread: t.forceMainThread ?? !t.getWorker,
646
704
  getWorker: t.getWorker ?? (() => {
647
705
  throw Error("InterpolationSystem cannot start a worker: no getWorker was supplied");
@@ -650,6 +708,6 @@ var Ne = 50, Pe = class extends i {
650
708
  }
651
709
  };
652
710
  //#endregion
653
- export { ne as BODY_CATEGORY_INDEX, re as BODY_MASK_INDEX, ie as BODY_SENSOR_INDEX, te as BODY_SHAPE_INDEX, ae as BODY_SIZE, ce as BOUNCINESS_INDEX, le as BOUNCINESS_SIZE, H as COLLIDABLE_QUERY, G as CollisionBroadphase, b as DEFAULT_COLLIDE_CATEGORY, x as DEFAULT_COLLIDE_MASK, Ne as DEFAULT_PHYSICS_STEP_MS, ge as INTERPOLATION_DURATION_INDEX, fe as INTERPOLATION_PREV_X_INDEX, pe as INTERPOLATION_PREV_Y_INDEX, me as INTERPOLATION_PROGRESS_INDEX, ve as INTERPOLATION_SIZE, he as INTERPOLATION_SYNCED_TICK_INDEX, _e as INTERPOLATION_TICK_INDEX, ue as INTERPOLATION_X_INDEX, de as INTERPOLATION_Y_INDEX, Ie as InterpolationSystem, ke as POSITION_UPDATED_EVENT, Pe as PhysicsSystem, v as SHAPE_CAPSULE, _ as SHAPE_CIRCLE, g as SHAPE_RECTANGLE, we as SpatialIndex, l as TRANSFORM_ANGLE_INDEX, c as TRANSFORM_HEIGHT_INDEX, u as TRANSFORM_SIZE, s as TRANSFORM_WIDTH_INDEX, a as TRANSFORM_X_INDEX, o as TRANSFORM_Y_INDEX, m as VELOCITY_SIZE, f as VELOCITY_X_INDEX, p as VELOCITY_Y_INDEX, S as bodyDefinition, C as bouncinessDefinition, N as boundsHalfHeight, M as boundsHalfWidth, ee as canCollide, T as capsuleHalfLength, Ae as createPhysicsUpdate, w as interpolationDefinition, Fe as interpolationUpdate, oe as isSensor, P as orientedBoxesOverlap, be as physicsRegistry, je as physicsUpdate, R as pointSegmentDistanceSquared, L as segmentBoxDistanceSquared, I as segmentSegmentDistanceSquared, k as shapeHalfHeight, O as shapeHalfWidth, D as shapeIsEmpty, E as shapeRadius, A as shapesOverlap, ye as snapEntity, d as transformDefinition, h as velocityDefinition };
711
+ export { T as BODY_CATEGORY_INDEX, E as BODY_MASK_INDEX, D as BODY_SENSOR_INDEX, w as BODY_SHAPE_INDEX, O as BODY_SIZE, te as BOUNCINESS_INDEX, ne as BOUNCINESS_SIZE, Oe as COLLIDABLE_QUERY, je as CollisionBroadphase, x as DEFAULT_COLLIDE_CATEGORY, S as DEFAULT_COLLIDE_MASK, Ke as DEFAULT_PHYSICS_STEP_MS, ue as INTERPOLATION_DURATION_INDEX, oe as INTERPOLATION_PREV_X_INDEX, se as INTERPOLATION_PREV_Y_INDEX, ce as INTERPOLATION_PROGRESS_INDEX, fe as INTERPOLATION_SIZE, le as INTERPOLATION_SYNCED_TICK_INDEX, de as INTERPOLATION_TICK_INDEX, ie as INTERPOLATION_X_INDEX, ae as INTERPOLATION_Y_INDEX, Ye as InterpolationSystem, Ve as POSITION_UPDATED_EVENT, qe as PhysicsSystem, y as SHAPE_CAPSULE, v as SHAPE_CIRCLE, _ as SHAPE_RECTANGLE, Ne as SpatialIndex, u as TRANSFORM_ANGLE_INDEX, l as TRANSFORM_HEIGHT_INDEX, d as TRANSFORM_SIZE, c as TRANSFORM_WIDTH_INDEX, o as TRANSFORM_X_INDEX, s as TRANSFORM_Y_INDEX, h as VELOCITY_SIZE, p as VELOCITY_X_INDEX, m as VELOCITY_Y_INDEX, A as bodyDefinition, re as bouncinessDefinition, Ce as boundsHalfHeight, Se as boundsHalfWidth, C as canCollide, N as capsuleHalfLength, ge as contactNormal, He as createPhysicsUpdate, pe as interpolationDefinition, Je as interpolationUpdate, k as isSensor, we as orientedBoxesOverlap, he as physicsRegistry, We as physicsUpdate, G as pointSegmentDistanceSquared, U as segmentBoxDistanceSquared, H as segmentSegmentDistanceSquared, L as shapeHalfHeight, I as shapeHalfWidth, F as shapeIsEmpty, P as shapeRadius, R as shapesOverlap, me as snapEntity, f as transformDefinition, g as velocityDefinition };
654
712
 
655
713
  //# sourceMappingURL=index.js.map