physicsEngine.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 { PhysicsRaycastResult } from "./physicsRaycastResult";
  7. import { _DevTools } from '../Misc/devTools';
  8. /**
  9. * Class used to control physics engine
  10. * @see http://doc.babylonjs.com/how_to/using_the_physics_engine
  11. */
  12. export class PhysicsEngine implements IPhysicsEngine {
  13. /**
  14. * Global value used to control the smallest number supported by the simulation
  15. */
  16. public static Epsilon = 0.001;
  17. private _impostors: Array<PhysicsImpostor> = [];
  18. private _joints: Array<PhysicsImpostorJoint> = [];
  19. /**
  20. * Gets the gravity vector used by the simulation
  21. */
  22. public gravity: Vector3;
  23. /**
  24. * Factory used to create the default physics plugin.
  25. * @returns The default physics plugin
  26. */
  27. public static DefaultPluginFactory(): IPhysicsEnginePlugin {
  28. throw _DevTools.WarnImport("CannonJSPlugin");
  29. }
  30. /**
  31. * Creates a new Physics Engine
  32. * @param gravity defines the gravity vector used by the simulation
  33. * @param _physicsPlugin defines the plugin to use (CannonJS by default)
  34. */
  35. constructor(gravity: Nullable<Vector3>, private _physicsPlugin: IPhysicsEnginePlugin = PhysicsEngine.DefaultPluginFactory()) {
  36. if (!this._physicsPlugin.isSupported()) {
  37. throw new Error("Physics Engine " + this._physicsPlugin.name + " cannot be found. "
  38. + "Please make sure it is included.");
  39. }
  40. gravity = gravity || new Vector3(0, -9.807, 0);
  41. this.setGravity(gravity);
  42. this.setTimeStep();
  43. }
  44. /**
  45. * Sets the gravity vector used by the simulation
  46. * @param gravity defines the gravity vector to use
  47. */
  48. public setGravity(gravity: Vector3): void {
  49. this.gravity = gravity;
  50. this._physicsPlugin.setGravity(this.gravity);
  51. }
  52. /**
  53. * Set the time step of the physics engine.
  54. * Default is 1/60.
  55. * To slow it down, enter 1/600 for example.
  56. * To speed it up, 1/30
  57. * @param newTimeStep defines the new timestep to apply to this world.
  58. */
  59. public setTimeStep(newTimeStep: number = 1 / 60) {
  60. this._physicsPlugin.setTimeStep(newTimeStep);
  61. }
  62. /**
  63. * Get the time step of the physics engine.
  64. * @returns the current time step
  65. */
  66. public getTimeStep(): number {
  67. return this._physicsPlugin.getTimeStep();
  68. }
  69. /**
  70. * Release all resources
  71. */
  72. public dispose(): void {
  73. this._impostors.forEach(function(impostor) {
  74. impostor.dispose();
  75. });
  76. this._physicsPlugin.dispose();
  77. }
  78. /**
  79. * Gets the name of the current physics plugin
  80. * @returns the name of the plugin
  81. */
  82. public getPhysicsPluginName(): string {
  83. return this._physicsPlugin.name;
  84. }
  85. /**
  86. * Adding a new impostor for the impostor tracking.
  87. * This will be done by the impostor itself.
  88. * @param impostor the impostor to add
  89. */
  90. public addImpostor(impostor: PhysicsImpostor) {
  91. impostor.uniqueId = this._impostors.push(impostor);
  92. //if no parent, generate the body
  93. if (!impostor.parent) {
  94. this._physicsPlugin.generatePhysicsBody(impostor);
  95. }
  96. }
  97. /**
  98. * Remove an impostor from the engine.
  99. * This impostor and its mesh will not longer be updated by the physics engine.
  100. * @param impostor the impostor to remove
  101. */
  102. public removeImpostor(impostor: PhysicsImpostor) {
  103. var index = this._impostors.indexOf(impostor);
  104. if (index > -1) {
  105. var removed = this._impostors.splice(index, 1);
  106. //Is it needed?
  107. if (removed.length) {
  108. this.getPhysicsPlugin().removePhysicsBody(impostor);
  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. /**
  204. * Does a raycast in the physics world
  205. * @param from when should the ray start?
  206. * @param to when should the ray end?
  207. * @returns PhysicsRaycastResult
  208. */
  209. public raycast(from: Vector3, to: Vector3): PhysicsRaycastResult {
  210. return this._physicsPlugin.raycast(from, to);
  211. }
  212. }