RapierCapsule.re.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import RAPIER from '@dimforge/rapier3d-compat';
  2. import * as RE from 'rogue-engine';
  3. import * as THREE from 'three';
  4. import RogueRapier from '../../Lib/RogueRapier';
  5. import RapierCollider from './RapierCollider';
  6. export default class RapierCapsule extends RapierCollider {
  7. private _halfHeight = 0.5;
  8. private _radius = 0.5;
  9. @RE.props.num()
  10. get halfHeight() {
  11. return this._halfHeight;
  12. }
  13. set halfHeight(value: number) {
  14. const oldValue = this._halfHeight;
  15. this._halfHeight = value;
  16. if (oldValue !== value && this.collider && RogueRapier.world) {
  17. RogueRapier.world.removeCollider(this.collider, false);
  18. this.init();
  19. }
  20. }
  21. @RE.props.num()
  22. get radius() {
  23. return this._radius;
  24. }
  25. set radius(value: number) {
  26. const oldValue = this._radius;
  27. this._radius = value;
  28. if (oldValue !== value && this.collider && RogueRapier.world) {
  29. RogueRapier.world.removeCollider(this.collider, false);
  30. this.init();
  31. }
  32. }
  33. worldScale = new THREE.Vector3();
  34. protected createShape(): void {
  35. this.object3d.getWorldScale(this.worldScale);
  36. const maxSide = Math.max(this.worldScale.x, this.worldScale.z);
  37. let colliderDesc = RAPIER.ColliderDesc.capsule(this.halfHeight * this.worldScale.y, this.radius * maxSide);
  38. this.collider = RogueRapier.world.createCollider(colliderDesc, this.body);
  39. }
  40. }
  41. RE.registerComponent(RapierCapsule);