physicsEngine.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { Vector3 } from "../../Maths/math.vector.js";
  2. import { _WarnImport } from "../../Misc/devTools.js";
  3. /**
  4. * Class used to control physics engine
  5. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
  6. */
  7. export class PhysicsEngine {
  8. /**
  9. *
  10. * @returns version
  11. */
  12. getPluginVersion() {
  13. return this._physicsPlugin.getPluginVersion();
  14. }
  15. /**
  16. * @virtual
  17. * Factory used to create the default physics plugin.
  18. * @returns The default physics plugin
  19. */
  20. static DefaultPluginFactory() {
  21. throw _WarnImport("CannonJSPlugin");
  22. }
  23. /**
  24. * Creates a new Physics Engine
  25. * @param gravity defines the gravity vector used by the simulation
  26. * @param _physicsPlugin defines the plugin to use (CannonJS by default)
  27. */
  28. constructor(gravity, _physicsPlugin = PhysicsEngine.DefaultPluginFactory()) {
  29. this._physicsPlugin = _physicsPlugin;
  30. /**
  31. * Global value used to control the smallest number supported by the simulation
  32. */
  33. this._impostors = [];
  34. this._joints = [];
  35. this._subTimeStep = 0;
  36. this._uniqueIdCounter = 0;
  37. if (!this._physicsPlugin.isSupported()) {
  38. throw new Error("Physics Engine " + this._physicsPlugin.name + " cannot be found. " + "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. setGravity(gravity) {
  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. setTimeStep(newTimeStep = 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. getTimeStep() {
  67. return this._physicsPlugin.getTimeStep();
  68. }
  69. /**
  70. * Set the sub time step of the physics engine.
  71. * Default is 0 meaning there is no sub steps
  72. * To increase physics resolution precision, set a small value (like 1 ms)
  73. * @param subTimeStep defines the new sub timestep used for physics resolution.
  74. */
  75. setSubTimeStep(subTimeStep = 0) {
  76. this._subTimeStep = subTimeStep;
  77. }
  78. /**
  79. * Get the sub time step of the physics engine.
  80. * @returns the current sub time step
  81. */
  82. getSubTimeStep() {
  83. return this._subTimeStep;
  84. }
  85. /**
  86. * Release all resources
  87. */
  88. dispose() {
  89. this._impostors.forEach(function (impostor) {
  90. impostor.dispose();
  91. });
  92. this._physicsPlugin.dispose();
  93. }
  94. /**
  95. * Gets the name of the current physics plugin
  96. * @returns the name of the plugin
  97. */
  98. getPhysicsPluginName() {
  99. return this._physicsPlugin.name;
  100. }
  101. /**
  102. * Adding a new impostor for the impostor tracking.
  103. * This will be done by the impostor itself.
  104. * @param impostor the impostor to add
  105. */
  106. addImpostor(impostor) {
  107. this._impostors.push(impostor);
  108. impostor.uniqueId = this._uniqueIdCounter++;
  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. removeImpostor(impostor) {
  120. const index = this._impostors.indexOf(impostor);
  121. if (index > -1) {
  122. const 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. addJoint(mainImpostor, connectedImpostor, joint) {
  136. const 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. removeJoint(mainImpostor, connectedImpostor, joint) {
  152. const matchingJoints = this._joints.filter(function (impostorJoint) {
  153. return impostorJoint.connectedImpostor === connectedImpostor && impostorJoint.joint === joint && impostorJoint.mainImpostor === mainImpostor;
  154. });
  155. if (matchingJoints.length) {
  156. this._physicsPlugin.removeJoint(matchingJoints[0]);
  157. //TODO remove it from the list as well
  158. }
  159. }
  160. /**
  161. * Called by the scene. No need to call it.
  162. * @param delta defines the timespan between frames
  163. */
  164. _step(delta) {
  165. //check if any mesh has no body / requires an update
  166. this._impostors.forEach((impostor) => {
  167. if (impostor.isBodyInitRequired()) {
  168. this._physicsPlugin.generatePhysicsBody(impostor);
  169. }
  170. });
  171. if (delta > 0.1) {
  172. delta = 0.1;
  173. }
  174. else if (delta <= 0) {
  175. delta = 1.0 / 60.0;
  176. }
  177. this._physicsPlugin.executeStep(delta, this._impostors);
  178. }
  179. /**
  180. * Gets the current plugin used to run the simulation
  181. * @returns current plugin
  182. */
  183. getPhysicsPlugin() {
  184. return this._physicsPlugin;
  185. }
  186. /**
  187. * Gets the list of physic impostors
  188. * @returns an array of PhysicsImpostor
  189. */
  190. getImpostors() {
  191. return this._impostors;
  192. }
  193. /**
  194. * Gets the impostor for a physics enabled object
  195. * @param object defines the object impersonated by the impostor
  196. * @returns the PhysicsImpostor or null if not found
  197. */
  198. getImpostorForPhysicsObject(object) {
  199. for (let i = 0; i < this._impostors.length; ++i) {
  200. if (this._impostors[i].object === object) {
  201. return this._impostors[i];
  202. }
  203. }
  204. return null;
  205. }
  206. /**
  207. * Gets the impostor for a physics body object
  208. * @param body defines physics body used by the impostor
  209. * @returns the PhysicsImpostor or null if not found
  210. */
  211. getImpostorWithPhysicsBody(body) {
  212. for (let i = 0; i < this._impostors.length; ++i) {
  213. if (this._impostors[i].physicsBody === body) {
  214. return this._impostors[i];
  215. }
  216. }
  217. return null;
  218. }
  219. /**
  220. * Does a raycast in the physics world
  221. * @param from when should the ray start?
  222. * @param to when should the ray end?
  223. * @returns PhysicsRaycastResult
  224. */
  225. raycast(from, to) {
  226. return this._physicsPlugin.raycast(from, to);
  227. }
  228. /**
  229. * Does a raycast in the physics world
  230. * @param from when should the ray start?
  231. * @param to when should the ray end?
  232. * @param result resulting PhysicsRaycastResult
  233. * @returns true if the ray hits an impostor, else false
  234. */
  235. raycastToRef(from, to, result) {
  236. return this._physicsPlugin.raycastToRef(from, to, result);
  237. }
  238. }
  239. //# sourceMappingURL=physicsEngine.js.map