babylon.physicsEngine.ts 5.5 KB

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