babylon.physicsEngine.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. module BABYLON {
  2. export interface PhysicsImpostorJoint {
  3. mainImpostor: PhysicsImpostor;
  4. connectedImpostor: PhysicsImpostor;
  5. joint: PhysicsJoint;
  6. }
  7. export class PhysicsEngine {
  8. public gravity: Vector3;
  9. constructor(gravity: Nullable<Vector3>, private _physicsPlugin: IPhysicsEnginePlugin = new CannonJSPlugin()) {
  10. if (!this._physicsPlugin.isSupported()) {
  11. throw new Error("Physics Engine " + this._physicsPlugin.name + " cannot be found. "
  12. + "Please make sure it is included.")
  13. }
  14. gravity = gravity || new Vector3(0, -9.807, 0)
  15. this.setGravity(gravity);
  16. this.setTimeStep();
  17. }
  18. public setGravity(gravity: Vector3): void {
  19. this.gravity = gravity;
  20. this._physicsPlugin.setGravity(this.gravity);
  21. }
  22. /**
  23. * Set the time step of the physics engine.
  24. * default is 1/60.
  25. * To slow it down, enter 1/600 for example.
  26. * To speed it up, 1/30
  27. * @param {number} newTimeStep the new timestep to apply to this world.
  28. */
  29. public setTimeStep(newTimeStep: number = 1 / 60) {
  30. this._physicsPlugin.setTimeStep(newTimeStep);
  31. }
  32. /**
  33. * Get the time step of the physics engine.
  34. */
  35. public getTimeStep(): number {
  36. return this._physicsPlugin.getTimeStep();
  37. }
  38. public dispose(): void {
  39. this._impostors.forEach(function (impostor) {
  40. impostor.dispose();
  41. })
  42. this._physicsPlugin.dispose();
  43. }
  44. public getPhysicsPluginName(): string {
  45. return this._physicsPlugin.name;
  46. }
  47. // Statics
  48. public static Epsilon = 0.001;
  49. //new methods and parameters
  50. private _impostors: Array<PhysicsImpostor> = [];
  51. private _joints: Array<PhysicsImpostorJoint> = [];
  52. /**
  53. * Adding a new impostor for the impostor tracking.
  54. * This will be done by the impostor itself.
  55. * @param {PhysicsImpostor} impostor the impostor to add
  56. */
  57. public addImpostor(impostor: PhysicsImpostor) {
  58. impostor.uniqueId = this._impostors.push(impostor);
  59. //if no parent, generate the body
  60. if (!impostor.parent) {
  61. this._physicsPlugin.generatePhysicsBody(impostor);
  62. }
  63. }
  64. /**
  65. * Remove an impostor from the engine.
  66. * This impostor and its mesh will not longer be updated by the physics engine.
  67. * @param {PhysicsImpostor} impostor the impostor to remove
  68. */
  69. public removeImpostor(impostor: PhysicsImpostor) {
  70. var index = this._impostors.indexOf(impostor);
  71. if (index > -1) {
  72. var removed = this._impostors.splice(index, 1);
  73. //Is it needed?
  74. if (removed.length) {
  75. //this will also remove it from the world.
  76. removed[0].physicsBody = null;
  77. }
  78. }
  79. }
  80. /**
  81. * Add a joint to the physics engine
  82. * @param {PhysicsImpostor} mainImpostor the main impostor to which the joint is added.
  83. * @param {PhysicsImpostor} connectedImpostor the impostor that is connected to the main impostor using this joint
  84. * @param {PhysicsJoint} the joint that will connect both impostors.
  85. */
  86. public addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) {
  87. var impostorJoint = {
  88. mainImpostor: mainImpostor,
  89. connectedImpostor: connectedImpostor,
  90. joint: joint
  91. }
  92. joint.physicsPlugin = this._physicsPlugin;
  93. this._joints.push(impostorJoint);
  94. this._physicsPlugin.generateJoint(impostorJoint);
  95. }
  96. public removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) {
  97. var matchingJoints = this._joints.filter(function (impostorJoint) {
  98. return (impostorJoint.connectedImpostor === connectedImpostor
  99. && impostorJoint.joint === joint
  100. && impostorJoint.mainImpostor === mainImpostor)
  101. });
  102. if (matchingJoints.length) {
  103. this._physicsPlugin.removeJoint(matchingJoints[0]);
  104. //TODO remove it from the list as well
  105. }
  106. }
  107. /**
  108. * Called by the scene. no need to call it.
  109. */
  110. public _step(delta: number) {
  111. //check if any mesh has no body / requires an update
  112. this._impostors.forEach((impostor) => {
  113. if (impostor.isBodyInitRequired()) {
  114. this._physicsPlugin.generatePhysicsBody(impostor);
  115. }
  116. });
  117. if (delta > 0.1) {
  118. delta = 0.1;
  119. } else if (delta <= 0) {
  120. delta = 1.0 / 60.0;
  121. }
  122. this._physicsPlugin.executeStep(delta, this._impostors);
  123. }
  124. public getPhysicsPlugin(): IPhysicsEnginePlugin {
  125. return this._physicsPlugin;
  126. }
  127. public getImpostorForPhysicsObject(object: IPhysicsEnabledObject): Nullable<PhysicsImpostor> {
  128. for (var i = 0; i < this._impostors.length; ++i) {
  129. if (this._impostors[i].object === object) {
  130. return this._impostors[i];
  131. }
  132. }
  133. return null;
  134. }
  135. public getImpostorWithPhysicsBody(body: any): Nullable<PhysicsImpostor> {
  136. for (var i = 0; i < this._impostors.length; ++i) {
  137. if (this._impostors[i].physicsBody === body) {
  138. return this._impostors[i];
  139. }
  140. }
  141. return null;
  142. }
  143. }
  144. export interface IPhysicsEnginePlugin {
  145. world: any;
  146. name: string;
  147. setGravity(gravity: Vector3): void;
  148. setTimeStep(timeStep: number): void;
  149. getTimeStep(): number;
  150. executeStep(delta: number, impostors: Array<PhysicsImpostor>): void; //not forgetting pre and post events
  151. applyImpulse(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3): void;
  152. applyForce(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3): void;
  153. generatePhysicsBody(impostor: PhysicsImpostor): void;
  154. removePhysicsBody(impostor: PhysicsImpostor): void;
  155. generateJoint(joint: PhysicsImpostorJoint): void;
  156. removeJoint(joint: PhysicsImpostorJoint): void;
  157. isSupported(): boolean;
  158. setTransformationFromPhysicsBody(impostor: PhysicsImpostor): void;
  159. setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion): void;
  160. setLinearVelocity(impostor: PhysicsImpostor, velocity: Vector3): void;
  161. setAngularVelocity(impostor: PhysicsImpostor, velocity: Vector3): void;
  162. getLinearVelocity(impostor: PhysicsImpostor) : Vector3;
  163. getAngularVelocity(impostor: PhysicsImpostor) : Vector3;
  164. setBodyMass(impostor: PhysicsImpostor, mass: number): void;
  165. getBodyMass(impostor: PhysicsImpostor): number;
  166. getBodyFriction(impostor: PhysicsImpostor): number;
  167. setBodyFriction(impostor: PhysicsImpostor, friction: number): void;
  168. getBodyRestitution(impostor: PhysicsImpostor): number;
  169. setBodyRestitution(impostor: PhysicsImpostor, restitution: number): void;
  170. sleepBody(impostor: PhysicsImpostor): void;
  171. wakeUpBody(impostor: PhysicsImpostor): void;
  172. //Joint Update
  173. updateDistanceJoint(joint: PhysicsJoint, maxDistance:number, minDistance?: number): void;
  174. setMotor(joint: IMotorEnabledJoint, speed: number, maxForce?: number, motorIndex?: number): void;
  175. setLimit(joint: IMotorEnabledJoint, upperLimit: number, lowerLimit?: number, motorIndex?: number): void;
  176. getRadius(impostor: PhysicsImpostor):number;
  177. getBoxSizeToRef(impostor: PhysicsImpostor, result:Vector3): void;
  178. syncMeshWithImpostor(mesh:AbstractMesh, impostor:PhysicsImpostor): void;
  179. dispose(): void;
  180. }
  181. }