@khr3/core 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/vehicle.js CHANGED
@@ -1,18 +1,18 @@
1
- import { a as e, b as t, d as n, f as r, i, r as a, t as o, x as s } from "./BundleSceneHost-BXLH_LrO.js";
2
- import { useCallback as c, useContext as l, useEffect as u, useLayoutEffect as d, useMemo as f, useRef as p, useState as m } from "react";
3
- import { useFrame as h } from "@react-three/fiber";
4
- import { useRapier as g } from "@react-three/rapier";
5
- import * as _ from "three";
6
- import { Fragment as v, jsx as y, jsxs as b } from "react/jsx-runtime";
7
- import { useCustomGravity as x } from "@pinkaphant/ecctrl/gravity";
8
- import { EcctrlVehicle as S, ShapeCastWheel as C, ThrustPropeller as ee } from "@pinkaphant/ecctrl/vehicle";
1
+ import { C as e, S as t, T as n, a as r, b as i, d as a, f as o, i as s, r as c, t as l, x as u } from "./BundleSceneHost-CE3HPB8a.js";
2
+ import { useCallback as d, useContext as f, useEffect as p, useLayoutEffect as m, useMemo as h, useRef as g, useState as _ } from "react";
3
+ import { useFrame as v } from "@react-three/fiber";
4
+ import { useAfterPhysicsStep as y, useRapier as b } from "@react-three/rapier";
5
+ import * as x from "three";
6
+ import { jsx as S, jsxs as C } from "react/jsx-runtime";
7
+ import { useCustomGravity as w } from "@pinkaphant/ecctrl/gravity";
8
+ import { EcctrlVehicle as T, ShapeCastWheel as ee, ThrustPropeller as te } from "@pinkaphant/ecctrl/vehicle";
9
9
  // src/khrVehicleChassis.ts
10
- var w = new _.Euler(), T = new _.Quaternion(), E = new _.Quaternion(), D = new _.Matrix4(), O = new _.Vector3(), te = new _.Vector3(1, 1, 1);
11
- function ne(e, t) {
12
- if (t !== void 0) throw new s(`KhrVehicle: colliders prop cannot be overridden (got ${JSON.stringify(t)}); colliders are authored in the .glb`);
13
- r(e, "KhrVehicle");
10
+ var E = new x.Euler(), D = new x.Quaternion(), O = new x.Matrix4(), k = new x.Vector3(), A = new x.Vector3(1, 1, 1);
11
+ function j(t, n) {
12
+ if (n !== void 0) throw new e(`KhrVehicle: colliders prop cannot be overridden (got ${JSON.stringify(n)}); colliders are authored in the .glb`);
13
+ o(t, "KhrVehicle");
14
14
  }
15
- function re(e) {
15
+ function M(e) {
16
16
  if (typeof e == "number") return [
17
17
  e,
18
18
  e,
@@ -37,153 +37,57 @@ function re(e) {
37
37
  0
38
38
  ];
39
39
  }
40
- function ie(e, t) {
40
+ function N(e, t) {
41
41
  return t === void 0 ? e === void 0 ? [
42
42
  0,
43
43
  0,
44
44
  0,
45
45
  1
46
- ] : (typeof e == "number" ? w.set(e, e, e, "XYZ") : Array.isArray(e) ? w.set(e[0], e[1], e[2], e[3] ?? "XYZ") : typeof e == "object" && "x" in e ? w.copy(e) : w.set(0, 0, 0, "XYZ"), T.setFromEuler(w), [
47
- T.x,
48
- T.y,
49
- T.z,
50
- T.w
51
- ]) : (Array.isArray(t) ? T.set(t[0], t[1], t[2], t[3]) : typeof t == "object" && "x" in t ? T.copy(t) : T.identity(), T.lengthSq() > 0 && T.normalize(), [
52
- T.x,
53
- T.y,
54
- T.z,
55
- T.w
46
+ ] : (typeof e == "number" ? E.set(e, e, e, "XYZ") : Array.isArray(e) ? E.set(e[0], e[1], e[2], e[3] ?? "XYZ") : typeof e == "object" && "x" in e ? E.copy(e) : E.set(0, 0, 0, "XYZ"), D.setFromEuler(E), [
47
+ D.x,
48
+ D.y,
49
+ D.z,
50
+ D.w
51
+ ]) : (Array.isArray(t) ? D.set(t[0], t[1], t[2], t[3]) : typeof t == "object" && "x" in t ? D.copy(t) : D.identity(), D.lengthSq() > 0 && D.normalize(), [
52
+ D.x,
53
+ D.y,
54
+ D.z,
55
+ D.w
56
56
  ]);
57
57
  }
58
- function ae(e, t, n, r, i, a) {
59
- let o = [
60
- [
61
- e,
62
- r,
63
- i
64
- ],
65
- [
66
- r,
67
- t,
68
- a
69
- ],
70
- [
71
- i,
72
- a,
73
- n
74
- ]
75
- ], s = [
76
- [
77
- 1,
78
- 0,
79
- 0
80
- ],
81
- [
82
- 0,
83
- 1,
84
- 0
85
- ],
86
- [
87
- 0,
88
- 0,
89
- 1
90
- ]
91
- ];
92
- for (let e = 0; e < 32; e++) {
93
- let e = 0, t = 1, n = Math.abs(o[0][1]);
94
- if (Math.abs(o[0][2]) > n && (e = 0, t = 2, n = Math.abs(o[0][2])), Math.abs(o[1][2]) > n && (e = 1, t = 2, n = Math.abs(o[1][2])), n < 1e-12) break;
95
- let r = o[e][t], i = o[e][e], a = o[t][t], c = (a - i) / (2 * r), l = c >= 0 ? 1 / (c + Math.sqrt(1 + c * c)) : -1 / (-c + Math.sqrt(1 + c * c)), u = 1 / Math.sqrt(1 + l * l), d = l * u, f = 3 - e - t;
96
- o[e][e] = u * u * i - 2 * d * u * r + d * d * a, o[t][t] = d * d * i + 2 * d * u * r + u * u * a, o[e][t] = o[t][e] = 0;
97
- let p = o[f][e], m = o[f][t];
98
- o[f][e] = o[e][f] = u * p - d * m, o[f][t] = o[t][f] = d * p + u * m;
99
- for (let n = 0; n < 3; n++) {
100
- let r = s[n][e], i = s[n][t];
101
- s[n][e] = u * r - d * i, s[n][t] = d * r + u * i;
102
- }
103
- }
104
- 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]), D.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), T.setFromRotationMatrix(D).normalize(), {
105
- inertia: [
106
- o[0][0],
107
- o[1][1],
108
- o[2][2]
109
- ],
110
- inertiaFrame: [
111
- T.x,
112
- T.y,
113
- T.z,
114
- T.w
115
- ]
116
- };
117
- }
118
- function oe(e) {
119
- let t = e.filter((e) => !e.isKinematic), n = 0, r = 0, i = 0, a = 0;
120
- for (let e of t) T.fromArray(e.localQuaternion), O.fromArray(e.centerOfMass).applyQuaternion(T), n += e.mass, r += (e.localPosition[0] + O.x) * e.mass, i += (e.localPosition[1] + O.y) * e.mass, a += (e.localPosition[2] + O.z) * e.mass;
121
- if (!(n > 0)) throw new s("KhrVehicle: bundle has no dynamic KHR motion.mass; author a dynamic chassis node with mass");
122
- let o = [
123
- r / n,
124
- i / n,
125
- a / n
126
- ], c = 0, l = 0, u = 0, d = 0, f = 0, p = 0, m = D.elements;
127
- for (let e of t) {
128
- T.fromArray(e.localQuaternion), O.fromArray(e.centerOfMass).applyQuaternion(T);
129
- let t = e.localPosition[0] + O.x - o[0], n = e.localPosition[1] + O.y - o[1], r = e.localPosition[2] + O.z - o[2];
130
- D.makeRotationFromQuaternion(T.multiply(E.fromArray(e.inertiaOrientation)));
131
- let [i, a, s] = e.inertiaDiagonal;
132
- c += m[0] * m[0] * i + m[4] * m[4] * a + m[8] * m[8] * s + e.mass * (n * n + r * r), l += m[1] * m[1] * i + m[5] * m[5] * a + m[9] * m[9] * s + e.mass * (t * t + r * r), u += m[2] * m[2] * i + m[6] * m[6] * a + m[10] * m[10] * s + e.mass * (t * t + n * n), d += m[0] * m[1] * i + m[4] * m[5] * a + m[8] * m[9] * s - e.mass * t * n, f += m[0] * m[2] * i + m[4] * m[6] * a + m[8] * m[10] * s - e.mass * t * r, p += m[1] * m[2] * i + m[5] * m[6] * a + m[9] * m[10] * s - e.mass * n * r;
133
- }
134
- let h = Math.max(Math.abs(d), Math.abs(f), Math.abs(p)), g, _;
135
- if (h < 1e-8) g = [
136
- c,
137
- l,
138
- u
139
- ], _ = [
140
- 0,
141
- 0,
142
- 0,
143
- 1
144
- ];
145
- else if (t.length === 1) {
146
- let e = t[0];
147
- T.fromArray(e.localQuaternion).multiply(E.fromArray(e.inertiaOrientation)).normalize(), g = [
148
- e.inertiaDiagonal[0],
149
- e.inertiaDiagonal[1],
150
- e.inertiaDiagonal[2]
151
- ], _ = [
152
- T.x,
153
- T.y,
154
- T.z,
155
- T.w
156
- ];
157
- } else ({inertia: g, inertiaFrame: _} = ae(c, l, u, d, f, p));
158
- if (!(g[0] > 0 && g[1] > 0 && g[2] > 0)) throw new s(`KhrVehicle: authored inertia is non-positive [${g.join(", ")}]`);
159
- let v = t.find((e) => e.joint === null) ?? t[0], y = t.filter((e) => e.gravityFactor !== void 0).map((e) => e.gravityFactor), b;
160
- if (y.length > 0) {
161
- if (y.some((e) => Math.abs(e - y[0]) > 1e-6)) throw new s("KhrVehicle: dynamic chassis bodies have conflicting motion.gravityFactor values");
162
- b = y[0];
58
+ function ne(t) {
59
+ let r = t.filter((e) => !e.isKinematic), i = n(r);
60
+ if (!i) throw new e("KhrVehicle: bundle has no dynamic KHR motion.mass; author a dynamic chassis node with mass");
61
+ let { mass: a, com: o, inertia: s, inertiaFrame: c } = i;
62
+ if (!(s[0] > 0 && s[1] > 0 && s[2] > 0)) throw new e(`KhrVehicle: authored inertia is non-positive [${s.join(", ")}]`);
63
+ let l = r.find((e) => e.joint === null) ?? r[0], u = r.filter((e) => e.gravityFactor !== void 0).map((e) => e.gravityFactor), d;
64
+ if (u.length > 0) {
65
+ if (u.some((e) => Math.abs(e - u[0]) > 1e-6)) throw new e("KhrVehicle: dynamic chassis bodies have conflicting motion.gravityFactor values");
66
+ d = u[0];
163
67
  }
164
- let x = t.filter((e) => e.linearVelocity !== void 0), S;
165
- if (x.length === 1) S = x[0].linearVelocity;
166
- else if (x.length > 1) {
167
- let e = x.reduce((e, t) => e + t.mass, 0);
168
- S = [
169
- x.reduce((e, t) => e + t.linearVelocity[0] * t.mass, 0) / e,
170
- x.reduce((e, t) => e + t.linearVelocity[1] * t.mass, 0) / e,
171
- x.reduce((e, t) => e + t.linearVelocity[2] * t.mass, 0) / e
68
+ let f = r.filter((e) => e.linearVelocity !== void 0), p;
69
+ if (f.length === 1) p = f[0].linearVelocity;
70
+ else if (f.length > 1) {
71
+ let e = f.reduce((e, t) => e + t.mass, 0);
72
+ p = [
73
+ f.reduce((e, t) => e + t.linearVelocity[0] * t.mass, 0) / e,
74
+ f.reduce((e, t) => e + t.linearVelocity[1] * t.mass, 0) / e,
75
+ f.reduce((e, t) => e + t.linearVelocity[2] * t.mass, 0) / e
172
76
  ];
173
77
  }
174
- let C = t.filter((e) => e.angularVelocity !== void 0), ee = C.length === 1 ? C[0].angularVelocity : v.angularVelocity ?? C[0]?.angularVelocity;
78
+ let m = r.filter((e) => e.angularVelocity !== void 0), h = m.length === 1 ? m[0].angularVelocity : l.angularVelocity ?? m[0]?.angularVelocity;
175
79
  return {
176
- mass: n,
80
+ mass: a,
177
81
  com: o,
178
- inertia: g,
179
- inertiaFrame: _,
180
- ...S ? { linearVelocity: S } : {},
181
- ...ee ? { angularVelocity: ee } : {},
182
- ...b === void 0 ? {} : { gravityFactor: b }
82
+ inertia: s,
83
+ inertiaFrame: c,
84
+ ...p ? { linearVelocity: p } : {},
85
+ ...h ? { angularVelocity: h } : {},
86
+ ...d === void 0 ? {} : { gravityFactor: d }
183
87
  };
184
88
  }
185
- function se(e, t) {
186
- let n = new _.Vector3(), r = new _.Quaternion(), i = new _.Vector3(), a = new _.Quaternion(), o = new _.Quaternion();
89
+ function re(e, t) {
90
+ let n = new x.Vector3(), r = new x.Quaternion(), i = new x.Vector3(), a = new x.Quaternion(), o = new x.Quaternion();
187
91
  return e.flatMap((e) => (n.fromArray(e.localPosition), r.fromArray(e.localQuaternion), e.colliders.map((s, c) => (i.fromArray(s.localPosition).applyQuaternion(r).add(n), a.copy(r).multiply(o.fromArray(s.localQuaternion)), {
188
92
  key: `${e.nodeName}-${c}`,
189
93
  spec: {
@@ -205,131 +109,63 @@ function se(e, t) {
205
109
  }
206
110
  }))));
207
111
  }
208
- function ce(e, t, n, r) {
209
- let i = new _.Matrix4();
210
- for (let n = e; n && n !== t; n = n.parent) {
211
- if (!n.parent) throw new s(`KhrVehicle: rotor node "${e.name}" is detached from bundle.scene`);
212
- n.updateMatrix(), i.premultiply(n.matrix);
112
+ function P(t, n, r, i) {
113
+ let a = new x.Matrix4();
114
+ for (let r = t; r && r !== n; r = r.parent) {
115
+ if (!r.parent) throw new e(`KhrVehicle: rotor node "${t.name}" is detached from bundle.scene`);
116
+ r.updateMatrix(), a.premultiply(r.matrix);
213
117
  }
214
- return O.set(n[0], n[1], n[2]), r ? T.set(r[0], r[1], r[2], r[3]).normalize() : T.identity(), D.compose(O, T, te).invert().clone().multiply(i);
215
- }
216
- // src/khrVehicleSettle.ts
217
- function le() {
218
- return {
219
- sleepLatched: !1,
220
- wokeNoGlobalGrav: !1,
221
- settleFrames: 0,
222
- prevGravX: 0,
223
- prevGravY: 0,
224
- prevGravZ: 0,
225
- prevGravInit: !1,
226
- hitMap: /* @__PURE__ */ new Map(),
227
- compMap: /* @__PURE__ */ new Map()
228
- };
229
- }
230
- var k = new _.Vector3(), A = new _.Vector3(), j = new _.Vector3(), M = new _.Vector3(), N = new _.Vector3(), P = new _.Vector3(), F = [];
231
- function I(e, t) {
232
- let n = e;
233
- n.wheelLinVel = 0, n.wheelAngVel = 0, t.currLinVel.set(0, 0, 0), t.currAngVel.set(0, 0, 0);
234
- }
235
- function L(e) {
236
- e.floatImp.set(0, 0, 0), e.lngFricImp.set(0, 0, 0), e.latFricImp.set(0, 0, 0);
237
- }
238
- function R(e) {
239
- return !e.input.brake && !!(e.input.forward || e.input.backward || Math.abs(e.input.joystickL?.y ?? 0) > 1e-4);
118
+ return k.set(r[0], r[1], r[2]), i ? D.set(i[0], i[1], i[2], i[3]).normalize() : D.identity(), O.compose(k, D, A).invert().clone().multiply(a);
240
119
  }
241
- function z(e) {
242
- return e.input.brake ? !1 : R(e) || !!(e.input.steerLeft || e.input.steerRight || Math.abs(e.input.joystickL?.x ?? 0) > 1e-4);
243
- }
244
- function ue(e, t) {
245
- if (!t) return 0;
246
- let n = k.subVectors(e.supPos, e.rayPos).dot(e.rayDir);
247
- return Math.max(0, (e.rayLength ?? 0) - Math.max(0, n));
248
- }
249
- function B(e) {
250
- Number.isFinite(e.x + e.y + e.z) || e.set(0, 0, 0);
120
+ // src/khrVehicleSlots.tsx
121
+ var F = new x.Vector3(), ie = c;
122
+ function ae({ object: e, center: t, quaternion: n, scene: r, origin: i }) {
123
+ let a = g(null);
124
+ return m(() => {
125
+ let o = a.current?.parent;
126
+ if (!o) return;
127
+ let l = i ?? c([e]).get(e), u = P(e, r, t, n);
128
+ return l.origParent && l.origParent.remove(e), u.decompose(e.position, e.quaternion, e.scale), o.add(e), () => {
129
+ e.parent === o && o.remove(e), s(e, l);
130
+ };
131
+ }, [
132
+ e,
133
+ t,
134
+ n,
135
+ r,
136
+ i
137
+ ]), /* @__PURE__ */ S("group", { ref: a });
251
138
  }
252
- function V(e, t, n) {
253
- let r = 0;
254
- for (let i of e.wheelsInfo.values()) {
255
- let a = i.current;
256
- if (t.has(a.name ?? "") || !(n.get(a.id) ?? !1)) continue;
257
- let o = Math.min(1, Math.max(-1, a.rayHitNormal.dot(e.upAxis)));
258
- r = Math.max(r, Math.sqrt(Math.max(0, 1 - o * o)));
139
+ function I(e) {
140
+ let t = 0;
141
+ for (let n of e.wheelsInfo.values()) {
142
+ let r = n.current;
143
+ if (!r.enable || !r.rayHit) continue;
144
+ let i = Math.min(1, Math.max(-1, r.rayHitNormal.dot(e.upAxis))), a = Math.sqrt(Math.max(0, 1 - i * i));
145
+ a > t && (t = a);
259
146
  }
260
- return r;
147
+ return t;
261
148
  }
262
- function H(e) {
263
- let t = 0, n = !1;
264
- for (let r of e.veh.wheelsInfo.values()) {
265
- let i = r.current;
266
- !e.disabledWheels.has(i.name ?? "") && (e.hitMap.get(i.id) ?? !1) && (B(i.floatImp), B(i.lngFricImp), B(i.latFricImp), i.floatImp.dot(i.rayHitNormal) < 0 && i.floatImp.set(0, 0, 0), t++, i.isOnPlatform && (n = !0));
267
- }
268
- let r = e.maxSlopeSin ?? V(e.veh, e.disabledWheels, e.hitMap), i = e.rb.linvel(), a = e.rb.angvel();
269
- A.set(i.x, i.y, i.z);
270
- let o = A.dot(e.veh.upAxis), s = Math.sqrt(Math.max(0, A.lengthSq() - o * o)), c = Math.hypot(a.x, a.y, a.z), l = R(e.veh), u = z(e.veh), d = !!e.veh.input.brake;
271
- if (!l && !n && t > 0 && s < (!d && r < .025 ? .5 : .15) && c < .5) {
272
- let t = Math.max(0, 1 - s / .15), n = Math.min(1, Math.max(0, (.5 - s) / .35)), i = Math.max(e.gMag * e.dt, 1e-6);
273
- for (let a of e.veh.wheelsInfo.values()) {
274
- let o = a.current;
275
- if (e.disabledWheels.has(o.name ?? "") || !(e.hitMap.get(o.id) ?? !1)) continue;
276
- let s = o.floatImp.length(), c = s / i, l = o.rayHitPointVel.dot(o.lngAxis), u = -.45 * c * l, f = -.45 * c * o.rayHitPointVel.dot(o.latAxis);
277
- if (r < .025) {
278
- j.copy(o.floatImp).addScaledVector(e.veh.upAxis, -o.floatImp.dot(e.veh.upAxis));
279
- let t = j.length(), n = .03 * s;
280
- t > n && t > 1e-12 && j.multiplyScalar(n / t), u -= j.dot(o.lngAxis), f -= j.dot(o.latAxis);
281
- }
282
- let p = (o.tireGripFactor ?? 1.5) * s, m = Math.hypot(u, f);
283
- if (m > p && m > 1e-12) {
284
- let e = p / m;
285
- u *= e, f *= e;
286
- }
287
- if (r < .025) {
288
- let e = o.lngFricImp.dot(o.lngAxis), r = !d && e * l > 0;
289
- r && o.lngFricImp.set(0, 0, 0);
290
- let i = !d && !r && Math.abs(e) > c * Math.abs(l) + .03 * s, a = d ? t : i ? 1 : n;
291
- a > 0 && (M.copy(o.lngAxis).multiplyScalar(u), o.lngFricImp.lerp(M, a)), t > 0 && (M.copy(o.latAxis).multiplyScalar(f), o.latFricImp.lerp(M, t));
292
- } else t > 0 && o.latFricImp.multiplyScalar(1 - .55 * t);
293
- }
294
- } else if (!l && !d && !n && t > 0 && r < .025 && c < .5) for (let t of e.veh.wheelsInfo.values()) {
295
- let n = t.current;
296
- !e.disabledWheels.has(n.name ?? "") && (e.hitMap.get(n.id) ?? !1) && n.lngFricImp.dot(n.lngAxis) * n.rayHitPointVel.dot(n.lngAxis) > 0 && n.lngFricImp.set(0, 0, 0);
297
- }
298
- return e.state.settleFrames = !u && !n && t > 0 && r < .025 && s < .025 && c < .025 && Math.abs(o) < .25 ? e.state.settleFrames + 1 : 0, e.state.settleFrames >= 15;
299
- }
300
- function U(e, t, n, r, i, a) {
301
- if (!Number.isFinite(i) || i <= 0 || a <= 0 || t.isSleeping()) return;
302
- let o = t.translation();
303
- F.length = 0;
304
- for (let t of e.wheelsInfo.values()) {
305
- let r = t.current;
306
- n.has(r.name ?? "") || (P.set(r.rayPos.x - o.x, r.rayPos.y - o.y, r.rayPos.z - o.z), F.push({
307
- cur: r,
308
- x: P.dot(e.bodyXAxis),
309
- z: P.dot(e.bodyZAxis)
310
- }));
311
- }
312
- let s = i;
313
- for (let n = 0; n < F.length; n++) {
314
- let i = F[n];
315
- if (i.x <= .05) continue;
316
- let o = null, c = .45;
317
- for (let e = 0; e < F.length; e++) {
318
- let t = F[e];
319
- if (t.x >= -.05) continue;
320
- let n = Math.abs(i.z - t.z);
321
- n < c && (c = n, o = t);
322
- }
323
- if (!o) continue;
324
- let l = r.hitMap.get(i.cur.id) ?? !1, u = r.hitMap.get(o.cur.id) ?? !1;
325
- if (!l && !u) continue;
326
- let d = s * ((r.compMap.get(i.cur.id) ?? 0) - (r.compMap.get(o.cur.id) ?? 0)) * a;
327
- Math.abs(d) <= 1e-9 || (N.copy(e.upAxis).multiplyScalar(d), t.applyImpulseAtPoint(N, l ? i.cur.supPos : i.cur.rayPos, !1), N.negate(), t.applyImpulseAtPoint(N, u ? o.cur.supPos : o.cur.rayPos, !1));
328
- }
149
+ function oe({ vehicleRef: e, active: n, enableCustomGravity: r, spatialZeroGRef: i, setSpatialZeroG: a }) {
150
+ let o = f(t), s = g(0);
151
+ return y(() => {
152
+ if (!r) return;
153
+ let t = e.current?.body;
154
+ if (!t) return;
155
+ let n = t.translation();
156
+ F.set(n.x, n.y, n.z);
157
+ let o = w.getState().gravityField(F).lengthSq() <= 1e-12;
158
+ o !== i.current && (i.current = o, a(o));
159
+ }), v(() => {
160
+ let t = e.current, r = t?.body;
161
+ if (!t || !r || !n || o !== null && !(o.current > 0)) return;
162
+ let i = t.input, a = Math.abs(i.joystickL?.x ?? 0) > .001, c = Math.abs(i.joystickL?.y ?? 0) > .001, l = Math.abs(i.joystickR?.x ?? 0) > .001 || Math.abs(i.joystickR?.y ?? 0) > .001, u = !!(i.forward || i.backward), d = !!(i.steerLeft || i.steerRight || a), f = !!i.brake, p = !!(c || l || i.throttleUp || i.throttleDown || i.yawLeft || i.yawRight || i.pitchForward || i.pitchBackward || i.rollLeft || i.rollRight), m = !!u | (d ? 2 : 0) | (f ? 4 : 0) | (p ? 8 : 0), h = s.current;
163
+ s.current = m, r.isSleeping() ? (m & 11 & ~(h & 11) || h & 4 && !f) && r.wakeUp() : (!f && d || f && I(t) >= .025) && r.wakeUp();
164
+ }), null;
329
165
  }
330
166
  // src/wheelLoads.ts
331
- var W = 9.80665;
332
- function G(e) {
167
+ var L = 9.80665;
168
+ function R(e) {
333
169
  let t = e.travel - e.sag, n = e.quaternion;
334
170
  if (!n) return [
335
171
  e.position[0],
@@ -343,407 +179,218 @@ function G(e) {
343
179
  e.position[2] + l * t
344
180
  ];
345
181
  }
346
- function de(e, t, n, r = .7) {
347
- let i = e.length;
348
- if (i === 0) return [];
349
- if (!Number.isFinite(t) || t <= 0) throw new s(`solveWheelLoads: totalMassKg must be positive finite (got ${t})`);
350
- for (let t of e) if (!Number.isFinite(t.sag) || t.sag < 0 || t.springK === void 0 && t.sag <= 0) throw new s(`solveWheelLoads: wheel "${t.nodeName}" requires positive sag (got ${t.sag})`);
351
- let a = t * W;
352
- if (i === 1) {
353
- let n = e[0], i = n.springK ?? a / n.sag;
182
+ var z = new x.Quaternion(), B = new x.Matrix4();
183
+ function se(t, n, r, i = .7, a, o) {
184
+ let s = t.length;
185
+ if (s === 0) return [];
186
+ if (!Number.isFinite(n) || n <= 0) throw new e(`solveWheelLoads: totalMassKg must be positive finite (got ${n})`);
187
+ for (let n of t) if (!Number.isFinite(n.sag) || n.sag < 0 || n.springK === void 0 && n.sag <= 0) throw new e(`solveWheelLoads: wheel "${n.nodeName}" requires positive sag (got ${n.sag})`);
188
+ let c = n * L;
189
+ if (s === 1) {
190
+ let e = t[0], r = e.springK ?? c / e.sag;
354
191
  return [{
355
- tag: n,
356
- cornerMassKg: t,
357
- springK: i,
358
- dampingC: n.dampingC ?? 2 * r * Math.sqrt(i * t),
359
- mountPos: G(n)
192
+ tag: e,
193
+ cornerMassKg: n,
194
+ springK: r,
195
+ dampingC: e.dampingC ?? 2 * i * Math.sqrt(r * n),
196
+ mountPos: R(e)
360
197
  }];
361
198
  }
362
- let o = 0, c = 0;
363
- for (let t of e) o += t.position[0], c += t.position[2];
364
- o /= i, c /= i;
365
- let l = 0, u = 0, d = 0, f = e.map((e) => {
366
- let t = e.position[0] - o, n = e.position[2] - c;
367
- return l += t * t, u += t * n, d += n * n, [t, n];
368
- }), p = (n[0] - o) * a, m = (n[2] - c) * a, h = l + d, g = l * d - u * u, _ = 0, v = 0;
369
- if (g > 1e-6 * h * h) _ = (d * p - u * m) / g, v = (-u * p + l * m) / g;
370
- else if (h > 1e-12) {
371
- let e = 1 / (h * h);
372
- _ = (l * p + u * m) * e, v = (u * p + d * m) * e;
199
+ let l = 0, u = 0;
200
+ for (let e of t) l += e.position[0], u += e.position[2];
201
+ l /= s, u /= s;
202
+ let d = 0, f = 0, p = 0, m = t.map((e) => {
203
+ let t = e.position[0] - l, n = e.position[2] - u;
204
+ return d += t * t, f += t * n, p += n * n, [t, n];
205
+ }), h = (r[0] - l) * c, g = (r[2] - u) * c, _ = d + p, v = d * p - f * f, y = 0, b = 0;
206
+ if (v > 1e-6 * _ * _) y = (p * h - f * g) / v, b = (-f * h + d * g) / v;
207
+ else if (_ > 1e-12) {
208
+ let e = 1 / (_ * _);
209
+ y = (d * h + f * g) * e, b = (f * h + p * g) * e;
373
210
  }
374
- let y = a / i, b = Math.min(a * .05, y), x = f.map(([e, t]) => y + e * _ + t * v);
375
- if (x.some((e) => e < b)) {
376
- let e = new Uint8Array(i);
377
- for (let t = 0; t < i; t++) {
211
+ let x = c / s, S = Math.min(c * .05, x), C = m.map(([e, t]) => x + e * y + t * b);
212
+ if (C.some((e) => e < S)) {
213
+ let e = new Uint8Array(s);
214
+ for (let t = 0; t < s; t++) {
378
215
  let t = 0, n = 0, r = 0;
379
- for (let a = 0; a < i; a++) e[a] || x[a] <= b ? (e[a] = 1, x[a] = b, t += b) : (n += x[a], r++);
216
+ for (let i = 0; i < s; i++) e[i] || C[i] <= S ? (e[i] = 1, C[i] = S, t += S) : (n += C[i], r++);
380
217
  if (r === 0) break;
381
- let o = a - t, s = n > 1e-12 ? o / n : 0, c = !1;
382
- for (let t = 0; t < i; t++) e[t] || (x[t] = n > 1e-12 ? x[t] * s : o / r, x[t] < b - 1e-9 && (c = !0));
383
- if (!c) break;
218
+ let i = c - t, a = n > 1e-12 ? i / n : 0, o = !1;
219
+ for (let t = 0; t < s; t++) e[t] || (C[t] = n > 1e-12 ? C[t] * a : i / r, C[t] < S - 1e-9 && (o = !0));
220
+ if (!o) break;
384
221
  }
385
222
  }
386
- return e.map((e, t) => {
387
- let n = x[t], i = n / W, a = e.springK ?? n / e.sag;
223
+ let w = t.map((e, t) => {
224
+ let n = C[t], r = n / L, a = e.springK ?? n / e.sag;
388
225
  return {
389
226
  tag: e,
390
- cornerMassKg: i,
227
+ cornerMassKg: r,
391
228
  springK: a,
392
- dampingC: e.dampingC ?? 2 * r * Math.sqrt(a * i),
393
- mountPos: G(e)
229
+ dampingC: e.dampingC ?? 2 * i * Math.sqrt(a * r),
230
+ mountPos: R(e)
394
231
  };
395
232
  });
396
- }
397
- var fe = new _.Quaternion(), K = new _.Vector3(), pe = new _.Quaternion(), me = new _.Matrix4();
398
- function he(e, t, n, r, i, a = .7) {
399
- if (i <= 0 || t.isSleeping()) return;
400
- let o = t.worldCom(), s = t.rotation();
401
- fe.set(s.x, s.y, s.z, s.w).invert();
402
- let c = 0, l = 0, u = 0, d = 0, f = 0, p = 0, m = 0;
403
- for (let t of e.wheelsInfo.values()) {
404
- let e = t.current;
405
- if (n.has(e.name ?? "") || !(r.hitMap.get(e.id) ?? !1)) continue;
406
- c++, K.set(e.supPos.x - o.x, e.supPos.y - o.y, e.supPos.z - o.z).applyQuaternion(fe);
407
- let i = K.x * K.x, a = K.z * K.z, s = e.dampingC ?? 0, h = e.springK ?? 0;
408
- l += s, u += s * i, d += s * a, f += h, p += h * i, m += h * a;
409
- }
410
- if (c === 0) return;
411
- let h = t.mass(), g = t.principalInertia(), _ = t.principalInertiaLocalFrame();
412
- pe.set(_.x, _.y, _.z, _.w), me.makeRotationFromQuaternion(pe);
413
- let v = me.elements, y = v[0] * v[0] * g.x + v[4] * v[4] * g.y + v[8] * v[8] * g.z, b = v[2] * v[2] * g.x + v[6] * v[6] * g.y + v[10] * v[10] * g.z, x = Math.max(h > 1e-6 ? l * i / h : 0, y > 1e-6 ? d * i / y : 0, b > 1e-6 ? u * i / b : 0), S = Math.max(h > 1e-6 ? f * i * i / h : 0, y > 1e-6 ? m * i * i / y : 0, b > 1e-6 ? p * i * i / b : 0), C = Math.min(a, Math.max(.2, 1.8 - .5 * S)), ee = x > C ? C / x : 1;
414
- for (let t of e.wheelsInfo.values()) {
415
- let e = t.current;
416
- if (n.has(e.name ?? "") || !(r.hitMap.get(e.id) ?? !1)) continue;
417
- let a = r.compMap.get(e.id) ?? 0, o = e.rayHitPointVel.dot(e.rayUpDir), s = Math.max(0, (e.springK ?? 0) * a - (e.dampingC ?? 0) * ee * o);
418
- e.floatImp.copy(e.rayHitNormal).multiplyScalar(s * i);
233
+ if (!a || !o) return w;
234
+ z.set(o[0], o[1], o[2], o[3]).normalize(), B.makeRotationFromQuaternion(z);
235
+ let T = B.elements, ee = T[0] * T[0] * a[0] + T[4] * T[4] * a[1] + T[8] * T[8] * a[2], te = T[2] * T[2] * a[0] + T[6] * T[6] * a[1] + T[10] * T[10] * a[2], E = 0, D = 0, O = 0, k = 0, A = 0, j = 0;
236
+ for (let e of w) {
237
+ let t = e.mountPos[0] - r[0], n = e.mountPos[2] - r[2];
238
+ E += e.dampingC, D += e.dampingC * t * t, O += e.dampingC * n * n, k += e.springK, A += e.springK * t * t, j += e.springK * n * n;
419
239
  }
420
- }
421
- // src/khrVehicleSlots.tsx
422
- var ge = a;
423
- function _e({ object: e, center: t, quaternion: n, scene: r, origin: o }) {
424
- let s = p(null);
425
- return d(() => {
426
- let c = s.current?.parent;
427
- if (!c) return;
428
- let l = o ?? a([e]).get(e), u = ce(e, r, t, n);
429
- return l.origParent && l.origParent.remove(e), u.decompose(e.position, e.quaternion, e.scale), c.add(e), () => {
430
- e.parent === c && c.remove(e), i(e, l);
431
- };
432
- }, [
433
- e,
434
- t,
435
- n,
436
- r,
437
- o
438
- ]), /* @__PURE__ */ y("group", { ref: s });
439
- }
440
- function q(e) {
441
- let t = e;
442
- t.rayHit = null, t.rayHitBody = null, L(e);
443
- }
444
- var J = new _.Vector3(), ve = new _.Vector3();
445
- function ye(e, t, n) {
446
- if (e) {
447
- let e = x.getState().gravityField(t);
448
- return [
449
- e.x,
450
- e.y,
451
- e.z,
452
- e.lengthSq()
453
- ];
454
- }
455
- return [
456
- n.x,
457
- n.y,
458
- n.z,
459
- n.x * n.x + n.y * n.y + n.z * n.z
460
- ];
461
- }
462
- function be(e) {
463
- let { world: t } = g();
464
- return h(() => {
465
- let n = e.vehicleRef.current, r = n?.body, i = e.gravDeltaRef.current, a = e.settleRef.current;
466
- if (i.active = !1, i.dx = i.dy = i.dz = 0, a.wokeNoGlobalGrav = !1, !n || !r) return;
467
- e.enableCustomGravity && J.copy(r.translation());
468
- let [o, s, c, l] = ye(e.enableCustomGravity, J, t.gravity);
469
- if (a.prevGravInit) {
470
- let t = o - a.prevGravX, n = s - a.prevGravY, i = c - a.prevGravZ;
471
- t * t + n * n + i * i > 1e-6 && (a.sleepLatched = !1, a.settleFrames = 0, r.isSleeping() && (a.wokeNoGlobalGrav = !e.enableCustomGravity, r.wakeUp()));
472
- }
473
- a.prevGravX = o, a.prevGravY = s, a.prevGravZ = c, a.prevGravInit = !0;
474
- let u = l <= 1e-12;
475
- if (u !== e.spatialZeroGRef.current && (e.spatialZeroGRef.current = u, e.setSpatialZeroG(u)), u || !e.enable) {
476
- a.sleepLatched = !1, a.settleFrames = 0;
477
- for (let e of n.wheelsInfo.values()) q(e.current);
478
- } else {
479
- if (r.isSleeping()) {
480
- let t = a.sleepLatched && [...n.wheelsInfo.values()].some((t) => !e.disabledWheels.has(t.current.name ?? "") && t.current.rayHit && t.current.isOnPlatform);
481
- if (z(n) || (n.gravityMag ?? 0) <= 1e-6 || t) a.sleepLatched = !1, a.settleFrames = 0, a.wokeNoGlobalGrav = !e.enableCustomGravity, r.wakeUp();
482
- else if (a.sleepLatched) for (let t of n.wheelsInfo.values()) e.disabledWheels.has(t.current.name ?? "") || I(t.current, n);
483
- } else a.sleepLatched = !1;
484
- for (let t of n.wheelsInfo.values()) {
485
- let n = t.current;
486
- e.disabledWheels.has(n.name ?? "") ? q(n) : (L(n), n.rayHitNormal.set(0, 0, 0));
487
- }
488
- if (e.enableCustomGravity && !r.isSleeping()) {
489
- let e = r.linvel();
490
- i.active = !0, i.preX = e.x, i.preY = e.y, i.preZ = e.z;
491
- }
492
- }
493
- }), null;
494
- }
495
- function xe(e) {
496
- return h(() => {
497
- let t = e.gravDeltaRef.current, n = e.vehicleRef.current?.body;
498
- if (!t.active || !n || n.isSleeping()) return;
499
- let r = n.linvel();
500
- t.dx = r.x - t.preX, t.dy = r.y - t.preY, t.dz = r.z - t.preZ, n.setLinvel({
501
- x: t.preX,
502
- y: t.preY,
503
- z: t.preZ
504
- }, !1);
505
- }), null;
506
- }
507
- function Se(e) {
508
- let { world: t } = g();
509
- return h((n, r) => {
510
- let i = e.vehicleRef.current, a = i?.body, o = e.gravDeltaRef.current, s = e.settleRef.current;
511
- if (!i || !a) return;
512
- if (o.active && (o.dx !== 0 || o.dy !== 0 || o.dz !== 0)) {
513
- let e = a.linvel();
514
- a.setLinvel({
515
- x: e.x + o.dx,
516
- y: e.y + o.dy,
517
- z: e.z + o.dz
518
- }, !1), o.active = !1;
519
- }
520
- e.enableCustomGravity && J.copy(a.translation());
521
- let [, , , c] = ye(e.enableCustomGravity, J, t.gravity);
522
- if (c <= 1e-12 || !e.enable) {
523
- s.sleepLatched = !1, s.settleFrames = 0;
524
- for (let e of i.wheelsInfo.values()) q(e.current);
525
- return;
526
- }
527
- s.hitMap.clear(), s.compMap.clear();
528
- let l = 0, u = !1, d = !1;
529
- for (let t of i.wheelsInfo.values()) {
530
- let n = t.current;
531
- if (e.disabledWheels.has(n.name ?? "")) {
532
- q(n);
533
- continue;
534
- }
535
- l++;
536
- let r = n.rayHitNormal.lengthSq() > .5;
537
- s.hitMap.set(n.id, r), s.compMap.set(n.id, ue(n, r)), r && (u = !0), r && n.isOnPlatform && (d = !0);
538
- }
539
- let f = V(i, e.disabledWheels, s.hitMap);
540
- if (a.isSleeping()) {
541
- if (l > 0 && !u || d || z(i) || f >= .025) s.sleepLatched = !1, s.settleFrames = 0, e.enableCustomGravity || (s.wokeNoGlobalGrav = !0), a.wakeUp();
542
- else for (let t of i.wheelsInfo.values()) e.disabledWheels.has(t.current.name ?? "") || I(t.current, i);
543
- for (let t of i.wheelsInfo.values()) e.disabledWheels.has(t.current.name ?? "") || L(t.current);
544
- return;
545
- }
546
- if (s.wokeNoGlobalGrav) {
547
- s.settleFrames = 0;
548
- for (let t of i.wheelsInfo.values()) e.disabledWheels.has(t.current.name ?? "") || L(t.current);
549
- return;
550
- }
551
- let p = t.timestep > 0 ? t.timestep : Math.min(Math.max(r, 0), 1 / 30);
552
- he(i, a, e.disabledWheels, s, p);
553
- let m = H({
554
- veh: i,
555
- rb: a,
556
- disabledWheels: e.disabledWheels,
557
- hitMap: s.hitMap,
558
- state: s,
559
- gMag: Math.sqrt(c),
560
- dt: p,
561
- maxSlopeSin: f
562
- });
563
- U(i, a, e.disabledWheels, s, e.antiRollStiffness, p);
564
- let h = !m && f >= .025, g = Number.isFinite(e.rollCenterHeight) ? e.rollCenterHeight : 0;
565
- for (let t of i.wheelsInfo.values()) {
566
- let n = t.current;
567
- if (e.disabledWheels.has(n.name ?? "") || n.floatImp.lengthSq() === 0 && n.lngFricImp.lengthSq() === 0 && n.latFricImp.lengthSq() === 0) continue;
568
- let r = Math.min(Math.max(g, 0), n.rayShapeR ?? 0);
569
- ve.copy(n.rayHitPos).addScaledVector(i.upAxis, r), a.applyImpulseAtPoint(n.floatImp, n.supPos, h), a.applyImpulseAtPoint(n.lngFricImp, n.rayHitPos, h), a.applyImpulseAtPoint(n.latFricImp, ve, h);
570
- }
571
- if (h && a.wakeUp(), m) {
572
- a.setLinvel({
573
- x: 0,
574
- y: 0,
575
- z: 0
576
- }, !1), a.setAngvel({
577
- x: 0,
578
- y: 0,
579
- z: 0
580
- }, !1);
581
- for (let t of i.wheelsInfo.values()) e.disabledWheels.has(t.current.name ?? "") || (I(t.current, i), L(t.current));
582
- s.sleepLatched = !0, a.sleep();
583
- }
584
- }), null;
240
+ let M = (e, t, n) => {
241
+ if (!(n > 1e-6) || !(e > 0)) return 1;
242
+ let r = e * .03333333333333333 / n, i = Math.max(.5, 1 - .5 * (t * .0011111111111111111 / n));
243
+ return r > i ? i / r : 1;
244
+ }, N = Math.min(M(E, k, n), M(O, j, ee), M(D, A, te));
245
+ return N < 1 ? w.map((e) => e.tag.dampingC === void 0 ? {
246
+ ...e,
247
+ dampingC: e.dampingC * N
248
+ } : e) : w;
585
249
  }
586
250
  // src/KhrVehicle.tsx
587
- var Ce = 220, we = new _.Quaternion(), Y = new _.Vector3();
588
- function Te({ ref: r, bundle: i, position: a = [
251
+ var V = 220, ce = new x.Quaternion(), H = new x.Vector3();
252
+ function le({ ref: t, bundle: n, position: o = [
589
253
  0,
590
254
  .5,
591
255
  0
592
- ], enable: h = !0, carConfig: _, droneConfig: w, wheelConfig: T, frontWheelConfig: E, rearWheelConfig: D, vehicleRef: O, children: te, scale: ae, colliders: ce, ...k }) {
593
- ne(ae, ce), i.scene.updateMatrixWorld(!0), n(i.scene);
594
- let { world: A } = g(), j = l(t), M = x((e) => e.gravityField), N = p(null), [P, F] = m(null), [I, L] = m(!1), R = p(!1), z = p({
595
- active: !1,
596
- preX: 0,
597
- preY: 0,
598
- preZ: 0,
599
- dx: 0,
600
- dy: 0,
601
- dz: 0
602
- }), ue = p(le()), B = c((e) => {
603
- N.current = e, O && (O.current = e), typeof r == "function" ? r(e) : r && (r.current = e);
604
- }, [r, O]), { mass: V, com: H, inertia: U, inertiaFrame: W, linearVelocity: G, angularVelocity: fe, gravityFactor: K } = f(() => oe(i.bodies), [i.bodies]), pe = f(() => ge([...i.wheels.map((e) => e.object), ...i.propellers.map((e) => e.object)]), [i]), [me, he, J] = re(a), [ve, ye, Ce, Te] = ie(k.rotation, k.quaternion), Ee = f(() => (we.set(ve, ye, Ce, Te), Y.set(H[0], H[1], H[2]).applyQuaternion(we), [
605
- me + Y.x,
606
- he + Y.y,
607
- J + Y.z
256
+ ], enable: s = !0, carConfig: c, droneConfig: v, wheelConfig: y, frontWheelConfig: x, rearWheelConfig: E, propellerConfig: D, vehicleRef: O, children: k, scale: A, colliders: P, ...F }) {
257
+ j(A, P), n.scene.updateMatrixWorld(!0), a(n.scene);
258
+ let { world: I } = b(), L = f(u), R = f(i), z = w((e) => e.gravityField), B = g(null), [V, le] = _(null), [ue, de] = _(!1), U = g(!1), fe = d((e) => {
259
+ B.current = e, O && (O.current = e), typeof t == "function" ? t(e) : t && (t.current = e);
260
+ }, [t, O]), { mass: W, com: G, inertia: K, inertiaFrame: q, linearVelocity: pe, angularVelocity: me, gravityFactor: he } = h(() => ne(n.bodies), [n.bodies]), ge = h(() => ie([...n.wheels.map((e) => e.object), ...n.propellers.map((e) => e.object)]), [n]), [_e, ve, ye] = M(o), [be, xe, Se, Ce] = N(F.rotation, F.quaternion), J = h(() => (ce.set(be, xe, Se, Ce), H.set(G[0], G[1], G[2]).applyQuaternion(ce), [
261
+ _e + H.x,
262
+ ve + H.y,
263
+ ye + H.z
608
264
  ]), [
609
- me,
610
- he,
611
- J,
265
+ _e,
612
266
  ve,
613
267
  ye,
268
+ be,
269
+ xe,
270
+ Se,
614
271
  Ce,
615
- Te,
616
- H
617
- ]), De = f(() => [
618
- -H[0],
619
- -H[1],
620
- -H[2]
621
- ], [H]), Oe = f(() => se(i.bodies, H), [i.bodies, H]), X = f(() => de(i.wheels, V, H).map((e) => ({
272
+ G
273
+ ]), we = h(() => [
274
+ -G[0],
275
+ -G[1],
276
+ -G[2]
277
+ ], [G]), Te = h(() => re(n.bodies, G), [n.bodies, G]), Ee = h(() => {
278
+ let t = {
279
+ ...n.tune,
280
+ ...c
281
+ };
282
+ if (n.wheels.some((e) => e.drive) && !(typeof t.engineHorsepower == "number" && t.engineHorsepower > 0)) throw new e("KhrVehicle: drive wheels need carConfig.engineHorsepower (author scene.extras.tune or pass the carConfig prop)");
283
+ return t;
284
+ }, [
285
+ n.tune,
286
+ n.wheels,
287
+ c
288
+ ]), Y = h(() => se(n.wheels, W, G, .7, K, q).map((e) => ({
622
289
  sw: e,
623
290
  localPos: [
624
- e.mountPos[0] - H[0],
625
- e.mountPos[1] - H[1],
626
- e.mountPos[2] - H[2]
291
+ e.mountPos[0] - G[0],
292
+ e.mountPos[1] - G[1],
293
+ e.mountPos[2] - G[2]
627
294
  ]
628
295
  })), [
629
- i.wheels,
630
- V,
631
- H
632
- ]), ke = f(() => i.propellers.map((e) => ({
296
+ n.wheels,
297
+ W,
298
+ G,
299
+ K,
300
+ q
301
+ ]), X = h(() => n.propellers.map((e) => ({
633
302
  p: e,
634
303
  localPos: [
635
- e.position[0] - H[0],
636
- e.position[1] - H[1],
637
- e.position[2] - H[2]
304
+ e.position[0] - G[0],
305
+ e.position[1] - G[1],
306
+ e.position[2] - G[2]
638
307
  ]
639
- })), [i.propellers, H]), { rollCenterHeight: Ae, antiRollStiffness: je, ...Me } = f(() => {
640
- let e = {
641
- ...i.tune,
642
- ..._
643
- };
644
- if (i.wheels.some((e) => e.drive) && !(typeof e.engineHorsepower == "number" && e.engineHorsepower > 0)) throw new s("KhrVehicle: drive wheels need carConfig.engineHorsepower (author scene.extras.tune or pass the carConfig prop)");
645
- return e;
646
- }, [
647
- i.tune,
648
- i.wheels,
649
- _
650
- ]), Ne = f(() => i.droneTune || w ? {
651
- ...i.droneTune,
652
- ...w
653
- } : void 0, [i.droneTune, w]), Pe = N.current?.body?.handle ?? null, Fe = Pe === null ? null : `${i.scene.uuid}:${Pe}`, Z = P !== null && P.startsWith(`${i.scene.uuid}:`);
654
- u(() => {
655
- let e = N.current?.body;
308
+ })), [n.propellers, G]), De = h(() => n.droneTune || v ? {
309
+ ...n.droneTune,
310
+ ...v
311
+ } : void 0, [n.droneTune, v]), Oe = B.current?.body?.handle ?? null, ke = Oe === null ? null : `${n.scene.uuid}:${Oe}`, Z = V !== null && V.startsWith(`${n.scene.uuid}:`);
312
+ p(() => {
313
+ let e = B.current?.body;
656
314
  if (!e) return;
657
- e.setAdditionalMassProperties(V, {
315
+ e.setAdditionalMassProperties(W, {
658
316
  x: 0,
659
317
  y: 0,
660
318
  z: 0
661
319
  }, {
662
- x: U[0],
663
- y: U[1],
664
- z: U[2]
320
+ x: K[0],
321
+ y: K[1],
322
+ z: K[2]
665
323
  }, {
666
- x: W[0],
667
- y: W[1],
668
- z: W[2],
669
- w: W[3]
324
+ x: q[0],
325
+ y: q[1],
326
+ z: q[2],
327
+ w: q[3]
670
328
  }, !0), e.recomputeMassPropertiesFromColliders();
671
- let t = `${i.scene.uuid}:${e.handle}`;
672
- (X.length > 0 || ke.length > 0) && P !== t && F(t);
329
+ let t = `${n.scene.uuid}:${e.handle}`;
330
+ (Y.length > 0 || X.length > 0) && V !== t && le(t);
673
331
  }, [
674
- i.scene.uuid,
675
- Fe,
676
- V,
677
- U,
332
+ n.scene.uuid,
333
+ ke,
678
334
  W,
335
+ K,
336
+ q,
337
+ Y.length,
679
338
  X.length,
680
- ke.length,
681
- P
339
+ V
682
340
  ]);
683
- let Ie = !!k.enableCustomGravity, Q;
684
- if (Ie) Z && N.current?.body ? Y.copy(N.current.body.translation()) : Y.set(Ee[0], Ee[1], Ee[2]), Q = M(Y).lengthSq() > 1e-12;
341
+ let Ae = !!F.enableCustomGravity, Q;
342
+ if (Ae) Z && B.current?.body ? H.copy(B.current.body.translation()) : H.set(J[0], J[1], J[2]), Q = z(H).lengthSq() > 1e-12;
685
343
  else {
686
- let [e, t, n] = j ?? [
687
- A.gravity.x,
688
- A.gravity.y,
689
- A.gravity.z
344
+ let [e, t, n] = L ?? [
345
+ I.gravity.x,
346
+ I.gravity.y,
347
+ I.gravity.z
690
348
  ];
691
349
  Q = e * e + t * t + n * n > 1e-12;
692
350
  }
693
- let Le = h && Q && !I, $ = f(() => {
694
- let e = /* @__PURE__ */ new Set();
695
- for (let { sw: t } of X) ((t.tag.steer ? E : D)?.enable ?? T?.enable ?? h) === !1 && e.add(t.tag.nodeName);
696
- return e;
697
- }, [
698
- X,
699
- E?.enable,
700
- D?.enable,
701
- T?.enable,
702
- h
703
- ]);
704
- return d(() => {
705
- Q && R.current && (R.current = !1, L(!1));
706
- let e = N.current;
707
- if (e) for (let t of e.wheelsInfo.values()) (!Le || $.has(t.current.name ?? "")) && q(t.current);
708
- }, [
709
- Q,
710
- Le,
711
- $
712
- ]), /* @__PURE__ */ b(v, { children: [/* @__PURE__ */ y(be, {
713
- vehicleRef: N,
714
- enable: h,
715
- enableCustomGravity: Ie,
716
- gravDeltaRef: z,
717
- spatialZeroGRef: R,
718
- setSpatialZeroG: L,
719
- disabledWheels: $,
720
- settleRef: ue
721
- }, `gate-${i.scene.uuid}`), /* @__PURE__ */ b(S, {
722
- ref: B,
723
- position: Ee,
724
- enable: h,
725
- carConfig: Me,
726
- droneConfig: Ne,
727
- ...G ? { linearVelocity: G } : {},
728
- ...fe ? { angularVelocity: fe } : {},
729
- ...K === void 0 ? {} : { gravityScale: K },
730
- ...k,
351
+ let $ = s && R, je = $ && Q && !ue, Me = g(/* @__PURE__ */ new Map()), Ne = g(/* @__PURE__ */ new Map());
352
+ return m(() => {
353
+ Q && U.current && (U.current = !1, de(!1));
354
+ }, [Q]), /* @__PURE__ */ C(T, {
355
+ ref: fe,
356
+ position: J,
357
+ enable: $,
358
+ carConfig: Ee,
359
+ droneConfig: De,
360
+ ...pe ? { linearVelocity: pe } : {},
361
+ ...me ? { angularVelocity: me } : {},
362
+ ...he === void 0 ? {} : { gravityScale: he },
363
+ ...F,
731
364
  children: [
732
- /* @__PURE__ */ b("group", {
733
- position: De,
734
- children: [/* @__PURE__ */ y(o, {
735
- bundle: i,
365
+ /* @__PURE__ */ C("group", {
366
+ position: we,
367
+ children: [/* @__PURE__ */ S(l, {
368
+ bundle: n,
736
369
  owner: "KhrVehicle"
737
- }), te]
370
+ }), k]
738
371
  }),
739
- Oe.map(({ key: t, spec: n }) => /* @__PURE__ */ y(e, { c: n }, t)),
740
- Z && Ie && X.length > 0 && /* @__PURE__ */ y(xe, {
741
- vehicleRef: N,
742
- gravDeltaRef: z
743
- }, `unbias-${P}`),
744
- Z && X.map(({ sw: e, localPos: t }) => {
745
- let n = e.tag.steer ? E : D, r = n?.enable ?? T?.enable ?? h;
746
- return /* @__PURE__ */ y(C, {
372
+ Te.map(({ key: e, spec: t }) => /* @__PURE__ */ S(r, { c: t }, e)),
373
+ Z && /* @__PURE__ */ S(oe, {
374
+ vehicleRef: B,
375
+ active: $,
376
+ enableCustomGravity: Ae,
377
+ spatialZeroGRef: U,
378
+ setSpatialZeroG: de
379
+ }, `sync-${V}`),
380
+ Z && Y.map(({ sw: e, localPos: t }) => {
381
+ let r = e.tag.steer ? x : E, i = !!((r?.enable ?? y?.enable ?? s) && je), a = e.tag.nodeName, o = Ne.current.get(a) ?? i;
382
+ Ne.current.set(a, i);
383
+ let c = Me.current.get(a) ?? 0;
384
+ if (o && !i && B.current?.body) {
385
+ let e = B.current.body.translation(), t = B.current.currPos;
386
+ if ((e.x - t.x) ** 2 + (e.y - t.y) ** 2 + (e.z - t.z) ** 2 < .25) {
387
+ for (let e of B.current.wheelsInfo.values()) if (e.current.name === a && e.current.rayHit !== null) {
388
+ c++, Me.current.set(a, c);
389
+ break;
390
+ }
391
+ }
392
+ }
393
+ return /* @__PURE__ */ S(ee, {
747
394
  quaternion: e.tag.quaternion,
748
395
  wheelModelDensity: e.tag.wheelDensity ?? 220,
749
396
  wheelModelRadius: e.tag.radius,
@@ -766,50 +413,49 @@ function Te({ ref: r, bundle: i, position: a = [
766
413
  lngSlipRatioCurveData: e.tag.lngSlipRatioCurveData,
767
414
  latSlipRatioCurveData: e.tag.latSlipRatioCurveData,
768
415
  maxBrakeTorque: e.tag.maxBrakeTorque ?? e.cornerMassKg * 9.80665 * e.tag.radius,
416
+ minLatRelaxCoeff: 1,
769
417
  groundDetection: "rayCast",
770
- ...T,
771
- ...n,
772
- name: e.tag.nodeName,
418
+ ...y,
419
+ ...r,
420
+ name: a,
773
421
  position: t,
774
- enable: r && Le,
775
- children: /* @__PURE__ */ y(_e, {
422
+ enable: i,
423
+ children: /* @__PURE__ */ S(ae, {
776
424
  object: e.tag.object,
777
425
  center: e.tag.position,
778
426
  quaternion: e.tag.quaternion,
779
- scene: i.scene,
780
- origin: pe.get(e.tag.object)
427
+ scene: n.scene,
428
+ origin: ge.get(e.tag.object)
781
429
  })
782
- }, `${e.tag.nodeName}-${P}`);
430
+ }, `${a}-${V}-${c}`);
783
431
  }),
784
- Z && X.length > 0 && /* @__PURE__ */ y(Se, {
785
- vehicleRef: N,
786
- enable: h,
787
- enableCustomGravity: Ie,
788
- gravDeltaRef: z,
789
- disabledWheels: $,
790
- settleRef: ue,
791
- rollCenterHeight: Ae,
792
- antiRollStiffness: je
793
- }, `imp-${P}`),
794
- Z && ke.map(({ p: e, localPos: t }) => /* @__PURE__ */ y(ee, {
795
- name: e.nodeName,
796
- position: t,
797
- quaternion: e.quaternion,
798
- debug: !1,
799
- enable: h,
800
- maxThrust: e.maxThrust,
801
- torqueRatio: e.torqueRatio,
802
- invertThrust: e.invertThrust,
803
- invertTorque: e.invertTorque,
804
- children: /* @__PURE__ */ y(_e, {
432
+ Z && X.map(({ p: e, localPos: t }) => {
433
+ let r = !!((D?.enable ?? s) && $), i = /* @__PURE__ */ S(ae, {
805
434
  object: e.object,
806
435
  center: e.position,
807
436
  quaternion: e.quaternion,
808
- scene: i.scene,
809
- origin: pe.get(e.object)
810
- })
811
- }, `${e.nodeName}-${P}`))
437
+ scene: n.scene,
438
+ origin: ge.get(e.object)
439
+ });
440
+ return r ? /* @__PURE__ */ S(te, {
441
+ debug: !1,
442
+ maxThrust: e.maxThrust,
443
+ torqueRatio: e.torqueRatio,
444
+ invertThrust: e.invertThrust,
445
+ invertTorque: e.invertTorque,
446
+ ...D,
447
+ name: e.nodeName,
448
+ position: t,
449
+ quaternion: e.quaternion,
450
+ enable: !0,
451
+ children: i
452
+ }, `${e.nodeName}-${V}`) : /* @__PURE__ */ S("group", {
453
+ position: t,
454
+ quaternion: e.quaternion,
455
+ children: i
456
+ }, `${e.nodeName}-${V}-idle`);
457
+ })
812
458
  ]
813
- }, i.scene.uuid)] });
459
+ }, n.scene.uuid);
814
460
  }
815
- export { W as BENCH_GRAVITY, Te as KhrVehicle, Ce as WHEEL_ASSEMBLY_DENSITY_KG_M3, de as solveWheelLoads };
461
+ export { L as BENCH_GRAVITY, le as KhrVehicle, V as WHEEL_ASSEMBLY_DENSITY_KG_M3, se as solveWheelLoads };