@pinkaphant/ecctrl 2.0.3 → 2.0.4-beta.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.
@@ -11,8 +11,8 @@ import { r as remap, t as createSlerpVec3 } from "./Math-BkrN2TZK.js";
11
11
  import { a as EC_GREEN, c as EC_PURPLE, l as EC_RED, n as EC_BLUE, s as EC_MED_PURPLE, t as EC_AZURE } from "./Color-rCfz0rNn.js";
12
12
  import * as THREE from "three";
13
13
  import { useFrame } from "@react-three/fiber";
14
- import { RigidBody, useBeforePhysicsStep, useRapier } from "@react-three/rapier";
15
- import React, { createContext, forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useLayoutEffect, useMemo, useRef } from "react";
14
+ import { RigidBody, useRapier } from "@react-three/rapier";
15
+ import React, { createContext, forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef } from "react";
16
16
  import { QueryFilterFlags } from "@dimforge/rapier3d-compat";
17
17
  import { clamp, generateUUID, lerp } from "three/src/math/MathUtils.js";
18
18
  //#region src/vehicles/EcctrlVehicle.tsx
@@ -94,60 +94,6 @@ var getDriveRatio = (gearRatios, gearIndex, finalDriveRatio) => {
94
94
  return ((_ref = (_gearRatios$gearIndex = gearRatios[gearIndex]) !== null && _gearRatios$gearIndex !== void 0 ? _gearRatios$gearIndex : gearRatios[0]) !== null && _ref !== void 0 ? _ref : 0) * finalDriveRatio;
95
95
  };
96
96
  var getMaxWheelAngVel = (engineMaxRPM, driveRatio) => driveRatio !== 0 ? engineMaxRPM / driveRatio * (2 * Math.PI / 60) : 0;
97
- function solve4x4(G, b, aug, out) {
98
- for (let r = 0; r < 4; r++) {
99
- const r4 = r * 4;
100
- const r5 = r * 5;
101
- aug[r5] = G[r4];
102
- aug[r5 + 1] = G[r4 + 1];
103
- aug[r5 + 2] = G[r4 + 2];
104
- aug[r5 + 3] = G[r4 + 3];
105
- aug[r5 + 4] = b[r];
106
- }
107
- for (let col = 0; col < 4; col++) {
108
- let pivotRow = col;
109
- let maxAbs = Math.abs(aug[col * 5 + col]);
110
- for (let r = col + 1; r < 4; r++) {
111
- const v = Math.abs(aug[r * 5 + col]);
112
- if (v > maxAbs) {
113
- maxAbs = v;
114
- pivotRow = r;
115
- }
116
- }
117
- if (maxAbs < 1e-15) {
118
- out[col] = 0;
119
- continue;
120
- }
121
- if (pivotRow !== col) {
122
- const c5 = col * 5;
123
- const p5 = pivotRow * 5;
124
- for (let k = col; k < 5; k++) {
125
- const tmp = aug[c5 + k];
126
- aug[c5 + k] = aug[p5 + k];
127
- aug[p5 + k] = tmp;
128
- }
129
- }
130
- const c5 = col * 5;
131
- const diag = aug[c5 + col];
132
- for (let r = col + 1; r < 4; r++) {
133
- const r5 = r * 5;
134
- const factor = aug[r5 + col] / diag;
135
- if (factor === 0) continue;
136
- for (let k = col + 1; k < 5; k++) aug[r5 + k] -= factor * aug[c5 + k];
137
- }
138
- }
139
- for (let r = 3; r >= 0; r--) {
140
- const r5 = r * 5;
141
- const diag = aug[r5 + r];
142
- if (Math.abs(diag) < 1e-15) {
143
- out[r] = 0;
144
- continue;
145
- }
146
- let sum = aug[r5 + 4];
147
- for (let c = r + 1; c < 4; c++) sum -= aug[r5 + c] * out[c];
148
- out[r] = sum / diag;
149
- }
150
- }
151
97
  var VehicleContext = createContext(null);
152
98
  var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carConfig = {}, droneConfig = {}, enableCustomGravity = false, gravityDirLerpSpeed = 6, ...props }, ref) => {
153
99
  /**
@@ -226,11 +172,6 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
226
172
  */
227
173
  const maxTiltTan = useMemo(() => Math.tan(droneControlConfig.maxTiltAngle), [droneControlConfig.maxTiltAngle]);
228
174
  const hoverThrottle = useRef(0);
229
- const desiredVertForce = useRef(0);
230
- const gramMatrix = useRef(new Float64Array(16));
231
- const wrenchRhs = useRef(new Float64Array(4));
232
- const wrenchAug = useRef(new Float64Array(20));
233
- const wrenchLambda = useRef(new Float64Array(4));
234
175
  const targetUp = useRef(new THREE.Vector3());
235
176
  const tiltError = useRef(new THREE.Vector3());
236
177
  const tiltAngVel = useRef(new THREE.Vector3());
@@ -259,12 +200,11 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
259
200
  */
260
201
  const gravityField = useCustomGravity((state) => state.gravityField);
261
202
  const applyGravityField = useCustomGravity((state) => state.applyGravityField);
262
- const upAxis = useRef(new THREE.Vector3(0, 1, 0));
203
+ const upAxis = useRef(new THREE.Vector3());
263
204
  const referenceGravity = useRef(new THREE.Vector3());
264
205
  const referenceGravityMag = useRef(0);
265
- const referenceGravityDir = useRef(new THREE.Vector3(0, -1, 0));
266
- const gravityDir = useRef(new THREE.Vector3(0, -1, 0));
267
- const gravityInitRef = useRef(false);
206
+ const referenceGravityDir = useRef(new THREE.Vector3());
207
+ const gravityDir = useRef(new THREE.Vector3());
268
208
  const slerpRef = useRef(createSlerpVec3());
269
209
  /**
270
210
  * User controls setup
@@ -304,17 +244,6 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
304
244
  const vehicleXAxis = useRef(new THREE.Vector3());
305
245
  const vehicleYAxis = useRef(new THREE.Vector3());
306
246
  const vehicleZAxis = useRef(new THREE.Vector3());
307
- if (!gravityInitRef.current) {
308
- gravityInitRef.current = true;
309
- referenceGravity.current.copy(enableCustomGravity ? gravityField(vehiclePos.current) : world.gravity);
310
- const initMag = referenceGravity.current.length();
311
- referenceGravityMag.current = initMag;
312
- if (initMag > 1e-6) {
313
- referenceGravityDir.current.copy(referenceGravity.current).divideScalar(initMag);
314
- gravityDir.current.copy(referenceGravityDir.current);
315
- upAxis.current.copy(gravityDir.current).negate();
316
- }
317
- }
318
247
  const setMovement = useCallback((movement) => {
319
248
  if (movement.forward !== void 0) movementState.current.forward = movement.forward;
320
249
  if (movement.backward !== void 0) movementState.current.backward = movement.backward;
@@ -525,11 +454,13 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
525
454
  vehicleZAxis.current.set(0, 0, 1).applyQuaternion(vehicleQuat.current);
526
455
  }, []);
527
456
  /**
528
- * Update gravity/upAxis direction and value (sensing only; custom gravity impulse is applied after wheel/propeller sampling)
457
+ * Update gravity/upAxis direction and value
529
458
  */
530
- const updateGravityInfo = useCallback(() => {
531
- if (enableCustomGravity) referenceGravity.current.copy(gravityField(vehiclePos.current));
532
- else referenceGravity.current.copy(world.gravity);
459
+ const updateGravityInfo = useCallback((body) => {
460
+ if (enableCustomGravity) {
461
+ referenceGravity.current.copy(gravityField(vehiclePos.current));
462
+ applyGravityField(body, world.timestep);
463
+ } else referenceGravity.current.copy(world.gravity);
533
464
  referenceGravityMag.current = referenceGravity.current.length();
534
465
  referenceGravityDir.current.copy(referenceGravity.current).normalize();
535
466
  if (referenceGravityDir.current.lengthSq() === 0) referenceGravityDir.current.copy(vehicleYAxis.current).negate();
@@ -538,7 +469,8 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
538
469
  }, [
539
470
  enableCustomGravity,
540
471
  gravityDirLerpSpeed,
541
- gravityField
472
+ gravityField,
473
+ applyGravityField
542
474
  ]);
543
475
  /**
544
476
  *
@@ -611,22 +543,17 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
611
543
  if (!body) return;
612
544
  if (body.isSleeping()) {
613
545
  let shouldWake = false;
614
- let enabledCount = 0;
615
- let hitCount = 0;
616
546
  for (const wheel of wheels.values()) {
617
547
  const w = wheel.current;
618
- if (!w.enable) continue;
619
- enabledCount++;
620
548
  if (!w.rayHit) continue;
621
- hitCount++;
622
549
  if (w.isOnPlatform || Math.abs(w.wheelLinVel) > 1e-4) {
623
550
  shouldWake = true;
624
551
  break;
625
552
  }
626
553
  }
627
- if (!shouldWake && enabledCount > 0 && hitCount === 0 && referenceGravityMag.current > 1e-6) shouldWake = true;
628
554
  if (!shouldWake) return;
629
555
  body.wakeUp();
556
+ return;
630
557
  }
631
558
  wheels.forEach((wheel) => {
632
559
  if (!wheel.current.rayHit) return;
@@ -639,109 +566,6 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
639
566
  if (!body) return;
640
567
  updateTransmission(wheels);
641
568
  velocityBasedCarControl(input, wheels);
642
- let hasSplitStep = false;
643
- wheels.forEach((wheel) => {
644
- if (wheel.current.stepWheelSuspension) {
645
- hasSplitStep = true;
646
- wheel.current.stepWheelSuspension();
647
- } else {
648
- var _wheel$current$stepWh, _wheel$current;
649
- (_wheel$current$stepWh = (_wheel$current = wheel.current).stepWheelPhysics) === null || _wheel$current$stepWh === void 0 || _wheel$current$stepWh.call(_wheel$current);
650
- }
651
- });
652
- if (hasSplitStep) {
653
- const driveIn = clamp((input.forward ? 1 : 0) - (input.backward ? 1 : 0), -1, 1);
654
- let brakedSupportSum = 0;
655
- let brkDirX = 0, brkDirY = 0, brkDirZ = 0;
656
- if (driveIn === 0) wheels.forEach((wheel) => {
657
- var _w$supportForceMag;
658
- const w = wheel.current;
659
- if (!w.enable || !w.rayHit) return;
660
- const fn = (_w$supportForceMag = w.supportForceMag) !== null && _w$supportForceMag !== void 0 ? _w$supportForceMag : 0;
661
- if (fn <= 1e-6 || !w.isActivelyBraking) return;
662
- brakedSupportSum += fn;
663
- brkDirX += fn * w.lngAxis.x;
664
- brkDirY += fn * w.lngAxis.y;
665
- brkDirZ += fn * w.lngAxis.z;
666
- });
667
- const brkDirLen = Math.hypot(brkDirX, brkDirY, brkDirZ);
668
- if (driveIn === 0 && brakedSupportSum > 1e-6 && brkDirLen > 1e-6) {
669
- const eBrkX = brkDirX / brkDirLen;
670
- const eBrkY = brkDirY / brkDirLen;
671
- const eBrkZ = brkDirZ / brkDirLen;
672
- let remX = 0, remY = 0, remZ = 0;
673
- wheels.forEach((wheel) => {
674
- var _w$supportForceMag2, _w$stepWheelTireAndSp2;
675
- const w = wheel.current;
676
- if (!w.enable) return;
677
- if (!w.rayHit || ((_w$supportForceMag2 = w.supportForceMag) !== null && _w$supportForceMag2 !== void 0 ? _w$supportForceMag2 : 0) <= 1e-6) {
678
- var _w$stepWheelTireAndSp;
679
- (_w$stepWheelTireAndSp = w.stepWheelTireAndSpin) === null || _w$stepWheelTireAndSp === void 0 || _w$stepWheelTireAndSp.call(w);
680
- return;
681
- }
682
- if (w.isActivelyBraking) return;
683
- const sg = w.slopeGravImp;
684
- const alongBrk = sg.x * eBrkX + sg.y * eBrkY + sg.z * eBrkZ;
685
- const crossX = sg.x - alongBrk * eBrkX;
686
- const crossY = sg.y - alongBrk * eBrkY;
687
- const crossZ = sg.z - alongBrk * eBrkZ;
688
- const crossLen = Math.hypot(crossX, crossY, crossZ);
689
- let latHoldImp = 0;
690
- if (crossLen > 1e-9) {
691
- const eCrossX = crossX / crossLen;
692
- const eCrossY = crossY / crossLen;
693
- const eCrossZ = crossZ / crossLen;
694
- const cosDelta = w.latAxis.x * eCrossX + w.latAxis.y * eCrossY + w.latAxis.z * eCrossZ;
695
- latHoldImp = (crossX * w.latAxis.x + crossY * w.latAxis.y + crossZ * w.latAxis.z) / Math.max(cosDelta * cosDelta, .25);
696
- }
697
- remX += sg.x - latHoldImp * w.latAxis.x;
698
- remY += sg.y - latHoldImp * w.latAxis.y;
699
- remZ += sg.z - latHoldImp * w.latAxis.z;
700
- (_w$stepWheelTireAndSp2 = w.stepWheelTireAndSpin) === null || _w$stepWheelTireAndSp2 === void 0 || _w$stepWheelTireAndSp2.call(w, 0, latHoldImp);
701
- });
702
- let excessAlongBrk = 0;
703
- let spareCapSum = 0;
704
- wheels.forEach((wheel) => {
705
- var _w$supportForceMag3, _w$supportForceMag4, _w$maxCaliperImp;
706
- const w = wheel.current;
707
- if (!w.enable || !w.rayHit || ((_w$supportForceMag3 = w.supportForceMag) !== null && _w$supportForceMag3 !== void 0 ? _w$supportForceMag3 : 0) <= 1e-6 || !w.isActivelyBraking) return;
708
- const share = ((_w$supportForceMag4 = w.supportForceMag) !== null && _w$supportForceMag4 !== void 0 ? _w$supportForceMag4 : 0) / brakedSupportSum;
709
- const sg = w.slopeGravImp;
710
- const effX = sg.x + share * remX;
711
- const effY = sg.y + share * remY;
712
- const effZ = sg.z + share * remZ;
713
- const effLng = effX * w.lngAxis.x + effY * w.lngAxis.y + effZ * w.lngAxis.z;
714
- const cap = (_w$maxCaliperImp = w.maxCaliperImp) !== null && _w$maxCaliperImp !== void 0 ? _w$maxCaliperImp : Infinity;
715
- if (Math.abs(effLng) > cap) {
716
- const clamped = Math.sign(effLng) * cap;
717
- excessAlongBrk += (effLng - clamped) * (w.lngAxis.x * eBrkX + w.lngAxis.y * eBrkY + w.lngAxis.z * eBrkZ);
718
- } else spareCapSum += cap - Math.abs(effLng);
719
- });
720
- wheels.forEach((wheel) => {
721
- var _w$supportForceMag5, _w$supportForceMag6, _w$maxCaliperImp2, _w$stepWheelTireAndSp3;
722
- const w = wheel.current;
723
- if (!w.enable || !w.rayHit || ((_w$supportForceMag5 = w.supportForceMag) !== null && _w$supportForceMag5 !== void 0 ? _w$supportForceMag5 : 0) <= 1e-6 || !w.isActivelyBraking) return;
724
- const share = ((_w$supportForceMag6 = w.supportForceMag) !== null && _w$supportForceMag6 !== void 0 ? _w$supportForceMag6 : 0) / brakedSupportSum;
725
- const sg = w.slopeGravImp;
726
- const effX = sg.x + share * remX;
727
- const effY = sg.y + share * remY;
728
- const effZ = sg.z + share * remZ;
729
- let effLng = effX * w.lngAxis.x + effY * w.lngAxis.y + effZ * w.lngAxis.z;
730
- const effLat = effX * w.latAxis.x + effY * w.latAxis.y + effZ * w.latAxis.z;
731
- const cap = (_w$maxCaliperImp2 = w.maxCaliperImp) !== null && _w$maxCaliperImp2 !== void 0 ? _w$maxCaliperImp2 : Infinity;
732
- if (Math.abs(effLng) > cap) effLng = Math.sign(effLng) * cap;
733
- else if (Math.abs(excessAlongBrk) > 1e-9 && spareCapSum > 1e-9) {
734
- const spare = cap - Math.abs(effLng);
735
- const align = w.lngAxis.x * eBrkX + w.lngAxis.y * eBrkY + w.lngAxis.z * eBrkZ;
736
- if (Math.abs(align) > .001) effLng = clamp(effLng + excessAlongBrk * (spare / spareCapSum) / align, -cap, cap);
737
- }
738
- (_w$stepWheelTireAndSp3 = w.stepWheelTireAndSpin) === null || _w$stepWheelTireAndSp3 === void 0 || _w$stepWheelTireAndSp3.call(w, effLng, effLat);
739
- });
740
- } else wheels.forEach((wheel) => {
741
- var _wheel$current$stepWh2, _wheel$current2;
742
- (_wheel$current$stepWh2 = (_wheel$current2 = wheel.current).stepWheelTireAndSpin) === null || _wheel$current$stepWh2 === void 0 || _wheel$current$stepWh2.call(_wheel$current2);
743
- });
744
- }
745
569
  applyWheelImpulse(wheels, body);
746
570
  }, [
747
571
  applyWheelImpulse,
@@ -791,7 +615,6 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
791
615
  let sumLX = 0, sumLY = 0, sumLZ = 0;
792
616
  let sumAX = 0, sumAY = 0, sumAZ = 0;
793
617
  propellers.forEach((p) => {
794
- if (!p.current.enable) return;
795
618
  sumLX += p.current.lx;
796
619
  sumLY += p.current.ly;
797
620
  sumLZ += p.current.lz;
@@ -813,8 +636,7 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
813
636
  const vertLinVelMag = vehicleLinVel.current.dot(upAxis.current);
814
637
  horizLinVel.current.copy(vehicleLinVel.current).projectOnPlane(upAxis.current);
815
638
  const vertForceMag = weight + clamp(vertPosErrorMag * droneControlConfig.VERT_POS_P, -droneControlConfig.VERT_POS_D * droneControlConfig.maxVertSpeed, droneControlConfig.VERT_POS_D * droneControlConfig.maxVertSpeed) - vertLinVelMag * droneControlConfig.VERT_POS_D;
816
- desiredVertForce.current = Math.max(0, vertForceMag);
817
- hoverThrottle.current = desiredVertForce.current / (sumWorldLY || 1);
639
+ hoverThrottle.current = Math.max(0, vertForceMag / (sumWorldLY || 1));
818
640
  horizForce.current.set(0, 0, 0).addScaledVector(horizPosError.current, droneControlConfig.HORIZ_POS_P).addScaledVector(horizLinVel.current, -droneControlConfig.HORIZ_POS_D).clampLength(0, droneControlConfig.HORIZ_POS_D * droneControlConfig.maxHorizSpeed);
819
641
  targetUp.current.copy(upAxis.current).multiplyScalar(weight).add(horizForce.current.clampLength(0, weight * maxTiltTan)).normalize();
820
642
  tiltError.current.crossVectors(vehicleYAxis.current, targetUp.current);
@@ -840,8 +662,7 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
840
662
  vertAccCmd.current = clamp(linVelError.current.dot(upAxis.current) * droneControlConfig.VERT_VEL_P, -referenceGravityMag.current, referenceGravityMag.current);
841
663
  horizAccCmd.current.copy(linVelError.current).projectOnPlane(upAxis.current).multiplyScalar(droneControlConfig.HORIZ_VEL_P).clampLength(0, referenceGravityMag.current * maxTiltTan);
842
664
  const verticalForceMag = weight + vertAccCmd.current * body.mass();
843
- desiredVertForce.current = Math.max(0, verticalForceMag);
844
- hoverThrottle.current = desiredVertForce.current / (sumWorldLY || 1);
665
+ hoverThrottle.current = Math.max(0, verticalForceMag / (sumWorldLY || 1));
845
666
  targetUp.current.copy(upAxis.current).multiplyScalar(referenceGravityMag.current).add(horizAccCmd.current).normalize();
846
667
  tiltError.current.crossVectors(vehicleYAxis.current, targetUp.current);
847
668
  tiltAngVel.current.copy(vehicleAngVel.current).projectOnPlane(upAxis.current);
@@ -850,95 +671,17 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
850
671
  torqueWorld.current.set(0, 0, 0).addScaledVector(tiltError.current, droneControlConfig.TILT_P).addScaledVector(tiltAngVel.current, -droneControlConfig.TILT_D).addScaledVector(upAxis.current, yawRateError * droneControlConfig.YAW_VEL_P);
851
672
  torqueBody.current.copy(torqueWorld.current).applyQuaternion(vehicleInvertQuat.current);
852
673
  }, [droneControlConfig, maxTiltTan]);
853
- const solveDroneWrenchLambda = useCallback((propellers, body, ux, uy, uz) => {
854
- const G = gramMatrix.current;
855
- G.fill(0);
856
- let sumAX = 0, sumAY = 0, sumAZ = 0;
857
- let sumAX2 = 0, sumAY2 = 0, sumAZ2 = 0;
858
- for (const propeller of propellers.values()) {
859
- const info = propeller.current;
860
- if (!info.enable) continue;
861
- const fUp = info.lx * ux + info.ly * uy + info.lz * uz;
862
- const ax = info.ax;
863
- const ay = info.ay;
864
- const az = info.az;
865
- sumAX += Math.abs(ax);
866
- sumAY += Math.abs(ay);
867
- sumAZ += Math.abs(az);
868
- sumAX2 += ax * ax;
869
- sumAY2 += ay * ay;
870
- sumAZ2 += az * az;
871
- G[0] += fUp * fUp;
872
- G[1] += fUp * ax;
873
- G[2] += fUp * ay;
874
- G[3] += fUp * az;
875
- G[4] += ax * fUp;
876
- G[5] += ax * ax;
877
- G[6] += ax * ay;
878
- G[7] += ax * az;
879
- G[8] += ay * fUp;
880
- G[9] += ay * ax;
881
- G[10] += ay * ay;
882
- G[11] += ay * az;
883
- G[12] += az * fUp;
884
- G[13] += az * ax;
885
- G[14] += az * ay;
886
- G[15] += az * az;
887
- }
888
- const trace = G[0] + G[5] + G[10] + G[15];
889
- if (trace <= 1e-12) {
890
- wrenchLambda.current.fill(0);
891
- return;
892
- }
893
- const eps = Math.max(1e-9, trace * 1e-9);
894
- G[0] += eps;
895
- G[5] += eps;
896
- G[10] += eps;
897
- G[15] += eps;
898
- let sx = sumAX > 1e-9 ? sumAX2 / sumAX : 0;
899
- let sy = sumAY > 1e-9 ? sumAY2 / sumAY : 0;
900
- let sz = sumAZ > 1e-9 ? sumAZ2 / sumAZ : 0;
901
- const dt = world.timestep;
902
- if (dt > 0) {
903
- const invI = body.effectiveWorldInvInertia();
904
- const invITrace = invI.m11 + invI.m22 + invI.m33;
905
- if (invITrace > 1e-9) {
906
- const tiltDamp = Math.max(droneControlConfig.TILT_D, .001);
907
- const yawDamp = Math.max(droneControlConfig.YAW_VEL_P, .001);
908
- const maxTiltScale = .8 / (invITrace * tiltDamp * dt);
909
- const maxYawScale = .8 / (invITrace * yawDamp * dt);
910
- sx = Math.min(sx, maxTiltScale);
911
- sy = Math.min(sy, maxYawScale);
912
- sz = Math.min(sz, maxTiltScale);
913
- }
914
- }
915
- const b = wrenchRhs.current;
916
- b[0] = desiredVertForce.current;
917
- b[1] = torqueBody.current.x * sx;
918
- b[2] = torqueBody.current.y * sy;
919
- b[3] = torqueBody.current.z * sz;
920
- solve4x4(G, b, wrenchAug.current, wrenchLambda.current);
921
- }, [
922
- droneControlConfig.TILT_D,
923
- droneControlConfig.YAW_VEL_P,
924
- world
925
- ]);
926
- const computePropellerFinalThrottle = useCallback((propeller, ux, uy, uz) => {
927
- if (!propeller.enable) return 0;
928
- const fUp = propeller.lx * ux + propeller.ly * uy + propeller.lz * uz;
929
- const lam = wrenchLambda.current;
930
- return clamp(fUp * lam[0] + propeller.ax * lam[1] + propeller.ay * lam[2] + propeller.az * lam[3], 0, 1);
674
+ const computePropellerFinalThrottle = useCallback((propeller, maxSafeMix) => {
675
+ const mix = torqueBody.current.x * propeller.ax / (propellerPotential.current.sumAX || 1) + torqueBody.current.z * propeller.az / (propellerPotential.current.sumAZ || 1) + torqueBody.current.y * propeller.ay / (propellerPotential.current.sumAY || 1);
676
+ return clamp(hoverThrottle.current + clamp(mix, -maxSafeMix, maxSafeMix), 0, 1);
931
677
  }, []);
932
678
  const applyMixerImpulse = useCallback((propellers, body) => {
933
- const ux = vehicleXAxis.current.dot(upAxis.current);
934
- const uy = vehicleYAxis.current.dot(upAxis.current);
935
- const uz = vehicleZAxis.current.dot(upAxis.current);
936
- solveDroneWrenchLambda(propellers, body, ux, uy, uz);
679
+ const maxSafeMix = Math.min(1 - hoverThrottle.current, hoverThrottle.current);
937
680
  if (body.isSleeping()) {
938
681
  let shouldWake = false;
939
682
  for (const propeller of propellers.values()) {
940
683
  const info = propeller.current;
941
- const finalThrottle = computePropellerFinalThrottle(info, ux, uy, uz);
684
+ const finalThrottle = computePropellerFinalThrottle(info, maxSafeMix);
942
685
  if (Math.abs(finalThrottle - info.throttle) > 1e-4) {
943
686
  shouldWake = true;
944
687
  break;
@@ -949,14 +692,7 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
949
692
  }
950
693
  propellers.forEach((propeller) => {
951
694
  const info = propeller.current;
952
- if (!info.enable) {
953
- info.finalThrottle = 0;
954
- if (info.setThrottle) info.setThrottle(0);
955
- info.thrustImpulse.set(0, 0, 0);
956
- info.torqueImpulse.set(0, 0, 0);
957
- return;
958
- }
959
- const finalThrottle = computePropellerFinalThrottle(info, ux, uy, uz);
695
+ const finalThrottle = computePropellerFinalThrottle(info, maxSafeMix);
960
696
  info.finalThrottle = finalThrottle;
961
697
  if (info.setThrottle) info.setThrottle(finalThrottle);
962
698
  worldThrustDir.current.copy(info.thrustDir).applyQuaternion(vehicleQuat.current).normalize();
@@ -970,17 +706,13 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
970
706
  body.applyImpulseAtPoint(info.thrustImpulse, info.worldThrustPos, false);
971
707
  body.applyTorqueImpulse(info.torqueImpulse, false);
972
708
  });
973
- }, [computePropellerFinalThrottle, solveDroneWrenchLambda]);
709
+ }, [computePropellerFinalThrottle]);
974
710
  const applyAirDrag = useCallback((body) => {
975
711
  airDragImpulse.current.copy(vehicleLinVel.current).multiplyScalar(-droneControlConfig.airDragFactor * world.timestep);
976
712
  body.applyImpulse(airDragImpulse.current, false);
977
713
  }, [droneControlConfig.airDragFactor, world.timestep]);
978
714
  const applyDroneControl = useCallback((propellers, body, input) => {
979
715
  if (!body) return;
980
- propellers.forEach((propeller) => {
981
- var _propeller$current$st, _propeller$current;
982
- (_propeller$current$st = (_propeller$current = propeller.current).stepPropellerPhysics) === null || _propeller$current$st === void 0 || _propeller$current$st.call(_propeller$current);
983
- });
984
716
  computePropellerPotential(propellers);
985
717
  const sumWorldLY = propellerPotential.current.sumLX * vehicleXAxis.current.dot(upAxis.current) + propellerPotential.current.sumLY * vehicleYAxis.current.dot(upAxis.current) + propellerPotential.current.sumLZ * vehicleZAxis.current.dot(upAxis.current);
986
718
  const weight = body.mass() * referenceGravityMag.current;
@@ -1023,10 +755,15 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
1023
755
  *
1024
756
  *
1025
757
  */
1026
- useBeforePhysicsStep(() => {
758
+ useFrame(() => {
1027
759
  if (!enable || !vehicleRef.current) return;
1028
- updateVehicleInfo(vehicleRef.current);
1029
- updateGravityInfo();
760
+ /**
761
+ * Update when character is not sleeping
762
+ */
763
+ if (!vehicleRef.current.isSleeping()) {
764
+ updateVehicleInfo(vehicleRef.current);
765
+ updateGravityInfo(vehicleRef.current);
766
+ }
1030
767
  /**
1031
768
  * Apply drive/brake/friction impulse from shape cast wheels
1032
769
  * whenever there is a wheel component
@@ -1037,11 +774,6 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
1037
774
  * whenever there is a propeller component
1038
775
  */
1039
776
  if (propellersInfo.current.size > 0) applyDroneControl(propellersInfo.current, vehicleRef.current, movementState.current);
1040
- /**
1041
- * Apply custom gravity impulse after wheels and propellers sample pre-step velocity so custom gravity
1042
- * neither biases suspension damping nor gets skipped on the step a sleeping body wakes up
1043
- */
1044
- if (enableCustomGravity && !vehicleRef.current.isSleeping()) applyGravityField(vehicleRef.current, world.timestep);
1045
777
  });
1046
778
  /**
1047
779
  *
@@ -1059,8 +791,6 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
1059
791
  */
1060
792
  const vehicleContextValue = useMemo(() => ({
1061
793
  body: vehicleRef,
1062
- enableCustomGravity,
1063
- referenceGravity,
1064
794
  upAxis,
1065
795
  gravityDir,
1066
796
  gravityMag: referenceGravityMag,
@@ -1079,7 +809,6 @@ var EcctrlVehicle = /* @__PURE__ */ forwardRef(({ children, enable = true, carCo
1079
809
  regPropeller,
1080
810
  unregPropeller
1081
811
  }), [
1082
- enableCustomGravity,
1083
812
  regWheel,
1084
813
  unregWheel,
1085
814
  regPropeller,
@@ -1155,7 +884,7 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1155
884
  /**
1156
885
  * Rapier preset
1157
886
  */
1158
- const { rapier, world, rigidBodyStates } = useRapier();
887
+ const { rapier, world } = useRapier();
1159
888
  /**
1160
889
  * Retrieve context object from ecctrl vehicle
1161
890
  */
@@ -1163,10 +892,9 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1163
892
  const fixedScale = useMemo(() => new THREE.Vector3(1, 1, 1), []);
1164
893
  useMemo(() => new THREE.Vector3(0, 0, 0), []);
1165
894
  useMemo(() => new THREE.Vector3(1, 0, 0), []);
1166
- const fixedYAxis = useMemo(() => new THREE.Vector3(0, 1, 0), []);
895
+ useMemo(() => new THREE.Vector3(0, 1, 0), []);
1167
896
  const fixedZAxis = useMemo(() => new THREE.Vector3(0, 0, 1), []);
1168
897
  const vehiclePos = useRef(new THREE.Vector3());
1169
- const vehicleCom = useRef(new THREE.Vector3());
1170
898
  const vehicleQuat = useRef(new THREE.Quaternion());
1171
899
  const vehicleInvertQuat = useRef(new THREE.Quaternion());
1172
900
  const vehicleMatrix = useRef(new THREE.Matrix4());
@@ -1208,12 +936,6 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1208
936
  const smoothedLatImpulse = useRef(0);
1209
937
  const desiredLngImpulse = useRef(0);
1210
938
  const desiredLatImpulse = useRef(0);
1211
- const defaultLngSlopeGravImp = useRef(0);
1212
- const defaultLatSlopeGravImp = useRef(0);
1213
- const slopeGravImpVec = useRef(new THREE.Vector3());
1214
- const baseLocalQuat = useRef(new THREE.Quaternion());
1215
- const baseLocalQuatCaptured = useRef(false);
1216
- const steerQuat = useRef(new THREE.Quaternion());
1217
939
  const steerAngle = useRef(0);
1218
940
  const steerTarget = useRef(0);
1219
941
  const steerIncrement = useRef(0);
@@ -1256,7 +978,6 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1256
978
  const rayHitPointOffset = useRef(new THREE.Vector3());
1257
979
  const rayHitPointPosition = useRef(new THREE.Vector3());
1258
980
  const rayHitPointVelocity = useRef(new THREE.Vector3());
1259
- const supportPointVelocity = useRef(new THREE.Vector3());
1260
981
  const rayHitPointVelOnPlane = useRef(new THREE.Vector3());
1261
982
  const rayHitPointNormal = useRef(new THREE.Vector3());
1262
983
  const rayHitPointFriction = useRef(0);
@@ -1289,16 +1010,9 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1289
1010
  const shapeCastWheelRef = useRef(null);
1290
1011
  const modelRef = useRef(null);
1291
1012
  const wheelRef = useRef(null);
1292
- const localPos = useRef(new THREE.Vector3());
1293
- const localQuat = useRef(new THREE.Quaternion());
1294
1013
  const worldPos = useRef(new THREE.Vector3());
1295
1014
  const worldQuat = useRef(new THREE.Quaternion());
1296
1015
  useImperativeHandle(ref, () => shapeCastWheelRef.current, []);
1297
- useLayoutEffect(() => {
1298
- if (!shapeCastWheelRef.current) return;
1299
- baseLocalQuat.current.copy(shapeCastWheelRef.current.quaternion);
1300
- baseLocalQuatCaptured.current = true;
1301
- }, [props.rotation, props.quaternion]);
1302
1016
  /**
1303
1017
  * Register wheel information
1304
1018
  */
@@ -1364,10 +1078,6 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1364
1078
  latAxis: new THREE.Vector3(),
1365
1079
  lngFricImp: new THREE.Vector3(),
1366
1080
  latFricImp: new THREE.Vector3(),
1367
- supportForceMag: 0,
1368
- maxCaliperImp: 0,
1369
- slopeGravImp: new THREE.Vector3(),
1370
- isActivelyBraking: false,
1371
1081
  effInertia: 0,
1372
1082
  supPos: new THREE.Vector3(),
1373
1083
  steerAngle: 0,
@@ -1381,12 +1091,9 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1381
1091
  setDriveWheelConfig,
1382
1092
  setSteerWheelConfig
1383
1093
  });
1384
- wheelInfo.current.enable = enable;
1385
1094
  wheelInfo.current.driveWheel = driveWheel;
1386
1095
  wheelInfo.current.driveTorqueWeight = driveTorqueWeight;
1387
1096
  wheelInfo.current.steerWheel = steerWheel;
1388
- wheelInfo.current.brakeWheel = brakeWheel;
1389
- wheelInfo.current.maxBrakeTorque = maxBrakeTorque;
1390
1097
  /**
1391
1098
  * Register the wheel to vehicle on mount, and unregister on unmount
1392
1099
  */
@@ -1502,7 +1209,15 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1502
1209
  * Update vehicle/wheel info function
1503
1210
  * (needs to recompute vehicle's info for each wheel, to gain the most updated info)
1504
1211
  */
1505
- const syncWheelInfo = useCallback(() => {
1212
+ const updateVehicleInfo = useCallback((body) => {
1213
+ vehiclePos.current.copy(body.translation());
1214
+ vehicleQuat.current.copy(body.rotation());
1215
+ vehicleMatrix.current.compose(vehiclePos.current, vehicleQuat.current, fixedScale);
1216
+ vehicleYAxis.current.set(0, 1, 0).applyQuaternion(vehicleQuat.current);
1217
+ vehicleXAxis.current.set(1, 0, 0).applyQuaternion(vehicleQuat.current);
1218
+ vehicleZAxis.current.set(0, 0, 1).applyQuaternion(vehicleQuat.current);
1219
+ vehicleLinVel.current.copy(body.linvel());
1220
+ vehicleAngVel.current.copy(body.angvel());
1506
1221
  wheelInfo.current.rayPos = rayOrigin.current;
1507
1222
  wheelInfo.current.rayDir = rayDirection.current;
1508
1223
  wheelInfo.current.rayRot = rayRotation.current;
@@ -1533,100 +1248,35 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1533
1248
  wheelInfo.current.wheelAngVel = wheelAngVel.current;
1534
1249
  wheelInfo.current.wheelLinVel = wheelAngVel.current * rayShapeR;
1535
1250
  }, [groundDetection, rayShapeR]);
1536
- const updateVehicleInfo = useCallback((body) => {
1537
- vehiclePos.current.copy(body.translation());
1538
- vehicleCom.current.copy(body.worldCom());
1539
- vehicleQuat.current.copy(body.rotation());
1540
- vehicleMatrix.current.compose(vehiclePos.current, vehicleQuat.current, fixedScale);
1541
- vehicleYAxis.current.set(0, 1, 0).applyQuaternion(vehicleQuat.current);
1542
- vehicleXAxis.current.set(1, 0, 0).applyQuaternion(vehicleQuat.current);
1543
- vehicleZAxis.current.set(0, 0, 1).applyQuaternion(vehicleQuat.current);
1544
- vehicleLinVel.current.copy(body.linvel());
1545
- vehicleAngVel.current.copy(body.angvel());
1546
- }, [fixedScale]);
1547
1251
  /**
1548
- * Compute ray hit point and support point relative velocities
1252
+ * Compute ray hit point relative velocity
1549
1253
  */
1550
1254
  const computeRelativeVelocity = useCallback(() => {
1551
- if (rayHitBody.current) {
1552
- distFromRayOriginToVehicle.current.copy(rayHitPointPosition.current).sub(vehicleCom.current);
1553
- angvelToLinvel.current.crossVectors(vehicleAngVel.current, distFromRayOriginToVehicle.current);
1554
- rayHitPointVelocity.current.copy(vehicleLinVel.current).add(angvelToLinvel.current);
1555
- distFromRayOriginToVehicle.current.copy(supportPoint.current).sub(vehicleCom.current);
1556
- angvelToLinvel.current.crossVectors(vehicleAngVel.current, distFromRayOriginToVehicle.current);
1557
- supportPointVelocity.current.copy(vehicleLinVel.current).add(angvelToLinvel.current);
1558
- } else {
1559
- rayHitPointVelocity.current.copy(rayOriginVel.current);
1560
- supportPointVelocity.current.copy(rayOriginVel.current);
1561
- }
1255
+ rayHitPointVelocity.current.copy(rayOriginVel.current);
1562
1256
  rayHitPointVelOnPlane.current.copy(rayHitPointVelocity.current).projectOnPlane(rayHitPointNormal.current);
1563
1257
  if (isOnMovingObject.current && followPlatform) {
1564
1258
  rayHitPointVelocity.current.sub(movingObjectVelocity.current);
1565
- supportPointVelocity.current.sub(movingObjectVelocity.current);
1566
1259
  rayHitPointVelOnPlane.current.sub(movingObjectVelocityOnPlane.current);
1567
1260
  }
1568
1261
  }, [followPlatform]);
1569
1262
  /**
1570
- * Update shape cast info directly from Rapier's rigid-body pose (immune to Three.js render interpolation)
1263
+ * Update shape cast info
1571
1264
  */
1572
- const updateShapeCastDir = useCallback((body) => {
1573
- var _rigidBodyStates$get;
1265
+ const updateShapeCastDir = useCallback(() => {
1574
1266
  if (!shapeCastWheelRef.current) return;
1575
- if (!baseLocalQuatCaptured.current) {
1576
- baseLocalQuat.current.copy(shapeCastWheelRef.current.quaternion);
1577
- baseLocalQuatCaptured.current = true;
1578
- }
1579
- if (steerWheel) {
1580
- steerQuat.current.setFromAxisAngle(fixedYAxis, steerAngle.current);
1581
- shapeCastWheelRef.current.quaternion.multiplyQuaternions(baseLocalQuat.current, steerQuat.current);
1582
- }
1583
- localPos.current.copy(shapeCastWheelRef.current.position);
1584
- localQuat.current.copy(shapeCastWheelRef.current.quaternion);
1585
- const rigidBodyObject = (_rigidBodyStates$get = rigidBodyStates.get(body.handle)) === null || _rigidBodyStates$get === void 0 ? void 0 : _rigidBodyStates$get.object;
1586
- let parent = shapeCastWheelRef.current.parent;
1587
- if (rigidBodyObject && parent && parent !== rigidBodyObject) while (parent && parent !== rigidBodyObject) {
1588
- localPos.current.multiply(parent.scale).applyQuaternion(parent.quaternion).add(parent.position);
1589
- localQuat.current.premultiply(parent.quaternion);
1590
- parent = parent.parent;
1591
- }
1592
- worldPos.current.copy(localPos.current).applyMatrix4(vehicleMatrix.current);
1593
- worldQuat.current.multiplyQuaternions(vehicleQuat.current, localQuat.current);
1267
+ if (steerWheel) shapeCastWheelRef.current.rotateY(steerIncrement.current);
1268
+ shapeCastWheelRef.current.getWorldPosition(worldPos.current);
1269
+ shapeCastWheelRef.current.getWorldQuaternion(worldQuat.current);
1594
1270
  rayOrigin.current.copy(worldPos.current);
1595
1271
  rayDirection.current.set(0, -1, 0).applyQuaternion(worldQuat.current);
1596
1272
  rayUpAxis.current.copy(rayDirection.current).negate();
1597
1273
  rayFWDAxis.current.set(0, 0, 1).applyQuaternion(worldQuat.current);
1598
1274
  rayBWDAxis.current.copy(rayFWDAxis.current).negate();
1599
1275
  rayLeftAxis.current.crossVectors(rayUpAxis.current, rayFWDAxis.current).normalize();
1600
- distFromRayOriginToVehicle.current.copy(rayOrigin.current).sub(vehicleCom.current);
1276
+ distFromRayOriginToVehicle.current.copy(rayOrigin.current).sub(vehiclePos.current);
1601
1277
  angvelToLinvel.current.crossVectors(vehicleAngVel.current, distFromRayOriginToVehicle.current);
1602
1278
  rayOriginVel.current.copy(vehicleLinVel.current).add(angvelToLinvel.current);
1603
- }, [
1604
- steerWheel,
1605
- fixedYAxis,
1606
- rigidBodyStates
1607
- ]);
1608
- /**
1609
- * Detect is on a moving object
1610
- */
1611
- const isOnMovingObjectDetect = useCallback((body) => {
1612
- if (followPlatform && rayHitBody.current && (rayHitBody.current.isDynamic() || rayHitBody.current.isKinematic())) {
1613
- isOnMovingObject.current = true;
1614
- if (rayHitBody.current.isDynamic()) massRatio.current = evaluateCurveLUT(clamp(rayHitBody.current.mass() / Math.max(body.mass(), 1e-6), 0, 1), massRatioFallOffCurve);
1615
- else massRatio.current = 1;
1616
- movingObjectPosition.current.copy(rayHitBody.current.worldCom());
1617
- distanceFromOriginToObjectPoint.current.copy(rayHitPointPosition.current).sub(movingObjectPosition.current);
1618
- movingObjectLinearVelocity.current.copy(rayHitBody.current.linvel());
1619
- movingObjectAngularVelocity.current.copy(rayHitBody.current.angvel());
1620
- movingObjectAngvelToLinvel.current.crossVectors(movingObjectAngularVelocity.current, distanceFromOriginToObjectPoint.current);
1621
- movingObjectVelocity.current.copy(movingObjectLinearVelocity.current).add(movingObjectAngvelToLinvel.current).multiplyScalar(massRatio.current);
1622
- movingObjectVelocityOnPlane.current.copy(movingObjectVelocity.current).projectOnPlane(rayHitPointNormal.current);
1623
- } else {
1624
- isOnMovingObject.current = false;
1625
- movingObjectVelocity.current.set(0, 0, 0);
1626
- movingObjectVelocityOnPlane.current.set(0, 0, 0);
1627
- massRatio.current = 1;
1628
- }
1629
- }, [followPlatform, massRatioFallOffCurve]);
1279
+ }, [steerWheel]);
1630
1280
  /**
1631
1281
  * Floating function
1632
1282
  */
@@ -1662,20 +1312,14 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1662
1312
  rayHitPointPosition.current.copy(stableRayHitPoint.current).addScaledVector(rayLeftAxis.current, supportOffset);
1663
1313
  supportPoint.current.copy(rayShapeCenter.current).addScaledVector(rayLeftAxis.current, supportOffset);
1664
1314
  if (rayHitPointFriction.current !== hitCollider.friction()) rayHitPointFriction.current = (_hitCollider$friction = hitCollider.friction()) !== null && _hitCollider$friction !== void 0 ? _hitCollider$friction : 0;
1665
- isOnMovingObjectDetect(body);
1666
- computeRelativeVelocity();
1667
1315
  const springForce = springK * Math.max(0, rayLength - suspensionToi.current);
1668
- const solverSteps = world.integrationParameters.numSolverIterations;
1669
- const gravityAlongUp = (world.gravity.x * rayUpAxis.current.x + world.gravity.y * rayUpAxis.current.y + world.gravity.z * rayUpAxis.current.z) * body.gravityScale();
1670
- const dampingForce = dampingC * (supportPointVelocity.current.dot(rayUpAxis.current) - (solverSteps - 1) / (2 * solverSteps) * world.timestep * gravityAlongUp);
1316
+ const dampingForce = dampingC * rayHitPointVelocity.current.dot(rayUpAxis.current);
1671
1317
  floatingImpulse.current.copy(rayHitPointNormal.current).multiplyScalar(springForce - dampingForce).multiplyScalar(world.timestep);
1672
1318
  } else {
1673
1319
  rayHitBody.current = null;
1674
1320
  suspensionToi.current = 0;
1675
1321
  rayHitPointFriction.current = 0;
1676
1322
  floatingImpulse.current.set(0, 0, 0);
1677
- isOnMovingObjectDetect(body);
1678
- computeRelativeVelocity();
1679
1323
  }
1680
1324
  }, [
1681
1325
  groundDetection,
@@ -1686,11 +1330,31 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1686
1330
  rayShapeH,
1687
1331
  springK,
1688
1332
  dampingC,
1689
- ecctrlVehicleRayFilter,
1690
- isOnMovingObjectDetect,
1691
- computeRelativeVelocity
1333
+ ecctrlVehicleRayFilter
1692
1334
  ]);
1693
1335
  /**
1336
+ * Detect is on a moving object
1337
+ */
1338
+ const isOnMovingObjectDetect = useCallback((body) => {
1339
+ if (followPlatform && rayHitBody.current && (rayHitBody.current.bodyType() === 0 || rayHitBody.current.bodyType() === 2)) {
1340
+ isOnMovingObject.current = true;
1341
+ if (rayHitBody.current.bodyType() === 0) massRatio.current = evaluateCurveLUT(clamp(rayHitBody.current.mass() / Math.max(body.mass(), 1e-6), 0, 1), massRatioFallOffCurve);
1342
+ else massRatio.current = 1;
1343
+ movingObjectPosition.current.copy(rayHitBody.current.translation());
1344
+ distanceFromOriginToObjectPoint.current.copy(rayOrigin.current).sub(movingObjectPosition.current);
1345
+ movingObjectLinearVelocity.current.copy(rayHitBody.current.linvel());
1346
+ movingObjectAngularVelocity.current.copy(rayHitBody.current.angvel());
1347
+ movingObjectAngvelToLinvel.current.crossVectors(movingObjectAngularVelocity.current, distanceFromOriginToObjectPoint.current);
1348
+ movingObjectVelocity.current.copy(movingObjectLinearVelocity.current).add(movingObjectAngvelToLinvel.current).multiplyScalar(massRatio.current);
1349
+ movingObjectVelocityOnPlane.current.copy(movingObjectVelocity.current).projectOnPlane(rayHitPointNormal.current);
1350
+ } else {
1351
+ isOnMovingObject.current = false;
1352
+ movingObjectVelocity.current.set(0, 0, 0);
1353
+ movingObjectVelocityOnPlane.current.set(0, 0, 0);
1354
+ massRatio.current = 1;
1355
+ }
1356
+ }, [followPlatform, massRatioFallOffCurve]);
1357
+ /**
1694
1358
  * Apply wheel mass on standing collider
1695
1359
  */
1696
1360
  const applyMassOnStandCollider = useCallback(() => {
@@ -1749,87 +1413,23 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1749
1413
  else frictionCoef.current = 0;
1750
1414
  }, [tireGripFactor]);
1751
1415
  /**
1752
- * Prepare contact axes, support load, and local slope-gravity impulse for vehicle-wide load sharing (Pass 1)
1753
- */
1754
- const prepareContactSlopeGravity = useCallback((gravityMag, body) => {
1755
- if (!rayHitBody.current) {
1756
- wheelSupportForceMag.current = wheelMass * gravityMag;
1757
- effectiveInertia.current = wheelInertia + (gravityMag > 1e-6 ? wheelSupportForceMag.current / gravityMag * rayShapeR * rayShapeR : 0);
1758
- defaultLngSlopeGravImp.current = 0;
1759
- defaultLatSlopeGravImp.current = 0;
1760
- slopeGravImpVec.current.set(0, 0, 0);
1761
- wheelInfo.current.rayHit = null;
1762
- wheelInfo.current.supportForceMag = 0;
1763
- wheelInfo.current.maxCaliperImp = 0;
1764
- wheelInfo.current.slopeGravImp = slopeGravImpVec.current;
1765
- wheelInfo.current.isActivelyBraking = false;
1766
- return;
1767
- }
1768
- wheelSupportForceMag.current = Math.max(floatingImpulse.current.dot(rayHitPointNormal.current), 0) / world.timestep;
1769
- const supportedMass = gravityMag > 1e-6 ? wheelSupportForceMag.current / gravityMag : wheelMass;
1770
- effectiveInertia.current = wheelInertia + supportedMass * rayShapeR * rayShapeR;
1771
- lngAxis.current.copy(rayFWDAxis.current).projectOnPlane(rayHitPointNormal.current).normalize();
1772
- latAxis.current.copy(rayLeftAxis.current).projectOnPlane(rayHitPointNormal.current).normalize();
1773
- const gravScale = body.gravityScale();
1774
- let totalGravX = world.gravity.x * gravScale;
1775
- let totalGravY = world.gravity.y * gravScale;
1776
- let totalGravZ = world.gravity.z * gravScale;
1777
- if (vehicleValue === null || vehicleValue === void 0 ? void 0 : vehicleValue.enableCustomGravity) {
1778
- const customGrav = vehicleValue.referenceGravity.current;
1779
- totalGravX += customGrav.x * gravScale;
1780
- totalGravY += customGrav.y * gravScale;
1781
- totalGravZ += customGrav.z * gravScale;
1782
- }
1783
- const totalGravMag = Math.hypot(totalGravX, totalGravY, totalGravZ);
1784
- const normalGrav = -(totalGravX * rayHitPointNormal.current.x + totalGravY * rayHitPointNormal.current.y + totalGravZ * rayHitPointNormal.current.z);
1785
- const slopeMass = totalGravMag > 1e-6 && normalGrav > 1e-6 ? wheelSupportForceMag.current / Math.max(normalGrav, .25 * totalGravMag) : supportedMass;
1786
- const totalGravLng = totalGravX * lngAxis.current.x + totalGravY * lngAxis.current.y + totalGravZ * lngAxis.current.z;
1787
- const totalGravLat = totalGravX * latAxis.current.x + totalGravY * latAxis.current.y + totalGravZ * latAxis.current.z;
1788
- defaultLngSlopeGravImp.current = slopeMass * totalGravLng * world.timestep;
1789
- defaultLatSlopeGravImp.current = slopeMass * totalGravLat * world.timestep;
1790
- slopeGravImpVec.current.copy(lngAxis.current).multiplyScalar(defaultLngSlopeGravImp.current).addScaledVector(latAxis.current, defaultLatSlopeGravImp.current);
1791
- const activelyBraking = Boolean(brakeWheel && brakeTorque.current > 0 && rayShapeR > 1e-6);
1792
- const maxStaticGripImp = wheelSupportForceMag.current * frictionCoef.current * world.timestep * lngFrictionEllipseScale;
1793
- wheelInfo.current.rayHit = groundDetection === "rayCast" ? rayHit.current : shapeRayHit.current;
1794
- wheelInfo.current.supportForceMag = wheelSupportForceMag.current;
1795
- wheelInfo.current.maxCaliperImp = activelyBraking ? Math.min(Math.abs(brakeTorque.current) * world.timestep / rayShapeR, maxStaticGripImp) : 0;
1796
- wheelInfo.current.lngAxis = lngAxis.current;
1797
- wheelInfo.current.latAxis = latAxis.current;
1798
- wheelInfo.current.slopeGravImp = slopeGravImpVec.current;
1799
- wheelInfo.current.isActivelyBraking = activelyBraking;
1800
- }, [
1801
- vehicleValue,
1802
- groundDetection,
1803
- brakeWheel,
1804
- wheelMass,
1805
- wheelInertia,
1806
- rayShapeR,
1807
- lngFrictionEllipseScale
1808
- ]);
1809
- /**
1810
1416
  * Compute longitudinal and lateral friction impulses based on wheel slip and contact conditions
1811
1417
  */
1812
- const computeWheelFrictionImpulse = useCallback((gravityMag, body, overrideLngGravImp, overrideLatGravImp) => {
1418
+ const computeWheelFrictionImpulse = useCallback((gravityMag) => {
1813
1419
  if (!rayHitBody.current) {
1420
+ wheelSupportForceMag.current = wheelMass * gravityMag;
1421
+ effectiveInertia.current = wheelInertia + wheelSupportForceMag.current / gravityMag * rayShapeR * rayShapeR;
1814
1422
  lngSlipRatio.current = 0;
1815
1423
  latSlipRatio.current = 0;
1816
1424
  slipStrength.current = 0;
1817
- desiredLngImpulse.current = 0;
1818
- desiredLatImpulse.current = 0;
1819
- smoothedLngImpulse.current = 0;
1820
- smoothedLatImpulse.current = 0;
1821
- lngFrictionImp.current.set(0, 0, 0);
1822
- latFrictionImp.current.set(0, 0, 0);
1823
1425
  return;
1824
1426
  }
1825
- const supportedMass = gravityMag > 1e-6 ? wheelSupportForceMag.current / gravityMag : wheelMass;
1826
- const solverSteps = world.integrationParameters.numSolverIterations;
1827
- const gravScale = body.gravityScale();
1828
- const substepWeight = (solverSteps - 1) / (2 * solverSteps) * world.timestep * gravScale;
1829
- const worldGravLng = world.gravity.x * lngAxis.current.x + world.gravity.y * lngAxis.current.y + world.gravity.z * lngAxis.current.z;
1830
- const worldGravLat = world.gravity.x * latAxis.current.x + world.gravity.y * latAxis.current.y + world.gravity.z * latAxis.current.z;
1831
- const lngContactVel = rayHitPointVelocity.current.dot(lngAxis.current) - substepWeight * worldGravLng;
1832
- const latContactVel = rayHitPointVelocity.current.dot(latAxis.current) - substepWeight * worldGravLat;
1427
+ wheelSupportForceMag.current = Math.max(floatingImpulse.current.dot(rayHitPointNormal.current), 0) / world.timestep;
1428
+ effectiveInertia.current = wheelInertia + wheelSupportForceMag.current / gravityMag * rayShapeR * rayShapeR;
1429
+ lngAxis.current.copy(rayFWDAxis.current).projectOnPlane(rayHitPointNormal.current).normalize();
1430
+ latAxis.current.copy(rayLeftAxis.current).projectOnPlane(rayHitPointNormal.current).normalize();
1431
+ const lngContactVel = rayHitPointVelocity.current.dot(lngAxis.current);
1432
+ const latContactVel = rayHitPointVelocity.current.dot(latAxis.current);
1833
1433
  const lngContactVelAbs = Math.abs(lngContactVel);
1834
1434
  const latContactVelAbs = Math.abs(latContactVel);
1835
1435
  const slipDiff = wheelAngVel.current * rayShapeR - lngContactVel;
@@ -1843,58 +1443,22 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1843
1443
  const finalLatSlipValue = remap(clamp(1 - Math.max(latContactVelAbs, lngContactVelAbs) / lowVelThreshold, 0, 1), 0, 1, latSlipValue, 1);
1844
1444
  const maxLngImp = wheelSupportForceMag.current * finalLngSlipValue * frictionCoef.current * world.timestep * lngFrictionEllipseScale;
1845
1445
  const maxLatImp = wheelSupportForceMag.current * finalLatSlipValue * frictionCoef.current * world.timestep * latFrictionEllipseScale;
1846
- if (maxLngImp <= 1e-9 || maxLatImp <= 1e-9) {
1847
- desiredLngImpulse.current = 0;
1848
- desiredLatImpulse.current = 0;
1849
- smoothedLngImpulse.current = 0;
1850
- smoothedLatImpulse.current = 0;
1851
- lngFrictionImp.current.set(0, 0, 0);
1852
- latFrictionImp.current.set(0, 0, 0);
1853
- return;
1854
- }
1855
- let latEffectiveMass = supportedMass;
1856
- const bodyMass = body.mass();
1857
- if (bodyMass > 0) {
1858
- const rx = rayHitPointPosition.current.x - vehicleCom.current.x;
1859
- const ry = rayHitPointPosition.current.y - vehicleCom.current.y;
1860
- const rz = rayHitPointPosition.current.z - vehicleCom.current.z;
1861
- const invI = body.effectiveWorldInvInertia();
1862
- const lx = latAxis.current.x;
1863
- const ly = latAxis.current.y;
1864
- const lz = latAxis.current.z;
1865
- const cx = ry * lz - rz * ly;
1866
- const cy = rz * lx - rx * lz;
1867
- const cz = rx * ly - ry * lx;
1868
- const rotTerm = cx * (invI.m11 * cx + invI.m12 * cy + invI.m13 * cz) + cy * (invI.m21 * cx + invI.m22 * cy + invI.m23 * cz) + cz * (invI.m31 * cx + invI.m32 * cy + invI.m33 * cz);
1869
- if (rotTerm > 0) latEffectiveMass = supportedMass / (1 + bodyMass * rotTerm);
1870
- }
1871
- const lngSlopeGravImp = overrideLngGravImp !== null && overrideLngGravImp !== void 0 ? overrideLngGravImp : defaultLngSlopeGravImp.current;
1872
- const latSlopeGravImp = overrideLatGravImp !== null && overrideLatGravImp !== void 0 ? overrideLatGravImp : defaultLatSlopeGravImp.current;
1873
- const freeSpinImp = slipDiff * effectiveInertia.current / (rayShapeR * rayShapeR);
1874
- if (brakeWheel && brakeTorque.current > 0 && rayShapeR > 1e-6) {
1875
- const maxCaliperImp = Math.abs(brakeTorque.current) * world.timestep / rayShapeR;
1876
- desiredLngImpulse.current = clamp(-(lngContactVel * effectiveInertia.current) / (rayShapeR * rayShapeR) - lngSlopeGravImp, freeSpinImp - maxCaliperImp, freeSpinImp + maxCaliperImp);
1877
- } else desiredLngImpulse.current = freeSpinImp;
1878
- desiredLatImpulse.current = latContactVel * latEffectiveMass + latSlopeGravImp;
1879
- const lngUsage = desiredLngImpulse.current / maxLngImp;
1880
- const latUsage = desiredLatImpulse.current / maxLatImp;
1881
- const ellipseUsage = Math.sqrt(lngUsage * lngUsage + latUsage * latUsage);
1446
+ desiredLngImpulse.current = slipDiff * effectiveInertia.current / (rayShapeR * rayShapeR);
1447
+ desiredLatImpulse.current = latContactVel * (wheelSupportForceMag.current / gravityMag);
1448
+ const ellipseUsage = Math.sqrt(desiredLngImpulse.current / maxLngImp * desiredLngImpulse.current / maxLngImp + desiredLatImpulse.current / maxLatImp * desiredLatImpulse.current / maxLatImp);
1882
1449
  if (ellipseUsage > 1) {
1883
1450
  desiredLngImpulse.current /= ellipseUsage;
1884
1451
  desiredLatImpulse.current /= ellipseUsage;
1885
1452
  }
1886
- const lngCoeff = clamp(Math.max(minLngRelaxCoeff, lngContactVelAbs / Math.max(relaxLngRate, 1e-6) * world.timestep), 0, 1);
1887
- const latCoeff = clamp(Math.max(minLatRelaxCoeff, latContactVelAbs / Math.max(relaxLatRate, 1e-6) * world.timestep), 0, 1);
1453
+ let lngCoeff = clamp(Math.max(minLngRelaxCoeff, lngContactVelAbs / Math.max(relaxLngRate, 1e-6) * world.timestep), 0, 1);
1454
+ let latCoeff = clamp(Math.max(minLatRelaxCoeff, latContactVelAbs / Math.max(relaxLatRate, 1e-6) * world.timestep), 0, 1);
1888
1455
  smoothedLngImpulse.current += (desiredLngImpulse.current - smoothedLngImpulse.current) * lngCoeff;
1889
1456
  smoothedLatImpulse.current += (desiredLatImpulse.current - smoothedLatImpulse.current) * latCoeff;
1890
- const capToward = (smoothed, desired) => smoothed * desired <= 0 ? 0 : Math.abs(smoothed) > Math.abs(desired) ? desired : smoothed;
1891
- smoothedLngImpulse.current = capToward(smoothedLngImpulse.current, desiredLngImpulse.current);
1892
- smoothedLatImpulse.current = capToward(smoothedLatImpulse.current, desiredLatImpulse.current);
1893
1457
  lngFrictionImp.current.copy(lngAxis.current).multiplyScalar(smoothedLngImpulse.current);
1894
1458
  latFrictionImp.current.copy(latAxis.current).multiplyScalar(-smoothedLatImpulse.current);
1895
1459
  }, [
1896
- brakeWheel,
1897
1460
  wheelMass,
1461
+ wheelInertia,
1898
1462
  rayShapeR,
1899
1463
  lowVelThreshold,
1900
1464
  lngSlipRatioCurve,
@@ -1935,20 +1499,19 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1935
1499
  /**
1936
1500
  * Update wheel models
1937
1501
  */
1938
- const updateWheelModel = useCallback((delta) => {
1502
+ const updateWheelModel = useCallback(() => {
1939
1503
  if (!wheelRef.current || !modelRef.current || !wheelModelUpdate) return;
1940
- const dt = delta > 0 ? delta : world.timestep;
1941
1504
  const offsetY = (groundDetection === "rayCast" ? rayHit.current : shapeRayHit.current) ? -(rayLength + rayShapeR) + wheelModelRadius + (rayLength - suspensionToi.current) : -(rayLength + rayShapeR) + wheelModelRadius;
1942
- wheelRef.current.position.y = THREE.MathUtils.lerp(wheelRef.current.position.y, offsetY, 1 - Math.exp(-wheelModelLerpPosRate * dt));
1943
- modelRef.current.rotation.x += wheelAngVel.current * dt * (wheelModelReversRotation ? -1 : 1);
1505
+ wheelRef.current.position.y = THREE.MathUtils.lerp(wheelRef.current.position.y, offsetY, 1 - Math.exp(-wheelModelLerpPosRate * world.timestep));
1506
+ modelRef.current.rotation.x += wheelAngVel.current * world.timestep * (wheelModelReversRotation ? -1 : 1);
1944
1507
  }, [
1945
1508
  groundDetection,
1946
1509
  wheelModelRadius,
1947
1510
  rayLength,
1948
1511
  rayShapeR,
1949
1512
  wheelModelLerpPosRate,
1950
- wheelModelUpdate,
1951
- wheelModelReversRotation
1513
+ rayLength,
1514
+ rayShapeR
1952
1515
  ]);
1953
1516
  /**
1954
1517
  * Update debug indicators
@@ -1981,62 +1544,50 @@ var ShapeCastWheel = /* @__PURE__ */ forwardRef(({ children, debug = false, enab
1981
1544
  currEngineArrowRef.current.setLength(length);
1982
1545
  }
1983
1546
  }, [groundDetection, debuggerArrowScale]);
1984
- const stepWheelSuspension = useCallback(() => {
1985
- if (!vehicleValue || !enable) {
1986
- wheelInfo.current.rayHit = null;
1987
- wheelInfo.current.supportForceMag = 0;
1988
- wheelInfo.current.maxCaliperImp = 0;
1989
- wheelInfo.current.isActivelyBraking = false;
1990
- return;
1991
- }
1547
+ /**
1548
+ *
1549
+ *
1550
+ *
1551
+ *
1552
+ *
1553
+ *
1554
+ *
1555
+ *
1556
+ *
1557
+ *
1558
+ *
1559
+ *
1560
+ *
1561
+ *
1562
+ *
1563
+ *
1564
+ *
1565
+ *
1566
+ *
1567
+ *
1568
+ *
1569
+ *
1570
+ *
1571
+ *
1572
+ */
1573
+ useFrame(() => {
1574
+ if (!vehicleValue || !enable) return;
1992
1575
  const vehicleBody = vehicleValue.body.current;
1576
+ const vehicleGravityMag = vehicleValue.gravityMag.current;
1993
1577
  if (!vehicleBody) return;
1994
1578
  updateVehicleInfo(vehicleBody);
1579
+ updateShapeCastDir();
1995
1580
  handleUserInput();
1996
1581
  steeringWheel();
1997
- updateShapeCastDir(vehicleBody);
1998
1582
  floatVehicle(vehicleBody);
1999
- computeContactFriction();
2000
- prepareContactSlopeGravity(vehicleValue.gravityMag.current, vehicleBody);
2001
- }, [
2002
- vehicleValue,
2003
- enable,
2004
- updateVehicleInfo,
2005
- handleUserInput,
2006
- steeringWheel,
2007
- updateShapeCastDir,
2008
- floatVehicle,
2009
- computeContactFriction,
2010
- prepareContactSlopeGravity
2011
- ]);
2012
- const stepWheelTireAndSpin = useCallback((overrideLngGravImp, overrideLatGravImp) => {
2013
- if (!vehicleValue || !enable) return;
2014
- const vehicleBody = vehicleValue.body.current;
2015
- if (!vehicleBody) return;
2016
- computeWheelFrictionImpulse(vehicleValue.gravityMag.current, vehicleBody, overrideLngGravImp, overrideLatGravImp);
1583
+ isOnMovingObjectDetect(vehicleBody);
2017
1584
  applyMassOnStandCollider();
2018
1585
  applyFricitonOnStandCollider();
1586
+ computeRelativeVelocity();
1587
+ computeContactFriction();
1588
+ computeWheelFrictionImpulse(vehicleGravityMag);
2019
1589
  solveWheelRotation();
2020
- syncWheelInfo();
2021
- }, [
2022
- vehicleValue,
2023
- enable,
2024
- computeWheelFrictionImpulse,
2025
- applyMassOnStandCollider,
2026
- applyFricitonOnStandCollider,
2027
- solveWheelRotation,
2028
- syncWheelInfo
2029
- ]);
2030
- const stepWheelPhysics = useCallback(() => {
2031
- stepWheelSuspension();
2032
- stepWheelTireAndSpin();
2033
- }, [stepWheelSuspension, stepWheelTireAndSpin]);
2034
- wheelInfo.current.stepWheelSuspension = stepWheelSuspension;
2035
- wheelInfo.current.stepWheelTireAndSpin = stepWheelTireAndSpin;
2036
- wheelInfo.current.stepWheelPhysics = stepWheelPhysics;
2037
- useFrame((_, delta) => {
2038
- if (!vehicleValue || !enable || !vehicleValue.body.current) return;
2039
- updateWheelModel(delta);
1590
+ updateWheelModel();
2040
1591
  if (debug) updateDebugger();
2041
1592
  });
2042
1593
  return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("group", {
@@ -2176,21 +1727,21 @@ var ThrustPropeller = /* @__PURE__ */ forwardRef(({ children, debug = true, enab
2176
1727
  /**
2177
1728
  * Rapier preset
2178
1729
  */
2179
- const { world, rigidBodyStates } = useRapier();
1730
+ const { rapier, world } = useRapier();
2180
1731
  /**
2181
1732
  * Retrieve context object from ecctrl vehicle
2182
1733
  */
2183
1734
  const vehicleValue = useContext(VehicleContext);
1735
+ const vehicleBody = vehicleValue.body.current;
2184
1736
  const vehiclePos = useRef(new THREE.Vector3());
2185
- const vehicleLocalCom = useRef(new THREE.Vector3());
2186
1737
  const vehicleQuat = useRef(new THREE.Quaternion());
1738
+ const vehicleInvertQuat = useRef(new THREE.Quaternion());
2187
1739
  const vehicleLinVel = useRef(new THREE.Vector3());
2188
1740
  const vehicleAngVel = useRef(new THREE.Vector3());
2189
1741
  /**
2190
1742
  * Control preset
2191
1743
  */
2192
1744
  const localPos = useRef(new THREE.Vector3());
2193
- const localArm = useRef(new THREE.Vector3());
2194
1745
  const localQuat = useRef(new THREE.Quaternion());
2195
1746
  const worldPos = useRef(new THREE.Vector3());
2196
1747
  const worldQuat = useRef(new THREE.Quaternion());
@@ -2249,14 +1800,6 @@ var ThrustPropeller = /* @__PURE__ */ forwardRef(({ children, debug = true, enab
2249
1800
  ay: 0,
2250
1801
  az: 0
2251
1802
  });
2252
- propellerInfo.current.name = name;
2253
- propellerInfo.current.enable = enable;
2254
- propellerInfo.current.debug = debug;
2255
- propellerInfo.current.maxThrust = maxThrust;
2256
- propellerInfo.current.torqueRatio = torqueRatio;
2257
- propellerInfo.current.debuggerScale = debuggerScale;
2258
- propellerInfo.current.invertThrust = invertThrust;
2259
- propellerInfo.current.invertTorque = invertTorque;
2260
1803
  useEffect(() => {
2261
1804
  vehicleValue === null || vehicleValue === void 0 || vehicleValue.regPropeller(propellerInfo);
2262
1805
  return () => vehicleValue === null || vehicleValue === void 0 ? void 0 : vehicleValue.unregPropeller(propellerId);
@@ -2352,32 +1895,23 @@ var ThrustPropeller = /* @__PURE__ */ forwardRef(({ children, debug = true, enab
2352
1895
  */
2353
1896
  const updateVehicleInfo = useCallback((body) => {
2354
1897
  vehiclePos.current.copy(body.translation());
2355
- vehicleLocalCom.current.copy(body.localCom());
2356
1898
  vehicleQuat.current.copy(body.rotation());
2357
1899
  vehicleLinVel.current.copy(body.linvel());
2358
1900
  vehicleAngVel.current.copy(body.angvel());
2359
1901
  }, []);
2360
1902
  /**
2361
- * Update propeller info function directly from local propeller transform (immune to Three.js render interpolation)
1903
+ * Update propeller info function
2362
1904
  */
2363
- const updatePropellerInfo = useCallback((body) => {
2364
- var _rigidBodyStates$get;
1905
+ const updatePropellerInfo = useCallback(() => {
2365
1906
  if (!thrustPropellerRef.current) return;
2366
- localPos.current.copy(thrustPropellerRef.current.position);
2367
- localQuat.current.copy(thrustPropellerRef.current.quaternion);
2368
- const rigidBodyObject = (_rigidBodyStates$get = rigidBodyStates.get(body.handle)) === null || _rigidBodyStates$get === void 0 ? void 0 : _rigidBodyStates$get.object;
2369
- let parent = thrustPropellerRef.current.parent;
2370
- if (rigidBodyObject && parent && parent !== rigidBodyObject) while (parent && parent !== rigidBodyObject) {
2371
- localPos.current.multiply(parent.scale).applyQuaternion(parent.quaternion).add(parent.position);
2372
- localQuat.current.premultiply(parent.quaternion);
2373
- parent = parent.parent;
2374
- }
2375
- worldQuat.current.multiplyQuaternions(vehicleQuat.current, localQuat.current);
2376
- worldPos.current.copy(localPos.current).applyQuaternion(vehicleQuat.current).add(vehiclePos.current);
1907
+ thrustPropellerRef.current.getWorldPosition(worldPos.current);
1908
+ thrustPropellerRef.current.getWorldQuaternion(worldQuat.current);
1909
+ vehicleInvertQuat.current.copy(vehicleQuat.current).invert();
1910
+ localPos.current.subVectors(worldPos.current, vehiclePos.current).applyQuaternion(vehicleInvertQuat.current);
1911
+ localQuat.current.multiplyQuaternions(vehicleInvertQuat.current, worldQuat.current);
2377
1912
  thrustDir.current.set(0, invertThrust ? -1 : 1, 0).applyQuaternion(localQuat.current);
2378
1913
  thrustForce.current.copy(thrustDir.current).multiplyScalar(maxThrust);
2379
- localArm.current.subVectors(localPos.current, vehicleLocalCom.current);
2380
- leverageTorque.current.crossVectors(localArm.current, thrustForce.current);
1914
+ leverageTorque.current.crossVectors(localPos.current, thrustForce.current);
2381
1915
  reactionTorqueDir.current.set(0, invertTorque ? -1 : 1, 0).applyQuaternion(localQuat.current);
2382
1916
  reactionTorque.current.copy(reactionTorqueDir.current).multiplyScalar(maxThrust * torqueRatio);
2383
1917
  torqueInfluence.current.copy(leverageTorque.current).add(reactionTorque.current);
@@ -2397,18 +1931,15 @@ var ThrustPropeller = /* @__PURE__ */ forwardRef(({ children, debug = true, enab
2397
1931
  invertThrust,
2398
1932
  invertTorque,
2399
1933
  maxThrust,
2400
- rigidBodyStates,
2401
1934
  torqueRatio
2402
1935
  ]);
2403
1936
  /**
2404
1937
  * Update propeller model function
2405
1938
  */
2406
- const updatePropellerModel = useCallback((delta) => {
1939
+ const updatePropellerModel = useCallback((frameRateCorrection) => {
2407
1940
  if (!modelRef.current) return;
2408
- const dt = delta > 0 ? delta : world.timestep;
2409
- const frameRateCorrection = 60 * dt;
2410
1941
  const targetVel = throttle.current * propellerModelMaxSpin * (invertTorque ? -1 : 1);
2411
- modelSpinVel.current = lerp(modelSpinVel.current, targetVel, 1 - Math.exp(-propellerModelLerpSpinRate * dt));
1942
+ modelSpinVel.current = lerp(modelSpinVel.current, targetVel, 1 - Math.exp(-propellerModelLerpSpinRate * world.timestep));
2412
1943
  modelRef.current.rotateY(modelSpinVel.current * frameRateCorrection);
2413
1944
  }, [
2414
1945
  propellerModelMaxSpin,
@@ -2422,22 +1953,35 @@ var ThrustPropeller = /* @__PURE__ */ forwardRef(({ children, debug = true, enab
2422
1953
  if (currThrustArrowRef.current) currThrustArrowRef.current.setLength(throttle.current * debuggerArrowScale);
2423
1954
  if (currTorqueArrowRef.current) currTorqueArrowRef.current.setLength(throttle.current * debuggerArrowScale * torqueRatio);
2424
1955
  }, [debuggerArrowScale, torqueRatio]);
2425
- const stepPropellerPhysics = useCallback(() => {
2426
- const body = vehicleValue === null || vehicleValue === void 0 ? void 0 : vehicleValue.body.current;
2427
- if (!body || !enable) return;
2428
- updateVehicleInfo(body);
2429
- updatePropellerInfo(body);
2430
- }, [
2431
- vehicleValue,
2432
- enable,
2433
- updateVehicleInfo,
2434
- updatePropellerInfo
2435
- ]);
2436
- propellerInfo.current.stepPropellerPhysics = stepPropellerPhysics;
2437
- useFrame((_, delta) => {
2438
- if (!(vehicleValue === null || vehicleValue === void 0 ? void 0 : vehicleValue.body.current) || !enable) return;
2439
- if (propellerModelUpdate) updatePropellerModel(delta);
2440
- if (debug) updateDebugger();
1956
+ /**
1957
+ *
1958
+ *
1959
+ *
1960
+ *
1961
+ *
1962
+ *
1963
+ *
1964
+ *
1965
+ *
1966
+ *
1967
+ *
1968
+ *
1969
+ *
1970
+ *
1971
+ *
1972
+ *
1973
+ *
1974
+ *
1975
+ *
1976
+ *
1977
+ */
1978
+ useFrame(() => {
1979
+ if (!vehicleBody || !enable) return;
1980
+ const frameRateCorrection = 60 * world.timestep;
1981
+ updateVehicleInfo(vehicleBody);
1982
+ updatePropellerInfo();
1983
+ if (propellerModelUpdate) updatePropellerModel(frameRateCorrection);
1984
+ updateDebugger();
2441
1985
  });
2442
1986
  return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("group", {
2443
1987
  ref: thrustPropellerRef,
@@ -2522,4 +2066,4 @@ var ThrustPropeller_default = React.memo(ThrustPropeller);
2522
2066
  //#endregion
2523
2067
  export { ShapeCastWheel_default as n, EcctrlVehicle_default as r, ThrustPropeller_default as t };
2524
2068
 
2525
- //# sourceMappingURL=vehicle-AEqO39Wm.js.map
2069
+ //# sourceMappingURL=vehicle-B8WqQHee.js.map