CannonPointToPointConstraint.re.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233
  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 CannonPointToPointConstraint extends CannonConstraint {
  7. constraint: CANNON.PointToPointConstraint;
  8. @RE.Prop("Object3D") target: THREE.Object3D;
  9. @RE.Prop("Vector3") privotA: THREE.Vector3 = new THREE.Vector3();
  10. @RE.Prop("Vector3") privotB: THREE.Vector3 = new THREE.Vector3();
  11. @RE.Prop("Number") maxForce: number = 1e6;
  12. protected createConstraint() {
  13. if (!this.target) throw "CannonHinge requires a target";
  14. const bodyA = this.getCannonBodyComponent(this.object3d).body;
  15. const bodyB = this.getCannonBodyComponent(this.target).body;
  16. this.constraint = new CANNON.PointToPointConstraint(
  17. bodyA,
  18. new CANNON.Vec3(this.privotA.x, this.privotA.y, this.privotA.z),
  19. bodyB,
  20. new CANNON.Vec3(this.privotB.x, this.privotB.y, this.privotB.z),
  21. this.maxForce
  22. );
  23. RogueCannon.getWorld().addConstraint(this.constraint);
  24. }
  25. }
  26. RE.registerComponent(CannonPointToPointConstraint);