physicsEngineComponent.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { AbstractMesh } from "../../Meshes/abstractMesh.js";
  2. import { PhysicsJoint } from "./physicsJoint.js";
  3. Object.defineProperty(AbstractMesh.prototype, "physicsImpostor", {
  4. get: function () {
  5. return this._physicsImpostor;
  6. },
  7. set: function (value) {
  8. if (this._physicsImpostor === value) {
  9. return;
  10. }
  11. if (this._disposePhysicsObserver) {
  12. this.onDisposeObservable.remove(this._disposePhysicsObserver);
  13. }
  14. this._physicsImpostor = value;
  15. if (value) {
  16. this._disposePhysicsObserver = this.onDisposeObservable.add(() => {
  17. // Physics
  18. if (this.physicsImpostor) {
  19. this.physicsImpostor.dispose( /*!doNotRecurse*/);
  20. this.physicsImpostor = null;
  21. }
  22. });
  23. }
  24. },
  25. enumerable: true,
  26. configurable: true,
  27. });
  28. /**
  29. * Gets the current physics impostor
  30. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics
  31. * @returns a physics impostor or null
  32. */
  33. AbstractMesh.prototype.getPhysicsImpostor = function () {
  34. return this.physicsImpostor;
  35. };
  36. /**
  37. * Apply a physic impulse to the mesh
  38. * @param force defines the force to apply
  39. * @param contactPoint defines where to apply the force
  40. * @returns the current mesh
  41. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
  42. */
  43. AbstractMesh.prototype.applyImpulse = function (force, contactPoint) {
  44. if (!this.physicsImpostor) {
  45. return this;
  46. }
  47. this.physicsImpostor.applyImpulse(force, contactPoint);
  48. return this;
  49. };
  50. /**
  51. * Creates a physic joint between two meshes
  52. * @param otherMesh defines the other mesh to use
  53. * @param pivot1 defines the pivot to use on this mesh
  54. * @param pivot2 defines the pivot to use on the other mesh
  55. * @param options defines additional options (can be plugin dependent)
  56. * @returns the current mesh
  57. * @see https://www.babylonjs-playground.com/#0BS5U0#0
  58. */
  59. AbstractMesh.prototype.setPhysicsLinkWith = function (otherMesh, pivot1, pivot2, options) {
  60. if (!this.physicsImpostor || !otherMesh.physicsImpostor) {
  61. return this;
  62. }
  63. this.physicsImpostor.createJoint(otherMesh.physicsImpostor, PhysicsJoint.HingeJoint, {
  64. mainPivot: pivot1,
  65. connectedPivot: pivot2,
  66. nativeParams: options,
  67. });
  68. return this;
  69. };
  70. //# sourceMappingURL=physicsEngineComponent.js.map