@codexo/exojs-physics 0.17.0 → 0.19.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/README.md +59 -97
- package/dist/esm/Aabb.d.ts.map +1 -1
- package/dist/esm/Collider.d.ts.map +1 -1
- package/dist/esm/Collider.js +18 -0
- package/dist/esm/Collider.js.map +1 -1
- package/dist/esm/ContactGraph.d.ts +34 -3
- package/dist/esm/ContactGraph.d.ts.map +1 -1
- package/dist/esm/ContactGraph.js +68 -5
- package/dist/esm/ContactGraph.js.map +1 -1
- package/dist/esm/PhysicsBody.d.ts.map +1 -1
- package/dist/esm/PhysicsBody.js +67 -6
- package/dist/esm/PhysicsBody.js.map +1 -1
- package/dist/esm/PhysicsBodyComponent.d.ts +45 -0
- package/dist/esm/PhysicsBodyComponent.d.ts.map +1 -0
- package/dist/esm/PhysicsBodyComponent.js +77 -0
- package/dist/esm/PhysicsBodyComponent.js.map +1 -0
- package/dist/esm/PhysicsWorld.d.ts +104 -9
- package/dist/esm/PhysicsWorld.d.ts.map +1 -1
- package/dist/esm/PhysicsWorld.js +235 -65
- package/dist/esm/PhysicsWorld.js.map +1 -1
- package/dist/esm/TimeStepper.d.ts.map +1 -1
- package/dist/esm/backend/NativePhysicsBackend.d.ts.map +1 -1
- package/dist/esm/binding/BindingRegistry.d.ts.map +1 -1
- package/dist/esm/binding/PhysicsBinding.d.ts +6 -2
- package/dist/esm/binding/PhysicsBinding.d.ts.map +1 -1
- package/dist/esm/binding/PhysicsBinding.js +8 -4
- package/dist/esm/binding/PhysicsBinding.js.map +1 -1
- package/dist/esm/binding/nodeWorldTransform.d.ts +18 -0
- package/dist/esm/binding/nodeWorldTransform.d.ts.map +1 -0
- package/dist/esm/binding/nodeWorldTransform.js +41 -0
- package/dist/esm/binding/nodeWorldTransform.js.map +1 -0
- package/dist/esm/broadphase/AabbTreeBroadPhase.d.ts +22 -6
- package/dist/esm/broadphase/AabbTreeBroadPhase.d.ts.map +1 -1
- package/dist/esm/broadphase/AabbTreeBroadPhase.js +45 -11
- package/dist/esm/broadphase/AabbTreeBroadPhase.js.map +1 -1
- package/dist/esm/collision/Manifold.d.ts.map +1 -1
- package/dist/esm/collision/dispatch.d.ts.map +1 -1
- package/dist/esm/collision/narrowphase.d.ts.map +1 -1
- package/dist/esm/collision/segments.d.ts.map +1 -1
- package/dist/esm/collision/sweep.d.ts.map +1 -1
- package/dist/esm/debug/PhysicsDebugDraw.d.ts.map +1 -1
- package/dist/esm/debug/PhysicsDebugDraw.js +13 -11
- package/dist/esm/debug/PhysicsDebugDraw.js.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/joints/DistanceJoint.d.ts +2 -1
- package/dist/esm/joints/DistanceJoint.d.ts.map +1 -1
- package/dist/esm/joints/DistanceJoint.js +5 -5
- package/dist/esm/joints/DistanceJoint.js.map +1 -1
- package/dist/esm/joints/Joint.d.ts +59 -3
- package/dist/esm/joints/Joint.d.ts.map +1 -1
- package/dist/esm/joints/Joint.js +39 -3
- package/dist/esm/joints/Joint.js.map +1 -1
- package/dist/esm/joints/MouseJoint.d.ts.map +1 -1
- package/dist/esm/joints/MouseJoint.js +1 -1
- package/dist/esm/joints/MouseJoint.js.map +1 -1
- package/dist/esm/joints/PrismaticJoint.d.ts +2 -1
- package/dist/esm/joints/PrismaticJoint.d.ts.map +1 -1
- package/dist/esm/joints/PrismaticJoint.js +2 -2
- package/dist/esm/joints/PrismaticJoint.js.map +1 -1
- package/dist/esm/joints/RevoluteJoint.d.ts +4 -6
- package/dist/esm/joints/RevoluteJoint.d.ts.map +1 -1
- package/dist/esm/joints/RevoluteJoint.js +39 -33
- package/dist/esm/joints/RevoluteJoint.js.map +1 -1
- package/dist/esm/joints/WeldJoint.d.ts +2 -1
- package/dist/esm/joints/WeldJoint.d.ts.map +1 -1
- package/dist/esm/joints/WeldJoint.js +9 -9
- package/dist/esm/joints/WeldJoint.js.map +1 -1
- package/dist/esm/joints/WheelJoint.d.ts +2 -1
- package/dist/esm/joints/WheelJoint.d.ts.map +1 -1
- package/dist/esm/joints/WheelJoint.js +5 -5
- package/dist/esm/joints/WheelJoint.js.map +1 -1
- package/dist/esm/math.d.ts.map +1 -1
- package/dist/esm/physicsBuildInfo.js +2 -2
- package/dist/esm/public.d.ts +2 -1
- package/dist/esm/public.d.ts.map +1 -1
- package/dist/esm/query/QueryEngine.d.ts.map +1 -1
- package/dist/esm/shapes/BoxShape.d.ts.map +1 -1
- package/dist/esm/shapes/CapsuleShape.d.ts +1 -1
- package/dist/esm/shapes/CapsuleShape.d.ts.map +1 -1
- package/dist/esm/shapes/ChainShape.d.ts +1 -1
- package/dist/esm/shapes/ChainShape.d.ts.map +1 -1
- package/dist/esm/shapes/CircleShape.d.ts +1 -1
- package/dist/esm/shapes/CircleShape.d.ts.map +1 -1
- package/dist/esm/shapes/PolygonShape.d.ts +1 -1
- package/dist/esm/shapes/PolygonShape.d.ts.map +1 -1
- package/dist/esm/shapes/SegmentShape.d.ts +1 -1
- package/dist/esm/shapes/SegmentShape.d.ts.map +1 -1
- package/dist/esm/shapes/convexParts.d.ts.map +1 -1
- package/dist/esm/solver/ContactSolver.d.ts.map +1 -1
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -9,12 +9,19 @@ import type { BroadPhase, CandidatePair } from './BroadPhase';
|
|
|
9
9
|
*
|
|
10
10
|
* Three phases per `computePairs` call: (1) sync every live collider (insert
|
|
11
11
|
* new ones, reinsert moved ones, querying only around reinsertions to discover
|
|
12
|
-
* NEW fat-overlap candidates
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
12
|
+
* NEW fat-overlap candidates; a collider whose AABB has not been rewritten
|
|
13
|
+
* since its last sync is skipped outright via `Collider._treeDirty`, so a
|
|
14
|
+
* sleeping world pays one flag read per collider); (2) one full pass over the
|
|
15
|
+
* persistent pair set dropping any pair whose fat AABBs no longer overlap -
|
|
16
|
+
* cheap (O(1) per pair) and run every step regardless of movement, exactly
|
|
17
|
+
* mirroring Box2D's own `b2ContactManager::Collide` (see the design's
|
|
18
|
+
* correctness argument for why a single global pass, not a per-moved-leaf
|
|
19
|
+
* rescan, is both correct and the right complexity); (3) emit, keeping only
|
|
20
|
+
* pairs whose *tight* AABBs overlap.
|
|
21
|
+
*
|
|
22
|
+
* Phases 2 and 3 are skipped whole when nothing they read has changed since the
|
|
23
|
+
* last call: the emitted list is a function of the pair set and the tight AABBs
|
|
24
|
+
* alone, and a step that rewrote no collider geometry touched neither.
|
|
18
25
|
*
|
|
19
26
|
* The persistent set is intentionally keyed on *fat* overlap so temporal
|
|
20
27
|
* coherence carries across steps (a leaf that stays inside its fat AABB is
|
|
@@ -40,6 +47,15 @@ export declare class AabbTreeBroadPhase implements BroadPhase, SpatialIndex {
|
|
|
40
47
|
private readonly _scratchFatAabb;
|
|
41
48
|
private _syncingCollider;
|
|
42
49
|
private _syncingProxy;
|
|
50
|
+
/**
|
|
51
|
+
* Whether anything the emitted candidate list is a function of has changed
|
|
52
|
+
* since it was built: a collider AABB taken into the tree, or a pair added or
|
|
53
|
+
* dropped. While it stays `false` the previous list is still exactly right.
|
|
54
|
+
*/
|
|
55
|
+
private _emitStale;
|
|
56
|
+
/** The buffer the current candidate list was emitted into, so a caller that swaps or clears its own buffer still gets a rebuild. */
|
|
57
|
+
private _emitTarget;
|
|
58
|
+
private _emitLength;
|
|
43
59
|
private readonly _onNeighborFound;
|
|
44
60
|
private readonly _dropIfStale;
|
|
45
61
|
private _collectTarget;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AabbTreeBroadPhase.d.ts","sourceRoot":"","sources":["../../../src/broadphase/AabbTreeBroadPhase.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAI9C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,KAAK,E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G,EAAE,aAAa,EAAE,GAAG,aAAa,EAAE,CAuBzF;IAED;;;;OAIG;IACI,IAAI,CAAC,SAAS,EAAE,SAAS,QAAQ,EAAE,GAAG,IAAI,CAIhD;IAED,8FAA8F;IACvF,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,QAAQ,EAAE,CAMtE;IAED,wGAAwG;IACjG,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,GAAG,IAAI,CAE9I;IAED,iGAAiG;IAC1F,cAAc,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAc9C;IAEM,OAAO,IAAI,IAAI,CAYrB;IAED,OAAO,CAAC,KAAK;IA4Bb,OAAO,CAAC,kBAAkB;IAS1B,OAAO,CAAC,QAAQ;CAkBjB"}
|
|
@@ -22,12 +22,19 @@ const byPairId = (p, q) => p.a.id - q.a.id || p.b.id - q.b.id;
|
|
|
22
22
|
*
|
|
23
23
|
* Three phases per `computePairs` call: (1) sync every live collider (insert
|
|
24
24
|
* new ones, reinsert moved ones, querying only around reinsertions to discover
|
|
25
|
-
* NEW fat-overlap candidates
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
25
|
+
* NEW fat-overlap candidates; a collider whose AABB has not been rewritten
|
|
26
|
+
* since its last sync is skipped outright via `Collider._treeDirty`, so a
|
|
27
|
+
* sleeping world pays one flag read per collider); (2) one full pass over the
|
|
28
|
+
* persistent pair set dropping any pair whose fat AABBs no longer overlap -
|
|
29
|
+
* cheap (O(1) per pair) and run every step regardless of movement, exactly
|
|
30
|
+
* mirroring Box2D's own `b2ContactManager::Collide` (see the design's
|
|
31
|
+
* correctness argument for why a single global pass, not a per-moved-leaf
|
|
32
|
+
* rescan, is both correct and the right complexity); (3) emit, keeping only
|
|
33
|
+
* pairs whose *tight* AABBs overlap.
|
|
34
|
+
*
|
|
35
|
+
* Phases 2 and 3 are skipped whole when nothing they read has changed since the
|
|
36
|
+
* last call: the emitted list is a function of the pair set and the tight AABBs
|
|
37
|
+
* alone, and a step that rewrote no collider geometry touched neither.
|
|
31
38
|
*
|
|
32
39
|
* The persistent set is intentionally keyed on *fat* overlap so temporal
|
|
33
40
|
* coherence carries across steps (a leaf that stays inside its fat AABB is
|
|
@@ -58,12 +65,24 @@ var AabbTreeBroadPhase = class {
|
|
|
58
65
|
};
|
|
59
66
|
_syncingCollider = null;
|
|
60
67
|
_syncingProxy = -1;
|
|
68
|
+
/**
|
|
69
|
+
* Whether anything the emitted candidate list is a function of has changed
|
|
70
|
+
* since it was built: a collider AABB taken into the tree, or a pair added or
|
|
71
|
+
* dropped. While it stays `false` the previous list is still exactly right.
|
|
72
|
+
*/
|
|
73
|
+
_emitStale = true;
|
|
74
|
+
/** The buffer the current candidate list was emitted into, so a caller that swaps or clears its own buffer still gets a rebuild. */
|
|
75
|
+
_emitTarget = null;
|
|
76
|
+
_emitLength = -1;
|
|
61
77
|
_onNeighborFound = (payload, proxy) => {
|
|
62
78
|
if (proxy === this._syncingProxy) return;
|
|
63
79
|
this._addPair(this._syncingCollider, payload);
|
|
64
80
|
};
|
|
65
81
|
_dropIfStale = (pair, key) => {
|
|
66
|
-
if (!this._tree.fatOverlaps(pair.a._treeProxy, pair.b._treeProxy))
|
|
82
|
+
if (!this._tree.fatOverlaps(pair.a._treeProxy, pair.b._treeProxy)) {
|
|
83
|
+
this._pairs.delete(key);
|
|
84
|
+
this._emitStale = true;
|
|
85
|
+
}
|
|
67
86
|
};
|
|
68
87
|
_collectTarget = [];
|
|
69
88
|
_collectPair = (pair) => {
|
|
@@ -78,11 +97,15 @@ var AabbTreeBroadPhase = class {
|
|
|
78
97
|
};
|
|
79
98
|
computePairs(colliders, out) {
|
|
80
99
|
this.sync(colliders);
|
|
100
|
+
if (!this._emitStale && out === this._emitTarget && out.length === this._emitLength) return out;
|
|
81
101
|
this._pairs.forEach(this._dropIfStale);
|
|
82
102
|
out.length = 0;
|
|
83
103
|
this._collectTarget = out;
|
|
84
104
|
this._pairs.forEach(this._collectPair);
|
|
85
105
|
sortInPlace(out, byPairId);
|
|
106
|
+
this._emitStale = false;
|
|
107
|
+
this._emitTarget = out;
|
|
108
|
+
this._emitLength = out.length;
|
|
86
109
|
return out;
|
|
87
110
|
}
|
|
88
111
|
/**
|
|
@@ -107,6 +130,7 @@ var AabbTreeBroadPhase = class {
|
|
|
107
130
|
/** Drop `collider`'s leaf and every pair referencing it. Called when a collider is destroyed. */
|
|
108
131
|
removeCollider(collider) {
|
|
109
132
|
if (collider._treeProxy === -1) return;
|
|
133
|
+
this._emitStale = true;
|
|
110
134
|
this._tree.remove(collider._treeProxy);
|
|
111
135
|
collider._treeProxy = -1;
|
|
112
136
|
for (const [key, pair] of this._pairs) if (pair.a === collider || pair.b === collider) this._pairs.delete(key);
|
|
@@ -115,14 +139,21 @@ var AabbTreeBroadPhase = class {
|
|
|
115
139
|
this._tree.query(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, this._resetTreeProxy);
|
|
116
140
|
this._tree.destroy();
|
|
117
141
|
this._pairs.clear();
|
|
142
|
+
this._emitStale = true;
|
|
143
|
+
this._emitTarget = null;
|
|
118
144
|
}
|
|
119
145
|
_sync(collider) {
|
|
120
146
|
const box = collider.aabb;
|
|
121
147
|
if (collider._treeProxy === -1) {
|
|
148
|
+
collider._treeDirty = false;
|
|
149
|
+
this._emitStale = true;
|
|
122
150
|
collider._treeProxy = this._tree.insert(box.minX, box.minY, box.maxX, box.maxY, collider);
|
|
123
151
|
this._discoverNeighbors(collider);
|
|
124
152
|
return;
|
|
125
153
|
}
|
|
154
|
+
if (!collider._treeDirty) return;
|
|
155
|
+
collider._treeDirty = false;
|
|
156
|
+
this._emitStale = true;
|
|
126
157
|
if (this._tree.update(collider._treeProxy, box.minX, box.minY, box.maxX, box.maxY)) this._discoverNeighbors(collider);
|
|
127
158
|
}
|
|
128
159
|
_discoverNeighbors(collider) {
|
|
@@ -137,10 +168,13 @@ var AabbTreeBroadPhase = class {
|
|
|
137
168
|
const lo = a.id < b.id ? a : b;
|
|
138
169
|
const hi = a.id < b.id ? b : a;
|
|
139
170
|
const key = pairKey(lo.id, hi.id);
|
|
140
|
-
if (!this._pairs.has(key))
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
171
|
+
if (!this._pairs.has(key)) {
|
|
172
|
+
this._pairs.set(key, {
|
|
173
|
+
a: lo,
|
|
174
|
+
b: hi
|
|
175
|
+
});
|
|
176
|
+
this._emitStale = true;
|
|
177
|
+
}
|
|
144
178
|
}
|
|
145
179
|
};
|
|
146
180
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AabbTreeBroadPhase.js","names":[],"sources":["../../../src/broadphase/AabbTreeBroadPhase.ts"],"sourcesContent":["import type { AabbLike } from '@codexo/exojs';\nimport { DynamicAabbTree } from '@codexo/exojs';\n\nimport { aabbOverlap } from '../Aabb';\nimport type { Collider } from '../Collider';\nimport { pairKey } from '../ContactGraph';\nimport type { SpatialIndex } from '../query/SpatialIndex';\nimport { sortInPlace } from '../sort';\nimport type { BroadPhase, CandidatePair } from './BroadPhase';\n\n/**\n * Fixed fat-AABB margin (world px) applied to every collider's tree leaf.\n * Chosen relative to the engine's typical small-collider scale (test/example\n * shapes commonly span 10-30px): large enough that a few steps of ordinary\n * motion stay inside the margin (skipping the sync phase's remove+reinsert),\n * small enough it doesn't inflate fat AABBs into spurious candidates for\n * densely packed scenes. Fixed per the design's v1 scope (no velocity-based\n * predictive expansion) - revisit if benchmarks want a different value.\n */\nconst AABB_MARGIN = 4;\n\nconst byPairId = (p: CandidatePair, q: CandidatePair): number => p.a.id - q.a.id || p.b.id - q.b.id;\n\n/**\n * Dynamic-AABB-tree broad phase (Box2D-style), wrapping a generic\n * `DynamicAabbTree<Collider>`. Stateful across steps: a collider whose tight\n * AABB stays inside its stored fat AABB costs nothing to re-sync.\n *\n * Three phases per `computePairs` call: (1) sync every live collider (insert\n * new ones, reinsert moved ones, querying only around reinsertions to discover\n * NEW fat-overlap candidates); (2) one full pass over the persistent pair set\n * dropping any pair whose fat AABBs no longer overlap - cheap (O(1) per pair)\n * and run every step regardless of movement, exactly mirroring Box2D's own\n * `b2ContactManager::Collide` (see the design's correctness argument for why a\n * single global pass, not a per-moved-leaf rescan, is both correct and the\n * right complexity); (3) emit, keeping only pairs whose *tight* AABBs overlap.\n *\n * The persistent set is intentionally keyed on *fat* overlap so temporal\n * coherence carries across steps (a leaf that stays inside its fat AABB is\n * never re-discovered). Emitting is filtered to tight-AABB overlap so the\n * candidate set the caller sees is exact - the fat margin never leaks a false\n * positive downstream, and a fat-but-not-tight pair simply stays parked in the\n * set (costing one O(1) re-check per step) until the leaves separate.\n *\n * **Single-owner contract.** A collider's tree membership is tracked through its\n * `Collider._treeProxy` field, which - like `PhysicsBody._islandIndex` /\n * `_sleepTime` - is a single slot on the shared object. Exactly ONE\n * `AabbTreeBroadPhase` instance may track a given collider at a time (in\n * production that is the world's own `_broadPhase`, for the collider's whole\n * lifetime until `removeCollider`). Do NOT stand up a second, throwaway\n * `AabbTreeBroadPhase` over colliders another instance already owns (e.g. for\n * debug visualization): the second instance would clobber the first's\n * `_treeProxy` values and corrupt its tree. Use a plain brute-force scan for\n * such one-off, read-only needs instead.\n */\nexport class AabbTreeBroadPhase implements BroadPhase, SpatialIndex {\n private readonly _tree = new DynamicAabbTree<Collider>(AABB_MARGIN);\n private readonly _pairs = new Map<number, CandidatePair>();\n private readonly _scratchFatAabb: AabbLike = { minX: 0, minY: 0, maxX: 0, maxY: 0 };\n\n private _syncingCollider: Collider | null = null;\n private _syncingProxy = -1;\n private readonly _onNeighborFound = (payload: Collider, proxy: number): void => {\n if (proxy === this._syncingProxy) {\n return;\n }\n\n this._addPair(this._syncingCollider!, payload);\n };\n\n private readonly _dropIfStale = (pair: CandidatePair, key: number): void => {\n if (!this._tree.fatOverlaps(pair.a._treeProxy, pair.b._treeProxy)) {\n this._pairs.delete(key);\n }\n };\n\n private _collectTarget: CandidatePair[] = [];\n private readonly _collectPair = (pair: CandidatePair): void => {\n // Emit only true (tight-AABB) overlaps; the persistent set is fat-keyed for\n // coherence, so a fat-but-not-tight pair is parked here, not a candidate.\n if (aabbOverlap(pair.a.aabb, pair.b.aabb)) {\n this._collectTarget.push(pair);\n }\n };\n\n private _queryTarget: Collider[] = [];\n private readonly _onQueryHit = (payload: Collider): void => {\n this._queryTarget.push(payload);\n };\n\n private readonly _resetTreeProxy = (payload: Collider): void => {\n payload._treeProxy = -1;\n };\n\n public computePairs(colliders: readonly Collider[], out: CandidatePair[]): CandidatePair[] {\n this.sync(colliders);\n\n this._pairs.forEach(this._dropIfStale);\n\n out.length = 0;\n this._collectTarget = out;\n this._pairs.forEach(this._collectPair);\n sortInPlace(out, byPairId);\n\n return out;\n }\n\n /**\n * Insert every not-yet-tracked collider and reinsert every moved one (the\n * `computePairs` phase-1 loop, extracted so {@link queryAabb} callers can\n * force it independently of a full detection pass - see {@link SpatialIndex.sync}).\n */\n public sync(colliders: readonly Collider[]): void {\n for (const collider of colliders) {\n this._sync(collider);\n }\n }\n\n /** Colliders whose AABB overlaps `aabb`. Writes into `out` (cleared first) and returns it. */\n public queryAabb(aabb: Readonly<AabbLike>, out: Collider[]): Collider[] {\n out.length = 0;\n this._queryTarget = out;\n this._tree.query(aabb.minX, aabb.minY, aabb.maxX, aabb.maxY, this._onQueryHit);\n\n return out;\n }\n\n /** Colliders whose AABB the ray could cross within `maxDistance`, via the tree's own ray-cast prune. */\n public rayCast(originX: number, originY: number, dirX: number, dirY: number, maxDistance: number, callback: (collider: Collider) => void): void {\n this._tree.rayCast(originX, originY, dirX, dirY, maxDistance, callback);\n }\n\n /** Drop `collider`'s leaf and every pair referencing it. Called when a collider is destroyed. */\n public removeCollider(collider: Collider): void {\n if (collider._treeProxy === -1) {\n return;\n }\n\n this._tree.remove(collider._treeProxy);\n collider._treeProxy = -1;\n\n for (const [key, pair] of this._pairs) {\n if (pair.a === collider || pair.b === collider) {\n this._pairs.delete(key);\n }\n }\n }\n\n public destroy(): void {\n // Reset `_treeProxy` on every still-tracked collider before tearing down the\n // tree: the single-owner contract only guarantees at most one owner AT A\n // TIME, so a collider handed to a later `AabbTreeBroadPhase` (sequential\n // reuse, not concurrent dual-ownership) must not carry a stale non-`-1`\n // proxy from this instance forward. `query` with infinite bounds visits\n // every leaf currently in the tree, allocation-free.\n this._tree.query(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, this._resetTreeProxy);\n this._tree.destroy();\n this._pairs.clear();\n }\n\n private _sync(collider: Collider): void {\n const box = collider.aabb;\n\n if (collider._treeProxy === -1) {\n collider._treeProxy = this._tree.insert(box.minX, box.minY, box.maxX, box.maxY, collider);\n this._discoverNeighbors(collider);\n\n return;\n }\n\n const moved = this._tree.update(collider._treeProxy, box.minX, box.minY, box.maxX, box.maxY);\n\n if (moved) {\n this._discoverNeighbors(collider);\n }\n }\n\n private _discoverNeighbors(collider: Collider): void {\n const proxy = collider._treeProxy;\n const fat = this._tree.fatAabbOf(proxy, this._scratchFatAabb);\n\n this._syncingCollider = collider;\n this._syncingProxy = proxy;\n this._tree.query(fat.minX, fat.minY, fat.maxX, fat.maxY, this._onNeighborFound);\n }\n\n private _addPair(a: Collider, b: Collider): void {\n // Two colliders of the same body are a compound shape, not a collision.\n // Rejecting the pair here rather than downstream also keeps it out of the\n // persistent set, so a compound body costs nothing per step - which matters\n // once convex decomposition attaches many edge-sharing colliders to one body.\n if (a.body === b.body) {\n return;\n }\n\n const lo = a.id < b.id ? a : b;\n const hi = a.id < b.id ? b : a;\n const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MAAM,KAAK,MAAM,UAAU,OAAO,KAAK,eAAe;EAE5D,KAAK,mBAAmB;EACxB,KAAK,gBAAgB;EACrB,KAAK,MAAM,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,KAAK,gBAAgB;CAChF;CAEA,AAAQ,SAAS,GAAa,GAAmB;EAK/C,IAAI,EAAE,SAAS,EAAE,MACf;EAGF,MAAM,KAAK,EAAE,KAAK,EAAE,KAAK,IAAI;EAC7B,MAAM,KAAK,EAAE,KAAK,EAAE,KAAK,IAAI;EAC7B,MAAM,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAE;EAEhC,IAAI,CAAC,KAAK,OAAO,IAAI,GAAG,GACtB,KAAK,OAAO,IAAI,KAAK;GAAE,GAAG;GAAI,GAAG;EAAG,CAAC;CAEzC;AACF"}
|
|
1
|
+
{"version":3,"file":"AabbTreeBroadPhase.js","names":[],"sources":["../../../src/broadphase/AabbTreeBroadPhase.ts"],"sourcesContent":["import type { AabbLike } from '@codexo/exojs';\nimport { DynamicAabbTree } from '@codexo/exojs';\n\nimport { aabbOverlap } from '../Aabb';\nimport type { Collider } from '../Collider';\nimport { pairKey } from '../ContactGraph';\nimport type { SpatialIndex } from '../query/SpatialIndex';\nimport { sortInPlace } from '../sort';\nimport type { BroadPhase, CandidatePair } from './BroadPhase';\n\n/**\n * Fixed fat-AABB margin (world px) applied to every collider's tree leaf.\n * Chosen relative to the engine's typical small-collider scale (test/example\n * shapes commonly span 10-30px): large enough that a few steps of ordinary\n * motion stay inside the margin (skipping the sync phase's remove+reinsert),\n * small enough it doesn't inflate fat AABBs into spurious candidates for\n * densely packed scenes. Fixed per the design's v1 scope (no velocity-based\n * predictive expansion) - revisit if benchmarks want a different value.\n */\nconst AABB_MARGIN = 4;\n\nconst byPairId = (p: CandidatePair, q: CandidatePair): number => p.a.id - q.a.id || p.b.id - q.b.id;\n\n/**\n * Dynamic-AABB-tree broad phase (Box2D-style), wrapping a generic\n * `DynamicAabbTree<Collider>`. Stateful across steps: a collider whose tight\n * AABB stays inside its stored fat AABB costs nothing to re-sync.\n *\n * Three phases per `computePairs` call: (1) sync every live collider (insert\n * new ones, reinsert moved ones, querying only around reinsertions to discover\n * NEW fat-overlap candidates; a collider whose AABB has not been rewritten\n * since its last sync is skipped outright via `Collider._treeDirty`, so a\n * sleeping world pays one flag read per collider); (2) one full pass over the\n * persistent pair set dropping any pair whose fat AABBs no longer overlap -\n * cheap (O(1) per pair) and run every step regardless of movement, exactly\n * mirroring Box2D's own `b2ContactManager::Collide` (see the design's\n * correctness argument for why a single global pass, not a per-moved-leaf\n * rescan, is both correct and the right complexity); (3) emit, keeping only\n * pairs whose *tight* AABBs overlap.\n *\n * Phases 2 and 3 are skipped whole when nothing they read has changed since the\n * last call: the emitted list is a function of the pair set and the tight AABBs\n * alone, and a step that rewrote no collider geometry touched neither.\n *\n * The persistent set is intentionally keyed on *fat* overlap so temporal\n * coherence carries across steps (a leaf that stays inside its fat AABB is\n * never re-discovered). Emitting is filtered to tight-AABB overlap so the\n * candidate set the caller sees is exact - the fat margin never leaks a false\n * positive downstream, and a fat-but-not-tight pair simply stays parked in the\n * set (costing one O(1) re-check per step) until the leaves separate.\n *\n * **Single-owner contract.** A collider's tree membership is tracked through its\n * `Collider._treeProxy` field, which - like `PhysicsBody._islandIndex` /\n * `_sleepTime` - is a single slot on the shared object. Exactly ONE\n * `AabbTreeBroadPhase` instance may track a given collider at a time (in\n * production that is the world's own `_broadPhase`, for the collider's whole\n * lifetime until `removeCollider`). Do NOT stand up a second, throwaway\n * `AabbTreeBroadPhase` over colliders another instance already owns (e.g. for\n * debug visualization): the second instance would clobber the first's\n * `_treeProxy` values and corrupt its tree. Use a plain brute-force scan for\n * such one-off, read-only needs instead.\n */\nexport class AabbTreeBroadPhase implements BroadPhase, SpatialIndex {\n private readonly _tree = new DynamicAabbTree<Collider>(AABB_MARGIN);\n private readonly _pairs = new Map<number, CandidatePair>();\n private readonly _scratchFatAabb: AabbLike = { minX: 0, minY: 0, maxX: 0, maxY: 0 };\n\n private _syncingCollider: Collider | null = null;\n private _syncingProxy = -1;\n\n /**\n * Whether anything the emitted candidate list is a function of has changed\n * since it was built: a collider AABB taken into the tree, or a pair added or\n * dropped. While it stays `false` the previous list is still exactly right.\n */\n private _emitStale = true;\n /** The buffer the current candidate list was emitted into, so a caller that swaps or clears its own buffer still gets a rebuild. */\n private _emitTarget: CandidatePair[] | null = null;\n private _emitLength = -1;\n\n private readonly _onNeighborFound = (payload: Collider, proxy: number): void => {\n if (proxy === this._syncingProxy) {\n return;\n }\n\n this._addPair(this._syncingCollider!, payload);\n };\n\n private readonly _dropIfStale = (pair: CandidatePair, key: number): void => {\n if (!this._tree.fatOverlaps(pair.a._treeProxy, pair.b._treeProxy)) {\n this._pairs.delete(key);\n this._emitStale = true;\n }\n };\n\n private _collectTarget: CandidatePair[] = [];\n private readonly _collectPair = (pair: CandidatePair): void => {\n // Emit only true (tight-AABB) overlaps; the persistent set is fat-keyed for\n // coherence, so a fat-but-not-tight pair is parked here, not a candidate.\n if (aabbOverlap(pair.a.aabb, pair.b.aabb)) {\n this._collectTarget.push(pair);\n }\n };\n\n private _queryTarget: Collider[] = [];\n private readonly _onQueryHit = (payload: Collider): void => {\n this._queryTarget.push(payload);\n };\n\n private readonly _resetTreeProxy = (payload: Collider): void => {\n payload._treeProxy = -1;\n };\n\n public computePairs(colliders: readonly Collider[], out: CandidatePair[]): CandidatePair[] {\n this.sync(colliders);\n\n // The candidate list is a function of the pair set and the tight AABBs\n // alone. A step in which no collider's geometry was rewritten and no pair\n // moved leaves both untouched, so the list already in `out` is the one both\n // passes below would rebuild - and a settled world is every such step.\n if (!this._emitStale && out === this._emitTarget && out.length === this._emitLength) {\n return out;\n }\n\n this._pairs.forEach(this._dropIfStale);\n\n out.length = 0;\n this._collectTarget = out;\n this._pairs.forEach(this._collectPair);\n sortInPlace(out, byPairId);\n\n this._emitStale = false;\n this._emitTarget = out;\n this._emitLength = out.length;\n\n return out;\n }\n\n /**\n * Insert every not-yet-tracked collider and reinsert every moved one (the\n * `computePairs` phase-1 loop, extracted so {@link queryAabb} callers can\n * force it independently of a full detection pass - see {@link SpatialIndex.sync}).\n */\n public sync(colliders: readonly Collider[]): void {\n for (const collider of colliders) {\n this._sync(collider);\n }\n }\n\n /** Colliders whose AABB overlaps `aabb`. Writes into `out` (cleared first) and returns it. */\n public queryAabb(aabb: Readonly<AabbLike>, out: Collider[]): Collider[] {\n out.length = 0;\n this._queryTarget = out;\n this._tree.query(aabb.minX, aabb.minY, aabb.maxX, aabb.maxY, this._onQueryHit);\n\n return out;\n }\n\n /** Colliders whose AABB the ray could cross within `maxDistance`, via the tree's own ray-cast prune. */\n public rayCast(originX: number, originY: number, dirX: number, dirY: number, maxDistance: number, callback: (collider: Collider) => void): void {\n this._tree.rayCast(originX, originY, dirX, dirY, maxDistance, callback);\n }\n\n /** Drop `collider`'s leaf and every pair referencing it. Called when a collider is destroyed. */\n public removeCollider(collider: Collider): void {\n if (collider._treeProxy === -1) {\n return;\n }\n\n this._emitStale = true;\n this._tree.remove(collider._treeProxy);\n collider._treeProxy = -1;\n\n for (const [key, pair] of this._pairs) {\n if (pair.a === collider || pair.b === collider) {\n this._pairs.delete(key);\n }\n }\n }\n\n public destroy(): void {\n // Reset `_treeProxy` on every still-tracked collider before tearing down the\n // tree: the single-owner contract only guarantees at most one owner AT A\n // TIME, so a collider handed to a later `AabbTreeBroadPhase` (sequential\n // reuse, not concurrent dual-ownership) must not carry a stale non-`-1`\n // proxy from this instance forward. `query` with infinite bounds visits\n // every leaf currently in the tree, allocation-free.\n this._tree.query(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, this._resetTreeProxy);\n this._tree.destroy();\n this._pairs.clear();\n this._emitStale = true;\n this._emitTarget = null;\n }\n\n private _sync(collider: Collider): void {\n const box = collider.aabb;\n\n if (collider._treeProxy === -1) {\n collider._treeDirty = false;\n this._emitStale = true;\n collider._treeProxy = this._tree.insert(box.minX, box.minY, box.maxX, box.maxY, collider);\n this._discoverNeighbors(collider);\n\n return;\n }\n\n // The leaf already holds this box, so `update` would do nothing but repeat\n // the containment test. A settled world is almost entirely this case.\n if (!collider._treeDirty) {\n return;\n }\n\n collider._treeDirty = false;\n this._emitStale = true;\n\n const moved = this._tree.update(collider._treeProxy, box.minX, box.minY, box.maxX, box.maxY);\n\n if (moved) {\n this._discoverNeighbors(collider);\n }\n }\n\n private _discoverNeighbors(collider: Collider): void {\n const proxy = collider._treeProxy;\n const fat = this._tree.fatAabbOf(proxy, this._scratchFatAabb);\n\n this._syncingCollider = collider;\n this._syncingProxy = proxy;\n this._tree.query(fat.minX, fat.minY, fat.maxX, fat.maxY, this._onNeighborFound);\n }\n\n private _addPair(a: Collider, b: Collider): void {\n // Two colliders of the same body are a compound shape, not a collision.\n // Rejecting the pair here rather than downstream also keeps it out of the\n // persistent set, so a compound body costs nothing per step - which matters\n // once convex decomposition attaches many edge-sharing colliders to one body.\n if (a.body === b.body) {\n return;\n }\n\n const lo = a.id < b.id ? a : b;\n const hi = a.id < b.id ? b : a;\n const key = pairKey(lo.id, hi.id);\n\n if (!this._pairs.has(key)) {\n this._pairs.set(key, { a: lo, b: hi });\n this._emitStale = true;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAmBA,MAAM,cAAc;AAEpB,MAAM,YAAY,GAAkB,MAA6B,EAAE,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCjG,IAAa,qBAAb,MAAoE;CAClE,AAAiB,QAAQ,IAAI,gBAA0B,WAAW;CAClE,AAAiB,yBAAS,IAAI,IAA2B;CACzD,AAAiB,kBAA4B;EAAE,MAAM;EAAG,MAAM;EAAG,MAAM;EAAG,MAAM;CAAE;CAElF,AAAQ,mBAAoC;CAC5C,AAAQ,gBAAgB;;;;;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MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,IAAI,MAAM,KAAK,gBAAgB;CAChF;CAEA,AAAQ,SAAS,GAAa,GAAmB;EAK/C,IAAI,EAAE,SAAS,EAAE,MACf;EAGF,MAAM,KAAK,EAAE,KAAK,EAAE,KAAK,IAAI;EAC7B,MAAM,KAAK,EAAE,KAAK,EAAE,KAAK,IAAI;EAC7B,MAAM,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAE;EAEhC,IAAI,CAAC,KAAK,OAAO,IAAI,GAAG,GAAG;GACzB,KAAK,OAAO,IAAI,KAAK;IAAE,GAAG;IAAI,GAAG;GAAG,CAAC;GACrC,KAAK,aAAa;EACpB;CACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Manifold.d.ts","sourceRoot":"","sources":["../../../src/collision/Manifold.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,MAAM,WAAW,aAAa;IAC5B,sCAAsC;IACtC,CAAC,EAAE,MAAM,CAAC;IACV,sCAAsC;IACtC,CAAC,EAAE,MAAM,CAAC;IACV,qCAAqC;IACrC,WAAW,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,EAAE,EAAE,MAAM,CAAC;CACZ;AAED;;;;;;GAMG;AACH,qBAAa,QAAQ;IACnB,wCAAwC;IACjC,OAAO,SAAK;IACnB,wCAAwC;IACjC,OAAO,SAAK;IACnB,6DAA6D;IACtD,UAAU,SAAK;IAEtB,SAAgB,MAAM,EAAE,SAAS,CAAC,aAAa,EAAE,aAAa,CAAC,CAG7D;IAEF,oCAAoC;IAC7B,KAAK,IAAI,IAAI;
|
|
1
|
+
{"version":3,"file":"Manifold.d.ts","sourceRoot":"","sources":["../../../src/collision/Manifold.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,MAAM,WAAW,aAAa;IAC5B,sCAAsC;IACtC,CAAC,EAAE,MAAM,CAAC;IACV,sCAAsC;IACtC,CAAC,EAAE,MAAM,CAAC;IACV,qCAAqC;IACrC,WAAW,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,EAAE,EAAE,MAAM,CAAC;CACZ;AAED;;;;;;GAMG;AACH,qBAAa,QAAQ;IACnB,wCAAwC;IACjC,OAAO,SAAK;IACnB,wCAAwC;IACjC,OAAO,SAAK;IACnB,6DAA6D;IACtD,UAAU,SAAK;IAEtB,SAAgB,MAAM,EAAE,SAAS,CAAC,aAAa,EAAE,aAAa,CAAC,CAG7D;IAEF,oCAAoC;IAC7B,KAAK,IAAI,IAAI,CAInB;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dispatch.d.ts","sourceRoot":"","sources":["../../../src/collision/dispatch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,EAAE,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAS9D,CAAC;AAEF,eAAO,MAAM,cAAc,QAAqC,CAAC;AAEjE;;;;;GAKG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;AAYhD,8DAA8D;AAC9D,eAAO,MAAM,cAAc,GAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"dispatch.d.ts","sourceRoot":"","sources":["../../../src/collision/dispatch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,EAAE,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAS9D,CAAC;AAEF,eAAO,MAAM,cAAc,QAAqC,CAAC;AAEjE;;;;;GAKG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;AAYhD,8DAA8D;AAC9D,eAAO,MAAM,cAAc,GAAI,CAAC,WAAW,aAAa,CAAC,SAAS,CAAC,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,KAAG,SAAS,CAAC,CAAC,CAWzG,CAAC;AAEF,4DAA4D;AAC5D,eAAO,MAAM,YAAY,GAAI,CAAC,WAAW,aAAa,CAAC,SAAS,CAAC,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,KAAG,SAAS,CAAC,CAAC,CAQvG,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,SAAS,CAAC,CAAC,CAAC,SAAS,SAAS,UAAU,SAAS,KAAG,CAAC,GAAG,SAKhG,CAAC;AAEF,gFAAgF;AAChF,eAAO,MAAM,SAAS,UAAW,SAAS,UAAU,SAAS,KAAG,OAAyD,CAAC;AAE1H,+FAA+F;AAC/F,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,SAAS,CAAC,CAAC,CAAC,SAAS,SAAS,UAAU,SAAS,KAAG,CAAC,GAAG,SACvB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"narrowphase.d.ts","sourceRoot":"","sources":["../../../src/collision/narrowphase.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGvD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAmB3C;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,
|
|
1
|
+
{"version":3,"file":"narrowphase.d.ts","sourceRoot":"","sources":["../../../src/collision/narrowphase.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGvD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAmB3C;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,MAAO,cAAc,KAAK,cAAc,YAAY,QAAQ,KAAG,OA0BlF,CAAC;AAkdF;;;GAGG;AACH,eAAO,MAAM,WAAW,MAAO,cAAc,KAAK,cAAc,KAAG,OAUlE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"segments.d.ts","sourceRoot":"","sources":["../../../src/collision/segments.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAW/C;;;GAGG;AACH,eAAO,MAAM,qBAAqB,
|
|
1
|
+
{"version":3,"file":"segments.d.ts","sourceRoot":"","sources":["../../../src/collision/segments.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAW/C;;;GAGG;AACH,eAAO,MAAM,qBAAqB,OAAQ,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,OAAO,SAAS,KAAG,MAkB9H,CAAC;AAEF,+DAA+D;AAC/D,eAAO,MAAM,2BAA2B,OAAQ,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,WAAW,SAAS,KAAG,MAOxI,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,6BAA6B,QACnC,MAAM,OACN,MAAM,OACN,MAAM,OACN,MAAM,OACN,MAAM,OACN,MAAM,OACN,MAAM,OACN,MAAM,QACL,SAAS,QACT,SAAS,KACd,MA4DF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sweep.d.ts","sourceRoot":"","sources":["../../../src/collision/sweep.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAKvD;;;GAGG;AACH,MAAM,WAAW,QAAQ;IACvB,4DAA4D;IAC5D,CAAC,EAAE,MAAM,CAAC;IACV,iHAAiH;IACjH,OAAO,EAAE,MAAM,CAAC;IAChB,oCAAoC;IACpC,OAAO,EAAE,MAAM,CAAC;CACjB;AASD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,YAAY,
|
|
1
|
+
{"version":3,"file":"sweep.d.ts","sourceRoot":"","sources":["../../../src/collision/sweep.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAKvD;;;GAGG;AACH,MAAM,WAAW,QAAQ;IACvB,4DAA4D;IAC5D,CAAC,EAAE,MAAM,CAAC;IACV,iHAAiH;IACjH,OAAO,EAAE,MAAM,CAAC;IAChB,oCAAoC;IACpC,OAAO,EAAE,MAAM,CAAC;CACjB;AASD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,YAAY,WAAY,cAAc,MAAM,MAAM,MAAM,MAAM,UAAU,cAAc,OAAO,QAAQ,KAAG,OAiBpH,CAAC;AAEF,oFAAoF;AACpF,eAAO,MAAM,QAAQ,WAAY,SAAS,UAAU,SAAS,KAAG,OAAiE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PhysicsDebugDraw.d.ts","sourceRoot":"","sources":["../../../src/debug/PhysicsDebugDraw.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAQhE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAGpD,6CAA6C;AAC7C,MAAM,WAAW,uBAAuB;IACtC,sEAAsE;IACtE,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,2GAA2G;IAC3G,QAAQ,C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C,8CAA8C;IAC9C,SAAgB,OAAO,EAAE,QAAQ,CAAC,uBAAuB,CAAC,CAAC;IAE3D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAe;IACtC,OAAO,CAAC,SAAS,CAAyB;IAC1C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAkB;IAC5C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuB;IAE9C,YAAmB,GAAG,EAAE,WAAW,EAAE,KAAK,EAAE,YAAY,EAAE,OAAO,GAAE,uBAA4B,EAc9F;IAED,IAAoB,QAAQ,IAAI,kBAAkB,CAEjD;IAEe,MAAM,IAAI,IAAI,CAE7B;IAEe,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI,CA0CnD;IAED,OAAO,CAAC,aAAa;IASL,OAAO,IAAI,IAAI,CAK9B;IAID,OAAO,CAAC,aAAa;IAYrB,OAAO,CAAC,YAAY;IAwDpB;;;;OAIG;IACH,OAAO,CAAC,cAAc;IAwBtB,OAAO,CAAC,WAAW;IAWnB,OAAO,CAAC,aAAa;IAUrB,OAAO,CAAC,YAAY;IAOpB;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,aAAa;IAqBrB,OAAO,CAAC,iBAAiB;IAazB,OAAO,CAAC,eAAe;CAkCxB"}
|
|
@@ -7,17 +7,19 @@ import { DebugLayer } from "@codexo/exojs/debug";
|
|
|
7
7
|
|
|
8
8
|
//#region src/debug/PhysicsDebugDraw.ts
|
|
9
9
|
const segments = 24;
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
10
|
+
/** `Color` channels are bytes, so a colour written as unit-range fractions has to be scaled to them. */
|
|
11
|
+
const unitColor = (r, g, b, a) => new Color(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255), a);
|
|
12
|
+
const colorStatic = unitColor(.3, .9, .4, .9);
|
|
13
|
+
const colorKinematic = unitColor(.4, .7, 1, .9);
|
|
14
|
+
const colorDynamic = unitColor(1, .85, .3, .9);
|
|
15
|
+
const colorSensor = unitColor(1, .3, 1, .9);
|
|
16
|
+
const colorAabb = unitColor(.5, .5, .5, .5);
|
|
17
|
+
const colorContact = unitColor(1, .2, .2, 1);
|
|
18
|
+
const colorNormal = unitColor(1, .6, .1, 1);
|
|
19
|
+
const colorCenter = unitColor(1, 1, 1, .9);
|
|
20
|
+
const colorBroadphase = unitColor(.2, .8, .8, .5);
|
|
21
|
+
const colorSleeping = unitColor(.45, .45, .5, .7);
|
|
22
|
+
const colorJoint = unitColor(.9, .5, 1, .8);
|
|
21
23
|
/**
|
|
22
24
|
* `DebugLayer` that visualises a {@link PhysicsWorld} - shapes, AABBs, contacts,
|
|
23
25
|
* normals, centres and broad-phase links. It reads the world's public state and
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PhysicsDebugDraw.js","names":[],"sources":["../../../src/debug/PhysicsDebugDraw.ts"],"sourcesContent":["import type { Application } from '@codexo/exojs';\nimport { Color, Graphics } from '@codexo/exojs';\nimport type { DebugLayerViewMode } from '@codexo/exojs/debug';\nimport { DebugLayer } from '@codexo/exojs/debug';\nimport type { RenderBackend } from '@codexo/exojs/renderer-sdk';\n\nimport { aabbOverlap } from '../Aabb';\nimport type { CandidatePair } from '../broadphase/BroadPhase';\nimport type { Collider } from '../Collider';\nimport { authoredCollider } from '../Collider';\nimport { Manifold } from '../collision/Manifold';\nimport { collide } from '../collision/narrowphase';\nimport type { PhysicsWorld } from '../PhysicsWorld';\nimport type { BodyType } from '../types';\n\n/** Toggles for the physics debug overlay. */\nexport interface PhysicsDebugDrawOptions {\n /** Collider shape outlines, coloured by body type. Default `true`. */\n drawShapes?: boolean;\n /** World AABBs of the geometry the broad phase holds - a chain as its per-edge leaves. Default `false`. */\n drawAabb?: boolean;\n /** Contact points. Default `false`. */\n drawContacts?: boolean;\n /** Contact normals. Default `false`. */\n drawNormals?: boolean;\n /** Body origins (centre markers). Default `false`. */\n drawCenters?: boolean;\n /** Broad-phase candidate links between AABB-overlapping leaves - a chain links per edge. Default `false`. */\n drawBroadphase?: boolean;\n /** Tint sleeping bodies distinctly (applies to the shape outline). Default `false`. */\n drawSleeping?: boolean;\n /** Lines connecting the bodies of each joint. Default `false`. */\n drawJoints?: boolean;\n}\n\nconst segments = 24;\n\nconst colorStatic = new Color(0.3, 0.9, 0.4, 0.9);\nconst colorKinematic = new Color(0.4, 0.7, 1, 0.9);\nconst colorDynamic = new Color(1, 0.85, 0.3, 0.9);\nconst colorSensor = new Color(1, 0.3, 1, 0.9);\nconst colorAabb = new Color(0.5, 0.5, 0.5, 0.5);\nconst colorContact = new Color(1, 0.2, 0.2, 1);\nconst colorNormal = new Color(1, 0.6, 0.1, 1);\nconst colorCenter = new Color(1, 1, 1, 0.9);\nconst colorBroadphase = new Color(0.2, 0.8, 0.8, 0.5);\nconst colorSleeping = new Color(0.45, 0.45, 0.5, 0.7);\nconst colorJoint = new Color(0.9, 0.5, 1, 0.8);\n\n/**\n * `DebugLayer` that visualises a {@link PhysicsWorld} - shapes, AABBs, contacts,\n * normals, centres and broad-phase links. It reads the world's public state and\n * recomputes contacts/broad-phase locally only while visible, so it never\n * perturbs the simulation and works over any backend. Lives in the `./debug`\n * subpath so it tree-shakes out of production bundles.\n */\nexport class PhysicsDebugDraw extends DebugLayer {\n /** Mutable option flags (read each frame). */\n public readonly options: Required<PhysicsDebugDrawOptions>;\n\n private readonly _world: PhysicsWorld;\n private _graphics: Graphics | null = null;\n private readonly _manifold = new Manifold();\n private readonly _pairs: CandidatePair[] = [];\n\n public constructor(app: Application, world: PhysicsWorld, options: PhysicsDebugDrawOptions = {}) {\n super(app);\n\n this._world = world;\n this.options = {\n drawShapes: options.drawShapes ?? true,\n drawAabb: options.drawAabb ?? false,\n drawContacts: options.drawContacts ?? false,\n drawNormals: options.drawNormals ?? false,\n drawCenters: options.drawCenters ?? false,\n drawBroadphase: options.drawBroadphase ?? false,\n drawSleeping: options.drawSleeping ?? false,\n drawJoints: options.drawJoints ?? false,\n };\n }\n\n public override get viewMode(): DebugLayerViewMode {\n return 'world';\n }\n\n public override update(): void {\n // All state is read from the world in render().\n }\n\n public override render(backend: RenderBackend): void {\n const gfx = (this._graphics ??= new Graphics());\n const options = this.options;\n\n gfx.clear();\n gfx.lineWidth = 1;\n\n if (options.drawBroadphase) {\n this._renderBroadphase(gfx);\n }\n\n if (options.drawAabb) {\n // The solver-side geometry, so a chain shows the leaves the broad phase\n // actually holds rather than the union box no phase ever tests.\n for (const collider of this._world.detectionGeometry) {\n this._strokeAabb(gfx, collider);\n }\n }\n\n if (options.drawShapes) {\n for (const collider of this._world.colliders) {\n this._strokeShape(gfx, collider);\n }\n }\n\n if (options.drawCenters) {\n gfx.lineColor = colorCenter;\n\n for (const body of this._world.bodies) {\n this._strokeCross(gfx, body.x, body.y, 4);\n }\n }\n\n if (options.drawContacts || options.drawNormals) {\n this._renderContacts(gfx);\n }\n\n if (options.drawJoints) {\n this._renderJoints(gfx);\n }\n\n gfx.render(backend);\n }\n\n private _renderJoints(gfx: Graphics): void {\n gfx.lineColor = colorJoint;\n\n for (const joint of this._world.joints) {\n gfx.moveTo(joint.bodyA.x, joint.bodyA.y);\n gfx.lineTo(joint.bodyB.x, joint.bodyB.y);\n }\n }\n\n public override destroy(): void {\n if (this._graphics !== null) {\n this._graphics.destroy();\n this._graphics = null;\n }\n }\n\n // ── helpers ────────────────────────────────────────────────────────────\n\n private _outlineColor(collider: Collider): Color {\n if (collider.isSensor) {\n return colorSensor;\n }\n\n if (this.options.drawSleeping && collider.body.isSleeping) {\n return colorSleeping;\n }\n\n return colorForType(collider.body.type);\n }\n\n private _strokeShape(gfx: Graphics, collider: Collider): void {\n gfx.lineColor = this._outlineColor(collider);\n\n if (collider.shape.type === 'circle') {\n const c = collider.worldCenter;\n const r = collider.shape.radius;\n\n this._strokeCircle(gfx, c.x, c.y, r);\n // A spoke to the surface conveys orientation once bodies rotate.\n gfx.moveTo(c.x, c.y);\n gfx.lineTo(c.x + collider.worldTransform.cos * r, c.y + collider.worldTransform.sin * r);\n\n return;\n }\n\n if (collider.shape.type === 'capsule') {\n this._strokeCapsule(gfx, collider.worldVertices, collider.shape.radius);\n\n return;\n }\n\n if (collider.shape.type === 'segment') {\n const ends = collider.worldVertices;\n\n gfx.moveTo(ends[0]!, ends[1]!);\n gfx.lineTo(ends[2]!, ends[3]!);\n\n return;\n }\n\n if (collider.shape.type === 'chain') {\n // A chain keeps no world geometry of its own - its edge proxies do, and\n // drawing them is drawing exactly what the solver sees, closing edge\n // included.\n for (const edge of collider.chainEdges ?? []) {\n const ends = edge.worldVertices;\n\n gfx.moveTo(ends[0]!, ends[1]!);\n gfx.lineTo(ends[2]!, ends[3]!);\n }\n\n return;\n }\n\n const verts = collider.worldVertices;\n const count = collider.shape.count;\n\n gfx.moveTo(verts[0]!, verts[1]!);\n\n for (let i = 1; i <= count; i++) {\n const j = i % count;\n\n gfx.lineTo(verts[j * 2]!, verts[j * 2 + 1]!);\n }\n }\n\n /**\n * Capsule outline: the two straight sides offset from the spine, joined by a\n * half-circle at each end. The arcs are approximated - a visualisation may\n * tessellate, the solver never does.\n */\n private _strokeCapsule(gfx: Graphics, spine: readonly number[], radius: number): void {\n const ax = spine[0]!;\n const ay = spine[1]!;\n const bx = spine[2]!;\n const by = spine[3]!;\n const along = Math.atan2(by - ay, bx - ax);\n const arcSegments = 12;\n\n gfx.moveTo(ax + Math.cos(along + Math.PI / 2) * radius, ay + Math.sin(along + Math.PI / 2) * radius);\n gfx.lineTo(bx + Math.cos(along + Math.PI / 2) * radius, by + Math.sin(along + Math.PI / 2) * radius);\n\n for (let i = 1; i <= arcSegments; i++) {\n const angle = along + Math.PI / 2 - (Math.PI * i) / arcSegments;\n\n gfx.lineTo(bx + Math.cos(angle) * radius, by + Math.sin(angle) * radius);\n }\n\n for (let i = 1; i <= arcSegments; i++) {\n const angle = along - Math.PI / 2 - (Math.PI * i) / arcSegments;\n\n gfx.lineTo(ax + Math.cos(angle) * radius, ay + Math.sin(angle) * radius);\n }\n }\n\n private _strokeAabb(gfx: Graphics, collider: Collider): void {\n const box = collider.aabb;\n\n gfx.lineColor = colorAabb;\n gfx.moveTo(box.minX, box.minY);\n gfx.lineTo(box.maxX, box.minY);\n gfx.lineTo(box.maxX, box.maxY);\n gfx.lineTo(box.minX, box.maxY);\n gfx.lineTo(box.minX, box.minY);\n }\n\n private _strokeCircle(gfx: Graphics, cx: number, cy: number, r: number): void {\n gfx.moveTo(cx + r, cy);\n\n for (let i = 1; i <= segments; i++) {\n const a = (i / segments) * Math.PI * 2;\n\n gfx.lineTo(cx + Math.cos(a) * r, cy + Math.sin(a) * r);\n }\n }\n\n private _strokeCross(gfx: Graphics, x: number, y: number, size: number): void {\n gfx.moveTo(x - size, y);\n gfx.lineTo(x + size, y);\n gfx.moveTo(x, y - size);\n gfx.lineTo(x, y + size);\n }\n\n /**\n * Recomputes candidate pairs from scratch for the current frame via a plain\n * brute-force AABB-overlap scan - deliberately NOT `AabbTreeBroadPhase`.\n * That class relies on `Collider._treeProxy`, a field shared on the\n * Collider object and valid only for whichever ONE broad phase currently\n * owns it (the world's own `NativePhysicsBackend`); a second instance here\n * would silently corrupt that ownership every time debug draw and a\n * physics step interleave. Debug-only/opt-in (tree-shaken from production\n * bundles), recomputed from scratch every visible frame regardless, so an\n * O(n^2) scan is an appropriate, fully self-contained trade for\n * correctness - it touches no state outside this method.\n */\n private _collectPairs(): void {\n // The solver-side geometry: a chain contributes one leaf per edge, which is\n // both what the broad phase pairs and what the narrow phase can solve.\n const colliders = this._world.detectionGeometry;\n const pairs = this._pairs;\n\n pairs.length = 0;\n\n for (let i = 0; i < colliders.length; i++) {\n const a = colliders[i]!;\n\n for (let j = i + 1; j < colliders.length; j++) {\n const b = colliders[j]!;\n\n if (a.body !== b.body && aabbOverlap(a.aabb, b.aabb)) {\n pairs.push(a.id < b.id ? { a, b } : { a: b, b: a });\n }\n }\n }\n }\n\n private _renderBroadphase(gfx: Graphics): void {\n this._collectPairs();\n gfx.lineColor = colorBroadphase;\n\n for (const pair of this._pairs) {\n const a = pair.a.aabb;\n const b = pair.b.aabb;\n\n gfx.moveTo((a.minX + a.maxX) * 0.5, (a.minY + a.maxY) * 0.5);\n gfx.lineTo((b.minX + b.maxX) * 0.5, (b.minY + b.maxY) * 0.5);\n }\n }\n\n private _renderContacts(gfx: Graphics): void {\n this._collectPairs();\n\n for (const pair of this._pairs) {\n const a = pair.a;\n const b = pair.b;\n\n // Material and sensor state live on the authored collider; an edge proxy\n // reads them from its chain.\n if (authoredCollider(a).isSensor || authoredCollider(b).isSensor) {\n continue;\n }\n\n if (!collide(a, b, this._manifold)) {\n continue;\n }\n\n for (let i = 0; i < this._manifold.pointCount; i++) {\n // i in 0..pointCount-1 and pointCount ≤ 2, so the point always exists.\n const point = i === 0 ? this._manifold.points[0] : this._manifold.points[1];\n\n if (this.options.drawContacts) {\n gfx.lineColor = colorContact;\n this._strokeCross(gfx, point.x, point.y, 3);\n }\n\n if (this.options.drawNormals) {\n gfx.lineColor = colorNormal;\n gfx.moveTo(point.x, point.y);\n gfx.lineTo(point.x + this._manifold.normalX * 12, point.y + this._manifold.normalY * 12);\n }\n }\n }\n }\n}\n\nconst colorForType = (type: BodyType): Color => {\n if (type === 'static') {\n return colorStatic;\n }\n\n return type === 'kinematic' ? colorKinematic : colorDynamic;\n};\n"],"mappings":";;;;;;;;AAmCA,MAAM,WAAW;AAEjB,MAAM,cAAc,IAAI,MAAM,IAAK,IAAK,IAAK,EAAG;AAChD,MAAM,iBAAiB,IAAI,MAAM,IAAK,IAAK,GAAG,EAAG;AACjD,MAAM,eAAe,IAAI,MAAM,GAAG,KAAM,IAAK,EAAG;AAChD,MAAM,cAAc,IAAI,MAAM,GAAG,IAAK,GAAG,EAAG;AAC5C,MAAM,YAAY,IAAI,MAAM,IAAK,IAAK,IAAK,EAAG;AAC9C,M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AAI,OAAO,MAAM,IAAI,KAAK,UAAU,UAAU,IAAI,MAAM,IAAI,KAAK,UAAU,UAAU,EAAE;IACzF;GACF;EACF;CACF;AACF;AAEA,MAAM,gBAAgB,SAA0B;CAC9C,IAAI,SAAS,UACX,OAAO;CAGT,OAAO,SAAS,cAAc,iBAAiB;AACjD"}
|
|
1
|
+
{"version":3,"file":"PhysicsDebugDraw.js","names":[],"sources":["../../../src/debug/PhysicsDebugDraw.ts"],"sourcesContent":["import type { Application } from '@codexo/exojs';\nimport { Color, Graphics } from '@codexo/exojs';\nimport type { DebugLayerViewMode } from '@codexo/exojs/debug';\nimport { DebugLayer } from '@codexo/exojs/debug';\nimport type { RenderBackend } from '@codexo/exojs/renderer-sdk';\n\nimport { aabbOverlap } from '../Aabb';\nimport type { CandidatePair } from '../broadphase/BroadPhase';\nimport type { Collider } from '../Collider';\nimport { authoredCollider } from '../Collider';\nimport { Manifold } from '../collision/Manifold';\nimport { collide } from '../collision/narrowphase';\nimport type { PhysicsWorld } from '../PhysicsWorld';\nimport type { BodyType } from '../types';\n\n/** Toggles for the physics debug overlay. */\nexport interface PhysicsDebugDrawOptions {\n /** Collider shape outlines, coloured by body type. Default `true`. */\n drawShapes?: boolean;\n /** World AABBs of the geometry the broad phase holds - a chain as its per-edge leaves. Default `false`. */\n drawAabb?: boolean;\n /** Contact points. Default `false`. */\n drawContacts?: boolean;\n /** Contact normals. Default `false`. */\n drawNormals?: boolean;\n /** Body origins (centre markers). Default `false`. */\n drawCenters?: boolean;\n /** Broad-phase candidate links between AABB-overlapping leaves - a chain links per edge. Default `false`. */\n drawBroadphase?: boolean;\n /** Tint sleeping bodies distinctly (applies to the shape outline). Default `false`. */\n drawSleeping?: boolean;\n /** Lines connecting the bodies of each joint. Default `false`. */\n drawJoints?: boolean;\n}\n\nconst segments = 24;\n\n/** `Color` channels are bytes, so a colour written as unit-range fractions has to be scaled to them. */\nconst unitColor = (r: number, g: number, b: number, a: number): Color => new Color(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255), a);\n\nconst colorStatic = unitColor(0.3, 0.9, 0.4, 0.9);\nconst colorKinematic = unitColor(0.4, 0.7, 1, 0.9);\nconst colorDynamic = unitColor(1, 0.85, 0.3, 0.9);\nconst colorSensor = unitColor(1, 0.3, 1, 0.9);\nconst colorAabb = unitColor(0.5, 0.5, 0.5, 0.5);\nconst colorContact = unitColor(1, 0.2, 0.2, 1);\nconst colorNormal = unitColor(1, 0.6, 0.1, 1);\nconst colorCenter = unitColor(1, 1, 1, 0.9);\nconst colorBroadphase = unitColor(0.2, 0.8, 0.8, 0.5);\nconst colorSleeping = unitColor(0.45, 0.45, 0.5, 0.7);\nconst colorJoint = unitColor(0.9, 0.5, 1, 0.8);\n\n/**\n * `DebugLayer` that visualises a {@link PhysicsWorld} - shapes, AABBs, contacts,\n * normals, centres and broad-phase links. It reads the world's public state and\n * recomputes contacts/broad-phase locally only while visible, so it never\n * perturbs the simulation and works over any backend. Lives in the `./debug`\n * subpath so it tree-shakes out of production bundles.\n */\nexport class PhysicsDebugDraw extends DebugLayer {\n /** Mutable option flags (read each frame). */\n public readonly options: Required<PhysicsDebugDrawOptions>;\n\n private readonly _world: PhysicsWorld;\n private _graphics: Graphics | null = null;\n private readonly _manifold = new Manifold();\n private readonly _pairs: CandidatePair[] = [];\n\n public constructor(app: Application, world: PhysicsWorld, options: PhysicsDebugDrawOptions = {}) {\n super(app);\n\n this._world = world;\n this.options = {\n drawShapes: options.drawShapes ?? true,\n drawAabb: options.drawAabb ?? false,\n drawContacts: options.drawContacts ?? false,\n drawNormals: options.drawNormals ?? false,\n drawCenters: options.drawCenters ?? false,\n drawBroadphase: options.drawBroadphase ?? false,\n drawSleeping: options.drawSleeping ?? false,\n drawJoints: options.drawJoints ?? false,\n };\n }\n\n public override get viewMode(): DebugLayerViewMode {\n return 'world';\n }\n\n public override update(): void {\n // All state is read from the world in render().\n }\n\n public override render(backend: RenderBackend): void {\n const gfx = (this._graphics ??= new Graphics());\n const options = this.options;\n\n gfx.clear();\n gfx.lineWidth = 1;\n\n if (options.drawBroadphase) {\n this._renderBroadphase(gfx);\n }\n\n if (options.drawAabb) {\n // The solver-side geometry, so a chain shows the leaves the broad phase\n // actually holds rather than the union box no phase ever tests.\n for (const collider of this._world.detectionGeometry) {\n this._strokeAabb(gfx, collider);\n }\n }\n\n if (options.drawShapes) {\n for (const collider of this._world.colliders) {\n this._strokeShape(gfx, collider);\n }\n }\n\n if (options.drawCenters) {\n gfx.lineColor = colorCenter;\n\n for (const body of this._world.bodies) {\n this._strokeCross(gfx, body.x, body.y, 4);\n }\n }\n\n if (options.drawContacts || options.drawNormals) {\n this._renderContacts(gfx);\n }\n\n if (options.drawJoints) {\n this._renderJoints(gfx);\n }\n\n gfx.render(backend);\n }\n\n private _renderJoints(gfx: Graphics): void {\n gfx.lineColor = colorJoint;\n\n for (const joint of this._world.joints) {\n gfx.moveTo(joint.bodyA.x, joint.bodyA.y);\n gfx.lineTo(joint.bodyB.x, joint.bodyB.y);\n }\n }\n\n public override destroy(): void {\n if (this._graphics !== null) {\n this._graphics.destroy();\n this._graphics = null;\n }\n }\n\n // ── helpers ────────────────────────────────────────────────────────────\n\n private _outlineColor(collider: Collider): Color {\n if (collider.isSensor) {\n return colorSensor;\n }\n\n if (this.options.drawSleeping && collider.body.isSleeping) {\n return colorSleeping;\n }\n\n return colorForType(collider.body.type);\n }\n\n private _strokeShape(gfx: Graphics, collider: Collider): void {\n gfx.lineColor = this._outlineColor(collider);\n\n if (collider.shape.type === 'circle') {\n const c = collider.worldCenter;\n const r = collider.shape.radius;\n\n this._strokeCircle(gfx, c.x, c.y, r);\n // A spoke to the surface conveys orientation once bodies rotate.\n gfx.moveTo(c.x, c.y);\n gfx.lineTo(c.x + collider.worldTransform.cos * r, c.y + collider.worldTransform.sin * r);\n\n return;\n }\n\n if (collider.shape.type === 'capsule') {\n this._strokeCapsule(gfx, collider.worldVertices, collider.shape.radius);\n\n return;\n }\n\n if (collider.shape.type === 'segment') {\n const ends = collider.worldVertices;\n\n gfx.moveTo(ends[0]!, ends[1]!);\n gfx.lineTo(ends[2]!, ends[3]!);\n\n return;\n }\n\n if (collider.shape.type === 'chain') {\n // A chain keeps no world geometry of its own - its edge proxies do, and\n // drawing them is drawing exactly what the solver sees, closing edge\n // included.\n for (const edge of collider.chainEdges ?? []) {\n const ends = edge.worldVertices;\n\n gfx.moveTo(ends[0]!, ends[1]!);\n gfx.lineTo(ends[2]!, ends[3]!);\n }\n\n return;\n }\n\n const verts = collider.worldVertices;\n const count = collider.shape.count;\n\n gfx.moveTo(verts[0]!, verts[1]!);\n\n for (let i = 1; i <= count; i++) {\n const j = i % count;\n\n gfx.lineTo(verts[j * 2]!, verts[j * 2 + 1]!);\n }\n }\n\n /**\n * Capsule outline: the two straight sides offset from the spine, joined by a\n * half-circle at each end. The arcs are approximated - a visualisation may\n * tessellate, the solver never does.\n */\n private _strokeCapsule(gfx: Graphics, spine: readonly number[], radius: number): void {\n const ax = spine[0]!;\n const ay = spine[1]!;\n const bx = spine[2]!;\n const by = spine[3]!;\n const along = Math.atan2(by - ay, bx - ax);\n const arcSegments = 12;\n\n gfx.moveTo(ax + Math.cos(along + Math.PI / 2) * radius, ay + Math.sin(along + Math.PI / 2) * radius);\n gfx.lineTo(bx + Math.cos(along + Math.PI / 2) * radius, by + Math.sin(along + Math.PI / 2) * radius);\n\n for (let i = 1; i <= arcSegments; i++) {\n const angle = along + Math.PI / 2 - (Math.PI * i) / arcSegments;\n\n gfx.lineTo(bx + Math.cos(angle) * radius, by + Math.sin(angle) * radius);\n }\n\n for (let i = 1; i <= arcSegments; i++) {\n const angle = along - Math.PI / 2 - (Math.PI * i) / arcSegments;\n\n gfx.lineTo(ax + Math.cos(angle) * radius, ay + Math.sin(angle) * radius);\n }\n }\n\n private _strokeAabb(gfx: Graphics, collider: Collider): void {\n const box = collider.aabb;\n\n gfx.lineColor = colorAabb;\n gfx.moveTo(box.minX, box.minY);\n gfx.lineTo(box.maxX, box.minY);\n gfx.lineTo(box.maxX, box.maxY);\n gfx.lineTo(box.minX, box.maxY);\n gfx.lineTo(box.minX, box.minY);\n }\n\n private _strokeCircle(gfx: Graphics, cx: number, cy: number, r: number): void {\n gfx.moveTo(cx + r, cy);\n\n for (let i = 1; i <= segments; i++) {\n const a = (i / segments) * Math.PI * 2;\n\n gfx.lineTo(cx + Math.cos(a) * r, cy + Math.sin(a) * r);\n }\n }\n\n private _strokeCross(gfx: Graphics, x: number, y: number, size: number): void {\n gfx.moveTo(x - size, y);\n gfx.lineTo(x + size, y);\n gfx.moveTo(x, y - size);\n gfx.lineTo(x, y + size);\n }\n\n /**\n * Recomputes candidate pairs from scratch for the current frame via a plain\n * brute-force AABB-overlap scan - deliberately NOT `AabbTreeBroadPhase`.\n * That class relies on `Collider._treeProxy`, a field shared on the\n * Collider object and valid only for whichever ONE broad phase currently\n * owns it (the world's own `NativePhysicsBackend`); a second instance here\n * would silently corrupt that ownership every time debug draw and a\n * physics step interleave. Debug-only/opt-in (tree-shaken from production\n * bundles), recomputed from scratch every visible frame regardless, so an\n * O(n^2) scan is an appropriate, fully self-contained trade for\n * correctness - it touches no state outside this method.\n */\n private _collectPairs(): void {\n // The solver-side geometry: a chain contributes one leaf per edge, which is\n // both what the broad phase pairs and what the narrow phase can solve.\n const colliders = this._world.detectionGeometry;\n const pairs = this._pairs;\n\n pairs.length = 0;\n\n for (let i = 0; i < colliders.length; i++) {\n const a = colliders[i]!;\n\n for (let j = i + 1; j < colliders.length; j++) {\n const b = colliders[j]!;\n\n if (a.body !== b.body && aabbOverlap(a.aabb, b.aabb)) {\n pairs.push(a.id < b.id ? { a, b } : { a: b, b: a });\n }\n }\n }\n }\n\n private _renderBroadphase(gfx: Graphics): void {\n this._collectPairs();\n gfx.lineColor = colorBroadphase;\n\n for (const pair of this._pairs) {\n const a = pair.a.aabb;\n const b = pair.b.aabb;\n\n gfx.moveTo((a.minX + a.maxX) * 0.5, (a.minY + a.maxY) * 0.5);\n gfx.lineTo((b.minX + b.maxX) * 0.5, (b.minY + b.maxY) * 0.5);\n }\n }\n\n private _renderContacts(gfx: Graphics): void {\n this._collectPairs();\n\n for (const pair of this._pairs) {\n const a = pair.a;\n const b = pair.b;\n\n // Material and sensor state live on the authored collider; an edge proxy\n // reads them from its chain.\n if (authoredCollider(a).isSensor || authoredCollider(b).isSensor) {\n continue;\n }\n\n if (!collide(a, b, this._manifold)) {\n continue;\n }\n\n for (let i = 0; i < this._manifold.pointCount; i++) {\n // i in 0..pointCount-1 and pointCount ≤ 2, so the point always exists.\n const point = i === 0 ? this._manifold.points[0] : this._manifold.points[1];\n\n if (this.options.drawContacts) {\n gfx.lineColor = colorContact;\n this._strokeCross(gfx, point.x, point.y, 3);\n }\n\n if (this.options.drawNormals) {\n gfx.lineColor = colorNormal;\n gfx.moveTo(point.x, point.y);\n gfx.lineTo(point.x + this._manifold.normalX * 12, point.y + this._manifold.normalY * 12);\n }\n }\n }\n }\n}\n\nconst colorForType = (type: BodyType): Color => {\n if (type === 'static') {\n return colorStatic;\n }\n\n return type === 'kinematic' ? colorKinematic : colorDynamic;\n};\n"],"mappings":";;;;;;;;AAmCA,MAAM,WAAW;;AAGjB,MAAM,aAAa,GAAW,GAAW,GAAW,MAAqB,IAAI,MAAM,KAAK,MAAM,IAAI,GAAG,GAAG,KAAK,MAAM,IAAI,GAAG,GAAG,KAAK,MAAM,IAAI,GAAG,GAAG,CAAC;AAEnJ,MAAM,cAAc,UAAU,IAAK,IAAK,IAAK,EAAG;AAChD,MAAM,iBAAiB,UAAU,IAAK,IAAK,GAAG,EAAG;AACjD,MAAM,eAAe,UAAU,GAAG,KAAM,IAAK,EAAG;AAChD,MAAM,cAAc,UAAU,GAAG,IAAK,GAAG,EAAG;AAC5C,MAAM,YAAY,UAAU,IAAK,IAAK,IAAK,EAAG;AAC9C,MAAM,eAAe,UAAU,GAAG,IAAK,IAAK,CAAC;AAC7C,MAAM,cAAc,UAAU,GAAG,IAAK,IAAK,CAAC;AAC5C,MAAM,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9C,IAAI,SAAS,UACX,OAAO;CAGT,OAAO,SAAS,cAAc,iBAAiB;AACjD"}
|
package/dist/esm/index.js
CHANGED
|
@@ -9,6 +9,7 @@ import { PrismaticJoint } from "./joints/PrismaticJoint.js";
|
|
|
9
9
|
import { RevoluteJoint } from "./joints/RevoluteJoint.js";
|
|
10
10
|
import { WeldJoint } from "./joints/WeldJoint.js";
|
|
11
11
|
import { WheelJoint } from "./joints/WheelJoint.js";
|
|
12
|
+
import { PhysicsBodyComponent } from "./PhysicsBodyComponent.js";
|
|
12
13
|
import { physicsBuildInfo } from "./physicsBuildInfo.js";
|
|
13
14
|
import { Shape } from "./shapes/Shape.js";
|
|
14
15
|
import { SegmentShape } from "./shapes/SegmentShape.js";
|
|
@@ -21,4 +22,4 @@ import { ChainShape } from "./shapes/ChainShape.js";
|
|
|
21
22
|
import { CircleShape } from "./shapes/CircleShape.js";
|
|
22
23
|
import { toConvexPolygonShapes } from "./shapes/convexParts.js";
|
|
23
24
|
|
|
24
|
-
export { BoxShape, CapsuleShape, ChainShape, CircleShape, Collider, DistanceJoint, Joint, MouseJoint, PhysicsBinding, PhysicsBody, PhysicsWorld, PolygonShape, PrismaticJoint, RevoluteJoint, SegmentShape, Shape, TimeStepper, WeldJoint, WheelJoint, defaultFilter, physicsBuildInfo, shouldCollide, toConvexPolygonShapes };
|
|
25
|
+
export { BoxShape, CapsuleShape, ChainShape, CircleShape, Collider, DistanceJoint, Joint, MouseJoint, PhysicsBinding, PhysicsBody, PhysicsBodyComponent, PhysicsWorld, PolygonShape, PrismaticJoint, RevoluteJoint, SegmentShape, Shape, TimeStepper, WeldJoint, WheelJoint, defaultFilter, physicsBuildInfo, shouldCollide, toConvexPolygonShapes };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { PointLike } from '@codexo/exojs';
|
|
2
2
|
import type { PhysicsBody } from '../PhysicsBody';
|
|
3
|
+
import type { JointOptions } from './Joint';
|
|
3
4
|
import { Joint } from './Joint';
|
|
4
5
|
/** Construction options for a {@link DistanceJoint}. */
|
|
5
|
-
export interface DistanceJointOptions {
|
|
6
|
+
export interface DistanceJointOptions extends JointOptions {
|
|
6
7
|
/** First body (often a static anchor). */
|
|
7
8
|
bodyA: PhysicsBody;
|
|
8
9
|
/** Second body. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DistanceJoint.d.ts","sourceRoot":"","sources":["../../../src/joints/DistanceJoint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAG/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,wDAAwD;AACxD,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"DistanceJoint.d.ts","sourceRoot":"","sources":["../../../src/joints/DistanceJoint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAG/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,KAAK,EAAE,YAAY,EAAiB,MAAM,SAAS,CAAC;AAC3D,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,wDAAwD;AACxD,MAAM,WAAW,oBAAqB,SAAQ,YAAY;IACxD,0CAA0C;IAC1C,KAAK,EAAE,WAAW,CAAC;IACnB,mBAAmB;IACnB,KAAK,EAAE,WAAW,CAAC;IACnB,4EAA4E;IAC5E,OAAO,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC;IAC9B,4EAA4E;IAC5E,OAAO,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC;IAC9B,4EAA4E;IAC5E,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,8EAA8E;IAC9E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,sEAAsE;IACtE,YAAY,CAAC,EAAE,MAAM,C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|
|
47
|
-
super(options.bodyA, options.bodyB);
|
|
47
|
+
super(options.bodyA, options.bodyB, options.collideConnected);
|
|
48
48
|
const ax = options.anchorA?.x ?? options.bodyA.x;
|
|
49
49
|
const ay = options.anchorA?.y ?? options.bodyA.y;
|
|
50
50
|
const bx = options.anchorB?.x ?? options.bodyB.x;
|
|
@@ -63,7 +63,7 @@ var DistanceJoint = class extends Joint {
|
|
|
63
63
|
this.maxLength = options.maxLength ?? (this._limited ? Infinity : this.length);
|
|
64
64
|
}
|
|
65
65
|
/** @internal */
|
|
66
|
-
_prepare(h) {
|
|
66
|
+
_prepare(h, rigid) {
|
|
67
67
|
const bodyA = this.bodyA;
|
|
68
68
|
const bodyB = this.bodyB;
|
|
69
69
|
this._active = this.enabled && !bodyA.isSleeping && !bodyB.isSleeping && (bodyA.invMass > 0 || bodyB.invMass > 0);
|
|
@@ -122,9 +122,9 @@ var DistanceJoint = class extends Joint {
|
|
|
122
122
|
this._massScale = a2 * a3;
|
|
123
123
|
this._impulseScale = a3;
|
|
124
124
|
} else {
|
|
125
|
-
this._biasRate = .
|
|
126
|
-
this._massScale =
|
|
127
|
-
this._impulseScale =
|
|
125
|
+
this._biasRate = rigid.biasRate;
|
|
126
|
+
this._massScale = rigid.massScale;
|
|
127
|
+
this._impulseScale = rigid.impulseScale;
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
/** @internal */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DistanceJoint.js","names":[],"sources":["../../../src/joints/DistanceJoint.ts"],"sourcesContent":["import type { PointLike } from '@codexo/exojs';\n\nimport { applyInverseTransform, applyTransform } from '../math';\nimport type { PhysicsBody } from '../PhysicsBody';\nimport { Joint } from './Joint';\n\n/** Construction options for a {@link DistanceJoint}. */\nexport interface DistanceJointOptions {\n /** First body (often a static anchor). */\n bodyA: PhysicsBody;\n /** Second body. */\n bodyB: PhysicsBody;\n /** World-space anchor on body A at creation. Default: body A's position. */\n anchorA?: Readonly<PointLike>;\n /** World-space anchor on body B at creation. Default: body B's position. */\n anchorB?: Readonly<PointLike>;\n /** Target distance between the anchors. Default: their initial distance. */\n length?: number;\n /** Soft-spring frequency in Hz; `0` (default) makes it a rigid constraint. */\n hertz?: number;\n /** Soft-spring damping ratio (used when `hertz > 0`). Default `1`. */\n dampingRatio?: number;\n /**\n * Minimum allowed distance. Specifying `minLength` and/or `maxLength` turns the\n * joint into a **rope/limit** (distance kept within `[minLength, maxLength]`,\n * slack between, rigid limits). Otherwise the joint is a rigid/soft **equality**\n * at `length`. Defaults to `0` in limit mode.\n */\n minLength?: number;\n /** Maximum allowed distance (the rope length). See {@link minLength}. Defaults to `Infinity` in limit mode. */\n maxLength?: number;\n}\n\n/** Reused output sink - physics steps single-threaded, so a shared scratch is safe. */\nconst scratch: PointLike = { x: 0, y: 0 };\n\n/**\n * Holds the anchor points on two bodies at a target {@link length} along their\n * connecting axis. With `hertz === 0` it is rigid; with `hertz > 0` it behaves\n * as a damped spring. Solved as a soft constraint in the sub-step loop, warm-\n * started across frames.\n */\nexport class DistanceJoint extends Joint {\n /** Target distance between the anchors. */\n public length: number;\n /** Soft-spring frequency in Hz (`0` = rigid). */\n public hertz: number;\n /** Soft-spring damping ratio. */\n public dampingRatio: number;\n /** Minimum allowed distance (rope/limit mode). */\n public minLength: number;\n /** Maximum allowed distance (rope/limit mode). */\n public maxLength: number;\n\n private readonly _limited: boolean;\n private _minImpulse = -Infinity;\n private _maxImpulse = Infinity;\n private readonly _localAnchorAx: number;\n private readonly _localAnchorAy: number;\n private readonly _localAnchorBx: number;\n private readonly _localAnchorBy: number;\n\n private _rAx = 0;\n private _rAy = 0;\n private _rBx = 0;\n private _rBy = 0;\n private _nx = 0;\n private _ny = 0;\n private _separation = 0;\n private _effMass = 0;\n private _biasRate = 0;\n private _massScale = 1;\n private _impulseScale = 0;\n private _impulse = 0;\n\n public constructor(options: DistanceJointOptions) {\n super(options.bodyA, options.bodyB);\n\n const ax = options.anchorA?.x ?? options.bodyA.x;\n const ay = options.anchorA?.y ?? options.bodyA.y;\n const bx = options.anchorB?.x ?? options.bodyB.x;\n const by = options.anchorB?.y ?? options.bodyB.y;\n\n applyInverseTransform(options.bodyA.transform, ax, ay, scratch);\n this._localAnchorAx = scratch.x;\n this._localAnchorAy = scratch.y;\n applyInverseTransform(options.bodyB.transform, bx, by, scratch);\n this._localAnchorBx = scratch.x;\n this._localAnchorBy = scratch.y;\n\n this.length = options.length ?? Math.hypot(bx - ax, by - ay);\n this.hertz = options.hertz ?? 0;\n this.dampingRatio = options.dampingRatio ?? 1;\n this._limited = options.minLength !== undefined || options.maxLength !== undefined;\n this.minLength = options.minLength ?? (this._limited ? 0 : this.length);\n this.maxLength = options.maxLength ?? (this._limited ? Infinity : this.length);\n }\n\n /** @internal */\n public override _prepare(h: number): void {\n const bodyA = this.bodyA;\n const bodyB = this.bodyB;\n\n this._active = this.enabled && !bodyA.isSleeping && !bodyB.isSleeping && (bodyA.invMass > 0 || bodyB.invMass > 0);\n\n if (!this._active) {\n return;\n }\n\n applyTransform(bodyA.transform, this._localAnchorAx, this._localAnchorAy, scratch);\n const pAx = scratch.x;\n const pAy = scratch.y;\n applyTransform(bodyB.transform, this._localAnchorBx, this._localAnchorBy, scratch);\n const pBx = scratch.x;\n const pBy = scratch.y;\n\n this._rAx = pAx - bodyA.worldCenterOfMassX;\n this._rAy = pAy - bodyA.worldCenterOfMassY;\n this._rBx = pBx - bodyB.worldCenterOfMassX;\n this._rBy = pBy - bodyB.worldCenterOfMassY;\n\n let dx = pBx - pAx;\n let dy = pBy - pAy;\n const len = Math.hypot(dx, dy);\n\n if (len > 1e-9) {\n dx /= len;\n dy /= len;\n } else {\n dx = 1;\n dy = 0;\n }\n\n this._nx = dx;\n this._ny = dy;\n\n const crA = this._rAx * dy - this._rAy * dx;\n const crB = this._rBx * dy - this._rBy * dx;\n const k = bodyA.invMass + bodyB.invMass + bodyA.invInertia * crA * crA + bodyB.invInertia * crB * crB;\n this._effMass = k > 0 ? 1 / k : 0;\n\n if (this._limited) {\n // Rope/limit: solve only the violated bound; slack between min and max.\n if (len > this.maxLength) {\n this._separation = len - this.maxLength;\n this._minImpulse = -Infinity; // pull together only (tension)\n this._maxImpulse = 0;\n } else if (len < this.minLength) {\n this._separation = len - this.minLength;\n this._minImpulse = 0; // push apart only (compression)\n this._maxImpulse = Infinity;\n } else {\n this._active = false; // slack — nothing to solve this frame\n this._impulse = 0;\n\n return;\n }\n } else {\n this._separation = len - this.length;\n this._minImpulse = -Infinity;\n this._maxImpulse = Infinity;\n }\n\n // Soft spring only for the equality joint; rope limits are rigid.\n if (this.hertz > 0 && !this._limited) {\n // Box2D-v3 soft factors from a damped spring at the sub-step `h`.\n const omega = 2 * Math.PI * this.hertz;\n const a1 = 2 * this.dampingRatio + h * omega;\n const a2 = h * omega * a1;\n const a3 = 1 / (1 + a2);\n\n this._biasRate = omega / a1;\n this._massScale = a2 * a3;\n this._impulseScale = a3;\n } else {\n // Rigid: full mass, Baumgarte position bias, no impulse decay.\n this._biasRate = 0.2 / h;\n this._massScale = 1;\n this._impulseScale = 0;\n }\n }\n\n /** @internal */\n public override _warmStart(): void {\n if (this._active) {\n this._applyAxisImpulse(this._impulse);\n }\n }\n\n /** @internal */\n public override _solve(useBias: boolean): void {\n if (!this._active) {\n return;\n }\n\n const bodyA = this.bodyA;\n const bodyB = this.bodyB;\n\n // Relative velocity of the anchors projected onto the axis.\n const vax = bodyA.linearVelocityX - bodyA.angularVelocity * this._rAy;\n const vay = bodyA.linearVelocityY + bodyA.angularVelocity * this._rAx;\n const vbx = bodyB.linearVelocityX - bodyB.angularVelocity * this._rBy;\n const vby = bodyB.linearVelocityY + bodyB.angularVelocity * this._rBx;\n const cdot = (vbx - vax) * this._nx + (vby - vay) * this._ny;\n\n const bias = useBias ? this._biasRate * this._separation : 0;\n const raw = -this._effMass * this._massScale * (cdot + bias) - this._impulseScale * this._impulse;\n // Clamp the accumulated impulse to the limit's sign range (±Infinity = equality, no clamp).\n const clamped = Math.min(this._maxImpulse, Math.max(this._minImpulse, this._impulse + raw));\n const applied = clamped - this._impulse;\n\n this._impulse = clamped;\n this._applyAxisImpulse(applied);\n }\n\n private _applyAxisImpulse(impulse: number): void {\n const jx = impulse * this._nx;\n const jy = impulse * this._ny;\n const bodyA = this.bodyA;\n const bodyB = this.bodyB;\n\n bodyA.linearVelocityX -= bodyA.invMass * jx;\n bodyA.linearVelocityY -= bodyA.invMass * jy;\n bodyA.angularVelocity -= bodyA.invInertia * (this._rAx * jy - this._rAy * jx);\n bodyB.linearVelocityX += bodyB.invMass * jx;\n bodyB.linearVelocityY += bodyB.invMass * jy;\n bodyB.angularVelocity += bodyB.invInertia * (this._rBx * jy - this._rBy * jx);\n }\n}\n"],"mappings":";;;;;AAkCA,MAAM,UAAqB;CAAE,GAAG;CAAG,GAAG;AAAE;;;;;;;AAQxC,IAAa,gBAAb,cAAmC,MAAM;;CAEvC,AAAO;;CAEP,AAAO;;CAEP,AAAO;;CAEP,AAAO;;CAEP,AAAO;CAEP,AAAiB;CACjB,AAAQ,cAAc;CACtB,AAAQ,cAAc;CACtB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,AAAQ,OAAO;CACf,AAAQ,OAAO;CACf,AAAQ,OAAO;CACf,AAAQ,OAAO;CACf,AAAQ,MAAM;CACd,AAAQ,MAAM;CACd,AAAQ,cAAc;CACtB,AAAQ,WAAW;CACnB,AAAQ,YAAY;CACpB,AAAQ,aAAa;CACrB,AAAQ,gBAAgB;CACxB,AAAQ,WAAW;CAEnB,AAAO,YAAY,SAA+B;EAChD,MAAM,QAAQ,OAAO,QAAQ,KAAK;EAElC,MAAM,KAAK,QAAQ,SAAS,KAAK,QAAQ,MAAM;EAC/C,MAAM,KAAK,QAAQ,SAAS,KAAK,QAAQ,MAAM;EAC/C,MAAM,KAAK,QAAQ,SAAS,KAAK,QAAQ,MAAM;EAC/C,MAAM,KAAK,QAAQ,SAAS,KAAK,QAAQ,MAA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AAK;EACjE,MAAM,MAAM,MAAM,kBAAkB,MAAM,kBAAkB,KAAK;EACjE,MAAM,MAAM,MAAM,kBAAkB,MAAM,kBAAkB,KAAK;EACjE,MAAM,MAAM,MAAM,kBAAkB,MAAM,kBAAkB,KAAK;EACjE,MAAM,QAAQ,MAAM,OAAO,KAAK,OAAO,MAAM,OAAO,KAAK;EAEzD,MAAM,OAAO,UAAU,KAAK,YAAY,KAAK,cAAc;EAC3D,MAAM,MAAM,CAAC,KAAK,WAAW,KAAK,cAAc,OAAO,QAAQ,KAAK,gBAAgB,KAAK;EAEzF,MAAM,UAAU,KAAK,IAAI,KAAK,aAAa,KAAK,IAAI,KAAK,aAAa,KAAK,WAAW,GAAG,CAAC;EAC1F,MAAM,UAAU,UAAU,KAAK;EAE/B,KAAK,WAAW;EAChB,KAAK,kBAAkB,OAAO;CAChC;CAEA,AAAQ,kBAAkB,SAAuB;EAC/C,MAAM,KAAK,UAAU,KAAK;EAC1B,MAAM,KAAK,UAAU,KAAK;EAC1B,MAAM,QAAQ,KAAK;EACnB,MAAM,QAAQ,KAAK;EAEnB,MAAM,mBAAmB,MAAM,UAAU;EACzC,MAAM,mBAAmB,MAAM,UAAU;EACzC,MAAM,mBAAmB,MAAM,cAAc,KAAK,OAAO,KAAK,KAAK,OAAO;EAC1E,MAAM,mBAAmB,MAAM,UAAU;EACzC,MAAM,mBAAmB,MAAM,UAAU;EACzC,MAAM,mBAAmB,MAAM,cAAc,KAAK,OAAO,KAAK,KAAK,OAAO;CAC5E;AACF"}
|
|
1
|
+
{"version":3,"file":"DistanceJoint.js","names":[],"sources":["../../../src/joints/DistanceJoint.ts"],"sourcesContent":["import type { PointLike } from '@codexo/exojs';\n\nimport { applyInverseTransform, applyTransform } from '../math';\nimport type { PhysicsBody } from '../PhysicsBody';\nimport type { JointOptions, JointSoftness } from './Joint';\nimport { Joint } from './Joint';\n\n/** Construction options for a {@link DistanceJoint}. */\nexport interface DistanceJointOptions extends JointOptions {\n /** First body (often a static anchor). */\n bodyA: PhysicsBody;\n /** Second body. */\n bodyB: PhysicsBody;\n /** World-space anchor on body A at creation. Default: body A's position. */\n anchorA?: Readonly<PointLike>;\n /** World-space anchor on body B at creation. Default: body B's position. */\n anchorB?: Readonly<PointLike>;\n /** Target distance between the anchors. Default: their initial distance. */\n length?: number;\n /** Soft-spring frequency in Hz; `0` (default) makes it a rigid constraint. */\n hertz?: number;\n /** Soft-spring damping ratio (used when `hertz > 0`). Default `1`. */\n dampingRatio?: number;\n /**\n * Minimum allowed distance. Specifying `minLength` and/or `maxLength` turns the\n * joint into a **rope/limit** (distance kept within `[minLength, maxLength]`,\n * slack between, rigid limits). Otherwise the joint is a rigid/soft **equality**\n * at `length`. Defaults to `0` in limit mode.\n */\n minLength?: number;\n /** Maximum allowed distance (the rope length). See {@link minLength}. Defaults to `Infinity` in limit mode. */\n maxLength?: number;\n}\n\n/** Reused output sink - physics steps single-threaded, so a shared scratch is safe. */\nconst scratch: PointLike = { x: 0, y: 0 };\n\n/**\n * Holds the anchor points on two bodies at a target {@link length} along their\n * connecting axis. With `hertz === 0` it is rigid; with `hertz > 0` it behaves\n * as a damped spring. Solved as a soft constraint in the sub-step loop, warm-\n * started across frames.\n */\nexport class DistanceJoint extends Joint {\n /** Target distance between the anchors. */\n public length: number;\n /** Soft-spring frequency in Hz (`0` = rigid). */\n public hertz: number;\n /** Soft-spring damping ratio. */\n public dampingRatio: number;\n /** Minimum allowed distance (rope/limit mode). */\n public minLength: number;\n /** Maximum allowed distance (rope/limit mode). */\n public maxLength: number;\n\n private readonly _limited: boolean;\n private _minImpulse = -Infinity;\n private _maxImpulse = Infinity;\n private readonly _localAnchorAx: number;\n private readonly _localAnchorAy: number;\n private readonly _localAnchorBx: number;\n private readonly _localAnchorBy: number;\n\n private _rAx = 0;\n private _rAy = 0;\n private _rBx = 0;\n private _rBy = 0;\n private _nx = 0;\n private _ny = 0;\n private _separation = 0;\n private _effMass = 0;\n private _biasRate = 0;\n private _massScale = 1;\n private _impulseScale = 0;\n private _impulse = 0;\n\n public constructor(options: DistanceJointOptions) {\n super(options.bodyA, options.bodyB, options.collideConnected);\n\n const ax = options.anchorA?.x ?? options.bodyA.x;\n const ay = options.anchorA?.y ?? options.bodyA.y;\n const bx = options.anchorB?.x ?? options.bodyB.x;\n const by = options.anchorB?.y ?? options.bodyB.y;\n\n applyInverseTransform(options.bodyA.transform, ax, ay, scratch);\n this._localAnchorAx = scratch.x;\n this._localAnchorAy = scratch.y;\n applyInverseTransform(options.bodyB.transform, bx, by, scratch);\n this._localAnchorBx = scratch.x;\n this._localAnchorBy = scratch.y;\n\n this.length = options.length ?? Math.hypot(bx - ax, by - ay);\n this.hertz = options.hertz ?? 0;\n this.dampingRatio = options.dampingRatio ?? 1;\n this._limited = options.minLength !== undefined || options.maxLength !== undefined;\n this.minLength = options.minLength ?? (this._limited ? 0 : this.length);\n this.maxLength = options.maxLength ?? (this._limited ? Infinity : this.length);\n }\n\n /** @internal */\n public override _prepare(h: number, rigid: JointSoftness): void {\n const bodyA = this.bodyA;\n const bodyB = this.bodyB;\n\n this._active = this.enabled && !bodyA.isSleeping && !bodyB.isSleeping && (bodyA.invMass > 0 || bodyB.invMass > 0);\n\n if (!this._active) {\n return;\n }\n\n applyTransform(bodyA.transform, this._localAnchorAx, this._localAnchorAy, scratch);\n const pAx = scratch.x;\n const pAy = scratch.y;\n applyTransform(bodyB.transform, this._localAnchorBx, this._localAnchorBy, scratch);\n const pBx = scratch.x;\n const pBy = scratch.y;\n\n this._rAx = pAx - bodyA.worldCenterOfMassX;\n this._rAy = pAy - bodyA.worldCenterOfMassY;\n this._rBx = pBx - bodyB.worldCenterOfMassX;\n this._rBy = pBy - bodyB.worldCenterOfMassY;\n\n let dx = pBx - pAx;\n let dy = pBy - pAy;\n const len = Math.hypot(dx, dy);\n\n if (len > 1e-9) {\n dx /= len;\n dy /= len;\n } else {\n dx = 1;\n dy = 0;\n }\n\n this._nx = dx;\n this._ny = dy;\n\n const crA = this._rAx * dy - this._rAy * dx;\n const crB = this._rBx * dy - this._rBy * dx;\n const k = bodyA.invMass + bodyB.invMass + bodyA.invInertia * crA * crA + bodyB.invInertia * crB * crB;\n this._effMass = k > 0 ? 1 / k : 0;\n\n if (this._limited) {\n // Rope/limit: solve only the violated bound; slack between min and max.\n if (len > this.maxLength) {\n this._separation = len - this.maxLength;\n this._minImpulse = -Infinity; // pull together only (tension)\n this._maxImpulse = 0;\n } else if (len < this.minLength) {\n this._separation = len - this.minLength;\n this._minImpulse = 0; // push apart only (compression)\n this._maxImpulse = Infinity;\n } else {\n this._active = false; // slack — nothing to solve this frame\n this._impulse = 0;\n\n return;\n }\n } else {\n this._separation = len - this.length;\n this._minImpulse = -Infinity;\n this._maxImpulse = Infinity;\n }\n\n // Soft spring only for the equality joint; rope limits are rigid.\n if (this.hertz > 0 && !this._limited) {\n // Box2D-v3 soft factors from a damped spring at the sub-step `h`.\n const omega = 2 * Math.PI * this.hertz;\n const a1 = 2 * this.dampingRatio + h * omega;\n const a2 = h * omega * a1;\n const a3 = 1 / (1 + a2);\n\n this._biasRate = omega / a1;\n this._massScale = a2 * a3;\n this._impulseScale = a3;\n } else {\n // The solver's own softness rather than a raw Baumgarte term; see\n // `softConstraint`. An unscaled bias relaxes no impulse, which a chain of\n // these constraints turns into energy.\n this._biasRate = rigid.biasRate;\n this._massScale = rigid.massScale;\n this._impulseScale = rigid.impulseScale;\n }\n }\n\n /** @internal */\n public override _warmStart(): void {\n if (this._active) {\n this._applyAxisImpulse(this._impulse);\n }\n }\n\n /** @internal */\n public override _solve(useBias: boolean): void {\n if (!this._active) {\n return;\n }\n\n const bodyA = this.bodyA;\n const bodyB = this.bodyB;\n\n // Relative velocity of the anchors projected onto the axis.\n const vax = bodyA.linearVelocityX - bodyA.angularVelocity * this._rAy;\n const vay = bodyA.linearVelocityY + bodyA.angularVelocity * this._rAx;\n const vbx = bodyB.linearVelocityX - bodyB.angularVelocity * this._rBy;\n const vby = bodyB.linearVelocityY + bodyB.angularVelocity * this._rBx;\n const cdot = (vbx - vax) * this._nx + (vby - vay) * this._ny;\n\n const bias = useBias ? this._biasRate * this._separation : 0;\n const raw = -this._effMass * this._massScale * (cdot + bias) - this._impulseScale * this._impulse;\n // Clamp the accumulated impulse to the limit's sign range (±Infinity = equality, no clamp).\n const clamped = Math.min(this._maxImpulse, Math.max(this._minImpulse, this._impulse + raw));\n const applied = clamped - this._impulse;\n\n this._impulse = clamped;\n this._applyAxisImpulse(applied);\n }\n\n private _applyAxisImpulse(impulse: number): void {\n const jx = impulse * this._nx;\n const jy = impulse * this._ny;\n const bodyA = this.bodyA;\n const bodyB = this.bodyB;\n\n bodyA.linearVelocityX -= bodyA.invMass * jx;\n bodyA.linearVelocityY -= bodyA.invMass * jy;\n bodyA.angularVelocity -= bodyA.invInertia * (this._rAx * jy - this._rAy * jx);\n bodyB.linearVelocityX += bodyB.invMass * jx;\n bodyB.linearVelocityY += bodyB.invMass * jy;\n bodyB.angularVelocity += bodyB.invInertia * (this._rBx * jy - this._rBy * jx);\n }\n}\n"],"mappings":";;;;;AAmCA,MAAM,UAAqB;CAAE,GAAG;CAAG,GAAG;AAAE;;;;;;;AAQxC,IAAa,gBAAb,cAAmC,MAAM;;CAEvC,AAAO;;CAEP,AAAO;;CAEP,AAAO;;CAEP,AAAO;;CAEP,AAAO;CAEP,AAAiB;CACjB,AAAQ,cAAc;CACtB,AAAQ,cAAc;CACtB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,AAAQ,OAAO;CACf,AAAQ,OAAO;CACf,AAAQ,OAAO;CACf,AAAQ,OAAO;CACf,AAAQ,MAAM;CACd,AAAQ,MAAM;CACd,AAAQ,cAAc;CACtB,AAAQ,WAAW;CACnB,AAAQ,YAAY;CACpB,AAAQ,aAAa;CACrB,AAAQ,gBAAgB;CACxB,AAAQ,WAAW;CAEnB,AAAO,YAAY,SAA+B;EAChD,MAAM,QAAQ,OAAO,QAAQ,OAAO,QAAQ,gBAAgB;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sBody';
|
|
2
|
+
/**
|
|
3
|
+
* Soft-constraint factors for one sub-step, as the solver's "soft step" uses
|
|
4
|
+
* them: how fast a position error is turned into a bias velocity, how much of
|
|
5
|
+
* the solved impulse is applied, and how much of the accumulated impulse is
|
|
6
|
+
* relaxed away again.
|
|
7
|
+
*/
|
|
8
|
+
export interface JointSoftness {
|
|
9
|
+
readonly biasRate: number;
|
|
10
|
+
readonly massScale: number;
|
|
11
|
+
readonly impulseScale: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Soft factors for a constraint of `hertz` stiffness at `dampingRatio`, over a
|
|
15
|
+
* sub-step of `h` seconds.
|
|
16
|
+
*
|
|
17
|
+
* The same formulation the contact solver uses. A joint asked for no stiffness
|
|
18
|
+
* of its own still gets these rather than a raw Baumgarte term: an unscaled
|
|
19
|
+
* `0.2 / h` bias applies the full position correction as a velocity every
|
|
20
|
+
* sub-step and relaxes none of the impulse it accumulated, which a single joint
|
|
21
|
+
* absorbs and a serial chain does not - each link hands its neighbour an
|
|
22
|
+
* over-correction that the next sub-step corrects again.
|
|
23
|
+
*/
|
|
24
|
+
export declare const softConstraint: (hertz: number, dampingRatio: number, h: number) => JointSoftness;
|
|
25
|
+
/** Options every joint accepts, whatever it constrains. */
|
|
26
|
+
export interface JointOptions {
|
|
27
|
+
/**
|
|
28
|
+
* Whether the two bodies this joint connects also collide with each other.
|
|
29
|
+
* Default `true`.
|
|
30
|
+
*
|
|
31
|
+
* `false` is what a chain, a ragdoll, a pendulum or a vehicle assembly
|
|
32
|
+
* usually wants, and what Box2D defaults to: a joint already decides how its
|
|
33
|
+
* two bodies may move relative to one another, so letting their touching
|
|
34
|
+
* colliders push each other apart at the same time gives the pair two
|
|
35
|
+
* constraint systems with different opinions, plus a contact per link that
|
|
36
|
+
* nothing needs.
|
|
37
|
+
*
|
|
38
|
+
* The default is nevertheless `true`, which is the behaviour every joint has
|
|
39
|
+
* had so far: an existing scene built against it would change shape under an
|
|
40
|
+
* update that flipped it. Nothing else argues for `true` any more - a chain
|
|
41
|
+
* of seven links and up used to gain energy without these contacts, and that
|
|
42
|
+
* defect is fixed in the joint solver rather than damped by them - so the
|
|
43
|
+
* default is a compatibility choice and is free to be revisited.
|
|
44
|
+
*
|
|
45
|
+
* Nothing about a contact is weakened under `true`: the narrow phase
|
|
46
|
+
* produces it, the contact graph holds it, collision events fire, a
|
|
47
|
+
* {@link ContactModifier} sees it and the solver resolves it, exactly as for
|
|
48
|
+
* any unjointed pair.
|
|
49
|
+
*
|
|
50
|
+
* It is fixed at construction, and it follows the joint's presence in the
|
|
51
|
+
* world rather than {@link Joint.enabled}: disabling a joint suspends its
|
|
52
|
+
* constraint, and a ragdoll whose joints are momentarily disabled must not
|
|
53
|
+
* start pushing its own limbs apart. Removing the joint from the world does
|
|
54
|
+
* let the pair collide again, and both bodies are woken so they respond to it.
|
|
55
|
+
*/
|
|
56
|
+
collideConnected?: boolean;
|
|
57
|
+
}
|
|
2
58
|
/**
|
|
3
59
|
* Base class for a two-body constraint solved alongside contacts in the
|
|
4
60
|
* sub-step loop. Concrete joints (distance, revolute, weld) implement the
|
|
@@ -11,10 +67,10 @@ export declare abstract class Joint {
|
|
|
11
67
|
readonly bodyA: PhysicsBody;
|
|
12
68
|
/** Second constrained body. */
|
|
13
69
|
readonly bodyB: PhysicsBody;
|
|
70
|
+
/** Whether {@link bodyA} and {@link bodyB} also collide with each other; see {@link JointOptions.collideConnected}. */
|
|
71
|
+
readonly collideConnected: boolean;
|
|
14
72
|
/** When `false`, the joint is skipped by the solver (but still tracked by the world). */
|
|
15
73
|
enabled: boolean;
|
|
16
|
-
|
|
17
|
-
protected _active: boolean;
|
|
18
|
-
protected constructor(bodyA: PhysicsBody, bodyB: PhysicsBody);
|
|
74
|
+
protected constructor(bodyA: PhysicsBody, bodyB: PhysicsBody, collideConnected?: boolean);
|
|
19
75
|
}
|
|
20
76
|
//# sourceMappingURL=Joint.d.ts.map
|