physicsEngine.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { Vector3 } from "../../Maths/math.vector.js";
  2. import { PhysicsRaycastResult } from "../physicsRaycastResult.js";
  3. import { _WarnImport } from "../../Misc/devTools.js";
  4. /**
  5. * Class used to control physics engine
  6. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
  7. */
  8. export class PhysicsEngine {
  9. /**
  10. *
  11. * @returns physics plugin version
  12. */
  13. getPluginVersion() {
  14. return this._physicsPlugin.getPluginVersion();
  15. }
  16. // eslint-disable-next-line jsdoc/require-returns-check
  17. /**
  18. * Factory used to create the default physics plugin.
  19. * @returns The default physics plugin
  20. */
  21. static DefaultPluginFactory() {
  22. throw _WarnImport("");
  23. }
  24. /**
  25. * Creates a new Physics Engine
  26. * @param gravity defines the gravity vector used by the simulation
  27. * @param _physicsPlugin defines the plugin to use (CannonJS by default)
  28. */
  29. constructor(gravity, _physicsPlugin = PhysicsEngine.DefaultPluginFactory()) {
  30. this._physicsPlugin = _physicsPlugin;
  31. /** @internal */
  32. this._physicsBodies = [];
  33. this._subTimeStep = 0;
  34. gravity = gravity || new Vector3(0, -9.807, 0);
  35. this.setGravity(gravity);
  36. this.setTimeStep();
  37. }
  38. /**
  39. * Sets the gravity vector used by the simulation
  40. * @param gravity defines the gravity vector to use
  41. */
  42. setGravity(gravity) {
  43. this.gravity = gravity;
  44. this._physicsPlugin.setGravity(this.gravity);
  45. }
  46. /**
  47. * Set the time step of the physics engine.
  48. * Default is 1/60.
  49. * To slow it down, enter 1/600 for example.
  50. * To speed it up, 1/30
  51. * Unit is seconds.
  52. * @param newTimeStep defines the new timestep to apply to this world.
  53. */
  54. setTimeStep(newTimeStep = 1 / 60) {
  55. this._physicsPlugin.setTimeStep(newTimeStep);
  56. }
  57. /**
  58. * Get the time step of the physics engine.
  59. * @returns the current time step
  60. */
  61. getTimeStep() {
  62. return this._physicsPlugin.getTimeStep();
  63. }
  64. /**
  65. * Set the sub time step of the physics engine.
  66. * Default is 0 meaning there is no sub steps
  67. * To increase physics resolution precision, set a small value (like 1 ms)
  68. * @param subTimeStep defines the new sub timestep used for physics resolution.
  69. */
  70. setSubTimeStep(subTimeStep = 0) {
  71. this._subTimeStep = subTimeStep;
  72. }
  73. /**
  74. * Get the sub time step of the physics engine.
  75. * @returns the current sub time step
  76. */
  77. getSubTimeStep() {
  78. return this._subTimeStep;
  79. }
  80. /**
  81. * Release all resources
  82. */
  83. dispose() {
  84. this._physicsPlugin.dispose();
  85. }
  86. /**
  87. * Gets the name of the current physics plugin
  88. * @returns the name of the plugin
  89. */
  90. getPhysicsPluginName() {
  91. return this._physicsPlugin.name;
  92. }
  93. /**
  94. * Adding a new impostor for the impostor tracking.
  95. * This will be done by the impostor itself.
  96. * @param impostor the impostor to add
  97. */
  98. /**
  99. * Called by the scene. No need to call it.
  100. * @param delta defines the timespan between frames
  101. */
  102. _step(delta) {
  103. if (delta > 0.1) {
  104. delta = 0.1;
  105. }
  106. else if (delta <= 0) {
  107. delta = 1.0 / 60.0;
  108. }
  109. this._physicsPlugin.executeStep(delta, this._physicsBodies);
  110. }
  111. /**
  112. * Add a body as an active component of this engine
  113. * @param physicsBody The body to add
  114. */
  115. addBody(physicsBody) {
  116. this._physicsBodies.push(physicsBody);
  117. }
  118. /**
  119. * Removes a particular body from this engine
  120. * @param physicsBody The body to remove from the simulation
  121. */
  122. removeBody(physicsBody) {
  123. const index = this._physicsBodies.indexOf(physicsBody);
  124. if (index > -1) {
  125. /*const removed =*/ this._physicsBodies.splice(index, 1);
  126. }
  127. }
  128. /**
  129. * @returns an array of bodies added to this engine
  130. */
  131. getBodies() {
  132. return this._physicsBodies;
  133. }
  134. /**
  135. * Gets the current plugin used to run the simulation
  136. * @returns current plugin
  137. */
  138. getPhysicsPlugin() {
  139. return this._physicsPlugin;
  140. }
  141. /**
  142. * Does a raycast in the physics world
  143. * @param from when should the ray start?
  144. * @param to when should the ray end?
  145. * @param result resulting PhysicsRaycastResult
  146. * @param query raycast query object
  147. */
  148. raycastToRef(from, to, result, query) {
  149. this._physicsPlugin.raycast(from, to, result, query);
  150. }
  151. /**
  152. * Does a raycast in the physics world
  153. * @param from when should the ray start?
  154. * @param to when should the ray end?
  155. * @param query raycast query object
  156. * @returns PhysicsRaycastResult
  157. */
  158. raycast(from, to, query) {
  159. const result = new PhysicsRaycastResult();
  160. this._physicsPlugin.raycast(from, to, result, query);
  161. return result;
  162. }
  163. }
  164. //# sourceMappingURL=physicsEngine.js.map