physicsEngine.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import type { Nullable } from "../../types";
  2. import { Vector3 } from "../../Maths/math.vector";
  3. import type { IPhysicsEnginePlugin } from "./IPhysicsEnginePlugin";
  4. import type { IPhysicsEngine } from "../IPhysicsEngine";
  5. import type { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor";
  6. import type { PhysicsJoint } from "./physicsJoint";
  7. import type { PhysicsRaycastResult } from "../physicsRaycastResult";
  8. /**
  9. * Class used to control physics engine
  10. * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
  11. */
  12. export declare class PhysicsEngine implements IPhysicsEngine {
  13. private _physicsPlugin;
  14. /**
  15. * Global value used to control the smallest number supported by the simulation
  16. */
  17. private _impostors;
  18. private _joints;
  19. private _subTimeStep;
  20. private _uniqueIdCounter;
  21. /**
  22. * Gets the gravity vector used by the simulation
  23. */
  24. gravity: Vector3;
  25. /**
  26. *
  27. * @returns version
  28. */
  29. getPluginVersion(): number;
  30. /**
  31. * @virtual
  32. * Factory used to create the default physics plugin.
  33. * @returns The default physics plugin
  34. */
  35. static DefaultPluginFactory(): IPhysicsEnginePlugin;
  36. /**
  37. * Creates a new Physics Engine
  38. * @param gravity defines the gravity vector used by the simulation
  39. * @param _physicsPlugin defines the plugin to use (CannonJS by default)
  40. */
  41. constructor(gravity: Nullable<Vector3>, _physicsPlugin?: IPhysicsEnginePlugin);
  42. /**
  43. * Sets the gravity vector used by the simulation
  44. * @param gravity defines the gravity vector to use
  45. */
  46. setGravity(gravity: Vector3): void;
  47. /**
  48. * Set the time step of the physics engine.
  49. * Default is 1/60.
  50. * To slow it down, enter 1/600 for example.
  51. * To speed it up, 1/30
  52. * @param newTimeStep defines the new timestep to apply to this world.
  53. */
  54. setTimeStep(newTimeStep?: number): void;
  55. /**
  56. * Get the time step of the physics engine.
  57. * @returns the current time step
  58. */
  59. getTimeStep(): number;
  60. /**
  61. * Set the sub time step of the physics engine.
  62. * Default is 0 meaning there is no sub steps
  63. * To increase physics resolution precision, set a small value (like 1 ms)
  64. * @param subTimeStep defines the new sub timestep used for physics resolution.
  65. */
  66. setSubTimeStep(subTimeStep?: number): void;
  67. /**
  68. * Get the sub time step of the physics engine.
  69. * @returns the current sub time step
  70. */
  71. getSubTimeStep(): number;
  72. /**
  73. * Release all resources
  74. */
  75. dispose(): void;
  76. /**
  77. * Gets the name of the current physics plugin
  78. * @returns the name of the plugin
  79. */
  80. getPhysicsPluginName(): string;
  81. /**
  82. * Adding a new impostor for the impostor tracking.
  83. * This will be done by the impostor itself.
  84. * @param impostor the impostor to add
  85. */
  86. addImpostor(impostor: PhysicsImpostor): void;
  87. /**
  88. * Remove an impostor from the engine.
  89. * This impostor and its mesh will not longer be updated by the physics engine.
  90. * @param impostor the impostor to remove
  91. */
  92. removeImpostor(impostor: PhysicsImpostor): void;
  93. /**
  94. * Add a joint to the physics engine
  95. * @param mainImpostor defines the main impostor to which the joint is added.
  96. * @param connectedImpostor defines the impostor that is connected to the main impostor using this joint
  97. * @param joint defines the joint that will connect both impostors.
  98. */
  99. addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint): void;
  100. /**
  101. * Removes a joint from the simulation
  102. * @param mainImpostor defines the impostor used with the joint
  103. * @param connectedImpostor defines the other impostor connected to the main one by the joint
  104. * @param joint defines the joint to remove
  105. */
  106. removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint): void;
  107. /**
  108. * Called by the scene. No need to call it.
  109. * @param delta defines the timespan between frames
  110. */
  111. _step(delta: number): void;
  112. /**
  113. * Gets the current plugin used to run the simulation
  114. * @returns current plugin
  115. */
  116. getPhysicsPlugin(): IPhysicsEnginePlugin;
  117. /**
  118. * Gets the list of physic impostors
  119. * @returns an array of PhysicsImpostor
  120. */
  121. getImpostors(): Array<PhysicsImpostor>;
  122. /**
  123. * Gets the impostor for a physics enabled object
  124. * @param object defines the object impersonated by the impostor
  125. * @returns the PhysicsImpostor or null if not found
  126. */
  127. getImpostorForPhysicsObject(object: IPhysicsEnabledObject): Nullable<PhysicsImpostor>;
  128. /**
  129. * Gets the impostor for a physics body object
  130. * @param body defines physics body used by the impostor
  131. * @returns the PhysicsImpostor or null if not found
  132. */
  133. getImpostorWithPhysicsBody(body: any): Nullable<PhysicsImpostor>;
  134. /**
  135. * Does a raycast in the physics world
  136. * @param from when should the ray start?
  137. * @param to when should the ray end?
  138. * @returns PhysicsRaycastResult
  139. */
  140. raycast(from: Vector3, to: Vector3): PhysicsRaycastResult;
  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. * @returns true if the ray hits an impostor, else false
  147. */
  148. raycastToRef(from: Vector3, to: Vector3, result: PhysicsRaycastResult): void;
  149. }