IPhysicsEngine.ts 6.8 KB

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