CannonHingeConstraint.re.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import * as RE from 'rogue-engine';
  2. import * as THREE from 'three';
  3. import * as CANNON from 'cannon-es';
  4. import CannonConstraint from './CannonConstraint';
  5. import * as RogueCannon from '../../Lib/RogueCannon';
  6. export default class CannonHingeConstraint extends CannonConstraint {
  7. constraint: CANNON.HingeConstraint;
  8. @RE.Prop("Object3D") target: THREE.Object3D;
  9. @RE.Prop("Vector3") pivotA: THREE.Vector3 = new THREE.Vector3(0.1, 0, 0);
  10. @RE.Prop("Vector3") axisA: THREE.Vector3 = new THREE.Vector3(0, 1, 0);
  11. @RE.Prop("Vector3") pivotB: THREE.Vector3 = new THREE.Vector3(-1, 0, 0);
  12. @RE.Prop("Vector3") axisB: THREE.Vector3 = new THREE.Vector3(0, 1, 0);
  13. @RE.Prop("Boolean") collideConnected: boolean;
  14. @RE.Prop("Number") maxForce: number = 1e6;
  15. protected createConstraint() {
  16. if (!this.target) throw "CannonHinge requires a target";
  17. const bodyA = this.getCannonBodyComponent(this.object3d).body;
  18. const bodyB = this.getCannonBodyComponent(this.target).body;
  19. this.constraint = new CANNON.HingeConstraint(bodyA, bodyB, {
  20. pivotA: new CANNON.Vec3(this.pivotA.x, this.pivotA.y, this.pivotA.z),
  21. axisA: new CANNON.Vec3(this.axisA.x, this.axisA.y, this.axisA.z),
  22. pivotB: new CANNON.Vec3(this.pivotB.x, this.pivotB.y, this.pivotB.z),
  23. axisB: new CANNON.Vec3(this.axisB.x, this.axisB.y, this.axisB.z),
  24. collideConnected: this.collideConnected,
  25. maxForce: this.maxForce,
  26. });
  27. RogueCannon.getWorld().addConstraint(this.constraint);
  28. }
  29. }
  30. RE.registerComponent(CannonHingeConstraint);