CannonConfig.re.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import * as RE from 'rogue-engine';
  2. import * as CANNON from 'cannon-es';
  3. import * as THREE from 'three';
  4. import * as RogueCannon from '../Lib/RogueCannon';
  5. export default class CannonConfig extends RE.Component {
  6. private _defaultFriction = 0.01;
  7. private _defaultRestitution = 0;
  8. @RE.Prop("Number") maxSubSteps: number = 1;
  9. @RE.Prop("Number")
  10. get defaultFriction() {
  11. return this._defaultFriction;
  12. }
  13. set defaultFriction(value: number) {
  14. this._defaultFriction = value;
  15. RogueCannon.getWorld().defaultContactMaterial.friction = value;
  16. }
  17. @RE.Prop("Number")
  18. get defaultRestitution() {
  19. return this._defaultRestitution;
  20. }
  21. set defaultRestitution(value: number) {
  22. this._defaultRestitution = value;
  23. RogueCannon.getWorld().defaultContactMaterial.restitution = value;
  24. }
  25. @RE.Prop("Vector3") gravity: THREE.Vector3 = new THREE.Vector3(0, -9.82, 0);
  26. awake() {
  27. RogueCannon.setWorld(new CANNON.World());
  28. RogueCannon.getWorld().gravity.set(this.gravity.x, this.gravity.y, this.gravity.z);
  29. RogueCannon.getWorld().broadphase = new CANNON.NaiveBroadphase();
  30. RogueCannon.getWorld().defaultContactMaterial.friction = this.defaultFriction;
  31. RogueCannon.getWorld().defaultContactMaterial.restitution = this.defaultRestitution;
  32. }
  33. beforeUpdate() {
  34. RogueCannon.getWorld().step(RE.Runtime.deltaTime, RE.Runtime.deltaTime, this.maxSubSteps || 1);
  35. }
  36. }
  37. RE.registerComponent( CannonConfig );