@daneren2005/shared-memory-physics 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,6 +5,8 @@
5
5
  lives in the ECS's shared-memory blocks, so the simulation can run on a worker thread while the main thread
6
6
  reads positions straight off its entities.
7
7
 
8
+ Examples at https://daneren2005.github.io/shared-memory-physics/
9
+
8
10
  The ECS (`>=1.1.1`) and the shared-memory primitives it is built on are **peer dependencies**: a game must
9
11
  register these components against the same copy of the ECS it builds its world with.
10
12
 
@@ -4,6 +4,7 @@ export interface BodyComponent {
4
4
  shape: number;
5
5
  collideCategory: number;
6
6
  collideMask: number;
7
+ sensor: boolean;
7
8
  }
8
9
  export declare const SHAPE_RECTANGLE = 1;
9
10
  export declare const SHAPE_CIRCLE = 2;
@@ -18,10 +19,13 @@ export interface BodyConfig {
18
19
  radius?: number;
19
20
  collideCategory?: number;
20
21
  collideMask?: number;
22
+ sensor?: boolean;
21
23
  }
22
24
  export declare const BODY_SHAPE_INDEX = 0;
23
25
  export declare const BODY_CATEGORY_INDEX = 1;
24
26
  export declare const BODY_MASK_INDEX = 2;
25
- export declare const BODY_SIZE = 3;
27
+ export declare const BODY_SENSOR_INDEX = 3;
28
+ export declare const BODY_SIZE = 4;
29
+ export declare function isSensor(body: Uint32Array): boolean;
26
30
  export declare const bodyDefinition: ComponentDefinition<BodyComponent, Uint32Array, BodyConfig>;
27
31
  export default bodyDefinition;
@@ -0,0 +1,12 @@
1
+ import type { ComponentDefinition } from '@daneren2005/shared-memory-ecs';
2
+ export interface BouncinessComponent {
3
+ index: number;
4
+ bounciness: number;
5
+ }
6
+ export interface BouncinessConfig {
7
+ bounciness?: number;
8
+ }
9
+ export declare const BOUNCINESS_INDEX = 0;
10
+ export declare const BOUNCINESS_SIZE = 1;
11
+ export declare const bouncinessDefinition: ComponentDefinition<BouncinessComponent, Float32Array, BouncinessConfig>;
12
+ export default bouncinessDefinition;
@@ -1,4 +1,5 @@
1
1
  import { type BodyComponent } from './body-component';
2
+ import { type BouncinessComponent } from './bounciness-component';
2
3
  import { type InterpolationComponent } from './interpolation-component';
3
4
  import { type TransformComponent } from './transform-component';
4
5
  import { type VelocityComponent } from './velocity-component';
@@ -6,12 +7,14 @@ export declare const physicsRegistry: {
6
7
  transform: import("@daneren2005/shared-memory-ecs").ComponentDefinition<TransformComponent, Float32Array<ArrayBufferLike>, import("./transform-component").TransformConfig, import("./transform-component").TransformSerialization>;
7
8
  velocity: import("@daneren2005/shared-memory-ecs").ComponentDefinition<VelocityComponent, Float32Array<ArrayBufferLike>, import("./velocity-component").VelocityConfig, import("./velocity-component").VelocityConfig>;
8
9
  body: import("@daneren2005/shared-memory-ecs").ComponentDefinition<BodyComponent, Uint32Array<ArrayBufferLike>, import("./body-component").BodyConfig, object>;
10
+ bounciness: import("@daneren2005/shared-memory-ecs").ComponentDefinition<BouncinessComponent, Float32Array<ArrayBufferLike>, import("./bounciness-component").BouncinessConfig, object>;
9
11
  interpolation: import("@daneren2005/shared-memory-ecs").ComponentDefinition<InterpolationComponent, Float32Array<ArrayBufferLike>, import("./interpolation-component").InterpolationConfig, object>;
10
12
  };
11
13
  export type PhysicsComponents = {
12
14
  transform: TransformComponent;
13
15
  velocity: VelocityComponent;
14
16
  body: BodyComponent;
17
+ bounciness: BouncinessComponent;
15
18
  interpolation: InterpolationComponent;
16
19
  };
17
20
  export type InterpolationComponents = {
@@ -22,6 +25,7 @@ export type PhysicsUpdateComponents = {
22
25
  transform: Float32Array;
23
26
  velocity: Float32Array;
24
27
  body?: Uint32Array;
28
+ bounciness?: Float32Array;
25
29
  interpolation?: Float32Array;
26
30
  };
27
31
  export type InterpolationUpdateComponents = {
package/dist/index.d.ts CHANGED
@@ -4,9 +4,12 @@ export type { TransformComponent, TransformConfig, TransformSerialization, } fro
4
4
  export { default as velocityDefinition } from './components/velocity-component';
5
5
  export { VELOCITY_X_INDEX, VELOCITY_Y_INDEX, VELOCITY_SIZE, } from './components/velocity-component';
6
6
  export type { VelocityComponent, VelocityConfig, VelocitySerialization, } from './components/velocity-component';
7
- export { default as bodyDefinition, canCollide } from './components/body-component';
8
- export { SHAPE_RECTANGLE, SHAPE_CIRCLE, SHAPE_CAPSULE, DEFAULT_COLLIDE_CATEGORY, DEFAULT_COLLIDE_MASK, BODY_SHAPE_INDEX, BODY_CATEGORY_INDEX, BODY_MASK_INDEX, BODY_SIZE, } from './components/body-component';
7
+ export { default as bodyDefinition, canCollide, isSensor } from './components/body-component';
8
+ export { SHAPE_RECTANGLE, SHAPE_CIRCLE, SHAPE_CAPSULE, DEFAULT_COLLIDE_CATEGORY, DEFAULT_COLLIDE_MASK, BODY_SHAPE_INDEX, BODY_CATEGORY_INDEX, BODY_MASK_INDEX, BODY_SENSOR_INDEX, BODY_SIZE, } from './components/body-component';
9
9
  export type { BodyComponent, BodyConfig, } from './components/body-component';
10
+ export { default as bouncinessDefinition } from './components/bounciness-component';
11
+ export { BOUNCINESS_INDEX, BOUNCINESS_SIZE, } from './components/bounciness-component';
12
+ export type { BouncinessComponent, BouncinessConfig, } from './components/bounciness-component';
10
13
  export { default as interpolationDefinition, snapEntity } from './components/interpolation-component';
11
14
  export { INTERPOLATION_X_INDEX, INTERPOLATION_Y_INDEX, INTERPOLATION_PREV_X_INDEX, INTERPOLATION_PREV_Y_INDEX, INTERPOLATION_PROGRESS_INDEX, INTERPOLATION_SYNCED_TICK_INDEX, INTERPOLATION_DURATION_INDEX, INTERPOLATION_TICK_INDEX, INTERPOLATION_SIZE, } from './components/interpolation-component';
12
15
  export type { InterpolationComponent, InterpolationConfig, SnappableEntity, } from './components/interpolation-component';
@@ -14,7 +17,7 @@ export { physicsRegistry } from './components/registry';
14
17
  export type { PhysicsComponents, PhysicsUpdateComponents, InterpolationComponents, InterpolationUpdateComponents, } from './components/registry';
15
18
  export { shapesOverlap, shapeHalfWidth, shapeHalfHeight, shapeIsEmpty, shapeRadius, capsuleHalfLength, boundsHalfWidth, boundsHalfHeight, orientedBoxesOverlap, segmentSegmentDistanceSquared, segmentBoxDistanceSquared, pointSegmentDistanceSquared, } from './math/shapes';
16
19
  export { default as CollisionBroadphase, COLLIDABLE_QUERY } from './systems/collision';
17
- export type { CollisionComponents, CollisionEntity, CollisionFunction, MovingEntity, SweepResult } from './systems/collision';
20
+ export type { CollisionComponents, CollisionEntity, CollisionFunction, MoveResult, MovingEntity, SweepResult } from './systems/collision';
18
21
  export { default as SpatialIndex } from './systems/spatial-index';
19
22
  export type { SpatialComponents, SpatialEntity, SpatialFilter } from './systems/spatial-index';
20
23
  export { default as physicsUpdate, createPhysicsUpdate, POSITION_UPDATED_EVENT } from './systems/physics-update';
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import e from "flatbush";
2
- import { addAtomicFloat32 as t, loadFloat32 as n, storeFloat32 as r } from "@daneren2005/shared-memory-objects";
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";
3
4
  import { ComponentSystem as i } from "@daneren2005/shared-memory-ecs";
4
5
  //#region src/components/transform-component.ts
5
6
  var a = 0, o = 1, s = 2, c = 3, l = 4, u = 5, d = {
@@ -99,22 +100,28 @@ var a = 0, o = 1, s = 2, c = 3, l = 4, u = 5, d = {
99
100
  function ee(e, t, n, r) {
100
101
  return (t & n) !== 0 && (r & e) !== 0;
101
102
  }
102
- var te = 0, ne = 1, re = 2, ie = 3, S = {
103
+ var te = 0, ne = 1, re = 2, ie = 3, ae = 4;
104
+ function oe(e) {
105
+ return e[3] !== 0;
106
+ }
107
+ var S = {
103
108
  type: Uint32Array,
104
- size: 3,
109
+ size: 4,
105
110
  loadProperties: [
106
111
  "width",
107
112
  "height",
108
113
  "radius",
109
114
  "shape",
110
115
  "collideCategory",
111
- "collideMask"
116
+ "collideMask",
117
+ "sensor"
112
118
  ],
113
119
  load(e, t, n) {
114
120
  let r = t.create([
115
- ae(n),
121
+ se(n),
116
122
  n.collideCategory ?? 1,
117
- n.collideMask ?? 4294967295
123
+ n.collideMask ?? 4294967295,
124
+ +!!n.sensor
118
125
  ]), i = t.getBlock(r);
119
126
  return {
120
127
  index: r,
@@ -135,19 +142,41 @@ var te = 0, ne = 1, re = 2, ie = 3, S = {
135
142
  },
136
143
  set collideMask(e) {
137
144
  i[2] = e;
145
+ },
146
+ get sensor() {
147
+ return i[3] !== 0;
148
+ },
149
+ set sensor(e) {
150
+ i[3] = +!!e;
138
151
  }
139
152
  };
140
153
  }
141
154
  };
142
- function ae(e) {
155
+ function se(e) {
143
156
  let t = e.shape;
144
157
  if (t === void 0) return e.radius === void 0 ? 1 : 2;
145
158
  if (!y.includes(t)) throw Error(`Unknown body shape: ${t}`);
146
159
  return t;
147
160
  }
148
161
  //#endregion
149
- //#region src/components/interpolation-component.ts
150
- var oe = 0, se = 1, ce = 2, le = 3, ue = 4, de = 5, fe = 6, C = 7, pe = 8, w = {
162
+ //#region src/components/bounciness-component.ts
163
+ var ce = 0, le = 1, C = {
164
+ type: Float32Array,
165
+ size: 1,
166
+ loadProperties: ["bounciness"],
167
+ load(e, t, n) {
168
+ let r = t.create([n.bounciness ?? 0]), i = t.getBlock(r);
169
+ return {
170
+ index: r,
171
+ get bounciness() {
172
+ return i[0];
173
+ },
174
+ set bounciness(e) {
175
+ i[0] = e;
176
+ }
177
+ };
178
+ }
179
+ }, ue = 0, de = 1, fe = 2, pe = 3, me = 4, he = 5, ge = 6, _e = 7, ve = 8, w = {
151
180
  type: Float32Array,
152
181
  size: 8,
153
182
  loadProperties: ["interpolate"],
@@ -191,16 +220,17 @@ var oe = 0, se = 1, ce = 2, le = 3, ue = 4, de = 5, fe = 6, C = 7, pe = 8, w = {
191
220
  };
192
221
  }
193
222
  };
194
- function me(e) {
223
+ function ye(e) {
195
224
  let { transform: t, interpolation: n } = e.components;
196
225
  !t || !n || (n.prevX = n.x = t.x, n.prevY = n.y = t.y);
197
226
  }
198
227
  //#endregion
199
228
  //#region src/components/registry.ts
200
- var he = {
229
+ var be = {
201
230
  transform: d,
202
231
  velocity: h,
203
232
  body: S,
233
+ bounciness: C,
204
234
  interpolation: w
205
235
  };
206
236
  //#endregion
@@ -251,13 +281,13 @@ function F(e, t, n, r, i, a, o, s, c, l, u, d) {
251
281
  function I(e, t, n, r, i, a, o, s) {
252
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;
253
283
  if (m <= 0 && h <= 0) return f * f + p * p;
254
- if (m <= 0) v = V(g / h);
284
+ if (m <= 0) v = B(g / h);
255
285
  else {
256
286
  let e = c * f + l * p;
257
- if (h <= 0) _ = V(-e / m);
287
+ if (h <= 0) _ = B(-e / m);
258
288
  else {
259
289
  let t = c * u + l * d, n = m * h - t * t;
260
- n !== 0 && (_ = V((t * g - e * h) / n)), v = (t * _ + g) / h, v < 0 ? (v = 0, _ = V(-e / m)) : v > 1 && (v = 1, _ = V((t - e) / m));
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));
261
291
  }
262
292
  }
263
293
  let y = e + c * _ - (i + u * v), b = t + l * _ - (a + d * v);
@@ -265,27 +295,30 @@ function I(e, t, n, r, i, a, o, s) {
265
295
  }
266
296
  function L(e, t, n, r, i, a, o, s, c) {
267
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;
268
- return z(_, v, y, b, d, f) ? Math.min(B(_, v, d, f), B(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;
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;
269
299
  }
270
300
  function R(e, t, n, r, i, a) {
271
- let o = i - n, s = a - r, c = o * o + s * s, l = c > 0 ? V(((e - n) * o + (t - r) * s) / c) : 0, u = e - (n + o * l), d = t - (r + s * l);
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);
272
302
  return u * u + d * d;
273
303
  }
274
- function z(e, t, n, r, i, a) {
304
+ function xe(e, t, n, r, i, a) {
275
305
  if (Math.max(e, n) < -i || Math.min(e, n) > i || Math.max(t, r) < -a || Math.min(t, r) > a) return !0;
276
306
  let o = -(r - t), s = n - e;
277
307
  return Math.abs(o * e + s * t) > i * Math.abs(o) + a * Math.abs(s);
278
308
  }
279
- function B(e, t, n, r) {
309
+ function z(e, t, n, r) {
280
310
  let i = Math.abs(e) - n, a = Math.abs(t) - r, o = i > 0 ? i : 0, s = a > 0 ? a : 0;
281
311
  return o * o + s * s;
282
312
  }
283
- function V(e) {
313
+ function B(e) {
284
314
  return e < 0 ? 0 : e > 1 ? 1 : e;
285
315
  }
286
316
  //#endregion
287
317
  //#region src/systems/collision.ts
288
- var H = 1e-4, ge = 24, U = "collidable", W = [], G = class {
318
+ var V = 1e-4, Se = 24, H = "collidable", U = [], W = {
319
+ fraction: 1,
320
+ blocking: U
321
+ }, G = class {
289
322
  buckets = [];
290
323
  constructor(t, n) {
291
324
  let r = /* @__PURE__ */ new Map();
@@ -327,25 +360,71 @@ var H = 1e-4, ge = 24, U = "collidable", W = [], G = class {
327
360
  let r = Math.sqrt(t * t + n * n), i = K(e);
328
361
  if (!i || r === 0) return {
329
362
  fraction: 1,
330
- blocking: W
363
+ blocking: U
331
364
  };
332
- let { x: a, y: o, halfWidth: s, halfHeight: c } = i, l = a + t, u = o + n, d = [];
333
- if (this.forEachCandidate(i, l - s, u - c, l + s, u + c, (e) => {
334
- q(i, l, u, e) && (q(i, a, o, e) || d.push(e));
335
- }), d.length === 0) return {
365
+ let a = this.gatherCandidates(i, t, n);
366
+ return a.length === 0 ? {
336
367
  fraction: 1,
337
- blocking: W
368
+ blocking: U
369
+ } : this.refine(i, a, t, n, r);
370
+ }
371
+ resolveMove(e, t, n, r) {
372
+ let i = Math.sqrt(t * t + n * n), a = K(e);
373
+ if (!a || i === 0) return {
374
+ moveX: t,
375
+ moveY: n,
376
+ blocking: U
338
377
  };
339
- let f = 0, p = 1, m = H / r;
340
- for (let e = 0; e < ge && p - f > m; e++) {
341
- let e = (f + p) / 2;
342
- this.blockedAt(i, d, t, n, e) ? p = e : f = e;
378
+ let o = this.gatherCandidates(a, t, n);
379
+ if (o.length === 0) return {
380
+ moveX: t,
381
+ moveY: n,
382
+ blocking: U
383
+ };
384
+ if (r && t !== 0 && n !== 0) {
385
+ let e = this.gatherCandidates(a, t, 0), r = this.gatherCandidates(a, 0, n);
386
+ if (e.length === 0 && r.length === 0) return Math.abs(t) >= Math.abs(n) ? {
387
+ moveX: t,
388
+ moveY: 0,
389
+ blocking: U
390
+ } : {
391
+ moveX: 0,
392
+ moveY: n,
393
+ blocking: U
394
+ };
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));
396
+ return {
397
+ moveX: t * i.fraction,
398
+ moveY: n * o.fraction,
399
+ blocking: Ce(i.blocking, o.blocking)
400
+ };
343
401
  }
344
- let h = [], g = a + t * p, _ = o + n * p;
345
- for (let e of d) q(i, g, _, e) && h.push(e);
402
+ let s = this.refine(a, o, t, n, i);
346
403
  return {
347
- fraction: f * r > H ? f : 0,
348
- blocking: h
404
+ moveX: t * s.fraction,
405
+ moveY: n * s.fraction,
406
+ blocking: s.blocking
407
+ };
408
+ }
409
+ gatherCandidates(e, t, n) {
410
+ let { x: r, y: i, halfWidth: a, halfHeight: o } = e;
411
+ if (e.sensor) return [];
412
+ let s = r + t, c = i + n, l = [];
413
+ 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));
415
+ }), l;
416
+ }
417
+ 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++) {
420
+ let i = (s + c) / 2;
421
+ this.blockedAt(e, t, n, r, i) ? c = i : s = i;
422
+ }
423
+ let u = [], d = a + n * c, f = o + r * c;
424
+ for (let n of t) q(e, d, f, n) && u.push(n);
425
+ return {
426
+ fraction: s * i > V ? s : 0,
427
+ blocking: u
349
428
  };
350
429
  }
351
430
  blockedAt(e, t, n, r, i) {
@@ -360,6 +439,13 @@ var H = 1e-4, ge = 24, U = "collidable", W = [], G = class {
360
439
  }
361
440
  }
362
441
  };
442
+ function Ce(e, t) {
443
+ if (e.length === 0) return t;
444
+ if (t.length === 0) return e;
445
+ let n = [...e];
446
+ for (let e of t) n.some((t) => t.entityId === e.entityId) || n.push(e);
447
+ return n;
448
+ }
363
449
  function K(e) {
364
450
  let t = e.components.body;
365
451
  if (!t) return;
@@ -379,7 +465,8 @@ function K(e) {
379
465
  halfWidth: O(i, a, o, s),
380
466
  halfHeight: k(i, a, o, s),
381
467
  category: t[1],
382
- mask: n
468
+ mask: n,
469
+ sensor: t[3] !== 0
383
470
  };
384
471
  }
385
472
  function q(e, t, n, r) {
@@ -388,7 +475,7 @@ function q(e, t, n, r) {
388
475
  }
389
476
  //#endregion
390
477
  //#region src/systems/spatial-index.ts
391
- var _e = class {
478
+ var we = class {
392
479
  entries = [];
393
480
  index;
394
481
  constructor(t) {
@@ -428,23 +515,61 @@ var _e = class {
428
515
  let o = this.entries;
429
516
  return (i ? a.neighbors(e, t, n, r, (e) => i(o[e])) : a.neighbors(e, t, n, r)).map((e) => o[e]);
430
517
  }
431
- }, J = "position-updated";
432
- function ve(e = {}) {
518
+ }, J = {
519
+ x: 0,
520
+ y: 0
521
+ }, Y = {
522
+ x: 0,
523
+ y: 0
524
+ }, X = {
525
+ x: 0,
526
+ y: 0
527
+ };
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;
543
+ }
544
+ function Ee(e) {
545
+ return e ? e[0] : 1;
546
+ }
547
+ function De(e) {
548
+ return e !== void 0 && e[3] !== 0;
549
+ }
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
+ //#endregion
555
+ //#region src/systems/physics-update.ts
556
+ var ke = "position-updated";
557
+ function Ae(e = {}) {
433
558
  let { onCollision: t, optional: n = [] } = e, r, i = Object.assign((e, n, i, a, o) => {
434
559
  let s = {
435
560
  entityId: n,
436
561
  components: i
437
562
  }, c = i.interpolation, l = e.elapsedTime / 1e3, u = i.velocity[0] * l, d = i.velocity[1] * l;
438
- if (X(c, i.transform, e.elapsedTime), !r) {
439
- Q(n, i.transform, u, d, o, e.reportMoves), Z(c, e.tick);
563
+ if (Me(c, i.transform, e.elapsedTime), !r) {
564
+ $(n, i.transform, u, d, o, e.reportMoves), Q(c, e.tick);
440
565
  return;
441
566
  }
442
- let f = r.sweep(s, u, d);
443
- if (Q(n, i.transform, u * f.fraction, d * f.fraction, o, e.reportMoves), Z(c, e.tick), t) {
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)) {
444
569
  r.forEachOverlapping(s, (n) => {
445
- t(e, s, n, a, o);
570
+ f && Z(s, n), t && t(e, s, n, a, o);
446
571
  });
447
- for (let n of f.blocking) t(e, s, n, a, o);
572
+ for (let n of p.blocking) f && Z(s, n), t && t(e, s, n, a, o);
448
573
  }
449
574
  }, { physics: {
450
575
  collision: !0,
@@ -454,39 +579,43 @@ function ve(e = {}) {
454
579
  r = new G(n.collidable ?? [], e.elapsedTime / 1e3);
455
580
  }, i;
456
581
  }
457
- var Y = (e, t, n, r, i) => {
582
+ var je = (e, t, n, r, i) => {
458
583
  let a = e.elapsedTime / 1e3, o = n.interpolation;
459
- X(o, n.transform, e.elapsedTime), Q(t, n.transform, n.velocity[0] * a, n.velocity[1] * a, i, e.reportMoves), Z(o, e.tick);
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);
460
585
  };
461
- function X(e, t, n) {
586
+ function Me(e, t, n) {
462
587
  e && (e[2] = t[0], e[3] = t[1], e[6] = n);
463
588
  }
464
- function Z(e, t) {
589
+ function Q(e, t) {
465
590
  e && r(e, 7, t);
466
591
  }
467
- function Q(e, n, r, i, a, o) {
468
- r === 0 && i === 0 || (r !== 0 && t(n, 0, r, Infinity), i !== 0 && t(n, 1, i, Infinity), o !== !1 && a.emitSystemEvent(J, e));
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));
469
594
  }
470
595
  //#endregion
471
596
  //#region src/systems/physics-system.ts
472
- var ye = 50, be = class extends i {
597
+ var Ne = 50, Pe = class extends i {
473
598
  tick = 0;
474
599
  reportMoves;
475
600
  constructor(e, t = {}) {
476
- let n = t.updateFunction ?? Y, r = t.optional ?? n.physics?.optional ?? [], i = t.collision ?? n.physics?.collision ?? !1, a = [];
477
- i && !r.includes("body") && a.push("body"), r.includes("interpolation") || a.push("interpolation");
478
- let o = a.length ? [...a, ...r] : r;
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]: {
604
+ required: ["transform", "body"],
605
+ optional: ["velocity", ...r]
606
+ } } : void 0, c = s || t.queries ? {
607
+ ...s,
608
+ ...t.queries
609
+ } : void 0;
479
610
  super(e, {
480
611
  name: t.name ?? "PhysicsSystem",
481
612
  deltaBetweenRuns: t.deltaBetweenRuns ?? 50,
482
613
  firstRun: t.firstRun,
483
614
  required: ["transform", "velocity"],
484
615
  optional: o,
616
+ filter: t.filter,
485
617
  updateFunction: n,
486
- queries: i ? { [U]: {
487
- required: ["transform", "body"],
488
- optional: ["velocity", ...r]
489
- } } : void 0,
618
+ queries: c,
490
619
  forceMainThread: t.forceMainThread ?? !t.getWorker,
491
620
  getWorker: t.getWorker ?? (() => {
492
621
  throw Error("PhysicsSystem cannot start a worker: no getWorker was supplied");
@@ -496,7 +625,7 @@ var ye = 50, be = class extends i {
496
625
  addDataToWorld(e) {
497
626
  e.tick = ++this.tick, e.reportMoves = this.reportMoves ?? this.listenerCount("position-updated") > 0;
498
627
  }
499
- }, $ = (e, t, r) => {
628
+ }, Fe = (e, t, r) => {
500
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);
501
630
  if (o !== f) return;
502
631
  if (l <= 0) {
@@ -505,14 +634,14 @@ var ye = 50, be = class extends i {
505
634
  }
506
635
  let p = i[4];
507
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;
508
- }, xe = class extends i {
637
+ }, Ie = class extends i {
509
638
  constructor(e, t = {}) {
510
639
  super(e, {
511
640
  name: t.name ?? "InterpolationSystem",
512
641
  deltaBetweenRuns: 0,
513
642
  firstRun: t.firstRun,
514
643
  required: ["transform", "interpolation"],
515
- updateFunction: $,
644
+ updateFunction: Fe,
516
645
  forceMainThread: t.forceMainThread ?? !t.getWorker,
517
646
  getWorker: t.getWorker ?? (() => {
518
647
  throw Error("InterpolationSystem cannot start a worker: no getWorker was supplied");
@@ -521,6 +650,6 @@ var ye = 50, be = class extends i {
521
650
  }
522
651
  };
523
652
  //#endregion
524
- export { ne as BODY_CATEGORY_INDEX, re as BODY_MASK_INDEX, te as BODY_SHAPE_INDEX, ie as BODY_SIZE, U as COLLIDABLE_QUERY, G as CollisionBroadphase, b as DEFAULT_COLLIDE_CATEGORY, x as DEFAULT_COLLIDE_MASK, ye as DEFAULT_PHYSICS_STEP_MS, fe as INTERPOLATION_DURATION_INDEX, ce as INTERPOLATION_PREV_X_INDEX, le as INTERPOLATION_PREV_Y_INDEX, ue as INTERPOLATION_PROGRESS_INDEX, pe as INTERPOLATION_SIZE, de as INTERPOLATION_SYNCED_TICK_INDEX, C as INTERPOLATION_TICK_INDEX, oe as INTERPOLATION_X_INDEX, se as INTERPOLATION_Y_INDEX, xe as InterpolationSystem, J as POSITION_UPDATED_EVENT, be as PhysicsSystem, v as SHAPE_CAPSULE, _ as SHAPE_CIRCLE, g as SHAPE_RECTANGLE, _e 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, N as boundsHalfHeight, M as boundsHalfWidth, ee as canCollide, T as capsuleHalfLength, ve as createPhysicsUpdate, w as interpolationDefinition, $ as interpolationUpdate, P as orientedBoxesOverlap, he as physicsRegistry, Y 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, me as snapEntity, d as transformDefinition, h as velocityDefinition };
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 };
525
654
 
526
655
  //# sourceMappingURL=index.js.map