@khr3/core 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -25,7 +25,7 @@ npm install @khr3/core @pinkaphant/ecctrl @dimforge/rapier3d-compat @react-three
25
25
 
26
26
  | Package | Version | Required By |
27
27
  | --- | --- | --- |
28
- | `@dimforge/rapier3d-compat` | `^0.19.0` | `@khr3/core`, `@khr3/core/vehicle` |
28
+ | `@dimforge/rapier3d-compat` | `0.19.2` | `@khr3/core`, `@khr3/core/vehicle` |
29
29
  | `@pinkaphant/ecctrl` | `^2.0.4-beta.3` | Optional (`@khr3/core/vehicle` only) |
30
30
  | `@react-three/fiber` | `^9.0.0` | `@khr3/core`, `@khr3/core/vehicle` |
31
31
  | `@react-three/rapier` | `^2.2.0` | `@khr3/core`, `@khr3/core/vehicle` |
@@ -40,7 +40,7 @@ npm install @khr3/core @pinkaphant/ecctrl @dimforge/rapier3d-compat @react-three
40
40
  | Subpath | Exports |
41
41
  | --- | --- |
42
42
  | `@khr3/core` | `PhysicsWorld`, `createKhrGltfPlugin`, `useKhrGlb`, `loadKhrGlb`, `cloneKhrBundle`, `disposeKhrBundle`, `claimBundleMount`, `releaseBundleMount`, `exportKhrGlb`, `patchGlbJsonChunk`, `KhrCollider`, `KhrAssembly`, `JointRegistryContext`, `assertUnitScale`, `assertIdentityWorldScale`, `assertSceneIdentityWorldScale`, `KhrValidationError`, KHR & rotor tag types |
43
- | `@khr3/core/vehicle` | `KhrVehicle`, `solveWheelLoads`, `BENCH_GRAVITY`, `WHEEL_ASSEMBLY_DENSITY_KG_M3`, `KhrVehicleProps`, `KhrWheelConfig`, `SolvedWheel` |
43
+ | `@khr3/core/vehicle` | `KhrVehicle`, `solveWheelLoads`, `boundWheelModalDamping`, `BENCH_GRAVITY`, `WHEEL_ASSEMBLY_DENSITY_KG_M3`, `KhrVehicleProps`, `KhrWheelConfig`, `SolvedWheel` |
44
44
  | `@khr3/core/curves` | `compilePacejkaCurve`, `tireGripForSurface`, `engineHorsepowerForPeakTorque`, `TIRE_ARCHETYPES`, `TIRE_SLIP_CURVES`, `ENGINE_TORQUE_CURVES`, `STEER_SPEED_CURVES`, `TireArchetypePreset`, `CurveData`, `CurvePoint` |
45
45
 
46
46
  ## Quick Start
@@ -2,38 +2,166 @@ import { createContext as e, useCallback as t, useEffect as n, useLayoutEffect a
2
2
  import { BallCollider as a, CapsuleCollider as o, ConvexHullCollider as s, CuboidCollider as c, CylinderCollider as l, TrimeshCollider as u } from "@react-three/rapier";
3
3
  import * as d from "three";
4
4
  import { jsx as f } from "react/jsx-runtime";
5
+ // src/khrMassProps.ts
6
+ var p = new d.Vector3(), m = new d.Quaternion(), h = new d.Quaternion(), g = new d.Vector3(), _ = new d.Vector3(1, 1, 1), v = new d.Matrix4(), y = new d.Matrix4(), b = new d.Vector3();
7
+ function x(e, t = new d.Matrix4()) {
8
+ return p.fromArray(e.localPosition), m.fromArray(e.localQuaternion), t.compose(p, m, _);
9
+ }
10
+ function S(e, t, n, r, i, a) {
11
+ let o = [
12
+ [
13
+ e,
14
+ r,
15
+ i
16
+ ],
17
+ [
18
+ r,
19
+ t,
20
+ a
21
+ ],
22
+ [
23
+ i,
24
+ a,
25
+ n
26
+ ]
27
+ ], s = [
28
+ [
29
+ 1,
30
+ 0,
31
+ 0
32
+ ],
33
+ [
34
+ 0,
35
+ 1,
36
+ 0
37
+ ],
38
+ [
39
+ 0,
40
+ 0,
41
+ 1
42
+ ]
43
+ ];
44
+ for (let e = 0; e < 32; e++) {
45
+ let e = Math.max(o[0][0] + o[1][1] + o[2][2], 0), t = 0, n = 1, r = Math.abs(o[0][1]);
46
+ if (Math.abs(o[0][2]) > r && (t = 0, n = 2, r = Math.abs(o[0][2])), Math.abs(o[1][2]) > r && (t = 1, n = 2, r = Math.abs(o[1][2])), r <= 1e-12 * e) break;
47
+ let i = o[t][n], a = o[t][t], c = o[n][n], l = (c - a) / (2 * i), u = l >= 0 ? 1 / (l + Math.sqrt(1 + l * l)) : -1 / (-l + Math.sqrt(1 + l * l)), d = 1 / Math.sqrt(1 + u * u), f = u * d, p = 3 - t - n;
48
+ o[t][t] = d * d * a - 2 * f * d * i + f * f * c, o[n][n] = f * f * a + 2 * f * d * i + d * d * c, o[t][n] = o[n][t] = 0;
49
+ let m = o[p][t], h = o[p][n];
50
+ o[p][t] = o[t][p] = d * m - f * h, o[p][n] = o[n][p] = f * m + d * h;
51
+ for (let e = 0; e < 3; e++) {
52
+ let r = s[e][t], i = s[e][n];
53
+ s[e][t] = d * r - f * i, s[e][n] = f * r + d * i;
54
+ }
55
+ }
56
+ return s[0][0] * (s[1][1] * s[2][2] - s[1][2] * s[2][1]) - s[0][1] * (s[1][0] * s[2][2] - s[1][2] * s[2][0]) + s[0][2] * (s[1][0] * s[2][1] - s[1][1] * s[2][0]) < 0 && (s[0][2] = -s[0][2], s[1][2] = -s[1][2], s[2][2] = -s[2][2]), v.set(s[0][0], s[0][1], s[0][2], 0, s[1][0], s[1][1], s[1][2], 0, s[2][0], s[2][1], s[2][2], 0, 0, 0, 0, 1), m.setFromRotationMatrix(v).normalize(), {
57
+ inertia: [
58
+ o[0][0],
59
+ o[1][1],
60
+ o[2][2]
61
+ ],
62
+ inertiaFrame: [
63
+ m.x,
64
+ m.y,
65
+ m.z,
66
+ m.w
67
+ ]
68
+ };
69
+ }
70
+ function C(e, t) {
71
+ t ? (v.multiplyMatrices(t, x(e, y)).decompose(p, m, g), m.normalize()) : (p.fromArray(e.localPosition), m.fromArray(e.localQuaternion).normalize());
72
+ }
73
+ function w(e, t) {
74
+ let n = e.filter((e) => !e.isKinematic && e.mass > 0);
75
+ if (n.length === 0) return null;
76
+ let r = 0, i = 0, a = 0, o = 0;
77
+ for (let e of n) C(e, t), b.fromArray(e.centerOfMass).applyQuaternion(m), r += e.mass, i += (p.x + b.x) * e.mass, a += (p.y + b.y) * e.mass, o += (p.z + b.z) * e.mass;
78
+ if (!(r > 0)) return null;
79
+ let s = [
80
+ i / r,
81
+ a / r,
82
+ o / r
83
+ ], c = 0, l = 0, u = 0, d = 0, f = 0, g = 0, _ = v.elements;
84
+ for (let e of n) {
85
+ C(e, t), b.fromArray(e.centerOfMass).applyQuaternion(m);
86
+ let n = p.x + b.x - s[0], r = p.y + b.y - s[1], i = p.z + b.z - s[2];
87
+ v.makeRotationFromQuaternion(m.multiply(h.fromArray(e.inertiaOrientation)));
88
+ let [a, o, y] = e.inertiaDiagonal;
89
+ c += _[0] * _[0] * a + _[4] * _[4] * o + _[8] * _[8] * y + e.mass * (r * r + i * i), l += _[1] * _[1] * a + _[5] * _[5] * o + _[9] * _[9] * y + e.mass * (n * n + i * i), u += _[2] * _[2] * a + _[6] * _[6] * o + _[10] * _[10] * y + e.mass * (n * n + r * r), d += _[0] * _[1] * a + _[4] * _[5] * o + _[8] * _[9] * y - e.mass * n * r, f += _[0] * _[2] * a + _[4] * _[6] * o + _[8] * _[10] * y - e.mass * n * i, g += _[1] * _[2] * a + _[5] * _[6] * o + _[9] * _[10] * y - e.mass * r * i;
90
+ }
91
+ if (n.length === 1) {
92
+ let e = n[0];
93
+ return C(e, t), m.multiply(h.fromArray(e.inertiaOrientation)).normalize(), {
94
+ mass: r,
95
+ com: s,
96
+ inertia: [
97
+ e.inertiaDiagonal[0],
98
+ e.inertiaDiagonal[1],
99
+ e.inertiaDiagonal[2]
100
+ ],
101
+ inertiaFrame: [
102
+ m.x,
103
+ m.y,
104
+ m.z,
105
+ m.w
106
+ ]
107
+ };
108
+ }
109
+ let y = Math.max(c + l + u, 0);
110
+ if (Math.max(Math.abs(d), Math.abs(f), Math.abs(g)) <= 1e-12 * y) return {
111
+ mass: r,
112
+ com: s,
113
+ inertia: [
114
+ c,
115
+ l,
116
+ u
117
+ ],
118
+ inertiaFrame: [
119
+ 0,
120
+ 0,
121
+ 0,
122
+ 1
123
+ ]
124
+ };
125
+ let { inertia: x, inertiaFrame: w } = S(c, l, u, d, f, g);
126
+ return {
127
+ mass: r,
128
+ com: s,
129
+ inertia: x,
130
+ inertiaFrame: w
131
+ };
132
+ }
5
133
  // src/khrTypes.ts
6
- var p = class extends Error {
134
+ var T = class extends Error {
7
135
  constructor(e) {
8
136
  super(e), this.name = "KhrValidationError";
9
137
  }
10
- }, m = e(null), h = e(null), g = 1e-4;
11
- function _(e) {
138
+ }, E = e(null), D = e(null), O = e(!0), k = e(null), A = 1e-4;
139
+ function j(e) {
12
140
  return typeof e == "number" && Number.isFinite(e) && Math.abs(e - 1) <= 1e-9;
13
141
  }
14
- function v(e, t) {
142
+ function M(e, t) {
15
143
  if (e === void 0) return;
16
144
  let n = !1;
17
- if (typeof e == "number") n = _(e);
18
- else if (Array.isArray(e)) n = e.length === 3 && e.every(_);
145
+ if (typeof e == "number") n = j(e);
146
+ else if (Array.isArray(e)) n = e.length === 3 && e.every(j);
19
147
  else if (typeof e == "object" && e && "x" in e && "y" in e && "z" in e) {
20
148
  let t = e;
21
- n = _(t.x) && _(t.y) && _(t.z);
149
+ n = j(t.x) && j(t.y) && j(t.z);
22
150
  }
23
- if (!n) throw new p(`${t}: scale must be 1 (got ${typeof e == "number" && !Number.isFinite(e) ? String(e) : JSON.stringify(e)}); bake scale into the .glb at import`);
151
+ if (!n) throw new T(`${t}: scale must be 1 (got ${typeof e == "number" && !Number.isFinite(e) ? String(e) : JSON.stringify(e)}); bake scale into the .glb at import`);
24
152
  }
25
- function y(e, t) {
26
- if (!Array.isArray(e) || e.length !== 3 || !e.every((e) => typeof e == "number" && Number.isFinite(e))) throw new p(`Invalid ${t}: expected finite 3-tuple`);
153
+ function N(e, t) {
154
+ if (!Array.isArray(e) || e.length !== 3 || !e.every((e) => typeof e == "number" && Number.isFinite(e))) throw new T(`Invalid ${t}: expected finite 3-tuple`);
27
155
  return [
28
156
  e[0],
29
157
  e[1],
30
158
  e[2]
31
159
  ];
32
160
  }
33
- function b(e, t) {
34
- if (!Array.isArray(e) || e.length !== 4 || !e.every((e) => typeof e == "number" && Number.isFinite(e))) throw new p(`Invalid ${t}: expected finite 4-tuple`);
161
+ function P(e, t) {
162
+ if (!Array.isArray(e) || e.length !== 4 || !e.every((e) => typeof e == "number" && Number.isFinite(e))) throw new T(`Invalid ${t}: expected finite 4-tuple`);
35
163
  let n = Math.hypot(e[0], e[1], e[2], e[3]);
36
- if (!(n > 1e-6)) throw new p(`Invalid ${t}: zero-norm quaternion`);
164
+ if (!(n > 1e-6)) throw new T(`Invalid ${t}: zero-norm quaternion`);
37
165
  return [
38
166
  e[0] / n,
39
167
  e[1] / n,
@@ -41,18 +169,18 @@ function b(e, t) {
41
169
  e[3] / n
42
170
  ];
43
171
  }
44
- function x(e, t) {
172
+ function F(e, t) {
45
173
  let n = e.getWorldScale(new d.Vector3());
46
- if (!Number.isFinite(n.x) || !Number.isFinite(n.y) || !Number.isFinite(n.z) || Math.abs(n.x - 1) > g || Math.abs(n.y - 1) > g || Math.abs(n.z - 1) > g) throw new p(`Non-identity world scale [${n.x.toFixed(4)}, ${n.y.toFixed(4)}, ${n.z.toFixed(4)}] on "${t}"; bake scale=[1,1,1]`);
174
+ if (!Number.isFinite(n.x) || !Number.isFinite(n.y) || !Number.isFinite(n.z) || Math.abs(n.x - 1) > A || Math.abs(n.y - 1) > A || Math.abs(n.z - 1) > A) throw new T(`Non-identity world scale [${n.x.toFixed(4)}, ${n.y.toFixed(4)}, ${n.z.toFixed(4)}] on "${t}"; bake scale=[1,1,1]`);
47
175
  let r = e.scale;
48
- if (!Number.isFinite(r.x) || !Number.isFinite(r.y) || !Number.isFinite(r.z) || Math.abs(r.x - 1) > g || Math.abs(r.y - 1) > g || Math.abs(r.z - 1) > g) throw new p(`Non-identity local scale [${r.x.toFixed(4)}, ${r.y.toFixed(4)}, ${r.z.toFixed(4)}] on "${t}"; bake scale=[1,1,1]`);
176
+ if (!Number.isFinite(r.x) || !Number.isFinite(r.y) || !Number.isFinite(r.z) || Math.abs(r.x - 1) > A || Math.abs(r.y - 1) > A || Math.abs(r.z - 1) > A) throw new T(`Non-identity local scale [${r.x.toFixed(4)}, ${r.y.toFixed(4)}, ${r.z.toFixed(4)}] on "${t}"; bake scale=[1,1,1]`);
49
177
  }
50
- function S(e) {
178
+ function I(e) {
51
179
  e.traverse((t) => {
52
- x(t, t.name || (t === e ? "gltf.scene" : "unnamed_node"));
180
+ F(t, t.name || (t === e ? "gltf.scene" : "unnamed_node"));
53
181
  });
54
182
  }
55
- function C(e, t) {
183
+ function L(e, t) {
56
184
  let n = e.getWorldPosition(new d.Vector3()), r = e.getWorldQuaternion(new d.Quaternion()).invert(), i = t.getWorldPosition(new d.Vector3()).sub(n).applyQuaternion(r), a = r.multiply(t.getWorldQuaternion(new d.Quaternion()));
57
185
  return {
58
186
  localPosition: [
@@ -68,31 +196,31 @@ function C(e, t) {
68
196
  ]
69
197
  };
70
198
  }
71
- function w(e, t, n) {
72
- x(e, t);
199
+ function ee(e, t, n) {
200
+ F(e, t);
73
201
  let r = e.matrixWorld.clone().invert(), i = new d.Matrix4(), a = new d.Vector3(), o = [], s = [];
74
202
  if (e.traverse((e) => {
75
203
  let n = e;
76
204
  if (!n.isMesh || !n.geometry) return;
77
- x(n, n.name || t);
205
+ F(n, n.name || t);
78
206
  let c = n.geometry.getAttribute("position");
79
207
  if (!c || c.count === 0) return;
80
208
  i.multiplyMatrices(r, n.matrixWorld);
81
209
  let l = o.length / 3;
82
210
  for (let e = 0; e < c.count; e++) {
83
- if (a.fromBufferAttribute(c, e).applyMatrix4(i), !Number.isFinite(a.x) || !Number.isFinite(a.y) || !Number.isFinite(a.z)) throw new p(`Non-finite vertex in collider mesh "${t}"`);
211
+ if (a.fromBufferAttribute(c, e).applyMatrix4(i), !Number.isFinite(a.x) || !Number.isFinite(a.y) || !Number.isFinite(a.z)) throw new T(`Non-finite vertex in collider mesh "${t}"`);
84
212
  o.push(a.x, a.y, a.z);
85
213
  }
86
214
  let u = n.geometry.getIndex();
87
215
  if (u) for (let e = 0; e < u.count; e++) s.push(l + u.getX(e));
88
216
  else for (let e = 0; e < c.count; e++) s.push(l + e);
89
- }), o.length < 9) throw new p(`Collider node "${t}" has fewer than 3 vertices`);
217
+ }), o.length < 9) throw new T(`Collider node "${t}" has fewer than 3 vertices`);
90
218
  if (n) return {
91
219
  type: "convexHull",
92
220
  vertices: new Float32Array(o),
93
221
  meshNodeName: t
94
222
  };
95
- if (s.length < 3) throw new p(`Trimesh node "${t}" has fewer than 3 indices`);
223
+ if (s.length < 3) throw new T(`Trimesh node "${t}" has fewer than 3 indices`);
96
224
  return {
97
225
  type: "trimesh",
98
226
  vertices: new Float32Array(o),
@@ -100,7 +228,7 @@ function w(e, t, n) {
100
228
  meshNodeName: t
101
229
  };
102
230
  }
103
- function T(e) {
231
+ function R(e) {
104
232
  if (e.type === "box") {
105
233
  let [t, n, r] = e.size;
106
234
  return {
@@ -163,25 +291,25 @@ function T(e) {
163
291
  ]
164
292
  };
165
293
  }
166
- function E(e, t, n, r, i) {
167
- if (e.length === 0) throw new p(`Dynamic node "${i}" is missing motion.inertiaDiagonal and has no colliders`);
168
- let a = e.map((e) => T(e.shape)), o = 0;
294
+ function z(e, t, n, r, i) {
295
+ if (e.length === 0) throw new T(`Dynamic node "${i}" is missing motion.inertiaDiagonal and has no colliders`);
296
+ let a = e.map((e) => R(e.shape)), o = 0;
169
297
  for (let e of a) o += e.volume;
170
- let s = new d.Quaternion().fromArray(r).invert(), c = new d.Vector3().fromArray(n), l = new d.Quaternion(), u = new d.Quaternion(), f = new d.Vector3(), m = new d.Matrix4(), h = m.elements, g = 0, _ = 0, v = 0;
298
+ let s = new d.Quaternion().fromArray(r).invert(), c = new d.Vector3().fromArray(n), l = new d.Quaternion(), u = new d.Quaternion(), f = new d.Vector3(), p = new d.Matrix4(), m = p.elements, h = 0, g = 0, _ = 0;
171
299
  for (let n = 0; n < e.length; n++) {
172
- let r = e[n], i = a[n], p = o > 0 ? t * (i.volume / o) : t / e.length;
173
- l.fromArray(r.localQuaternion), f.copy(i.centroid).applyQuaternion(l).add(new d.Vector3().fromArray(r.localPosition)).sub(c).applyQuaternion(s), u.copy(s).multiply(l), m.makeRotationFromQuaternion(u);
174
- let [y, b, x] = i.unitInertia, S = h[0] * h[0] * y + h[4] * h[4] * b + h[8] * h[8] * x, C = h[1] * h[1] * y + h[5] * h[5] * b + h[9] * h[9] * x, w = h[2] * h[2] * y + h[6] * h[6] * b + h[10] * h[10] * x;
175
- g += p * (S + f.y * f.y + f.z * f.z), _ += p * (C + f.x * f.x + f.z * f.z), v += p * (w + f.x * f.x + f.y * f.y);
300
+ let r = e[n], i = a[n], v = o > 0 ? t * (i.volume / o) : t / e.length;
301
+ l.fromArray(r.localQuaternion), f.copy(i.centroid).applyQuaternion(l).add(new d.Vector3().fromArray(r.localPosition)).sub(c).applyQuaternion(s), u.copy(s).multiply(l), p.makeRotationFromQuaternion(u);
302
+ let [y, b, x] = i.unitInertia, S = m[0] * m[0] * y + m[4] * m[4] * b + m[8] * m[8] * x, C = m[1] * m[1] * y + m[5] * m[5] * b + m[9] * m[9] * x, w = m[2] * m[2] * y + m[6] * m[6] * b + m[10] * m[10] * x;
303
+ h += v * (S + f.y * f.y + f.z * f.z), g += v * (C + f.x * f.x + f.z * f.z), _ += v * (w + f.x * f.x + f.y * f.y);
176
304
  }
177
- if (!Number.isFinite(g) || !Number.isFinite(_) || !Number.isFinite(v) || g <= 0 || _ <= 0 || v <= 0) throw new p(`Dynamic node "${i}" has degenerate collider geometry; cannot compute positive motion.inertiaDiagonal`);
305
+ if (!Number.isFinite(h) || !Number.isFinite(g) || !Number.isFinite(_) || h <= 0 || g <= 0 || _ <= 0) throw new T(`Dynamic node "${i}" has degenerate collider geometry; cannot compute positive motion.inertiaDiagonal`);
178
306
  return [
307
+ h,
179
308
  g,
180
- _,
181
- v
309
+ _
182
310
  ];
183
311
  }
184
- function D(e) {
312
+ function B(e) {
185
313
  let t = 0, n = 0, r = 0, i = 0, a = new d.Quaternion(), o = new d.Vector3();
186
314
  for (let s of e) s.isKinematic || (t += s.mass, o.fromArray(s.centerOfMass).applyQuaternion(a.fromArray(s.localQuaternion)), n += (s.localPosition[0] + o.x) * s.mass, r += (s.localPosition[1] + o.y) * s.mass, i += (s.localPosition[2] + o.z) * s.mass);
187
315
  return {
@@ -198,47 +326,47 @@ function D(e) {
198
326
  };
199
327
  }
200
328
  // src/khrClone.ts
201
- var O = /* @__PURE__ */ new WeakSet(), k = /* @__PURE__ */ new WeakSet(), A = /* @__PURE__ */ new WeakSet();
202
- function j(e, t) {
203
- if (A.has(e.scene)) throw new p(`${t}: KhrAssetBundle "${e.scene.name || e.scene.uuid}" has already been disposed`);
204
- if (O.has(e.scene)) throw new p(`${t}: KhrAssetBundle "${e.scene.name || e.scene.uuid}" is already mounted; call cloneKhrBundle(template) per concurrent instance`);
205
- O.add(e.scene);
329
+ var V = /* @__PURE__ */ new WeakSet(), H = /* @__PURE__ */ new WeakSet(), U = /* @__PURE__ */ new WeakSet();
330
+ function W(e, t) {
331
+ if (U.has(e.scene)) throw new T(`${t}: KhrAssetBundle "${e.scene.name || e.scene.uuid}" has already been disposed`);
332
+ if (V.has(e.scene)) throw new T(`${t}: KhrAssetBundle "${e.scene.name || e.scene.uuid}" is already mounted; call cloneKhrBundle(template) per concurrent instance`);
333
+ V.add(e.scene);
206
334
  }
207
- function M(e) {
208
- O.delete(e.scene);
335
+ function G(e) {
336
+ V.delete(e.scene);
209
337
  }
210
- function N(e, t, n) {
338
+ function K(e, t, n) {
211
339
  if (t === e) return n;
212
340
  let r = [], i = t;
213
341
  for (; i && i !== e;) {
214
342
  let e = i.parent;
215
- if (!e) throw new p(`cloneKhrBundle: body node "${t.name}" is detached from bundle.scene`);
343
+ if (!e) throw new T(`cloneKhrBundle: body node "${t.name}" is detached from bundle.scene`);
216
344
  let n = e.children.indexOf(i);
217
- if (n < 0) throw new p(`cloneKhrBundle: child index missing for "${t.name}"`);
345
+ if (n < 0) throw new T(`cloneKhrBundle: child index missing for "${t.name}"`);
218
346
  r.push(n), i = e;
219
347
  }
220
348
  let a = n;
221
349
  for (let e = r.length - 1; e >= 0; e--) a = a.children[r[e]];
222
350
  return a;
223
351
  }
224
- function P(e) {
352
+ function q(e) {
225
353
  if (e) return {
226
354
  ...e,
227
355
  points: e.points.map((e) => ({ ...e }))
228
356
  };
229
357
  }
230
- function F(e) {
358
+ function J(e) {
231
359
  if (e) return {
232
360
  ...e,
233
361
  ...e.gearRatios ? { gearRatios: [...e.gearRatios] } : {},
234
- ...e.engineTorqueCurveData ? { engineTorqueCurveData: P(e.engineTorqueCurveData) } : {},
235
- ...e.steerAngleCurveData ? { steerAngleCurveData: P(e.steerAngleCurveData) } : {}
362
+ ...e.engineTorqueCurveData ? { engineTorqueCurveData: q(e.engineTorqueCurveData) } : {},
363
+ ...e.steerAngleCurveData ? { steerAngleCurveData: q(e.steerAngleCurveData) } : {}
236
364
  };
237
365
  }
238
- function I(e) {
239
- if (A.has(e.scene)) throw new p(`cloneKhrBundle: cannot clone disposed KhrAssetBundle "${e.scene.name || e.scene.uuid}"`);
366
+ function Y(e) {
367
+ if (U.has(e.scene)) throw new T(`cloneKhrBundle: cannot clone disposed KhrAssetBundle "${e.scene.name || e.scene.uuid}"`);
240
368
  let t = e.scene.clone(!0);
241
- return k.add(t), {
369
+ return H.add(t), {
242
370
  scene: t,
243
371
  bodies: e.bodies.map((n) => {
244
372
  let r = n.joint ? n.joint.drives.map((e) => ({ ...e })) : [];
@@ -269,36 +397,36 @@ function I(e) {
269
397
  anchor1: [...n.joint.anchor1],
270
398
  anchor2: [...n.joint.anchor2]
271
399
  } : null,
272
- object: N(e.scene, n.object, t)
400
+ object: K(e.scene, n.object, t)
273
401
  };
274
402
  }),
275
403
  wheels: e.wheels.map((n) => ({
276
404
  ...n,
277
- object: N(e.scene, n.object, t),
405
+ object: K(e.scene, n.object, t),
278
406
  position: [...n.position],
279
407
  ...n.quaternion ? { quaternion: [...n.quaternion] } : {},
280
- ...n.lngSlipRatioCurveData ? { lngSlipRatioCurveData: P(n.lngSlipRatioCurveData) } : {},
281
- ...n.latSlipRatioCurveData ? { latSlipRatioCurveData: P(n.latSlipRatioCurveData) } : {}
408
+ ...n.lngSlipRatioCurveData ? { lngSlipRatioCurveData: q(n.lngSlipRatioCurveData) } : {},
409
+ ...n.latSlipRatioCurveData ? { latSlipRatioCurveData: q(n.latSlipRatioCurveData) } : {}
282
410
  })),
283
411
  propellers: e.propellers.map((n) => ({
284
412
  ...n,
285
- object: N(e.scene, n.object, t),
413
+ object: K(e.scene, n.object, t),
286
414
  position: [...n.position],
287
415
  ...n.quaternion ? { quaternion: [...n.quaternion] } : {}
288
416
  })),
289
417
  sockets: Object.fromEntries(Object.entries(e.sockets).map(([e, t]) => [e, [...t]])),
290
418
  totalDynamicMass: e.totalDynamicMass,
291
419
  centerOfMass: [...e.centerOfMass],
292
- ...e.tune ? { tune: F(e.tune) } : {},
420
+ ...e.tune ? { tune: J(e.tune) } : {},
293
421
  ...e.droneTune ? { droneTune: { ...e.droneTune } } : {}
294
422
  };
295
423
  }
296
- function L(e) {
297
- if (O.delete(e.scene), k.has(e.scene) || A.has(e.scene)) {
298
- A.add(e.scene);
424
+ function X(e) {
425
+ if (V.delete(e.scene), H.has(e.scene) || U.has(e.scene)) {
426
+ U.add(e.scene);
299
427
  return;
300
428
  }
301
- A.add(e.scene);
429
+ U.add(e.scene);
302
430
  let t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Set(), i = (e) => {
303
431
  e.traverse((e) => {
304
432
  let i = e;
@@ -319,7 +447,7 @@ function L(e) {
319
447
  for (let t of e.propellers) i(t.object);
320
448
  }
321
449
  // src/KhrCollider.tsx
322
- function R({ c: e, position: r, quaternion: d, massProperties: p, ref: m }) {
450
+ function Z({ c: e, position: r, quaternion: d, massProperties: p, ref: m }) {
323
451
  let h = i(null), g = t((e) => {
324
452
  h.current = e, typeof m == "function" ? m(e) : m && (m.current = e);
325
453
  }, [m]);
@@ -365,7 +493,7 @@ function R({ c: e, position: r, quaternion: d, massProperties: p, ref: m }) {
365
493
  });
366
494
  }
367
495
  // src/BundleSceneHost.tsx
368
- function z(e) {
496
+ function Q(e) {
369
497
  let t = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map();
370
498
  for (let r of e) {
371
499
  let e = r.parent, i = /* @__PURE__ */ new Map(), a = -1;
@@ -384,7 +512,7 @@ function z(e) {
384
512
  }
385
513
  return n;
386
514
  }
387
- function B(e, t) {
515
+ function $(e, t) {
388
516
  e.position.copy(t.origPos), e.quaternion.copy(t.origQuat), e.scale.copy(t.origScale);
389
517
  let { origParent: n, origIndex: r, origOrder: i } = t;
390
518
  if (n) {
@@ -401,23 +529,23 @@ function B(e, t) {
401
529
  n.children.splice(a, 0, e);
402
530
  }
403
531
  }
404
- function V({ bundle: e, owner: t }) {
532
+ function te({ bundle: e, owner: t }) {
405
533
  let n = i(null);
406
534
  return r(() => {
407
535
  let r = n.current;
408
- if (r) return j(e, t), r.add(e.scene), () => {
409
- e.scene.parent === r && r.remove(e.scene), M(e);
536
+ if (r) return W(e, t), r.add(e.scene), () => {
537
+ e.scene.parent === r && r.remove(e.scene), G(e);
410
538
  };
411
539
  }, [e, t]), /* @__PURE__ */ f("group", { ref: n });
412
540
  }
413
- function H({ object: e, position: t, quaternion: n, origin: a }) {
541
+ function ne({ object: e, position: t, quaternion: n, origin: a }) {
414
542
  let o = i(null);
415
543
  return r(() => {
416
544
  let t = o.current;
417
545
  if (!t) return;
418
- let n = a ?? z([e]).get(e);
546
+ let n = a ?? Q([e]).get(e);
419
547
  return n.origParent && n.origParent.remove(e), e.position.set(0, 0, 0), e.quaternion.set(0, 0, 0, 1), e.scale.set(1, 1, 1), t.add(e), () => {
420
- e.parent === t && t.remove(e), B(e, n);
548
+ e.parent === t && t.remove(e), $(e, n);
421
549
  };
422
550
  }, [e, a]), /* @__PURE__ */ f("group", {
423
551
  ref: o,
@@ -425,4 +553,4 @@ function H({ object: e, position: t, quaternion: n, origin: a }) {
425
553
  quaternion: n
426
554
  });
427
555
  }
428
- export { y as _, R as a, h as b, L as c, S as d, v as f, w as g, C as h, B as i, M as l, E as m, H as n, j as o, D as p, z as r, I as s, V as t, x as u, b as v, p as x, m as y };
556
+ export { T as C, k as S, w as T, N as _, Z as a, O as b, X as c, I as d, M as f, ee as g, L as h, $ as i, G as l, z as m, ne as n, W as o, B as p, Q as r, Y as s, te as t, F as u, P as v, x as w, D as x, E as y };
@@ -3,9 +3,8 @@ import { type EcctrlVehicleHandle, type EcctrlVehicleProps, type ShapeCastWheelP
3
3
  import { type KhrAssetBundle, type KhrCarConfig, type KhrDroneConfig } from "./khrTypes.js";
4
4
  export { solveWheelLoads, type SolvedWheel } from "./wheelLoads.js";
5
5
  /**
6
- * Wheel + tire assembly density (kg/m³) used only when the .glb omits `ecctrlWheel.wheelDensity`.
7
- * 220 kg/m³ × a 0.357 m × 0.273 m cylinder (0.109 m³) = ~24 kg, a real passenger-car wheel+tire mass.
8
- * ecctrl's own default (1.5) assumes density-1 bodies and breaks heavy cars (verified: mid-air NaN + rollover tests).
6
+ * Wheel + tire assembly density (kg/m^3) used only when the .glb omits `ecctrlWheel.wheelDensity`.
7
+ * 220 kg/m^3 * a 0.357 m * 0.273 m cylinder (0.109 m^3) = ~24 kg, a real passenger-car wheel+tire mass.
9
8
  */
10
9
  export declare const WHEEL_ASSEMBLY_DENSITY_KG_M3 = 220;
11
10
  export type KhrWheelConfig = Partial<Omit<ShapeCastWheelProps, "ref" | "key" | "children">>;
@@ -1,9 +1,10 @@
1
- import { type ReactNode } from "react";
1
+ import { type ReactNode, type RefObject } from "react";
2
2
  import { type PhysicsProps } from "@react-three/rapier";
3
+ export type TimeControlValue = number | RefObject<number>;
3
4
  export interface PhysicsWorldProps extends Omit<PhysicsProps, "paused" | "timeStep" | "gravity" | "children"> {
4
5
  paused?: boolean;
5
- timeScale?: number;
6
- maxDelta?: number;
6
+ timeScale?: TimeControlValue;
7
+ maxDelta?: TimeControlValue;
7
8
  gravity?: [number, number, number];
8
9
  children: ReactNode;
9
10
  }
@@ -28,7 +28,7 @@ export interface PlannedCollider {
28
28
  /** Rest pose in the root body frame. */
29
29
  position: Vec3;
30
30
  quaternion: Quat;
31
- /** The owning body's authored mass, carried by its first collider so compound mass is exact. */
31
+ /** Composite mass properties carried by the stationary root's first collider; moving child colliders stay massless. */
32
32
  massProperties?: ColliderMass;
33
33
  }
34
34
  export interface PartPlan {
@@ -49,6 +49,8 @@ export interface RootPlan {
49
49
  mountInRoot: THREE.Matrix4;
50
50
  mountPosition: Vec3;
51
51
  mountQuaternion: Quat;
52
+ /** Composite mass properties in the root RigidBody frame when `colliders` is empty on a dynamic root. */
53
+ rootMass?: ColliderMass;
52
54
  colliders: PlannedCollider[];
53
55
  /** Parent-first order. */
54
56
  parts: PartPlan[];
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { PhysicsWorld, type PhysicsWorldProps } from "./PhysicsWorld.js";
1
+ export { PhysicsWorld, type PhysicsWorldProps, type TimeControlValue } from "./PhysicsWorld.js";
2
2
  export { KhrValidationError, createKhrGltfPlugin, loadKhrGlb, type EcctrlPropellerTag, type EcctrlWheelTag, type KhrAssetBundle, type KhrBodySpec, type KhrColliderShape, type KhrColliderSpec, type KhrExportNodeSpec, type KhrImplicitShape, type KhrJointDriveSpec, type KhrJointSpec, type LoadKhrGlbOptions, } from "./khrLoader.js";
3
3
  export { exportKhrGlb, patchGlbJsonChunk } from "./khrExporter.js";
4
4
  export { claimBundleMount, cloneKhrBundle, disposeKhrBundle, releaseBundleMount } from "./khrClone.js";