babylon.physicsEngine.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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?: 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. public dispose(): void {
  33. this._impostors.forEach(function (impostor) {
  34. impostor.dispose();
  35. })
  36. this._physicsPlugin.dispose();
  37. }
  38. public getPhysicsPluginName(): string {
  39. return this._physicsPlugin.name;
  40. }
  41. // Statics
  42. public static Epsilon = 0.001;
  43. //new methods and parameters
  44. private _impostors: Array<PhysicsImpostor> = [];
  45. private _joints: Array<PhysicsImpostorJoint> = [];
  46. /**
  47. * Adding a new impostor for the impostor tracking.
  48. * This will be done by the impostor itself.
  49. * @param {PhysicsImpostor} impostor the impostor to add
  50. */
  51. public addImpostor(impostor: PhysicsImpostor) {
  52. impostor.uniqueId = this._impostors.push(impostor);
  53. //if no parent, generate the body
  54. if (!impostor.parent) {
  55. this._physicsPlugin.generatePhysicsBody(impostor);
  56. }
  57. }
  58. /**
  59. * Remove an impostor from the engine.
  60. * This impostor and its mesh will not longer be updated by the physics engine.
  61. * @param {PhysicsImpostor} impostor the impostor to remove
  62. */
  63. public removeImpostor(impostor: PhysicsImpostor) {
  64. var index = this._impostors.indexOf(impostor);
  65. if (index > -1) {
  66. var removed = this._impostors.splice(index, 1);
  67. //Is it needed?
  68. if (removed.length) {
  69. //this will also remove it from the world.
  70. removed[0].physicsBody = null;
  71. }
  72. }
  73. }
  74. /**
  75. * Add a joint to the physics engine
  76. * @param {PhysicsImpostor} mainImpostor the main impostor to which the joint is added.
  77. * @param {PhysicsImpostor} connectedImpostor the impostor that is connected to the main impostor using this joint
  78. * @param {PhysicsJoint} the joint that will connect both impostors.
  79. */
  80. public addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) {
  81. var impostorJoint = {
  82. mainImpostor: mainImpostor,
  83. connectedImpostor: connectedImpostor,
  84. joint: joint
  85. }
  86. joint.physicsPlugin = this._physicsPlugin;
  87. this._joints.push(impostorJoint);
  88. this._physicsPlugin.generateJoint(impostorJoint);
  89. }
  90. public removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) {
  91. var matchingJoints = this._joints.filter(function (impostorJoint) {
  92. return (impostorJoint.connectedImpostor === connectedImpostor
  93. && impostorJoint.joint === joint
  94. && impostorJoint.mainImpostor === mainImpostor)
  95. });
  96. if (matchingJoints.length) {
  97. this._physicsPlugin.removeJoint(matchingJoints[0]);
  98. //TODO remove it from the list as well
  99. }
  100. }
  101. /**
  102. * Called by the scene. no need to call it.
  103. */
  104. public _step(delta: number) {
  105. //check if any mesh has no body / requires an update
  106. this._impostors.forEach((impostor) => {
  107. if (impostor.isBodyInitRequired()) {
  108. this._physicsPlugin.generatePhysicsBody(impostor);
  109. }
  110. });
  111. if (delta > 0.1) {
  112. delta = 0.1;
  113. } else if (delta <= 0) {
  114. delta = 1.0 / 60.0;
  115. }
  116. this._physicsPlugin.executeStep(delta, this._impostors);
  117. }
  118. public getPhysicsPlugin(): IPhysicsEnginePlugin {
  119. return this._physicsPlugin;
  120. }
  121. public getImpostorForPhysicsObject(object: IPhysicsEnabledObject) {
  122. for (var i = 0; i < this._impostors.length; ++i) {
  123. if (this._impostors[i].object === object) {
  124. return this._impostors[i];
  125. }
  126. }
  127. }
  128. public getImpostorWithPhysicsBody(body: any): PhysicsImpostor {
  129. for (var i = 0; i < this._impostors.length; ++i) {
  130. if (this._impostors[i].physicsBody === body) {
  131. return this._impostors[i];
  132. }
  133. }
  134. }
  135. }
  136. export interface IPhysicsEnginePlugin {
  137. world: any;
  138. name: string;
  139. setGravity(gravity: Vector3);
  140. setTimeStep(timeStep: number);
  141. executeStep(delta: number, impostors: Array<PhysicsImpostor>): void; //not forgetting pre and post events
  142. applyImpulse(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3);
  143. applyForce(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3);
  144. generatePhysicsBody(impostor: PhysicsImpostor);
  145. removePhysicsBody(impostor: PhysicsImpostor);
  146. generateJoint(joint: PhysicsImpostorJoint);
  147. removeJoint(joint: PhysicsImpostorJoint)
  148. isSupported(): boolean;
  149. setTransformationFromPhysicsBody(impostor: PhysicsImpostor);
  150. setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion);
  151. setLinearVelocity(impostor: PhysicsImpostor, velocity: Vector3);
  152. setAngularVelocity(impostor: PhysicsImpostor, velocity: Vector3);
  153. getLinearVelocity(impostor: PhysicsImpostor) : Vector3;
  154. getAngularVelocity(impostor: PhysicsImpostor) : Vector3;
  155. setBodyMass(impostor: PhysicsImpostor, mass: number);
  156. sleepBody(impostor: PhysicsImpostor);
  157. wakeUpBody(impostor: PhysicsImpostor);
  158. //Joint Update
  159. updateDistanceJoint(joint: PhysicsJoint, maxDistance:number, minDistance?: number);
  160. setMotor(joint: IMotorEnabledJoint, speed: number, maxForce?: number, motorIndex?: number);
  161. setLimit(joint: IMotorEnabledJoint, upperLimit: number, lowerLimit?: number, motorIndex?: number);
  162. dispose();
  163. }
  164. }