babylon.physicsEngine.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. */
  28. public setTimeStep(newTimeStep: number = 1/60) {
  29. this._physicsPlugin.setTimeStep(newTimeStep);
  30. }
  31. public dispose(): void {
  32. this._impostors.forEach(function(impostor) {
  33. impostor.dispose();
  34. })
  35. this._physicsPlugin.dispose();
  36. }
  37. public getPhysicsPluginName(): string {
  38. return this._physicsPlugin.name;
  39. }
  40. // Statics, Legacy support.
  41. /**
  42. * @Deprecated
  43. *
  44. */
  45. public static NoImpostor = PhysicsImpostor.NoImpostor;
  46. public static SphereImpostor = PhysicsImpostor.SphereImpostor;
  47. public static BoxImpostor = PhysicsImpostor.BoxImpostor;
  48. public static PlaneImpostor = PhysicsImpostor.PlaneImpostor;
  49. public static MeshImpostor = PhysicsImpostor.MeshImpostor;
  50. public static CapsuleImpostor = PhysicsImpostor.CapsuleImpostor;
  51. public static ConeImpostor = PhysicsImpostor.ConeImpostor;
  52. public static CylinderImpostor = PhysicsImpostor.CylinderImpostor;
  53. public static ConvexHullImpostor = PhysicsImpostor.ConvexHullImpostor;
  54. public static HeightmapImpostor = PhysicsImpostor.HeightmapImpostor;
  55. public static Epsilon = 0.001;
  56. //new methods and parameters
  57. private _impostors: Array<PhysicsImpostor> = [];
  58. private _joints: Array<PhysicsImpostorJoint> = [];
  59. public addImpostor(impostor: PhysicsImpostor) {
  60. this._impostors.push(impostor);
  61. //if no parent, generate the body
  62. if (!impostor.parent) {
  63. this._physicsPlugin.generatePhysicsBody(impostor);
  64. }
  65. }
  66. public removeImpostor(impostor: PhysicsImpostor) {
  67. var index = this._impostors.indexOf(impostor);
  68. if (index > -1) {
  69. var removed = this._impostors.splice(index, 1);
  70. //Is it needed?
  71. if (removed.length) {
  72. //this will also remove it from the world.
  73. removed[0].physicsBody = null;
  74. }
  75. }
  76. }
  77. public addJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) {
  78. var impostorJoint = {
  79. mainImpostor: mainImpostor,
  80. connectedImpostor: connectedImpostor,
  81. joint: joint
  82. }
  83. this._joints.push(impostorJoint);
  84. this._physicsPlugin.generateJoint(impostorJoint);
  85. }
  86. public removeJoint(mainImpostor: PhysicsImpostor, connectedImpostor: PhysicsImpostor, joint: PhysicsJoint) {
  87. var matchingJoints = this._joints.filter(function(impostorJoint) {
  88. return (impostorJoint.connectedImpostor === connectedImpostor
  89. && impostorJoint.joint === joint
  90. && impostorJoint.mainImpostor === mainImpostor)
  91. });
  92. if (matchingJoints.length) {
  93. this._physicsPlugin.removeJoint(matchingJoints[0]);
  94. //TODO remove it from the list as well
  95. }
  96. }
  97. /**
  98. * Called by the scene. no need to call it.
  99. */
  100. public _step(delta: number) {
  101. //check if any mesh has no body / requires an update
  102. this._impostors.forEach((impostor) => {
  103. if (impostor.isBodyInitRequired()) {
  104. this._physicsPlugin.generatePhysicsBody(impostor);
  105. }
  106. });
  107. if (delta > 0.1) {
  108. delta = 0.1;
  109. } else if (delta <= 0) {
  110. delta = 1.0 / 60.0;
  111. }
  112. this._physicsPlugin.executeStep(delta, this._impostors);
  113. }
  114. public getPhysicsPlugin(): IPhysicsEnginePlugin {
  115. return this._physicsPlugin;
  116. }
  117. public getImpostorWithPhysicsBody(body: any): PhysicsImpostor {
  118. for (var i = 0; i < this._impostors.length; ++i) {
  119. if (this._impostors[i].physicsBody === body) {
  120. return this._impostors[i];
  121. }
  122. }
  123. }
  124. }
  125. export interface IPhysicsEnginePlugin {
  126. world: any;
  127. name: string;
  128. setGravity(gravity: Vector3);
  129. setTimeStep(timeStep: number);
  130. executeStep(delta: number, impostors: Array<PhysicsImpostor>): void; //not forgetting pre and post events
  131. applyImpulse(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3);
  132. applyForce(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3);
  133. generatePhysicsBody(impostor: PhysicsImpostor);
  134. removePhysicsBody(impostor: PhysicsImpostor);
  135. generateJoint(joint: PhysicsImpostorJoint);
  136. removeJoint(joint: PhysicsImpostorJoint)
  137. isSupported(): boolean;
  138. setTransformationFromPhysicsBody(impostor: PhysicsImpostor);
  139. setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion);
  140. setVelocity(impostor: PhysicsImpostor, velocity: Vector3);
  141. sleepBody(impostor: PhysicsImpostor);
  142. wakeUpBody(impostor: PhysicsImpostor);
  143. dispose();
  144. }
  145. }