IPhysicsEngine.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { Nullable } from "../types";
  2. import { Vector3, Quaternion } from "../Maths/math.vector";
  3. import { AbstractMesh } from "../Meshes/abstractMesh";
  4. import { PhysicsImpostor, IPhysicsEnabledObject } from "./physicsImpostor";
  5. import { PhysicsJoint, IMotorEnabledJoint } from "./physicsJoint";
  6. import { PhysicsRaycastResult } from "./physicsRaycastResult";
  7. /**
  8. * Interface used to describe a physics joint
  9. */
  10. export interface PhysicsImpostorJoint {
  11. /** Defines the main impostor to which the joint is linked */
  12. mainImpostor: PhysicsImpostor;
  13. /** Defines the impostor that is connected to the main impostor using this joint */
  14. connectedImpostor: PhysicsImpostor;
  15. /** Defines the joint itself */
  16. joint: PhysicsJoint;
  17. }
  18. /** @hidden */
  19. export interface IPhysicsEnginePlugin {
  20. world: any;
  21. name: string;
  22. setGravity(gravity: Vector3): void;
  23. setTimeStep(timeStep: number): void;
  24. getTimeStep(): number;
  25. executeStep(delta: number, impostors: Array<PhysicsImpostor>): void; //not forgetting pre and post events
  26. applyImpulse(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3): void;
  27. applyForce(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3): void;
  28. generatePhysicsBody(impostor: PhysicsImpostor): void;
  29. removePhysicsBody(impostor: PhysicsImpostor): void;
  30. generateJoint(joint: PhysicsImpostorJoint): void;
  31. removeJoint(joint: PhysicsImpostorJoint): void;
  32. isSupported(): boolean;
  33. setTransformationFromPhysicsBody(impostor: PhysicsImpostor): void;
  34. setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion): void;
  35. setLinearVelocity(impostor: PhysicsImpostor, velocity: Nullable<Vector3>): void;
  36. setAngularVelocity(impostor: PhysicsImpostor, velocity: Nullable<Vector3>): void;
  37. getLinearVelocity(impostor: PhysicsImpostor): Nullable<Vector3>;
  38. getAngularVelocity(impostor: PhysicsImpostor): Nullable<Vector3>;
  39. setBodyMass(impostor: PhysicsImpostor, mass: number): void;
  40. getBodyMass(impostor: PhysicsImpostor): number;
  41. getBodyFriction(impostor: PhysicsImpostor): number;
  42. setBodyFriction(impostor: PhysicsImpostor, friction: number): void;
  43. getBodyRestitution(impostor: PhysicsImpostor): number;
  44. setBodyRestitution(impostor: PhysicsImpostor, restitution: number): void;
  45. getBodyPressure?(impostor: PhysicsImpostor): number;
  46. setBodyPressure?(impostor: PhysicsImpostor, pressure: number): void;
  47. getBodyStiffness?(impostor: PhysicsImpostor): number;
  48. setBodyStiffness?(impostor: PhysicsImpostor, stiffness: number): void;
  49. getBodyVelocityIterations?(impostor: PhysicsImpostor): number;
  50. setBodyVelocityIterations?(impostor: PhysicsImpostor, velocityIterations: number): void;
  51. getBodyPositionIterations?(impostor: PhysicsImpostor): number;
  52. setBodyPositionIterations?(impostor: PhysicsImpostor, positionIterations: number): void;
  53. appendAnchor?(impostor: PhysicsImpostor, otherImpostor: PhysicsImpostor, width: number, height: number, influence: number, noCollisionBetweenLinkedBodies: boolean): void;
  54. appendHook?(impostor: PhysicsImpostor, otherImpostor: PhysicsImpostor, length: number, influence: number, noCollisionBetweenLinkedBodies: boolean): void;
  55. sleepBody(impostor: PhysicsImpostor): void;
  56. wakeUpBody(impostor: PhysicsImpostor): void;
  57. raycast(from: Vector3, to: Vector3): PhysicsRaycastResult;
  58. //Joint Update
  59. updateDistanceJoint(joint: PhysicsJoint, maxDistance: number, minDistance?: number): void;
  60. setMotor(joint: IMotorEnabledJoint, speed: number, maxForce?: number, motorIndex?: number): void;
  61. setLimit(joint: IMotorEnabledJoint, upperLimit: number, lowerLimit?: number, motorIndex?: number): void;
  62. getRadius(impostor: PhysicsImpostor): number;
  63. getBoxSizeToRef(impostor: PhysicsImpostor, result: Vector3): void;
  64. syncMeshWithImpostor(mesh: AbstractMesh, impostor: PhysicsImpostor): void;
  65. dispose(): void;
  66. }
  67. /**
  68. * Interface used to define a physics engine
  69. * @see http://doc.babylonjs.com/how_to/using_the_physics_engine
  70. */
  71. export interface IPhysicsEngine {
  72. /**
  73. * Gets the gravity vector used by the simulation
  74. */
  75. gravity: Vector3;
  76. /**
  77. * Sets the gravity vector used by the simulation
  78. * @param gravity defines the gravity vector to use
  79. */
  80. setGravity(gravity: Vector3): void;
  81. /**
  82. * Set the time step of the physics engine.
  83. * Default is 1/60.
  84. * To slow it down, enter 1/600 for example.
  85. * To speed it up, 1/30
  86. * @param newTimeStep the new timestep to apply to this world.
  87. */
  88. setTimeStep(newTimeStep: number): void;
  89. /**
  90. * Get the time step of the physics engine.
  91. * @returns the current time step
  92. */
  93. getTimeStep(): number;
  94. /**
  95. * Release all resources
  96. */
  97. dispose(): void;
  98. /**
  99. * Gets the name of the current physics plugin
  100. * @returns the name of the plugin
  101. */
  102. getPhysicsPluginName(): string;
  103. /**
  104. * Adding a new impostor for the impostor tracking.
  105. * This will be done by the impostor itself.
  106. * @param impostor the impostor to add
  107. */
  108. addImpostor(impostor: PhysicsImpostor): void;
  109. /**
  110. * Remove an impostor from the engine.
  111. * This impostor and its mesh will not longer be updated by the physics engine.
  112. * @param impostor the impostor to remove
  113. */
  114. removeImpostor(impostor: PhysicsImpostor): void;
  115. /**
  116. * Add a joint to the physics engine
  117. * @param mainImpostor defines the main impostor to which the joint is added.
  118. * @param connectedImpostor defines the impostor that is connected to the main impostor using this joint
  119. * @param joint defines the joint that will connect both impostors.
  120. */
  121. addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint): void;
  122. /**
  123. * Removes a joint from the simulation
  124. * @param mainImpostor defines the impostor used with the joint
  125. * @param connectedImpostor defines the other impostor connected to the main one by the joint
  126. * @param joint defines the joint to remove
  127. */
  128. removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint): void;
  129. /**
  130. * Gets the current plugin used to run the simulation
  131. * @returns current plugin
  132. */
  133. getPhysicsPlugin(): IPhysicsEnginePlugin;
  134. /**
  135. * Gets the list of physic impostors
  136. * @returns an array of PhysicsImpostor
  137. */
  138. getImpostors(): Array<PhysicsImpostor>;
  139. /**
  140. * Gets the impostor for a physics enabled object
  141. * @param object defines the object impersonated by the impostor
  142. * @returns the PhysicsImpostor or null if not found
  143. */
  144. getImpostorForPhysicsObject(object: IPhysicsEnabledObject): Nullable<PhysicsImpostor>;
  145. /**
  146. * Gets the impostor for a physics body object
  147. * @param body defines physics body used by the impostor
  148. * @returns the PhysicsImpostor or null if not found
  149. */
  150. getImpostorWithPhysicsBody(body: any): Nullable<PhysicsImpostor>;
  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. * @returns PhysicsRaycastResult
  156. */
  157. raycast(from: Vector3, to: Vector3): PhysicsRaycastResult;
  158. /**
  159. * Called by the scene. No need to call it.
  160. * @param delta defines the timespam between frames
  161. */
  162. _step(delta: number): void;
  163. }