physicsEngine.ts 8.6 KB

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