@pinkaphant/ecctrl 2.0.3 → 2.0.4-beta.1

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