babylon.physicsEngine.ts 7.8 KB

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