babylon.physicsEngineComponent.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. module BABYLON {
  2. export interface Scene {
  3. /** @hidden (Backing field) */
  4. _physicsEngine: Nullable<IPhysicsEngine>;
  5. /**
  6. * Gets the current physics engine
  7. * @returns a IPhysicsEngine or null if none attached
  8. */
  9. getPhysicsEngine(): Nullable<IPhysicsEngine>;
  10. /**
  11. * Enables physics to the current scene
  12. * @param gravity defines the scene's gravity for the physics engine
  13. * @param plugin defines the physics engine to be used. defaults to OimoJS.
  14. * @return a boolean indicating if the physics engine was initialized
  15. */
  16. enablePhysics(gravity: Nullable<Vector3>, plugin?: IPhysicsEnginePlugin): boolean;
  17. /**
  18. * Disables and disposes the physics engine associated with the scene
  19. */
  20. disablePhysicsEngine(): void;
  21. /**
  22. * Gets a boolean indicating if there is an active physics engine
  23. * @returns a boolean indicating if there is an active physics engine
  24. */
  25. isPhysicsEnabled(): boolean;
  26. /**
  27. * Deletes a physics compound impostor
  28. * @param compound defines the compound to delete
  29. */
  30. deleteCompoundImpostor(compound: any): void;
  31. }
  32. /**
  33. * Gets the current physics engine
  34. * @returns a IPhysicsEngine or null if none attached
  35. */
  36. Scene.prototype.getPhysicsEngine = function(): Nullable<IPhysicsEngine> {
  37. return this._physicsEngine;
  38. }
  39. /**
  40. * Enables physics to the current scene
  41. * @param gravity defines the scene's gravity for the physics engine
  42. * @param plugin defines the physics engine to be used. defaults to OimoJS.
  43. * @return a boolean indicating if the physics engine was initialized
  44. */
  45. Scene.prototype.enablePhysics = function(gravity: Nullable<Vector3> = null, plugin?: IPhysicsEnginePlugin): boolean {
  46. if (this._physicsEngine) {
  47. return true;
  48. }
  49. try {
  50. this._physicsEngine = new PhysicsEngine(gravity, plugin);
  51. return true;
  52. } catch (e) {
  53. Tools.Error(e.message);
  54. return false;
  55. }
  56. }
  57. /**
  58. * Disables and disposes the physics engine associated with the scene
  59. */
  60. Scene.prototype.disablePhysicsEngine = function(): void {
  61. if (!this._physicsEngine) {
  62. return;
  63. }
  64. this._physicsEngine.dispose();
  65. this._physicsEngine = null;
  66. }
  67. /**
  68. * Gets a boolean indicating if there is an active physics engine
  69. * @returns a boolean indicating if there is an active physics engine
  70. */
  71. Scene.prototype.isPhysicsEnabled = function(): boolean {
  72. return this._physicsEngine !== undefined;
  73. }
  74. /**
  75. * Deletes a physics compound impostor
  76. * @param compound defines the compound to delete
  77. */
  78. Scene.prototype.deleteCompoundImpostor = function(compound: any): void {
  79. var mesh: AbstractMesh = compound.parts[0].mesh;
  80. if (mesh.physicsImpostor) {
  81. mesh.physicsImpostor.dispose(/*true*/);
  82. mesh.physicsImpostor = null;
  83. }
  84. }
  85. }