physicsEngine.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { Nullable } from "../types";
  2. import { Vector3 } from "../Maths/math";
  3. import { IPhysicsEngine, PhysicsImpostorJoint, IPhysicsEnginePlugin } from "./IPhysicsEngine";
  4. import { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor";
  5. import { PhysicsJoint } from "./physicsJoint";
  6. import { _DevTools } from '../Misc/devTools';
  7. /**
  8. * Class used to control physics engine
  9. * @see http://doc.babylonjs.com/how_to/using_the_physics_engine
  10. */
  11. export class PhysicsEngine implements IPhysicsEngine {
  12. /**
  13. * Global value used to control the smallest number supported by the simulation
  14. */
  15. public static Epsilon = 0.001;
  16. private _impostors: Array<PhysicsImpostor> = [];
  17. private _joints: Array<PhysicsImpostorJoint> = [];
  18. /**
  19. * Gets the gravity vector used by the simulation
  20. */
  21. public gravity: Vector3;
  22. /**
  23. * Factory used to create the default physics plugin.
  24. * @returns The default physics plugin
  25. */
  26. public static DefaultPluginFactory(): IPhysicsEnginePlugin {
  27. throw _DevTools.WarnImport("CannonJSPlugin");
  28. }
  29. /**
  30. * Creates a new Physics Engine
  31. * @param gravity defines the gravity vector used by the simulation
  32. * @param _physicsPlugin defines the plugin to use (CannonJS by default)
  33. */
  34. constructor(gravity: Nullable<Vector3>, private _physicsPlugin: IPhysicsEnginePlugin = PhysicsEngine.DefaultPluginFactory()) {
  35. if (!this._physicsPlugin.isSupported()) {
  36. throw new Error("Physics Engine " + this._physicsPlugin.name + " cannot be found. "
  37. + "Please make sure it is included.");
  38. }
  39. gravity = gravity || new Vector3(0, -9.807, 0);
  40. this.setGravity(gravity);
  41. this.setTimeStep();
  42. }
  43. /**
  44. * Sets the gravity vector used by the simulation
  45. * @param gravity defines the gravity vector to use
  46. */
  47. public setGravity(gravity: Vector3): void {
  48. this.gravity = gravity;
  49. this._physicsPlugin.setGravity(this.gravity);
  50. }
  51. /**
  52. * Set the time step of the physics engine.
  53. * Default is 1/60.
  54. * To slow it down, enter 1/600 for example.
  55. * To speed it up, 1/30
  56. * @param newTimeStep defines the new timestep to apply to this world.
  57. */
  58. public setTimeStep(newTimeStep: number = 1 / 60) {
  59. this._physicsPlugin.setTimeStep(newTimeStep);
  60. }
  61. /**
  62. * Get the time step of the physics engine.
  63. * @returns the current time step
  64. */
  65. public getTimeStep(): number {
  66. return this._physicsPlugin.getTimeStep();
  67. }
  68. /**
  69. * Release all resources
  70. */
  71. public dispose(): void {
  72. this._impostors.forEach(function(impostor) {
  73. impostor.dispose();
  74. });
  75. this._physicsPlugin.dispose();
  76. }
  77. /**
  78. * Gets the name of the current physics plugin
  79. * @returns the name of the plugin
  80. */
  81. public getPhysicsPluginName(): string {
  82. return this._physicsPlugin.name;
  83. }
  84. /**
  85. * Adding a new impostor for the impostor tracking.
  86. * This will be done by the impostor itself.
  87. * @param impostor the impostor to add
  88. */
  89. public addImpostor(impostor: PhysicsImpostor) {
  90. impostor.uniqueId = this._impostors.push(impostor);
  91. //if no parent, generate the body
  92. if (!impostor.parent) {
  93. this._physicsPlugin.generatePhysicsBody(impostor);
  94. }
  95. }
  96. /**
  97. * Remove an impostor from the engine.
  98. * This impostor and its mesh will not longer be updated by the physics engine.
  99. * @param impostor the impostor to remove
  100. */
  101. public removeImpostor(impostor: PhysicsImpostor) {
  102. var index = this._impostors.indexOf(impostor);
  103. if (index > -1) {
  104. var removed = this._impostors.splice(index, 1);
  105. //Is it needed?
  106. if (removed.length) {
  107. //this will also remove it from the world.
  108. removed[0].physicsBody = null;
  109. }
  110. }
  111. }
  112. /**
  113. * Add a joint to the physics engine
  114. * @param mainImpostor defines the main impostor to which the joint is added.
  115. * @param connectedImpostor defines the impostor that is connected to the main impostor using this joint
  116. * @param joint defines the joint that will connect both impostors.
  117. */
  118. public addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) {
  119. var impostorJoint = {
  120. mainImpostor: mainImpostor,
  121. connectedImpostor: connectedImpostor,
  122. joint: joint
  123. };
  124. joint.physicsPlugin = this._physicsPlugin;
  125. this._joints.push(impostorJoint);
  126. this._physicsPlugin.generateJoint(impostorJoint);
  127. }
  128. /**
  129. * Removes a joint from the simulation
  130. * @param mainImpostor defines the impostor used with the joint
  131. * @param connectedImpostor defines the other impostor connected to the main one by the joint
  132. * @param joint defines the joint to remove
  133. */
  134. public removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) {
  135. var matchingJoints = this._joints.filter(function(impostorJoint) {
  136. return (impostorJoint.connectedImpostor === connectedImpostor
  137. && impostorJoint.joint === joint
  138. && impostorJoint.mainImpostor === mainImpostor);
  139. });
  140. if (matchingJoints.length) {
  141. this._physicsPlugin.removeJoint(matchingJoints[0]);
  142. //TODO remove it from the list as well
  143. }
  144. }
  145. /**
  146. * Called by the scene. No need to call it.
  147. * @param delta defines the timespam between frames
  148. */
  149. public _step(delta: number) {
  150. //check if any mesh has no body / requires an update
  151. this._impostors.forEach((impostor) => {
  152. if (impostor.isBodyInitRequired()) {
  153. this._physicsPlugin.generatePhysicsBody(impostor);
  154. }
  155. });
  156. if (delta > 0.1) {
  157. delta = 0.1;
  158. } else if (delta <= 0) {
  159. delta = 1.0 / 60.0;
  160. }
  161. this._physicsPlugin.executeStep(delta, this._impostors);
  162. }
  163. /**
  164. * Gets the current plugin used to run the simulation
  165. * @returns current plugin
  166. */
  167. public getPhysicsPlugin(): IPhysicsEnginePlugin {
  168. return this._physicsPlugin;
  169. }
  170. /**
  171. * Gets the list of physic impostors
  172. * @returns an array of PhysicsImpostor
  173. */
  174. public getImpostors(): Array<PhysicsImpostor> {
  175. return this._impostors;
  176. }
  177. /**
  178. * Gets the impostor for a physics enabled object
  179. * @param object defines the object impersonated by the impostor
  180. * @returns the PhysicsImpostor or null if not found
  181. */
  182. public getImpostorForPhysicsObject(object: IPhysicsEnabledObject): Nullable<PhysicsImpostor> {
  183. for (var i = 0; i < this._impostors.length; ++i) {
  184. if (this._impostors[i].object === object) {
  185. return this._impostors[i];
  186. }
  187. }
  188. return null;
  189. }
  190. /**
  191. * Gets the impostor for a physics body object
  192. * @param body defines physics body used by the impostor
  193. * @returns the PhysicsImpostor or null if not found
  194. */
  195. public getImpostorWithPhysicsBody(body: any): Nullable<PhysicsImpostor> {
  196. for (var i = 0; i < this._impostors.length; ++i) {
  197. if (this._impostors[i].physicsBody === body) {
  198. return this._impostors[i];
  199. }
  200. }
  201. return null;
  202. }
  203. }