CannonDistanceConstraint.re.ts 926 B

12345678910111213141516171819202122232425
  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 CannonDistanceConstraint extends CannonConstraint {
  7. constraint: CANNON.DistanceConstraint;
  8. @RE.Prop("Object3D") target: THREE.Object3D;
  9. @RE.Prop("Number") distance: number = 1;
  10. @RE.Prop("Number") maxForce: number = 1e6;
  11. protected createConstraint() {
  12. if (!this.target) throw "CannonHinge requires a target";
  13. const bodyA = this.getCannonBodyComponent(this.object3d).body;
  14. const bodyB = this.getCannonBodyComponent(this.target).body;
  15. this.constraint = new CANNON.DistanceConstraint(bodyA, bodyB, this.distance, this.maxForce);
  16. RogueCannon.getWorld().addConstraint(this.constraint);
  17. }
  18. }
  19. RE.registerComponent(CannonDistanceConstraint);