dacha 0.18.0-alpha.9 → 0.18.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.
Files changed (49) hide show
  1. package/build/contrib/components/index.d.ts +1 -0
  2. package/build/contrib/components/index.js +1 -0
  3. package/build/contrib/components/interpolation/index.d.ts +85 -0
  4. package/build/contrib/components/interpolation/index.js +101 -0
  5. package/build/contrib/components/rigid-body/index.d.ts +8 -0
  6. package/build/contrib/components/rigid-body/index.js +8 -0
  7. package/build/contrib/systems/index.d.ts +2 -0
  8. package/build/contrib/systems/index.js +1 -0
  9. package/build/contrib/systems/interpolator/api.d.ts +40 -0
  10. package/build/contrib/systems/interpolator/api.js +70 -0
  11. package/build/contrib/systems/interpolator/index.d.ts +3 -0
  12. package/build/contrib/systems/interpolator/index.js +2 -0
  13. package/build/contrib/systems/interpolator/system.d.ts +31 -0
  14. package/build/contrib/systems/interpolator/system.js +96 -0
  15. package/build/contrib/systems/interpolator/tests/helpers.d.ts +13 -0
  16. package/build/contrib/systems/interpolator/tests/helpers.js +45 -0
  17. package/build/contrib/systems/interpolator/utils.d.ts +5 -0
  18. package/build/contrib/systems/interpolator/utils.js +41 -0
  19. package/build/contrib/systems/physics-system/api.d.ts +45 -1
  20. package/build/contrib/systems/physics-system/api.js +49 -0
  21. package/build/contrib/systems/physics-system/consts.d.ts +3 -1
  22. package/build/contrib/systems/physics-system/consts.js +4 -1
  23. package/build/contrib/systems/physics-system/physics-system.js +7 -1
  24. package/build/contrib/systems/physics-system/subsystems/collision-detection/dynamic-aabb-tree/index.d.ts +1 -1
  25. package/build/contrib/systems/physics-system/subsystems/collision-detection/dynamic-aabb-tree/index.js +21 -15
  26. package/build/contrib/systems/physics-system/subsystems/collision-detection/index.d.ts +11 -11
  27. package/build/contrib/systems/physics-system/subsystems/collision-detection/index.js +130 -124
  28. package/build/contrib/systems/physics-system/subsystems/collision-detection/query-utils.d.ts +5 -0
  29. package/build/contrib/systems/physics-system/subsystems/collision-detection/query-utils.js +97 -81
  30. package/build/contrib/systems/physics-system/subsystems/collision-detection/types.d.ts +0 -15
  31. package/build/contrib/systems/physics-system/subsystems/collision-detection/utils.d.ts +3 -0
  32. package/build/contrib/systems/physics-system/subsystems/collision-detection/utils.js +9 -0
  33. package/build/contrib/systems/physics-system/subsystems/physics/mass-properties.js +1 -1
  34. package/build/contrib/systems/physics-system/types.d.ts +12 -2
  35. package/build/contrib/systems/renderer/actor-render-tree.js +15 -6
  36. package/build/engine/data-lib/index.d.ts +1 -1
  37. package/build/engine/data-lib/index.js +1 -1
  38. package/build/engine/data-lib/pool.d.ts +9 -0
  39. package/build/engine/data-lib/pool.js +19 -0
  40. package/build/engine/game-loop.js +3 -3
  41. package/build/engine/math-lib/math/ops.d.ts +16 -0
  42. package/build/engine/math-lib/math/ops.js +25 -0
  43. package/package.json +1 -1
  44. package/build/contrib/systems/physics-system/subsystems/collision-detection/dispersion-calculator/index.d.ts +0 -11
  45. package/build/contrib/systems/physics-system/subsystems/collision-detection/dispersion-calculator/index.js +0 -41
  46. package/build/engine/data-lib/sort/index.d.ts +0 -1
  47. package/build/engine/data-lib/sort/index.js +0 -1
  48. package/build/engine/data-lib/sort/insertion-sort.d.ts +0 -1
  49. package/build/engine/data-lib/sort/insertion-sort.js +0 -14
@@ -1,5 +1,5 @@
1
1
  import type { Vector2 } from '../../../engine/math-lib';
2
- import type { CastHit, OverlapHit, RaycastParams, OverlapParams, OverlapActorParams, ShapeCastParams, CastActorParams } from './types';
2
+ import type { CastHit, OverlapHit, RaycastParams, OverlapParams, OverlapActorParams, ShapeCastParams, CastActorParams, CastHitCallback, OverlapHitCallback } from './types';
3
3
  export interface PhysicsAPIHandlers {
4
4
  raycast(params: RaycastParams): CastHit | null;
5
5
  raycastAll(params: RaycastParams): CastHit[];
@@ -9,6 +9,11 @@ export interface PhysicsAPIHandlers {
9
9
  shapeCastAll(params: ShapeCastParams): CastHit[];
10
10
  castActor(params: CastActorParams): CastHit | null;
11
11
  castActorAll(params: CastActorParams): CastHit[];
12
+ raycastEach(params: RaycastParams, callback: CastHitCallback): void;
13
+ shapeCastEach(params: ShapeCastParams, callback: CastHitCallback): void;
14
+ overlapEach(params: OverlapParams, callback: OverlapHitCallback): void;
15
+ castActorEach(params: CastActorParams, callback: CastHitCallback): void;
16
+ overlapActorEach(params: OverlapActorParams, callback: OverlapHitCallback): void;
12
17
  getGravity(): Vector2;
13
18
  setGravity(gravity: Vector2): void;
14
19
  }
@@ -55,6 +60,13 @@ export declare class PhysicsAPI {
55
60
  * @returns All hits sorted from nearest to farthest
56
61
  */
57
62
  raycastAll(params: RaycastParams): CastHit[];
63
+ /**
64
+ * Casts a ray and invokes `callback` for every hit.
65
+ *
66
+ * @param params - Raycast parameters
67
+ * @param callback - Invoked once per hit with a reused hit object
68
+ */
69
+ raycastEach(params: RaycastParams, callback: CastHitCallback): void;
58
70
  /**
59
71
  * Returns all collider intersections for the given query shape.
60
72
  *
@@ -62,6 +74,13 @@ export declare class PhysicsAPI {
62
74
  * @returns All overlap hits
63
75
  */
64
76
  overlapShape(params: OverlapParams): OverlapHit[];
77
+ /**
78
+ * Invokes `callback` for every collider intersecting the query shape.
79
+ *
80
+ * @param params - Overlap parameters
81
+ * @param callback - Invoked once per overlap with a reused hit object
82
+ */
83
+ overlapEach(params: OverlapParams, callback: OverlapHitCallback): void;
65
84
  /**
66
85
  * Returns all collider intersections for an actor's collider.
67
86
  *
@@ -69,6 +88,13 @@ export declare class PhysicsAPI {
69
88
  * @returns All overlap hits
70
89
  */
71
90
  overlapActor(params: OverlapActorParams): OverlapHit[];
91
+ /**
92
+ * Invokes `callback` for every collider intersecting an actor's collider.
93
+ *
94
+ * @param params - Actor overlap parameters
95
+ * @param callback - Invoked once per overlap with a reused hit object
96
+ */
97
+ overlapActorEach(params: OverlapActorParams, callback: OverlapHitCallback): void;
72
98
  /**
73
99
  * Casts a shape and returns the nearest hit, if any.
74
100
  *
@@ -83,6 +109,13 @@ export declare class PhysicsAPI {
83
109
  * @returns All hits sorted from nearest to farthest
84
110
  */
85
111
  shapeCastAll(params: ShapeCastParams): CastHit[];
112
+ /**
113
+ * Casts a shape and invokes `callback` for every hit.
114
+ *
115
+ * @param params - Shape cast parameters
116
+ * @param callback - Invoked once per hit with a reused hit object
117
+ */
118
+ shapeCastEach(params: ShapeCastParams, callback: CastHitCallback): void;
86
119
  /**
87
120
  * Casts an actor's collider and returns the nearest hit, if any.
88
121
  *
@@ -97,4 +130,15 @@ export declare class PhysicsAPI {
97
130
  * @returns All hits sorted from nearest to farthest
98
131
  */
99
132
  castActorAll(params: CastActorParams): CastHit[];
133
+ /**
134
+ * Casts an actor's collider and invokes `callback` for every hit.
135
+ *
136
+ * The `hit` passed to `callback` is a single object reused across
137
+ * invocations and is only valid for the duration of the call. Copy any
138
+ * fields you need to keep. Hits are delivered in arbitrary order.
139
+ *
140
+ * @param params - Actor cast parameters
141
+ * @param callback - Invoked once per hit with a reused hit object
142
+ */
143
+ castActorEach(params: CastActorParams, callback: CastHitCallback): void;
100
144
  }
@@ -51,6 +51,15 @@ export class PhysicsAPI {
51
51
  raycastAll(params) {
52
52
  return this.handlers.raycastAll(params);
53
53
  }
54
+ /**
55
+ * Casts a ray and invokes `callback` for every hit.
56
+ *
57
+ * @param params - Raycast parameters
58
+ * @param callback - Invoked once per hit with a reused hit object
59
+ */
60
+ raycastEach(params, callback) {
61
+ this.handlers.raycastEach(params, callback);
62
+ }
54
63
  /**
55
64
  * Returns all collider intersections for the given query shape.
56
65
  *
@@ -60,6 +69,15 @@ export class PhysicsAPI {
60
69
  overlapShape(params) {
61
70
  return this.handlers.overlapShape(params);
62
71
  }
72
+ /**
73
+ * Invokes `callback` for every collider intersecting the query shape.
74
+ *
75
+ * @param params - Overlap parameters
76
+ * @param callback - Invoked once per overlap with a reused hit object
77
+ */
78
+ overlapEach(params, callback) {
79
+ this.handlers.overlapEach(params, callback);
80
+ }
63
81
  /**
64
82
  * Returns all collider intersections for an actor's collider.
65
83
  *
@@ -69,6 +87,15 @@ export class PhysicsAPI {
69
87
  overlapActor(params) {
70
88
  return this.handlers.overlapActor(params);
71
89
  }
90
+ /**
91
+ * Invokes `callback` for every collider intersecting an actor's collider.
92
+ *
93
+ * @param params - Actor overlap parameters
94
+ * @param callback - Invoked once per overlap with a reused hit object
95
+ */
96
+ overlapActorEach(params, callback) {
97
+ this.handlers.overlapActorEach(params, callback);
98
+ }
72
99
  /**
73
100
  * Casts a shape and returns the nearest hit, if any.
74
101
  *
@@ -87,6 +114,15 @@ export class PhysicsAPI {
87
114
  shapeCastAll(params) {
88
115
  return this.handlers.shapeCastAll(params);
89
116
  }
117
+ /**
118
+ * Casts a shape and invokes `callback` for every hit.
119
+ *
120
+ * @param params - Shape cast parameters
121
+ * @param callback - Invoked once per hit with a reused hit object
122
+ */
123
+ shapeCastEach(params, callback) {
124
+ this.handlers.shapeCastEach(params, callback);
125
+ }
90
126
  /**
91
127
  * Casts an actor's collider and returns the nearest hit, if any.
92
128
  *
@@ -105,4 +141,17 @@ export class PhysicsAPI {
105
141
  castActorAll(params) {
106
142
  return this.handlers.castActorAll(params);
107
143
  }
144
+ /**
145
+ * Casts an actor's collider and invokes `callback` for every hit.
146
+ *
147
+ * The `hit` passed to `callback` is a single object reused across
148
+ * invocations and is only valid for the duration of the call. Copy any
149
+ * fields you need to keep. Hits are delivered in arbitrary order.
150
+ *
151
+ * @param params - Actor cast parameters
152
+ * @param callback - Invoked once per hit with a reused hit object
153
+ */
154
+ castActorEach(params, callback) {
155
+ this.handlers.castActorEach(params, callback);
156
+ }
108
157
  }
@@ -1,5 +1,5 @@
1
1
  export declare const DEFAULT_SOLVER_ITERATIONS = 8;
2
- export declare const DEFAULT_LINEAR_SLEEP_THRESHOLD = 2;
2
+ export declare const DEFAULT_LINEAR_SLEEP_THRESHOLD = 1;
3
3
  export declare const DEFAULT_ANGULAR_SLEEP_THRESHOLD = 0.05;
4
4
  export declare const DEFAULT_SLEEP_TIME_THRESHOLD = 0.5;
5
5
  export declare const DEFAULT_MAX_BIAS_VELOCITY = 60;
@@ -9,3 +9,5 @@ export declare const BIAS_ANGULAR_SLEEP_MULTIPLIER = 2;
9
9
  export declare const RESTITUTION_VELOCITY_THRESHOLD = 1;
10
10
  export declare const PENETRATION_SLEEP_MARGIN = 0.25;
11
11
  export declare const SUPPORT_MIN_GRAVITY_DOT = 0.5;
12
+ export declare const DEFAULT_GRAVITY_X = 0;
13
+ export declare const DEFAULT_GRAVITY_Y = 980;
@@ -1,5 +1,5 @@
1
1
  export const DEFAULT_SOLVER_ITERATIONS = 8;
2
- export const DEFAULT_LINEAR_SLEEP_THRESHOLD = 2;
2
+ export const DEFAULT_LINEAR_SLEEP_THRESHOLD = 1;
3
3
  export const DEFAULT_ANGULAR_SLEEP_THRESHOLD = 0.05;
4
4
  export const DEFAULT_SLEEP_TIME_THRESHOLD = 0.5;
5
5
  export const DEFAULT_MAX_BIAS_VELOCITY = 60;
@@ -12,3 +12,6 @@ export const RESTITUTION_VELOCITY_THRESHOLD = 1;
12
12
  export const PENETRATION_SLEEP_MARGIN = 0.25;
13
13
  // 0.5 treats surfaces up to ~60° from horizontal as holding a body up.
14
14
  export const SUPPORT_MIN_GRAVITY_DOT = 0.5;
15
+ // Default gravity in world units (px/s²).
16
+ export const DEFAULT_GRAVITY_X = 0;
17
+ export const DEFAULT_GRAVITY_Y = 980;
@@ -2,6 +2,7 @@ import { SceneSystem } from '../../../engine/system';
2
2
  import { Vector2 } from '../../../engine/math-lib';
3
3
  import { PhysicsSubsystem, CollisionDetectionSubsystem, CollisionBroadcastSubsystem, ConstraintSolver, } from './subsystems';
4
4
  import { PhysicsAPI } from './api';
5
+ import { DEFAULT_GRAVITY_X, DEFAULT_GRAVITY_Y } from './consts';
5
6
  /**
6
7
  * Physics system that handles 2D physics simulation and collision detection
7
8
  *
@@ -21,7 +22,7 @@ export class PhysicsSystem extends SceneSystem {
21
22
  gravity;
22
23
  constructor(options) {
23
24
  super();
24
- const { gravityX = 0, gravityY = 0, solverIterations, linearSleepThreshold, angularSleepThreshold, sleepTimeThreshold, maxAllowedPenetration, maxBiasVelocity, } = options;
25
+ const { gravityX = DEFAULT_GRAVITY_X, gravityY = DEFAULT_GRAVITY_Y, solverIterations, linearSleepThreshold, angularSleepThreshold, sleepTimeThreshold, maxAllowedPenetration, maxBiasVelocity, } = options;
25
26
  this.gravity = new Vector2(gravityX, gravityY);
26
27
  this.world = options.world;
27
28
  this.physicsSubsystem = new PhysicsSubsystem({
@@ -51,6 +52,11 @@ export class PhysicsSystem extends SceneSystem {
51
52
  shapeCastAll: (params) => this.collisionDetectionSubsystem.shapeCastAll(params),
52
53
  castActor: (params) => this.collisionDetectionSubsystem.castActor(params),
53
54
  castActorAll: (params) => this.collisionDetectionSubsystem.castActorAll(params),
55
+ raycastEach: (params, callback) => this.collisionDetectionSubsystem.raycastEach(params, callback),
56
+ shapeCastEach: (params, callback) => this.collisionDetectionSubsystem.shapeCastEach(params, callback),
57
+ overlapEach: (params, callback) => this.collisionDetectionSubsystem.overlapEach(params, callback),
58
+ castActorEach: (params, callback) => this.collisionDetectionSubsystem.castActorEach(params, callback),
59
+ overlapActorEach: (params, callback) => this.collisionDetectionSubsystem.overlapActorEach(params, callback),
54
60
  getGravity: () => this.gravity,
55
61
  setGravity: (gravity) => {
56
62
  this.gravity = gravity;
@@ -4,7 +4,7 @@ export declare class DynamicAABBTree<T> {
4
4
  private root;
5
5
  private nodesById;
6
6
  private freeInternalNodes;
7
- private queryStack;
7
+ private stackPool;
8
8
  private nextId;
9
9
  get size(): number;
10
10
  insert(aabb: AABB, value: T): DynamicAABBTreeEntryId;
@@ -1,3 +1,4 @@
1
+ import { Pool } from '../../../../../../engine/data-lib';
1
2
  const isLeaf = (node) => node.child1 === null;
2
3
  const createAABB = () => ({
3
4
  min: { x: 0, y: 0 },
@@ -26,7 +27,9 @@ export class DynamicAABBTree {
26
27
  root = null;
27
28
  nodesById = new Map();
28
29
  freeInternalNodes = [];
29
- queryStack = [];
30
+ stackPool = new Pool(() => [], (stack) => {
31
+ stack.length = 0;
32
+ });
30
33
  nextId = 1;
31
34
  get size() {
32
35
  return this.nodesById.size;
@@ -52,22 +55,25 @@ export class DynamicAABBTree {
52
55
  if (!this.root) {
53
56
  return;
54
57
  }
55
- const stack = this.queryStack;
56
- stack.length = 0;
58
+ const stack = this.stackPool.acquire();
57
59
  stack.push(this.root);
58
- while (stack.length > 0) {
59
- const node = stack.pop();
60
- if (!overlaps(node.aabb, aabb)) {
61
- continue;
62
- }
63
- if (isLeaf(node)) {
64
- if (visitor(node.value) === false) {
65
- stack.length = 0;
66
- return;
60
+ try {
61
+ while (stack.length > 0) {
62
+ const node = stack.pop();
63
+ if (!overlaps(node.aabb, aabb)) {
64
+ continue;
67
65
  }
68
- continue;
66
+ if (isLeaf(node)) {
67
+ if (visitor(node.value) === false) {
68
+ return;
69
+ }
70
+ continue;
71
+ }
72
+ stack.push(node.child1, node.child2);
69
73
  }
70
- stack.push(node.child1, node.child2);
74
+ }
75
+ finally {
76
+ this.stackPool.release(stack);
71
77
  }
72
78
  }
73
79
  queryAll(aabb, output = []) {
@@ -80,7 +86,7 @@ export class DynamicAABBTree {
80
86
  this.root = null;
81
87
  this.nodesById.clear();
82
88
  this.freeInternalNodes.length = 0;
83
- this.queryStack.length = 0;
89
+ this.stackPool.clear();
84
90
  }
85
91
  createNode(aabb, value) {
86
92
  return {
@@ -1,16 +1,17 @@
1
1
  import type { SceneSystemOptions } from '../../../../../engine/system';
2
- import type { RaycastParams, CastHit, OverlapHit, OverlapParams, OverlapActorParams, ShapeCastParams, CastActorParams } from '../../types';
2
+ import type { RaycastParams, CastHit, OverlapHit, OverlapParams, OverlapActorParams, ShapeCastParams, CastActorParams, CastHitCallback, OverlapHitCallback } from '../../types';
3
3
  import type { Contact } from './types';
4
4
  export declare class CollisionDetectionSubsystem {
5
5
  private actorQuery;
6
- private axis;
7
6
  private queryTree;
8
7
  private proxiesByActorId;
9
8
  private proxyPairs;
10
9
  private contacts;
11
10
  private actorIdsToDelete;
12
11
  private collisionMatrix;
13
- private queryCandidates;
12
+ private candidateBufferPool;
13
+ private castHitPool;
14
+ private overlapHitPool;
14
15
  constructor(options: SceneSystemOptions);
15
16
  destroy(): void;
16
17
  raycast(params: RaycastParams): CastHit | null;
@@ -21,21 +22,20 @@ export declare class CollisionDetectionSubsystem {
21
22
  shapeCastAll(params: ShapeCastParams): CastHit[];
22
23
  castActor(params: CastActorParams): CastHit | null;
23
24
  castActorAll(params: CastActorParams): CastHit[];
25
+ raycastEach(params: RaycastParams, callback: CastHitCallback): void;
26
+ shapeCastEach(params: ShapeCastParams, callback: CastHitCallback): void;
27
+ overlapEach(params: OverlapParams, callback: OverlapHitCallback): void;
28
+ castActorEach(params: CastActorParams, callback: CastHitCallback): void;
29
+ overlapActorEach(params: OverlapActorParams, callback: OverlapHitCallback): void;
24
30
  private handleActorAdd;
25
31
  private handleActorRemove;
26
32
  private checkOnReorientation;
27
33
  private addProxy;
28
34
  private updateProxy;
29
- private addToSortedList;
30
- private updateSortedList;
31
- private clearSortedList;
32
- private getAxes;
33
- private areStaticBodies;
34
- private testAABB;
35
35
  private testCollisionLayers;
36
- private testState;
36
+ private withCandidates;
37
37
  private collectQueryCandidates;
38
- private sweepAndPrune;
38
+ private collectPairs;
39
39
  private checkOnIntersection;
40
40
  private storeContact;
41
41
  private clearDeletedProxies;