@khr3/core 0.2.0 → 0.2.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 +7 -7
- package/dist/AssemblyRootBody.d.ts +2 -3
- package/dist/KhrAssembly.d.ts +3 -5
- package/dist/assemblyPlan.d.ts +8 -9
- package/dist/index.d.ts +2 -2
- package/dist/index.js +266 -202
- package/dist/index.js.map +1 -1
- package/dist/jointDynamics.d.ts +23 -17
- package/dist/khrExtras.d.ts +2 -1
- package/dist/khrGlb.d.ts +0 -5
- package/dist/khrTypes.d.ts +5 -0
- package/dist/khrVehicleJointHelpers.d.ts +13 -15
- package/dist/khrVehicleJoints.d.ts +7 -1
- package/dist/khrVehicleSlots.d.ts +7 -9
- package/dist/{BundleSceneHost-DUqqPtJF.js → physicsEcs-B9XGoZtl.js} +407 -358
- package/dist/physicsEcs-B9XGoZtl.js.map +1 -0
- package/dist/physicsEcs.d.ts +12 -40
- package/dist/vehicle.js +279 -208
- package/dist/vehicle.js.map +1 -1
- package/docs/api-reference.md +15 -15
- package/package.json +1 -1
- package/dist/BundleSceneHost-DUqqPtJF.js.map +0 -1
package/dist/vehicle.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { E as KhrValidationError, a as MountSubtreeSlot, d as computeLumpedMassProps, f as KhrCollider, i as BundleSceneHost, l as toColliderMass, n as buildJointRuntime, o as captureSubtreeOrigins, r as createRapierImpulseJoint, s as restoreSubtreeOrigin, t as JointRegistryContext, u as bodyPoseMatrix, v as assertSceneIdentityWorldScale, y as assertUnitScale } from "./physicsEcs-B9XGoZtl.js";
|
|
2
|
+
import { useCallback, useContext, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
import { useFrame } from "@react-three/fiber";
|
|
4
|
-
import { useRapier } from "@react-three/rapier";
|
|
5
|
-
import * as THREE from "three";
|
|
4
|
+
import { RigidBody, useRapier } from "@react-three/rapier";
|
|
6
5
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import * as THREE from "three";
|
|
7
7
|
import { EcctrlVehicle, ShapeCastWheel, ThrustPropeller } from "@pinkaphant/ecctrl/vehicle";
|
|
8
8
|
import { useCustomGravity } from "@pinkaphant/ecctrl/gravity";
|
|
9
9
|
// src/khrVehicleChassis.ts
|
|
@@ -82,7 +82,7 @@ function readSpawnQuat(rotation, quaternion) {
|
|
|
82
82
|
];
|
|
83
83
|
}
|
|
84
84
|
function computeChassisMassProps(bodies) {
|
|
85
|
-
const dyn = bodies.filter((b) => !b.isKinematic);
|
|
85
|
+
const dyn = bodies.filter((b) => !b.isKinematic).filter((b) => b.joint === null);
|
|
86
86
|
if (dyn.length === 0) throw new KhrValidationError("KhrVehicle: bundle has no dynamic rigid body for the chassis");
|
|
87
87
|
const lumped = computeLumpedMassProps(dyn);
|
|
88
88
|
if (!lumped || !(lumped.mass > 0)) throw new KhrValidationError("KhrVehicle: chassis dynamic mass must be positive");
|
|
@@ -182,10 +182,6 @@ function computeRotorLocalMatrix(node, scene, center, quaternion) {
|
|
|
182
182
|
return new THREE.Matrix4().compose(new THREE.Vector3(center[0], center[1], center[2]), quaternion ? new THREE.Quaternion(quaternion[0], quaternion[1], quaternion[2], quaternion[3]).normalize() : new THREE.Quaternion(), new THREE.Vector3(1, 1, 1)).invert().multiply(nodeToScene);
|
|
183
183
|
}
|
|
184
184
|
// src/khrVehicleJointHelpers.ts
|
|
185
|
-
function isJointDescendant(body, ancestorName, byName) {
|
|
186
|
-
for (let cur = body.joint ? byName.get(body.joint.connectedNodeName) : void 0; cur; cur = cur.joint ? byName.get(cur.joint.connectedNodeName) : void 0) if (cur.nodeName === ancestorName) return true;
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
185
|
function planVehicleJointParts(bodies) {
|
|
190
186
|
const dyn = bodies.filter((b) => !b.isKinematic);
|
|
191
187
|
const byName = new Map(dyn.map((b) => [b.nodeName, b]));
|
|
@@ -201,43 +197,81 @@ function planVehicleJointParts(bodies) {
|
|
|
201
197
|
};
|
|
202
198
|
jointed.sort((a, b) => depthOf(a) - depthOf(b));
|
|
203
199
|
const one = new THREE.Vector3(1, 1, 1);
|
|
204
|
-
const
|
|
205
|
-
const
|
|
200
|
+
const rpPos = new THREE.Vector3();
|
|
201
|
+
const rpQuat = new THREE.Quaternion();
|
|
202
|
+
const rcPos = new THREE.Vector3();
|
|
203
|
+
const rcQuat = new THREE.Quaternion();
|
|
206
204
|
const scl = new THREE.Vector3();
|
|
207
205
|
const partByName = /* @__PURE__ */ new Map();
|
|
208
206
|
return jointed.map((b) => {
|
|
209
207
|
const joint = b.joint;
|
|
210
208
|
const parent = partByName.get(joint.connectedNodeName) ?? null;
|
|
211
209
|
const bodyMat = bodyPoseMatrix(b);
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
210
|
+
(parent ? bodyPoseMatrix(parent.spec) : new THREE.Matrix4()).clone().invert().multiply(bodyMat).decompose(rpPos, rpQuat, scl);
|
|
211
|
+
rpQuat.normalize();
|
|
212
|
+
bodyMat.decompose(rcPos, rcQuat, scl);
|
|
213
|
+
rcQuat.normalize();
|
|
214
|
+
const axChildVec = new THREE.Vector3().fromArray(joint.axis).normalize();
|
|
215
|
+
const axParentVec = axChildVec.clone().applyQuaternion(rpQuat).normalize();
|
|
216
|
+
const ownLumped = computeLumpedMassProps([b], bodyMat.clone().invert());
|
|
217
|
+
const colliders = b.colliders.map((spec, idx) => {
|
|
215
218
|
const colQuat = new THREE.Quaternion().fromArray(spec.localQuaternion);
|
|
216
219
|
const local = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(spec.localPosition), colQuat, one);
|
|
217
|
-
|
|
218
|
-
quat.normalize();
|
|
219
|
-
return {
|
|
220
|
+
const planned = {
|
|
220
221
|
spec,
|
|
221
222
|
local,
|
|
222
223
|
position: [
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
224
|
+
spec.localPosition[0],
|
|
225
|
+
spec.localPosition[1],
|
|
226
|
+
spec.localPosition[2]
|
|
226
227
|
],
|
|
227
228
|
quaternion: [
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
229
|
+
colQuat.x,
|
|
230
|
+
colQuat.y,
|
|
231
|
+
colQuat.z,
|
|
232
|
+
colQuat.w
|
|
232
233
|
]
|
|
233
234
|
};
|
|
235
|
+
if (idx === 0 && ownLumped) planned.massProperties = toColliderMass(ownLumped, local, colQuat);
|
|
236
|
+
return planned;
|
|
234
237
|
});
|
|
235
238
|
const plan = {
|
|
236
239
|
spec: b,
|
|
237
240
|
joint,
|
|
238
241
|
parent,
|
|
239
|
-
|
|
240
|
-
|
|
242
|
+
restRelParentPos: [
|
|
243
|
+
rpPos.x,
|
|
244
|
+
rpPos.y,
|
|
245
|
+
rpPos.z
|
|
246
|
+
],
|
|
247
|
+
restRelParentQuat: [
|
|
248
|
+
rpQuat.x,
|
|
249
|
+
rpQuat.y,
|
|
250
|
+
rpQuat.z,
|
|
251
|
+
rpQuat.w
|
|
252
|
+
],
|
|
253
|
+
restRelChassisPos: [
|
|
254
|
+
rcPos.x,
|
|
255
|
+
rcPos.y,
|
|
256
|
+
rcPos.z
|
|
257
|
+
],
|
|
258
|
+
restRelChassisQuat: [
|
|
259
|
+
rcQuat.x,
|
|
260
|
+
rcQuat.y,
|
|
261
|
+
rcQuat.z,
|
|
262
|
+
rcQuat.w
|
|
263
|
+
],
|
|
264
|
+
axisParent: [
|
|
265
|
+
axParentVec.x,
|
|
266
|
+
axParentVec.y,
|
|
267
|
+
axParentVec.z
|
|
268
|
+
],
|
|
269
|
+
axisChild: [
|
|
270
|
+
axChildVec.x,
|
|
271
|
+
axChildVec.y,
|
|
272
|
+
axChildVec.z
|
|
273
|
+
],
|
|
274
|
+
...ownLumped && colliders.length === 0 ? { partMass: toColliderMass(ownLumped) } : {},
|
|
241
275
|
colliders
|
|
242
276
|
};
|
|
243
277
|
partByName.set(b.nodeName, plan);
|
|
@@ -250,130 +284,191 @@ function openTargetForLimits(limits) {
|
|
|
250
284
|
function isVehicleJointOpen(rt) {
|
|
251
285
|
if (!rt.limits) return false;
|
|
252
286
|
const openTgt = openTargetForLimits(rt.limits);
|
|
253
|
-
return Math.abs(openTgt) > 1e-6 && Math.abs((rt.positionTarget ?? rt.
|
|
254
|
-
}
|
|
255
|
-
function createVehicleJointRuntime(part, assemblyId, parent, colliderRefs) {
|
|
256
|
-
const drive = part.joint.drive;
|
|
257
|
-
return {
|
|
258
|
-
assemblyId,
|
|
259
|
-
nodeName: part.spec.nodeName,
|
|
260
|
-
role: part.joint.role,
|
|
261
|
-
kind: part.joint.kind,
|
|
262
|
-
object: part.spec.object,
|
|
263
|
-
axis: new THREE.Vector3().fromArray(part.joint.axis).normalize(),
|
|
264
|
-
restPosition: part.spec.object.position.clone(),
|
|
265
|
-
restQuaternion: part.spec.object.quaternion.clone(),
|
|
266
|
-
q: 0,
|
|
267
|
-
v: 0,
|
|
268
|
-
positionTarget: drive?.positionTarget ?? null,
|
|
269
|
-
velocityTarget: drive?.velocityTarget ?? 0,
|
|
270
|
-
drive,
|
|
271
|
-
limits: part.joint.limits,
|
|
272
|
-
limitStiffness: part.joint.limitStiffness ?? null,
|
|
273
|
-
limitDamping: part.joint.limitDamping ?? 0,
|
|
274
|
-
inertia: part.inertia,
|
|
275
|
-
parent,
|
|
276
|
-
restRelParent: part.restRelParent,
|
|
277
|
-
current: part.restRelParent.clone(),
|
|
278
|
-
stamp: -1,
|
|
279
|
-
colliders: part.colliders.map((c, idx) => ({
|
|
280
|
-
ref: colliderRefs[idx],
|
|
281
|
-
local: c.local
|
|
282
|
-
}))
|
|
283
|
-
};
|
|
287
|
+
return Math.abs(openTgt) > 1e-6 && Math.abs((rt.positionTarget ?? rt.getCoordinate()) - openTgt) < Math.abs(openTgt) * .5;
|
|
284
288
|
}
|
|
285
289
|
// src/khrVehicleJoints.tsx
|
|
286
|
-
|
|
287
|
-
|
|
290
|
+
function VehicleArticulatedPanel({ part, spawnPos, spawnQuat, parentBodyGetter, origin, onBody, onJointReady }) {
|
|
291
|
+
const { rapier, world } = useRapier();
|
|
288
292
|
const registry = useContext(JointRegistryContext);
|
|
289
|
-
const
|
|
290
|
-
const
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
293
|
+
const childRef = useRef(null);
|
|
294
|
+
const rtRef = useRef(null);
|
|
295
|
+
const worldPose = useMemo(() => {
|
|
296
|
+
const sq = new THREE.Quaternion(spawnQuat[0], spawnQuat[1], spawnQuat[2], spawnQuat[3]);
|
|
297
|
+
const pos = new THREE.Vector3().fromArray(part.restRelChassisPos).applyQuaternion(sq).add(new THREE.Vector3(spawnPos[0], spawnPos[1], spawnPos[2]));
|
|
298
|
+
const quat = sq.clone().multiply(new THREE.Quaternion().fromArray(part.restRelChassisQuat)).normalize();
|
|
299
|
+
return {
|
|
300
|
+
pos: [
|
|
301
|
+
pos.x,
|
|
302
|
+
pos.y,
|
|
303
|
+
pos.z
|
|
304
|
+
],
|
|
305
|
+
quat: [
|
|
306
|
+
quat.x,
|
|
307
|
+
quat.y,
|
|
308
|
+
quat.z,
|
|
309
|
+
quat.w
|
|
310
|
+
]
|
|
311
|
+
};
|
|
312
|
+
}, [
|
|
313
|
+
part.restRelChassisPos,
|
|
314
|
+
part.restRelChassisQuat,
|
|
315
|
+
spawnPos,
|
|
316
|
+
spawnQuat
|
|
317
|
+
]);
|
|
318
|
+
useEffect(() => {
|
|
319
|
+
onBody(part.spec.nodeName, childRef.current);
|
|
320
|
+
return () => onBody(part.spec.nodeName, null);
|
|
321
|
+
}, [part.spec.nodeName, onBody]);
|
|
322
|
+
useEffect(() => {
|
|
323
|
+
const rb = childRef.current;
|
|
324
|
+
const pm = part.partMass;
|
|
325
|
+
if (rb && pm) {
|
|
326
|
+
rb.setAdditionalMassProperties(pm.mass, pm.centerOfMass, pm.principalAngularInertia, pm.angularInertiaLocalFrame, true);
|
|
327
|
+
rb.recomputeMassPropertiesFromColliders();
|
|
304
328
|
}
|
|
305
|
-
|
|
306
|
-
|
|
329
|
+
}, [part.partMass]);
|
|
330
|
+
useEffect(() => {
|
|
331
|
+
const parentBody = parentBodyGetter();
|
|
332
|
+
const childBody = childRef.current;
|
|
333
|
+
if (!parentBody || !childBody) return;
|
|
334
|
+
const imp = createRapierImpulseJoint(rapier, world, parentBody, childBody, part.joint, part.restRelParentPos, [
|
|
335
|
+
0,
|
|
336
|
+
0,
|
|
337
|
+
0
|
|
338
|
+
], part.axisParent, part.axisChild, part.restRelParentQuat);
|
|
339
|
+
const rt = buildJointRuntime({
|
|
340
|
+
rapier,
|
|
341
|
+
world,
|
|
342
|
+
nodeName: part.spec.nodeName,
|
|
343
|
+
role: part.joint.role,
|
|
344
|
+
kind: part.joint.kind,
|
|
345
|
+
object: part.spec.object,
|
|
346
|
+
axisParent: part.axisParent,
|
|
347
|
+
anchorParent: part.restRelParentPos,
|
|
348
|
+
restRelQuat: part.restRelParentQuat,
|
|
349
|
+
limits: part.joint.limits,
|
|
350
|
+
drive: part.joint.drive,
|
|
351
|
+
breakaway: part.spec.breakaway,
|
|
352
|
+
rapierJoint: imp,
|
|
353
|
+
parentBody,
|
|
354
|
+
childBody
|
|
355
|
+
});
|
|
356
|
+
rtRef.current = rt;
|
|
357
|
+
registry?.add(rt);
|
|
358
|
+
onJointReady(rt.nodeName, part.spec.object, rt);
|
|
307
359
|
return () => {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
rt.object.position.copy(rt.restPosition);
|
|
313
|
-
rt.object.quaternion.copy(rt.restQuaternion);
|
|
314
|
-
}
|
|
360
|
+
rtRef.current = null;
|
|
361
|
+
registry?.delete(rt);
|
|
362
|
+
onJointReady(rt.nodeName, part.spec.object, null);
|
|
363
|
+
if (rt.rapierJoint && world.getImpulseJoint(rt.rapierJoint.handle)) world.removeImpulseJoint(rt.rapierJoint, true);
|
|
315
364
|
};
|
|
316
365
|
}, [
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
366
|
+
rapier,
|
|
367
|
+
world,
|
|
368
|
+
part,
|
|
369
|
+
parentBodyGetter,
|
|
370
|
+
registry,
|
|
371
|
+
onJointReady
|
|
321
372
|
]);
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
373
|
+
const onForce = part.spec.breakaway ? (p) => {
|
|
374
|
+
rtRef.current?.applyImpactImpulse(p.totalForceMagnitude * Math.max(world.timestep, 1 / 240));
|
|
375
|
+
} : void 0;
|
|
376
|
+
return /* @__PURE__ */ jsxs(RigidBody, {
|
|
377
|
+
ref: childRef,
|
|
378
|
+
type: "dynamic",
|
|
379
|
+
position: worldPose.pos,
|
|
380
|
+
quaternion: worldPose.quat,
|
|
381
|
+
colliders: false,
|
|
382
|
+
onContactForce: onForce,
|
|
383
|
+
children: [/* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
384
|
+
object: part.spec.object,
|
|
385
|
+
position: [
|
|
386
|
+
0,
|
|
387
|
+
0,
|
|
388
|
+
0
|
|
389
|
+
],
|
|
390
|
+
quaternion: [
|
|
391
|
+
0,
|
|
392
|
+
0,
|
|
393
|
+
0,
|
|
394
|
+
1
|
|
395
|
+
],
|
|
396
|
+
origin
|
|
397
|
+
}), part.colliders.map((col, cIdx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
398
|
+
c: col.spec,
|
|
399
|
+
position: col.position,
|
|
400
|
+
quaternion: col.quaternion,
|
|
401
|
+
massProperties: col.massProperties
|
|
402
|
+
}, `vj_${part.spec.nodeName}:${cIdx}`))]
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
function KhrVehicleJoints({ bodies, vehicleRef, spawnPos, spawnQuat, jointsRef }) {
|
|
406
|
+
const parts = useMemo(() => planVehicleJointParts(bodies), [bodies]);
|
|
407
|
+
const origins = useMemo(() => parts.length > 0 ? captureSubtreeOrigins(parts.map((p) => p.spec.object)) : null, [parts]);
|
|
408
|
+
const bodyMapRef = useRef(/* @__PURE__ */ new Map());
|
|
409
|
+
const runtimeMapRef = useRef(/* @__PURE__ */ new Map());
|
|
410
|
+
const byObjectRef = useRef(/* @__PURE__ */ new Map());
|
|
411
|
+
const handleBody = useCallback((nodeName, rb) => {
|
|
412
|
+
if (rb) bodyMapRef.current.set(nodeName, rb);
|
|
413
|
+
else bodyMapRef.current.delete(nodeName);
|
|
414
|
+
}, []);
|
|
415
|
+
const handleJointReady = useCallback((nodeName, obj, rt) => {
|
|
416
|
+
if (rt) {
|
|
417
|
+
runtimeMapRef.current.set(nodeName, rt);
|
|
418
|
+
byObjectRef.current.set(obj, rt);
|
|
419
|
+
} else {
|
|
420
|
+
runtimeMapRef.current.delete(nodeName);
|
|
421
|
+
byObjectRef.current.delete(obj);
|
|
422
|
+
}
|
|
423
|
+
}, []);
|
|
424
|
+
useImperativeHandle(jointsRef, () => ({
|
|
425
|
+
get hasJoints() {
|
|
426
|
+
return parts.length > 0;
|
|
427
|
+
},
|
|
428
|
+
get jointNames() {
|
|
429
|
+
return parts.map((p) => p.spec.nodeName);
|
|
430
|
+
},
|
|
431
|
+
getBody: (nodeName) => bodyMapRef.current.get(nodeName) ?? null,
|
|
432
|
+
getJointRuntime: (nodeName) => runtimeMapRef.current.get(nodeName) ?? null,
|
|
433
|
+
getJointAngle: (nodeName) => runtimeMapRef.current.get(nodeName)?.getCoordinate() ?? null,
|
|
434
|
+
setJointTarget: (nodeName, target) => {
|
|
435
|
+
runtimeMapRef.current.get(nodeName)?.applyTarget(target);
|
|
436
|
+
},
|
|
437
|
+
setRoleTarget: (role, target) => {
|
|
438
|
+
for (const rt of runtimeMapRef.current.values()) if (rt.role === role) rt.applyTarget(target);
|
|
439
|
+
},
|
|
440
|
+
toggleHitObject: (hit) => {
|
|
441
|
+
for (let cur = hit; cur; cur = cur.parent) {
|
|
442
|
+
const rt = byObjectRef.current.get(cur);
|
|
443
|
+
if (rt && rt.limits && rt.drive?.stiffness !== void 0) {
|
|
444
|
+
rt.applyTarget(isVehicleJointOpen(rt) ? 0 : openTargetForLimits(rt.limits));
|
|
445
|
+
return true;
|
|
351
446
|
}
|
|
352
|
-
return false;
|
|
353
|
-
},
|
|
354
|
-
toggleRolePrefix: (prefix) => {
|
|
355
|
-
const low = prefix.toLowerCase();
|
|
356
|
-
const matches = [...runtimeMapRef.current.values()].filter((rt) => rt.limits !== null && rt.drive?.stiffness !== void 0 && ((rt.role ?? "").toLowerCase().startsWith(low) || rt.nodeName.toLowerCase().startsWith(low)));
|
|
357
|
-
if (matches.length === 0) return;
|
|
358
|
-
const anyOpen = matches.some((rt) => isVehicleJointOpen(rt));
|
|
359
|
-
for (const rt of matches) applyTarget(rt, anyOpen ? 0 : openTargetForLimits(rt.limits));
|
|
360
447
|
}
|
|
361
|
-
|
|
362
|
-
|
|
448
|
+
return false;
|
|
449
|
+
},
|
|
450
|
+
toggleRolePrefix: (prefix) => {
|
|
451
|
+
const low = prefix.toLowerCase();
|
|
452
|
+
const matches = [...runtimeMapRef.current.values()].filter((rt) => rt.limits !== null && rt.drive?.stiffness !== void 0 && ((rt.role ?? "").toLowerCase().startsWith(low) || rt.nodeName.toLowerCase().startsWith(low)));
|
|
453
|
+
if (matches.length === 0) return;
|
|
454
|
+
const anyOpen = matches.some((rt) => isVehicleJointOpen(rt));
|
|
455
|
+
for (const rt of matches) rt.applyTarget(anyOpen ? 0 : openTargetForLimits(rt.limits));
|
|
456
|
+
}
|
|
457
|
+
}), [parts]);
|
|
363
458
|
if (parts.length === 0) return null;
|
|
364
|
-
return /* @__PURE__ */ jsx(Fragment, { children: parts.map((part
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
459
|
+
return /* @__PURE__ */ jsx(Fragment, { children: parts.map((part) => /* @__PURE__ */ jsx(VehicleArticulatedPanel, {
|
|
460
|
+
part,
|
|
461
|
+
spawnPos,
|
|
462
|
+
spawnQuat,
|
|
463
|
+
parentBodyGetter: () => part.parent ? bodyMapRef.current.get(part.parent.spec.nodeName) ?? null : vehicleRef.current?.body ?? null,
|
|
464
|
+
origin: origins?.get(part.spec.object),
|
|
465
|
+
onBody: handleBody,
|
|
466
|
+
onJointReady: handleJointReady
|
|
467
|
+
}, part.spec.nodeName)) });
|
|
370
468
|
}
|
|
371
469
|
// src/khrVehicleSlots.tsx
|
|
372
470
|
var captureRotorOrigins = captureSubtreeOrigins;
|
|
373
471
|
var _samplePos = new THREE.Vector3();
|
|
374
|
-
function isZeroGravityVec(x, y, z) {
|
|
375
|
-
return x * x + y * y + z * z <= 1e-12;
|
|
376
|
-
}
|
|
377
472
|
function resetAllWheelHits(handle) {
|
|
378
473
|
if (!handle) return;
|
|
379
474
|
for (const wheelRef of handle.wheelsInfo.values()) {
|
|
@@ -391,46 +486,38 @@ function resetAllWheelHits(handle) {
|
|
|
391
486
|
w.effInertia = 0;
|
|
392
487
|
}
|
|
393
488
|
}
|
|
394
|
-
|
|
489
|
+
/**
|
|
490
|
+
* Gates `<ShapeCastWheel>` only when gravity is non-zero (`gravityMag > 1e-6`), preventing
|
|
491
|
+
* `ShapeCastWheel`'s internal `smoothedLngImpulse += Infinity` closure poisoning in 0G.
|
|
492
|
+
*/
|
|
493
|
+
function ZeroGWheelGate({ vehicleRef, enable, enableCustomGravity, spawnPos, zeroGRef, onZeroGChange }) {
|
|
395
494
|
const { world } = useRapier();
|
|
396
495
|
useFrame(() => {
|
|
397
496
|
if (!enable) return;
|
|
398
497
|
const handle = vehicleRef.current;
|
|
399
|
-
let
|
|
498
|
+
let gSq;
|
|
400
499
|
if (enableCustomGravity) {
|
|
401
|
-
if (handle?.body && !handle.body.isSleeping())
|
|
500
|
+
if (handle?.body && !handle.body.isSleeping()) gSq = handle.gravityMag * handle.gravityMag;
|
|
402
501
|
else {
|
|
403
|
-
const
|
|
404
|
-
if (
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
} else _samplePos.set(spawnPos[0], spawnPos[1], spawnPos[2]);
|
|
408
|
-
const g = useCustomGravity.getState().gravityField(_samplePos);
|
|
409
|
-
isZeroG = isZeroGravityVec(g.x, g.y, g.z);
|
|
410
|
-
}
|
|
411
|
-
if (!customPrimedRef.current) {
|
|
412
|
-
customPrimedRef.current = true;
|
|
413
|
-
onCustomPrimed();
|
|
502
|
+
const t = handle?.body?.translation();
|
|
503
|
+
if (t) _samplePos.set(t.x, t.y, t.z);
|
|
504
|
+
else _samplePos.set(spawnPos[0], spawnPos[1], spawnPos[2]);
|
|
505
|
+
gSq = useCustomGravity.getState().gravityField(_samplePos).lengthSq();
|
|
414
506
|
}
|
|
415
507
|
} else {
|
|
416
508
|
const g = world.gravity;
|
|
417
|
-
|
|
509
|
+
gSq = g.x * g.x + g.y * g.y + g.z * g.z;
|
|
418
510
|
}
|
|
511
|
+
const isZeroG = gSq <= 1e-12;
|
|
419
512
|
if (isZeroG !== zeroGRef.current) {
|
|
420
|
-
const wasActive = wheelActiveRef.current;
|
|
421
513
|
zeroGRef.current = isZeroG;
|
|
422
|
-
if (isZeroG && wasActive) needsEpochBumpRef.current = true;
|
|
423
|
-
else if (!isZeroG && needsEpochBumpRef.current) {
|
|
424
|
-
needsEpochBumpRef.current = false;
|
|
425
|
-
onBumpWheelEpoch();
|
|
426
|
-
}
|
|
427
514
|
onZeroGChange(isZeroG);
|
|
428
515
|
}
|
|
429
516
|
if (isZeroG) resetAllWheelHits(handle);
|
|
430
517
|
});
|
|
431
518
|
return null;
|
|
432
519
|
}
|
|
433
|
-
function
|
|
520
|
+
function ZeroGWheelPostClear({ vehicleRef, zeroGRef }) {
|
|
434
521
|
useFrame(() => {
|
|
435
522
|
if (zeroGRef.current) resetAllWheelHits(vehicleRef.current);
|
|
436
523
|
});
|
|
@@ -652,46 +739,34 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
|
|
|
652
739
|
assertSceneIdentityWorldScale(bundle.scene);
|
|
653
740
|
const { ref, vehicleRef, jointsRef, position, rotation, quaternion, enable = true, enableCustomGravity = false, carConfig, droneConfig, wheelConfig, frontWheelConfig, rearWheelConfig, propellerConfig, linearVelocity, angularVelocity, gravityScale, children, ...ecctrlRest } = rest;
|
|
654
741
|
const { world } = useRapier();
|
|
655
|
-
const
|
|
656
|
-
const
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
742
|
+
const [rotorsMounted, setRotorsMounted] = useState(false);
|
|
743
|
+
const [isZeroG, setIsZeroG] = useState(() => {
|
|
744
|
+
if (enableCustomGravity) return true;
|
|
745
|
+
const g = world.gravity;
|
|
746
|
+
return g.x * g.x + g.y * g.y + g.z * g.z <= 1e-12;
|
|
747
|
+
});
|
|
660
748
|
const [wheelEpoch, setWheelEpoch] = useState(0);
|
|
661
|
-
const
|
|
662
|
-
const
|
|
663
|
-
const needsEpochBumpRef = useRef(false);
|
|
664
|
-
const wheelEnable = enable && customPrimed && !(!enableCustomGravity && propZeroG) && !runtimeZeroG;
|
|
665
|
-
const wheelActiveRef = useRef(wheelEnable);
|
|
666
|
-
wheelActiveRef.current = wheelEnable;
|
|
667
|
-
const bumpWheelEpoch = useCallback(() => setWheelEpoch((e) => e + 1), []);
|
|
668
|
-
const markCustomPrimed = useCallback(() => setCustomPrimed(true), []);
|
|
749
|
+
const zeroGRef = useRef(isZeroG);
|
|
750
|
+
const poisonedRef = useRef(false);
|
|
669
751
|
useLayoutEffect(() => {
|
|
670
|
-
|
|
752
|
+
setRotorsMounted(true);
|
|
671
753
|
}, []);
|
|
672
|
-
|
|
673
|
-
if (
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
} else {
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
const isZeroG = propZeroG || isZeroGravityVec(world.gravity.x, world.gravity.y, world.gravity.z);
|
|
680
|
-
zeroGRef.current = isZeroG;
|
|
681
|
-
setRuntimeZeroG(isZeroG);
|
|
682
|
-
if (isZeroG) resetAllWheelHits(innerRef.current);
|
|
754
|
+
const handleZeroGChange = useCallback((nextZeroG) => {
|
|
755
|
+
if (nextZeroG) {
|
|
756
|
+
poisonedRef.current = true;
|
|
757
|
+
resetAllWheelHits(innerRef.current);
|
|
758
|
+
} else if (poisonedRef.current) {
|
|
759
|
+
poisonedRef.current = false;
|
|
760
|
+
setWheelEpoch((e) => e + 1);
|
|
683
761
|
}
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
propZeroG,
|
|
687
|
-
world
|
|
688
|
-
]);
|
|
762
|
+
setIsZeroG(nextZeroG);
|
|
763
|
+
}, []);
|
|
689
764
|
const chassis = useMemo(() => computeChassisMassProps(bundle.bodies), [bundle]);
|
|
690
765
|
const collidersList = useMemo(() => buildChassisColliders(bundle.bodies, chassis), [bundle.bodies, chassis]);
|
|
691
|
-
const solvedWheels = useMemo(() => solveWheelLoads(bundle.wheels,
|
|
766
|
+
const solvedWheels = useMemo(() => solveWheelLoads(bundle.wheels, bundle.totalDynamicMass, bundle.centerOfMass), [
|
|
692
767
|
bundle.wheels,
|
|
693
|
-
|
|
694
|
-
|
|
768
|
+
bundle.totalDynamicMass,
|
|
769
|
+
bundle.centerOfMass
|
|
695
770
|
]);
|
|
696
771
|
const rotorOrigins = useMemo(() => captureRotorOrigins([...bundle.wheels.map((w) => w.object), ...bundle.propellers.map((p) => p.object)]), [bundle.wheels, bundle.propellers]);
|
|
697
772
|
const [px, py, pz] = readPosTuple(position);
|
|
@@ -748,7 +823,7 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
|
|
|
748
823
|
const effLinVel = linearVelocity ?? chassis.linearVelocity;
|
|
749
824
|
const effAngVel = angularVelocity ?? chassis.angularVelocity;
|
|
750
825
|
const effGravScale = gravityScale ?? chassis.gravityFactor;
|
|
751
|
-
return /* @__PURE__ */ jsxs(EcctrlVehicle, {
|
|
826
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(EcctrlVehicle, {
|
|
752
827
|
ref: assignRef,
|
|
753
828
|
enable,
|
|
754
829
|
enableCustomGravity,
|
|
@@ -770,24 +845,14 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
|
|
|
770
845
|
c: spec,
|
|
771
846
|
massProperties
|
|
772
847
|
}, key)),
|
|
773
|
-
/* @__PURE__ */
|
|
774
|
-
|
|
775
|
-
vehicleRef: innerRef,
|
|
776
|
-
jointsRef
|
|
777
|
-
}),
|
|
778
|
-
rotorsPrimed && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
779
|
-
/* @__PURE__ */ jsx(ZeroGWheelPreGuard, {
|
|
848
|
+
rotorsMounted && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
849
|
+
/* @__PURE__ */ jsx(ZeroGWheelGate, {
|
|
780
850
|
vehicleRef: innerRef,
|
|
781
851
|
enable,
|
|
782
852
|
enableCustomGravity,
|
|
783
853
|
spawnPos: bodyPos,
|
|
784
|
-
wheelActiveRef,
|
|
785
854
|
zeroGRef,
|
|
786
|
-
|
|
787
|
-
needsEpochBumpRef,
|
|
788
|
-
onZeroGChange: setRuntimeZeroG,
|
|
789
|
-
onCustomPrimed: markCustomPrimed,
|
|
790
|
-
onBumpWheelEpoch: bumpWheelEpoch
|
|
855
|
+
onZeroGChange: handleZeroGChange
|
|
791
856
|
}),
|
|
792
857
|
/* @__PURE__ */ jsx(KhrVehicleRotors, {
|
|
793
858
|
solvedWheels,
|
|
@@ -796,20 +861,26 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
|
|
|
796
861
|
comZ: chassis.com[2],
|
|
797
862
|
rotorOrigins,
|
|
798
863
|
enable,
|
|
799
|
-
wheelEnable,
|
|
864
|
+
wheelEnable: enable && !isZeroG,
|
|
800
865
|
wheelEpoch,
|
|
801
866
|
wheelConfig,
|
|
802
867
|
frontWheelConfig,
|
|
803
868
|
rearWheelConfig,
|
|
804
869
|
propellerConfig
|
|
805
870
|
}),
|
|
806
|
-
/* @__PURE__ */ jsx(
|
|
871
|
+
/* @__PURE__ */ jsx(ZeroGWheelPostClear, {
|
|
807
872
|
vehicleRef: innerRef,
|
|
808
873
|
zeroGRef
|
|
809
|
-
})
|
|
874
|
+
}, `post:${wheelEpoch}`)
|
|
810
875
|
] })
|
|
811
876
|
]
|
|
812
|
-
})
|
|
877
|
+
}), rotorsMounted && /* @__PURE__ */ jsx(KhrVehicleJoints, {
|
|
878
|
+
bodies: bundle.bodies,
|
|
879
|
+
vehicleRef: innerRef,
|
|
880
|
+
spawnPos: bodyPos,
|
|
881
|
+
spawnQuat: bodyQuat,
|
|
882
|
+
jointsRef
|
|
883
|
+
})] });
|
|
813
884
|
}
|
|
814
885
|
export { KhrVehicle, solveWheelLoads };
|
|
815
886
|
|