CannonWheel.re.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import * as RE from 'rogue-engine';
  2. import * as THREE from 'three';
  3. import * as CANNON from 'cannon-es';
  4. import CannonRaycastVehicle from './CannonRaycastVehicle.re';
  5. export default class CannonWheel extends RE.Component {
  6. @RE.props.object3d() wheel: THREE.Object3D;
  7. @RE.props.num() radiusOffset = 0;
  8. connectionPoint = new THREE.Vector3(0, 0, 0);
  9. raycastVehicle: CannonRaycastVehicle;
  10. wheelInfo: CANNON.WheelInfo;
  11. private matrixA = new THREE.Matrix4();
  12. private matrixB = new THREE.Matrix4();
  13. private matrixC = new THREE.Matrix4();
  14. start() {
  15. this.raycastVehicle = RE.getComponent(CannonRaycastVehicle, this.object3d) as CannonRaycastVehicle;
  16. if (!(this.raycastVehicle instanceof CannonRaycastVehicle)) return;
  17. let radius = 0.3;
  18. if (this.wheel) {
  19. this.connectionPoint.copy(this.wheel.position);
  20. const bbox = new THREE.Box3().setFromObject(this.wheel);
  21. radius = bbox.max.x - bbox.min.x;
  22. }
  23. this.wheelInfo = new CANNON.WheelInfo({
  24. radius: radius + this.radiusOffset,
  25. directionLocal: new CANNON.Vec3(0, -1, 0),
  26. suspensionStiffness: this.raycastVehicle.suspensionStiffness,
  27. suspensionRestLength: this.raycastVehicle.suspensionRestLength,
  28. frictionSlip: this.raycastVehicle.frictionSlip,
  29. dampingRelaxation: this.raycastVehicle.dampingRelaxation,
  30. dampingCompression: this.raycastVehicle.dampingCompression,
  31. maxSuspensionForce: this.raycastVehicle.maxSuspensionForce,
  32. rollInfluence: this.raycastVehicle.rollInfluence,
  33. axleLocal: new CANNON.Vec3(-1, 0, 0),
  34. chassisConnectionPointLocal: new CANNON.Vec3(this.connectionPoint.x, this.connectionPoint.y, this.connectionPoint.z),
  35. maxSuspensionTravel: this.raycastVehicle.maxSuspensionTravel,
  36. customSlidingRotationalSpeed: this.raycastVehicle.customSlidingRotationalSpeed,
  37. useCustomSlidingRotationalSpeed: this.raycastVehicle.useCustomSlidingRotationalSpeed,
  38. });
  39. this.raycastVehicle.vehicle.wheelInfos.push(this.wheelInfo);
  40. }
  41. afterUpdate(): void {
  42. if (!this.wheel) return;
  43. const wheel = this.wheelInfo;
  44. if (!wheel) return;
  45. const pos = wheel.worldTransform.position;
  46. const rot = wheel.worldTransform.quaternion;
  47. this.wheel.position.set(pos.x, pos.y, pos.z);
  48. this.wheel.parent?.worldToLocal(this.wheel.position);
  49. this.wheel.quaternion.set(rot.x, rot.y, rot.z, rot.w);
  50. this.matrixA.makeRotationFromQuaternion(this.wheel.quaternion);
  51. this.wheel.updateMatrixWorld();
  52. this.matrixB.copy((this.wheel.parent as THREE.Object3D).matrixWorld).invert();
  53. this.matrixC.extractRotation(this.matrixB);
  54. this.matrixA.premultiply(this.matrixC);
  55. this.wheel.quaternion.setFromRotationMatrix(this.matrixA);
  56. }
  57. }
  58. RE.registerComponent(CannonWheel);