babylon.physicsEngine.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.PhysicsEngine = function (gravity) {
  5. this.gravity = gravity || new BABYLON.Vector3(0, 0, -9.82);
  6. this._world = new CANNON.World();
  7. this._world.broadphase = new CANNON.NaiveBroadphase();
  8. this._world.gravity.set(this.gravity.x, this.gravity.y, this.gravity.z);
  9. this._registeredMeshes = [];
  10. this._physicsMaterials = [];
  11. };
  12. BABYLON.PhysicsEngine.prototype._runOneStep = function (delta) {
  13. if (delta > 1.0) {
  14. delta = 1.0;
  15. }
  16. this._world.step(delta);
  17. for (var index = 0; index < this._registeredMeshes.length; index++) {
  18. var registeredMesh = this._registeredMeshes[index];
  19. registeredMesh.mesh.position.x = registeredMesh.body.position.x;
  20. registeredMesh.mesh.position.y = registeredMesh.body.position.z;
  21. registeredMesh.mesh.position.z = registeredMesh.body.position.y;
  22. if (!registeredMesh.mesh.rotationQuaternion) {
  23. registeredMesh.mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  24. }
  25. registeredMesh.mesh.rotationQuaternion.x = registeredMesh.body.quaternion.x;
  26. registeredMesh.mesh.rotationQuaternion.y = registeredMesh.body.quaternion.z;
  27. registeredMesh.mesh.rotationQuaternion.z = registeredMesh.body.quaternion.y;
  28. registeredMesh.mesh.rotationQuaternion.w = -registeredMesh.body.quaternion.w;
  29. }
  30. };
  31. BABYLON.PhysicsEngine.prototype._addMaterial = function (friction, restitution) {
  32. var index;
  33. var mat;
  34. for (index = 0; index < this._physicsMaterials.length; index++) {
  35. mat = this._physicsMaterials[index];
  36. if (mat.friction === friction && mat.restitution === restitution) {
  37. return mat;
  38. }
  39. }
  40. var currentMat = new CANNON.Material();
  41. currentMat.friction = friction;
  42. currentMat.restitution = restitution;
  43. this._physicsMaterials.push(currentMat);
  44. for (index = 0; index < this._physicsMaterials.length; index++) {
  45. mat = this._physicsMaterials[index];
  46. var contactMaterial = new CANNON.ContactMaterial(mat, currentMat, Math.max(mat.friction, currentMat.friction), mat.restitution * currentMat.restitution);
  47. this._world.addContactMaterial(contactMaterial);
  48. }
  49. return currentMat;
  50. };
  51. BABYLON.PhysicsEngine.prototype._setGravity = function (gravity) {
  52. this._world.gravity.set(this.gravity.x, this.gravity.y, this.gravity.z);
  53. };
  54. BABYLON.PhysicsEngine.prototype._registerMesh = function (mesh, options) {
  55. var shape = null;
  56. this._unregisterMesh(mesh);
  57. mesh.computeWorldMatrix(true);
  58. switch (options.impostor) {
  59. case BABYLON.PhysicsEngine.SphereImpostor:
  60. var bbox = mesh.getBoundingInfo().boundingBox;
  61. var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
  62. var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
  63. var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
  64. shape = new CANNON.Sphere(Math.max(radiusX, radiusY, radiusZ) / 2);
  65. break;
  66. case BABYLON.PhysicsEngine.BoxImpostor:
  67. var bbox = mesh.getBoundingInfo().boundingBox;
  68. var min = bbox.minimumWorld;
  69. var max = bbox.maximumWorld;
  70. var box = max.subtract(min).scale(0.5);
  71. shape = new CANNON.Box(new CANNON.Vec3(box.x, box.z, box.y));
  72. break;
  73. case BABYLON.PhysicsEngine.PlaneImpostor:
  74. shape = new CANNON.Plane();
  75. break;
  76. }
  77. var material = this._addMaterial(options.friction, options.restitution);
  78. var body = new CANNON.RigidBody(options.mass, shape, material);
  79. body.position.set(mesh.position.x, mesh.position.z, mesh.position.y);
  80. this._world.add(body);
  81. this._registeredMeshes.push({ mesh: mesh, body: body, material:material});
  82. };
  83. BABYLON.PhysicsEngine.prototype._unregisterMesh = function (mesh) {
  84. for (var index = 0; index < this._registeredMeshes.length; index++) {
  85. var registeredMesh = this._registeredMeshes[index];
  86. if (registeredMesh.mesh === mesh) {
  87. // Remove
  88. this._world.remove(registeredMesh.body);
  89. this._registeredMeshes.splice(index, 1);
  90. return;
  91. }
  92. }
  93. };
  94. BABYLON.PhysicsEngine.prototype._applyImpulse = function (mesh, force, contactPoint) {
  95. var worldPoint = new CANNON.Vec3(contactPoint.x, contactPoint.z, contactPoint.y);
  96. var impulse = new CANNON.Vec3(force.x, force.z, force.y);
  97. for (var index = 0; index < this._registeredMeshes.length; index++) {
  98. var registeredMesh = this._registeredMeshes[index];
  99. if (registeredMesh.mesh === mesh) {
  100. registeredMesh.body.applyImpulse(impulse, worldPoint);
  101. return;
  102. }
  103. }
  104. };
  105. BABYLON.PhysicsEngine.prototype._createLink = function (mesh1, mesh2, pivot1, pivot2) {
  106. var body1, body2;
  107. for (var index = 0; index < this._registeredMeshes.length; index++) {
  108. var registeredMesh = this._registeredMeshes[index];
  109. if (registeredMesh.mesh === mesh1) {
  110. body1 = registeredMesh.body;
  111. } else if (registeredMesh.mesh === mesh2) {
  112. body2 = registeredMesh.body;
  113. }
  114. }
  115. if (!body1 || !body2) {
  116. return;
  117. }
  118. var constraint = new CANNON.PointToPointConstraint(body1, new CANNON.Vec3(pivot1.x, pivot1.z, pivot1.y), body2, new CANNON.Vec3(pivot2.x, pivot2.z, pivot2.y));
  119. this._world.addConstraint(constraint);
  120. };
  121. BABYLON.PhysicsEngine.prototype.dispose = function () {
  122. while (this._registeredMeshes.length) {
  123. this._unregisterMesh(this._registeredMeshes[0].mesh);
  124. }
  125. };
  126. // Statics
  127. BABYLON.PhysicsEngine.IsSupported = function() {
  128. return CANNON !== undefined;
  129. };
  130. BABYLON.PhysicsEngine.NoImpostor = 0;
  131. BABYLON.PhysicsEngine.SphereImpostor = 1;
  132. BABYLON.PhysicsEngine.BoxImpostor = 2;
  133. BABYLON.PhysicsEngine.PlaneImpostor = 3;
  134. })();