CannonRaycastVehicle.re.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import * as RE from 'rogue-engine';
  2. import * as THREE from 'three';
  3. import * as CANNON from 'cannon-es';
  4. import CannonBody from '../CannonBody.re';
  5. import * as RogueCannon from '../../Lib/RogueCannon';
  6. export default class CannonRaycastVehicle extends RE.Component {
  7. @RE.Prop("Object3D") chasis: THREE.Object3D;
  8. @RE.Prop("Number") mass = 500;
  9. @RE.Prop("Number") suspensionStiffness = 30;
  10. @RE.Prop("Number") suspensionRestLength = 0.1;
  11. @RE.Prop("Number") frictionSlip = 0.7;
  12. @RE.Prop("Number") dampingRelaxation: 2.3;
  13. @RE.Prop("Number") dampingCompression: 4.4;
  14. @RE.Prop("Number") maxSuspensionForce: 100000;
  15. @RE.Prop("Number") rollInfluence: 0.01;
  16. @RE.Prop("Number") maxSuspensionTravel = 0.2;
  17. @RE.Prop("Number") customSlidingRotationalSpeed = -30;
  18. @RE.Prop("Boolean") useCustomSlidingRotationalSpeed = true;
  19. vehicle: CANNON.RaycastVehicle;
  20. start() {
  21. if (!RogueCannon.getWorld()) return;
  22. let body = RE.getComponent(CannonBody, this.object3d);
  23. if (!body) {
  24. body = new CannonBody("CarBody", this.object3d);
  25. body.mass = this.mass;
  26. RE.addComponent(body);
  27. }
  28. if (!this.chasis) return;
  29. const chassisBody = RE.getComponent(CannonBody, this.chasis);
  30. if (!(chassisBody instanceof CannonBody)) return;
  31. this.vehicle = new CANNON.RaycastVehicle({
  32. chassisBody: body.body,
  33. indexForwardAxis: 2,
  34. indexUpAxis: 1,
  35. indexRightAxis: 0,
  36. });
  37. this.vehicle.addToWorld(RogueCannon.getWorld());
  38. }
  39. }
  40. RE.registerComponent(CannonRaycastVehicle);