@khr3/core 0.2.12 → 0.2.14
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 +54 -3
- package/dist/KhrAssembly-BOY_g5we.js +1968 -0
- package/dist/KhrAssembly-BOY_g5we.js.map +1 -0
- package/dist/KhrAssembly.d.ts +31 -4
- package/dist/decoders.d.ts +5 -2
- package/dist/heightfield.d.ts +45 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +255 -496
- package/dist/index.js.map +1 -1
- package/dist/inertia.d.ts +29 -17
- package/dist/types.d.ts +12 -0
- package/dist/vehicle.js +37 -198
- package/dist/vehicle.js.map +1 -1
- package/dist/vehicleJoints.d.ts +4 -2
- package/docs/api-reference.md +160 -0
- package/package.json +4 -3
- package/dist/joints-5IgWDD2w.js +0 -865
- package/dist/joints-5IgWDD2w.js.map +0 -1
package/dist/inertia.d.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import * as THREE from "three";
|
|
2
2
|
import type { RapierRigidBody } from "@react-three/rapier";
|
|
3
|
-
import { type KhrBodySpec, type KhrColliderShape } from "./types.js";
|
|
3
|
+
import { type KhrBodySpec, type KhrColliderShape, type KhrColliderSpec } from "./types.js";
|
|
4
4
|
export type Vec3Tuple = [number, number, number];
|
|
5
5
|
export type QuatTuple = [number, number, number, number];
|
|
6
|
-
export interface
|
|
6
|
+
export interface LumpedMassProps {
|
|
7
7
|
readonly mass: number;
|
|
8
8
|
readonly com: Vec3Tuple;
|
|
9
|
-
readonly
|
|
10
|
-
readonly inertiaDiagonal: Vec3Tuple;
|
|
9
|
+
readonly inertia: Vec3Tuple;
|
|
11
10
|
readonly inertiaFrame: QuatTuple;
|
|
12
|
-
readonly hasExplicitInertia: boolean;
|
|
13
11
|
}
|
|
14
12
|
export declare function assertUnitScale(scale: unknown, owner: string): void;
|
|
15
13
|
export declare function readFiniteVec3(val: unknown, fieldLabel: string): Vec3Tuple;
|
|
@@ -21,21 +19,35 @@ export declare function computeRelativePose(ownerObj: THREE.Object3D, childObj:
|
|
|
21
19
|
localQuaternion: QuatTuple;
|
|
22
20
|
};
|
|
23
21
|
export declare function extractMeshColliderShape(targetObj: THREE.Object3D, meshNodeName: string, convexHull: boolean): KhrColliderShape;
|
|
24
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Exact 3x3 symmetric Jacobi eigendecomposition returning positive principal inertia diagonal
|
|
24
|
+
* and right-handed `SO(3)` principal frame quaternion `[x, y, z, w]`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function jacobiPrincipalInertia(ixx: number, iyy: number, izz: number, ixy: number, ixz: number, iyz: number): {
|
|
27
|
+
inertia: Vec3Tuple;
|
|
28
|
+
inertiaFrame: QuatTuple;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Computes exact principal inertia diagonal `[I1, I2, I3]` for a single `KhrColliderShape` of `mass` kg
|
|
32
|
+
* about its centroid.
|
|
33
|
+
*/
|
|
34
|
+
export declare function computeShapeInertia(shape: KhrColliderShape, mass: number): Vec3Tuple;
|
|
35
|
+
/**
|
|
36
|
+
* Resolves exact center of mass, full 3x3 Steiner parallel-axis inertia tensor, and Jacobi principal frame
|
|
37
|
+
* for a dynamic `KhrBodySpec` from its colliders when `motion.inertiaDiagonal` is omitted in the `.glb`.
|
|
38
|
+
*/
|
|
39
|
+
export declare function resolveBodyMassFromColliders(colliders: readonly KhrColliderSpec[], mass: number, explicitCom: Vec3Tuple | null, explicitInertiaOrientation: QuatTuple | null, nodeName: string): {
|
|
40
|
+
centerOfMass: Vec3Tuple;
|
|
41
|
+
inertiaDiagonal: Vec3Tuple;
|
|
42
|
+
inertiaOrientation: QuatTuple;
|
|
43
|
+
};
|
|
25
44
|
export declare function computeAssemblyMassAndCom(bodies: readonly KhrBodySpec[]): {
|
|
26
45
|
totalDynamicMass: number;
|
|
27
46
|
centerOfMass: Vec3Tuple;
|
|
28
47
|
};
|
|
48
|
+
export declare function bodyPoseMatrix(body: KhrBodySpec, target?: THREE.Matrix4): THREE.Matrix4;
|
|
49
|
+
export declare function computeLumpedMassProps(bodies: readonly KhrBodySpec[], invFrame?: THREE.Matrix4): LumpedMassProps | null;
|
|
29
50
|
/**
|
|
30
|
-
*
|
|
31
|
-
* When `hasExplicitInertia` is false, Rapier WASM (`parry3d`) computes exact volume, CoM,
|
|
32
|
-
* parallel-axis tensor, and principal inertia directly from the attached colliders.
|
|
33
|
-
*/
|
|
34
|
-
export declare function buildBodyMassTarget(body: KhrBodySpec, bodyRotInRigidBody?: readonly [number, number, number, number], bodyOffsetInRigidBody?: readonly [number, number, number]): RapierBodyMassTarget | null;
|
|
35
|
-
/**
|
|
36
|
-
* Applies `KHR_physics_rigid_bodies` mass properties to a live Rapier WASM `RigidBody`:
|
|
37
|
-
* - Explicit `motion.inertiaDiagonal`: passes authored `{ mass, com, inertiaDiagonal, inertiaFrame }` directly to Rapier.
|
|
38
|
-
* - Omitted `motion.inertiaDiagonal`: delegates volume, CoM, parallel-axis lumping, and Jacobi principal-axis inertia
|
|
39
|
-
* to Rapier's Rust `parry3d` engine across all attached colliders, preserving explicit `motion.centerOfMass` if authored.
|
|
51
|
+
* Applies exact `LumpedMassProps` directly in `RigidBody` local space (`density = 0` on colliders).
|
|
40
52
|
*/
|
|
41
|
-
export declare function applyRapierBodyMass(rb: RapierRigidBody,
|
|
53
|
+
export declare function applyRapierBodyMass(rb: RapierRigidBody, lumped: LumpedMassProps | null): void;
|
package/dist/types.d.ts
CHANGED
|
@@ -183,12 +183,23 @@ export interface KhrBreakawaySpec {
|
|
|
183
183
|
latchImpulseNs?: number;
|
|
184
184
|
breakImpulseNs: number;
|
|
185
185
|
}
|
|
186
|
+
export interface KhrEmitterSpec {
|
|
187
|
+
nodeName: string;
|
|
188
|
+
kind: string;
|
|
189
|
+
drive?: string;
|
|
190
|
+
color?: [number, number, number];
|
|
191
|
+
size?: number;
|
|
192
|
+
position: [number, number, number];
|
|
193
|
+
quaternion: [number, number, number, number];
|
|
194
|
+
object: THREE.Object3D;
|
|
195
|
+
}
|
|
186
196
|
export interface KhrBodySpec {
|
|
187
197
|
nodeName: string;
|
|
188
198
|
isKinematic: boolean;
|
|
189
199
|
hasExplicitMotion?: boolean;
|
|
190
200
|
hasExplicitCom?: boolean;
|
|
191
201
|
hasExplicitInertia?: boolean;
|
|
202
|
+
hasExplicitInertiaOrientation?: boolean;
|
|
192
203
|
mass: number;
|
|
193
204
|
centerOfMass: [number, number, number];
|
|
194
205
|
inertiaDiagonal: [number, number, number];
|
|
@@ -209,6 +220,7 @@ export interface KhrAssetBundle {
|
|
|
209
220
|
bodies: KhrBodySpec[];
|
|
210
221
|
wheels: EcctrlWheelTag[];
|
|
211
222
|
propellers: EcctrlPropellerTag[];
|
|
223
|
+
emitters: KhrEmitterSpec[];
|
|
212
224
|
sockets: Record<string, [number, number, number]>;
|
|
213
225
|
totalDynamicMass: number;
|
|
214
226
|
centerOfMass: [number, number, number];
|
package/dist/vehicle.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { useCallback,
|
|
1
|
+
import { useCallback, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
2
2
|
import * as THREE from "three";
|
|
3
3
|
import { useFrame } from "@react-three/fiber";
|
|
4
|
-
import {
|
|
4
|
+
import { useRapier } from "@react-three/rapier";
|
|
5
5
|
import { useCustomGravity } from "@pinkaphant/ecctrl/gravity";
|
|
6
6
|
import { EcctrlVehicle, ShapeCastWheel, ThrustPropeller } from "@pinkaphant/ecctrl/vehicle";
|
|
7
|
-
import { C as
|
|
7
|
+
import { C as restoreSubtreeOrigin, D as assertSceneIdentityWorldScale, H as assignRef, O as assertUnitScale, T as applyRapierBodyMass, V as KhrValidationError, _ as captureSubtreeOrigins, f as openTargetForLimits, g as BundleSceneHost, h as KhrCollider, i as ATTACHED_PANEL_INTERACTION_GROUPS, j as computeLumpedMassProps, k as bodyPoseMatrix, p as toggleDrivenJoint, s as closedTargetForLimits, t as ArticulatedPartBody, u as isJointOpen, x as registerMountedSubtree } from "./KhrAssembly-BOY_g5we.js";
|
|
8
8
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
9
9
|
// src/vehicleJoints.tsx
|
|
10
10
|
function mergeVehiclePanelUserData(userData) {
|
|
@@ -68,8 +68,8 @@ function planVehicleJointParts(bodies) {
|
|
|
68
68
|
rcQuat.z,
|
|
69
69
|
rcQuat.w
|
|
70
70
|
];
|
|
71
|
-
const
|
|
72
|
-
const
|
|
71
|
+
const invPartFrame = new THREE.Matrix4().compose(new THREE.Vector3().fromArray(b.localPosition), new THREE.Quaternion(), one).invert();
|
|
72
|
+
const lumpedMass = computeLumpedMassProps([b], invPartFrame);
|
|
73
73
|
const frameMat = new THREE.Matrix4().makeRotationFromQuaternion(rcQuat);
|
|
74
74
|
const cPos = new THREE.Vector3(), cQuat = new THREE.Quaternion();
|
|
75
75
|
const colliders = b.colliders.map((spec) => {
|
|
@@ -91,21 +91,23 @@ function planVehicleJointParts(bodies) {
|
|
|
91
91
|
cQuat.y,
|
|
92
92
|
cQuat.z,
|
|
93
93
|
cQuat.w
|
|
94
|
-
]
|
|
95
|
-
density
|
|
94
|
+
]
|
|
96
95
|
};
|
|
97
96
|
});
|
|
97
|
+
const restRelPos = [
|
|
98
|
+
rcPos.x,
|
|
99
|
+
rcPos.y,
|
|
100
|
+
rcPos.z
|
|
101
|
+
];
|
|
98
102
|
const plan = {
|
|
99
103
|
spec: b,
|
|
100
104
|
joint,
|
|
101
105
|
parent,
|
|
102
106
|
restRelParentPos: anchorParent,
|
|
103
107
|
restRelParentQuat: rcQuatTuple,
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
rcPos.z
|
|
108
|
-
],
|
|
108
|
+
restRelRootPos: restRelPos,
|
|
109
|
+
restRelRootQuat: rcQuatTuple,
|
|
110
|
+
restRelChassisPos: restRelPos,
|
|
109
111
|
restRelChassisQuat: rcQuatTuple,
|
|
110
112
|
axisParent: [
|
|
111
113
|
axParentVec.x,
|
|
@@ -117,122 +119,13 @@ function planVehicleJointParts(bodies) {
|
|
|
117
119
|
axChildVec.y,
|
|
118
120
|
axChildVec.z
|
|
119
121
|
],
|
|
120
|
-
|
|
122
|
+
lumpedMass,
|
|
121
123
|
colliders
|
|
122
124
|
};
|
|
123
125
|
partByName.set(b.nodeName, plan);
|
|
124
126
|
return plan;
|
|
125
127
|
});
|
|
126
128
|
}
|
|
127
|
-
function VehicleArticulatedPanel({ part, spawnPos, spawnQuat, linearVelocity, angularVelocity, gravityScale, panelUserData, parentBody, origin, onBody, onJointReady }) {
|
|
128
|
-
const { rapier, world } = useRapier();
|
|
129
|
-
const registry = useContext(JointRegistryContext);
|
|
130
|
-
const rtRef = useRef(null);
|
|
131
|
-
const [childBody, setChildBody] = useState(null);
|
|
132
|
-
const worldPose = useMemo(() => {
|
|
133
|
-
const sq = new THREE.Quaternion(spawnQuat[0], spawnQuat[1], spawnQuat[2], spawnQuat[3]).normalize();
|
|
134
|
-
const pos = new THREE.Vector3().fromArray(part.restRelChassisPos).applyQuaternion(sq).add(new THREE.Vector3(spawnPos[0], spawnPos[1], spawnPos[2]));
|
|
135
|
-
return {
|
|
136
|
-
pos: [
|
|
137
|
-
pos.x,
|
|
138
|
-
pos.y,
|
|
139
|
-
pos.z
|
|
140
|
-
],
|
|
141
|
-
quat: [
|
|
142
|
-
sq.x,
|
|
143
|
-
sq.y,
|
|
144
|
-
sq.z,
|
|
145
|
-
sq.w
|
|
146
|
-
]
|
|
147
|
-
};
|
|
148
|
-
}, [
|
|
149
|
-
part.restRelChassisPos,
|
|
150
|
-
spawnPos,
|
|
151
|
-
spawnQuat
|
|
152
|
-
]);
|
|
153
|
-
const assignChildRef = useCallback((rb) => {
|
|
154
|
-
setChildBody(rb);
|
|
155
|
-
onBody(part.spec.nodeName, rb);
|
|
156
|
-
}, [part.spec.nodeName, onBody]);
|
|
157
|
-
useEffect(() => () => onBody(part.spec.nodeName, null), [part.spec.nodeName, onBody]);
|
|
158
|
-
useEffect(() => {
|
|
159
|
-
if (childBody && part.massTarget) applyRapierBodyMass(childBody, part.massTarget);
|
|
160
|
-
}, [childBody, part.massTarget]);
|
|
161
|
-
useEffect(() => {
|
|
162
|
-
if (!parentBody || !childBody) return;
|
|
163
|
-
const imp = createRapierImpulseJoint(rapier, world, parentBody, childBody, part.joint, part.restRelParentPos, [
|
|
164
|
-
0,
|
|
165
|
-
0,
|
|
166
|
-
0
|
|
167
|
-
], part.axisParent, part.axisChild, part.restRelParentQuat);
|
|
168
|
-
const rt = buildJointRuntime({
|
|
169
|
-
rapier,
|
|
170
|
-
world,
|
|
171
|
-
nodeName: part.spec.nodeName,
|
|
172
|
-
role: part.joint.role,
|
|
173
|
-
kind: part.joint.kind,
|
|
174
|
-
object: part.spec.object,
|
|
175
|
-
axisParent: part.axisParent,
|
|
176
|
-
anchorParent: part.restRelParentPos,
|
|
177
|
-
limits: part.joint.limits,
|
|
178
|
-
drive: part.joint.drive,
|
|
179
|
-
breakaway: part.spec.breakaway,
|
|
180
|
-
rapierJoint: imp,
|
|
181
|
-
parentBody,
|
|
182
|
-
childBody
|
|
183
|
-
});
|
|
184
|
-
rtRef.current = rt;
|
|
185
|
-
registry?.add(rt);
|
|
186
|
-
onJointReady(rt.nodeName, part.spec.object, rt);
|
|
187
|
-
return () => {
|
|
188
|
-
rtRef.current = null;
|
|
189
|
-
registry?.delete(rt);
|
|
190
|
-
onJointReady(rt.nodeName, part.spec.object, null);
|
|
191
|
-
if (rt.rapierJoint && world.getImpulseJoint(rt.rapierJoint.handle)) world.removeImpulseJoint(rt.rapierJoint, true);
|
|
192
|
-
};
|
|
193
|
-
}, [
|
|
194
|
-
rapier,
|
|
195
|
-
world,
|
|
196
|
-
part,
|
|
197
|
-
parentBody,
|
|
198
|
-
childBody,
|
|
199
|
-
registry,
|
|
200
|
-
onJointReady
|
|
201
|
-
]);
|
|
202
|
-
const onForce = useMemo(() => part.spec.breakaway ? (p) => {
|
|
203
|
-
if (world.timestep > 0) rtRef.current?.applyImpactImpulse(p.totalForceMagnitude * world.timestep);
|
|
204
|
-
} : void 0, [part.spec.breakaway, world]);
|
|
205
|
-
const linVel = part.spec.linearVelocity ?? linearVelocity;
|
|
206
|
-
const angVel = part.spec.angularVelocity ?? angularVelocity;
|
|
207
|
-
const gravScale = part.spec.gravityFactor ?? gravityScale;
|
|
208
|
-
return /* @__PURE__ */ jsxs(RigidBody, {
|
|
209
|
-
ref: assignChildRef,
|
|
210
|
-
type: part.spec.isKinematic ? "kinematicPosition" : "dynamic",
|
|
211
|
-
position: worldPose.pos,
|
|
212
|
-
quaternion: worldPose.quat,
|
|
213
|
-
colliders: false,
|
|
214
|
-
userData: panelUserData,
|
|
215
|
-
onContactForce: onForce,
|
|
216
|
-
...linVel ? { linearVelocity: linVel } : {},
|
|
217
|
-
...angVel ? { angularVelocity: angVel } : {},
|
|
218
|
-
...gravScale === void 0 ? {} : { gravityScale: gravScale },
|
|
219
|
-
children: [/* @__PURE__ */ jsx(MountSubtreeSlot, {
|
|
220
|
-
object: part.spec.object,
|
|
221
|
-
position: [
|
|
222
|
-
0,
|
|
223
|
-
0,
|
|
224
|
-
0
|
|
225
|
-
],
|
|
226
|
-
quaternion: part.restRelChassisQuat,
|
|
227
|
-
origin
|
|
228
|
-
}), part.colliders.map((col, cIdx) => /* @__PURE__ */ jsx(KhrCollider, {
|
|
229
|
-
c: col.spec,
|
|
230
|
-
position: col.position,
|
|
231
|
-
quaternion: col.quaternion,
|
|
232
|
-
density: col.density
|
|
233
|
-
}, `vj_${part.spec.nodeName}:${cIdx}`))]
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
129
|
var _pPos = new THREE.Vector3();
|
|
237
130
|
var _pQuat = new THREE.Quaternion();
|
|
238
131
|
var _anchor = new THREE.Vector3();
|
|
@@ -339,14 +232,14 @@ function KhrVehicleJoints({ bundleScene, bodies, vehicleRef, chassisBody, spawnP
|
|
|
339
232
|
}
|
|
340
233
|
}), [parts, vehicleRef]);
|
|
341
234
|
if (parts.length === 0) return null;
|
|
342
|
-
return /* @__PURE__ */ jsx(Fragment, { children: parts.map((part) => /* @__PURE__ */ jsx(
|
|
235
|
+
return /* @__PURE__ */ jsx(Fragment, { children: parts.map((part) => /* @__PURE__ */ jsx(ArticulatedPartBody, {
|
|
343
236
|
part,
|
|
344
|
-
spawnPos,
|
|
345
|
-
spawnQuat,
|
|
237
|
+
rootPos: spawnPos,
|
|
238
|
+
rootQuat: spawnQuat,
|
|
346
239
|
linearVelocity,
|
|
347
240
|
angularVelocity,
|
|
348
241
|
gravityScale,
|
|
349
|
-
panelUserData,
|
|
242
|
+
userData: panelUserData,
|
|
350
243
|
parentBody: part.parent ? partBodies.get(part.parent.spec.nodeName) ?? null : chassisBody ?? vehicleRef.current?.body ?? null,
|
|
351
244
|
origin: origins?.get(part.spec.object),
|
|
352
245
|
onBody: handleBody,
|
|
@@ -520,44 +413,8 @@ function readSpawnQuat(rotation, quaternion) {
|
|
|
520
413
|
function computeChassisMassProps(bodies) {
|
|
521
414
|
const dyn = bodies.filter((b) => !b.isKinematic && b.joint === null);
|
|
522
415
|
if (dyn.length === 0) throw new KhrValidationError("KhrVehicle: bundle has no dynamic rigid body for the chassis");
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
const rPos = new THREE.Vector3(), rQuat = new THREE.Quaternion();
|
|
526
|
-
for (const b of dyn) {
|
|
527
|
-
if (!(b.mass > 0)) continue;
|
|
528
|
-
mass += b.mass;
|
|
529
|
-
rPos.fromArray(b.centerOfMass).applyQuaternion(rQuat.fromArray(b.localQuaternion));
|
|
530
|
-
const cx = b.localPosition[0] + rPos.x;
|
|
531
|
-
const cy = b.localPosition[1] + rPos.y;
|
|
532
|
-
const cz = b.localPosition[2] + rPos.z;
|
|
533
|
-
mx += cx * b.mass;
|
|
534
|
-
my += cy * b.mass;
|
|
535
|
-
mz += cz * b.mass;
|
|
536
|
-
if (b.hasExplicitCom || Math.abs(cx) > 1e-9 || Math.abs(cy) > 1e-9 || Math.abs(cz) > 1e-9) anyExplicitCom = true;
|
|
537
|
-
}
|
|
538
|
-
if (!(mass > 0)) throw new KhrValidationError("KhrVehicle: chassis dynamic mass must be positive");
|
|
539
|
-
const com = [
|
|
540
|
-
mx / mass,
|
|
541
|
-
my / mass,
|
|
542
|
-
mz / mass
|
|
543
|
-
];
|
|
544
|
-
const massTarget = (dyn.length === 1 ? buildBodyMassTarget(dyn[0], dyn[0].localQuaternion, dyn[0].localPosition) : null) ?? {
|
|
545
|
-
mass,
|
|
546
|
-
com,
|
|
547
|
-
hasExplicitCom: anyExplicitCom,
|
|
548
|
-
inertiaDiagonal: [
|
|
549
|
-
0,
|
|
550
|
-
0,
|
|
551
|
-
0
|
|
552
|
-
],
|
|
553
|
-
inertiaFrame: [
|
|
554
|
-
0,
|
|
555
|
-
0,
|
|
556
|
-
0,
|
|
557
|
-
1
|
|
558
|
-
],
|
|
559
|
-
hasExplicitInertia: false
|
|
560
|
-
};
|
|
416
|
+
const lumpedMass = computeLumpedMassProps(dyn);
|
|
417
|
+
if (!lumpedMass) throw new KhrValidationError("KhrVehicle: chassis dynamic mass must be positive");
|
|
561
418
|
let gravityFactor;
|
|
562
419
|
if (dyn.some((b) => b.gravityFactor !== void 0)) {
|
|
563
420
|
const g0 = dyn[0].gravityFactor ?? 1;
|
|
@@ -578,9 +435,9 @@ function computeChassisMassProps(bodies) {
|
|
|
578
435
|
vz += lv[2] * b.mass;
|
|
579
436
|
}
|
|
580
437
|
linearVelocity = [
|
|
581
|
-
vx / mass,
|
|
582
|
-
vy / mass,
|
|
583
|
-
vz / mass
|
|
438
|
+
vx / lumpedMass.mass,
|
|
439
|
+
vy / lumpedMass.mass,
|
|
440
|
+
vz / lumpedMass.mass
|
|
584
441
|
];
|
|
585
442
|
}
|
|
586
443
|
const rootDyn = dyn.find((b) => b.angularVelocity !== void 0);
|
|
@@ -590,21 +447,18 @@ function computeChassisMassProps(bodies) {
|
|
|
590
447
|
rootDyn.angularVelocity[2]
|
|
591
448
|
] : void 0;
|
|
592
449
|
return {
|
|
593
|
-
|
|
594
|
-
com,
|
|
595
|
-
massTarget,
|
|
450
|
+
lumpedMass,
|
|
596
451
|
...linearVelocity ? { linearVelocity } : {},
|
|
597
452
|
...angularVelocity ? { angularVelocity } : {},
|
|
598
453
|
...gravityFactor !== void 0 ? { gravityFactor } : {}
|
|
599
454
|
};
|
|
600
455
|
}
|
|
601
|
-
function buildChassisColliders(bodies
|
|
456
|
+
function buildChassisColliders(bodies) {
|
|
602
457
|
const out = [];
|
|
603
458
|
const one = new THREE.Vector3(1, 1, 1);
|
|
604
459
|
const pos = new THREE.Vector3();
|
|
605
460
|
const quat = new THREE.Quaternion();
|
|
606
461
|
const scl = new THREE.Vector3();
|
|
607
|
-
const density = massTarget.hasExplicitInertia ? 0 : 1;
|
|
608
462
|
for (const b of bodies) {
|
|
609
463
|
if (b.joint !== null || b.isKinematic && b.hasExplicitMotion) continue;
|
|
610
464
|
const bodyMat = bodyPoseMatrix(b);
|
|
@@ -628,8 +482,7 @@ function buildChassisColliders(bodies, massTarget) {
|
|
|
628
482
|
quat.z,
|
|
629
483
|
quat.w
|
|
630
484
|
]
|
|
631
|
-
}
|
|
632
|
-
density
|
|
485
|
+
}
|
|
633
486
|
});
|
|
634
487
|
}
|
|
635
488
|
}
|
|
@@ -749,25 +602,14 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
|
|
|
749
602
|
const zeroGRef = useRef(isZeroG);
|
|
750
603
|
const [chassisBody, setChassisBody] = useState(null);
|
|
751
604
|
const chassis = useMemo(() => computeChassisMassProps(bundle.bodies), [bundle]);
|
|
752
|
-
const collidersList = useMemo(() => buildChassisColliders(bundle.bodies
|
|
753
|
-
const [rapierCom, setRapierCom] = useState(null);
|
|
754
|
-
const effCom = rapierCom ?? bundle.centerOfMass;
|
|
605
|
+
const collidersList = useMemo(() => buildChassisColliders(bundle.bodies), [bundle.bodies]);
|
|
755
606
|
useEffect(() => {
|
|
756
|
-
if (
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
const lc = chassisBody.localCom();
|
|
760
|
-
setRapierCom((prev) => prev && Math.hypot(prev[0] - lc.x, prev[1] - lc.y, prev[2] - lc.z) < 1e-4 ? prev : [
|
|
761
|
-
lc.x,
|
|
762
|
-
lc.y,
|
|
763
|
-
lc.z
|
|
764
|
-
]);
|
|
765
|
-
} else setRapierCom(null);
|
|
766
|
-
}, [chassisBody, chassis.massTarget]);
|
|
767
|
-
const solvedWheels = useMemo(() => solveWheelLoads(bundle.wheels, bundle.totalDynamicMass, effCom, .7, refGrav), [
|
|
607
|
+
if (chassisBody) applyRapierBodyMass(chassisBody, chassis.lumpedMass);
|
|
608
|
+
}, [chassisBody, chassis.lumpedMass]);
|
|
609
|
+
const solvedWheels = useMemo(() => solveWheelLoads(bundle.wheels, bundle.totalDynamicMass, bundle.centerOfMass, .7, refGrav), [
|
|
768
610
|
bundle.wheels,
|
|
769
611
|
bundle.totalDynamicMass,
|
|
770
|
-
|
|
612
|
+
bundle.centerOfMass,
|
|
771
613
|
refGrav
|
|
772
614
|
]);
|
|
773
615
|
const rotorOrigins = useMemo(() => captureSubtreeOrigins([...bundle.wheels.map((w) => w.object), ...bundle.propellers.map((p) => p.object)], bundle.scene), [
|
|
@@ -778,11 +620,11 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
|
|
|
778
620
|
const forwardSign = useMemo(() => {
|
|
779
621
|
const steered = solvedWheels.filter((w) => w.tag.steer && !w.tag.steerInvert);
|
|
780
622
|
if (steered.length === 0) return 1;
|
|
781
|
-
return steered.reduce((s, w) => s + w.mountPos[2], 0) / steered.length <
|
|
782
|
-
}, [solvedWheels,
|
|
783
|
-
const frontWheelNames = useMemo(() => new Set(solvedWheels.filter((w) => (w.mountPos[2] -
|
|
623
|
+
return steered.reduce((s, w) => s + w.mountPos[2], 0) / steered.length < bundle.centerOfMass[2] - .001 ? -1 : 1;
|
|
624
|
+
}, [solvedWheels, bundle.centerOfMass]);
|
|
625
|
+
const frontWheelNames = useMemo(() => new Set(solvedWheels.filter((w) => (w.mountPos[2] - bundle.centerOfMass[2]) * forwardSign >= 0).map((w) => w.tag.nodeName)), [
|
|
784
626
|
solvedWheels,
|
|
785
|
-
|
|
627
|
+
bundle.centerOfMass,
|
|
786
628
|
forwardSign
|
|
787
629
|
]);
|
|
788
630
|
const wheelsAllOff = !enable || isZeroG || wheelConfig?.enable === false && frontWheelConfig?.enable !== true && rearWheelConfig?.enable !== true;
|
|
@@ -913,10 +755,7 @@ function KhrVehicle({ scale, colliders, bundle, ...rest }) {
|
|
|
913
755
|
owner: "KhrVehicle"
|
|
914
756
|
}),
|
|
915
757
|
children,
|
|
916
|
-
collidersList.map(({ key, spec
|
|
917
|
-
c: spec,
|
|
918
|
-
density
|
|
919
|
-
}, key)),
|
|
758
|
+
collidersList.map(({ key, spec }) => /* @__PURE__ */ jsx(KhrCollider, { c: spec }, key)),
|
|
920
759
|
solvedWheels.map(({ tag, cornerMassKg, springK, dampingC, mountPos }) => {
|
|
921
760
|
const axleCfg = frontWheelNames.has(tag.nodeName) ? frontWheelConfig : rearWheelConfig;
|
|
922
761
|
const wheelVol = Math.max(Math.PI * tag.radius * tag.radius * tag.width, 1e-6);
|