babylon.physicsEngine.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.PhysicsEngine = function (gravity, iterations) {
  5. this._world = new CANNON.World();
  6. this._world.broadphase = new CANNON.NaiveBroadphase();
  7. this._world.solver.iterations = iterations;
  8. this._registeredMeshes = [];
  9. this._physicsMaterials = [];
  10. this._setGravity(gravity);
  11. };
  12. BABYLON.PhysicsEngine.prototype._runOneStep = function (delta) {
  13. if (delta > 0.1) {
  14. delta = 0.1;
  15. } else if (delta <= 0) {
  16. delta = 1.0 / 60.0;
  17. }
  18. this._world.step(delta);
  19. for (var index = 0; index < this._registeredMeshes.length; index++) {
  20. var registeredMesh = this._registeredMeshes[index];
  21. if (registeredMesh.isChild) {
  22. continue;
  23. }
  24. registeredMesh.mesh.position.x = registeredMesh.body.position.x;
  25. registeredMesh.mesh.position.y = registeredMesh.body.position.z;
  26. registeredMesh.mesh.position.z = registeredMesh.body.position.y;
  27. if (!registeredMesh.mesh.rotationQuaternion) {
  28. registeredMesh.mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  29. }
  30. registeredMesh.mesh.rotationQuaternion.x = registeredMesh.body.quaternion.x;
  31. registeredMesh.mesh.rotationQuaternion.y = registeredMesh.body.quaternion.z;
  32. registeredMesh.mesh.rotationQuaternion.z = registeredMesh.body.quaternion.y;
  33. registeredMesh.mesh.rotationQuaternion.w = -registeredMesh.body.quaternion.w;
  34. }
  35. };
  36. BABYLON.PhysicsEngine.prototype._addMaterial = function (friction, restitution) {
  37. var index;
  38. var mat;
  39. for (index = 0; index < this._physicsMaterials.length; index++) {
  40. mat = this._physicsMaterials[index];
  41. if (mat.friction === friction && mat.restitution === restitution) {
  42. return mat;
  43. }
  44. }
  45. var currentMat = new CANNON.Material();
  46. currentMat.friction = friction;
  47. currentMat.restitution = restitution;
  48. this._physicsMaterials.push(currentMat);
  49. for (index = 0; index < this._physicsMaterials.length; index++) {
  50. mat = this._physicsMaterials[index];
  51. var contactMaterial = new CANNON.ContactMaterial(mat, currentMat, mat.friction * currentMat.friction, mat.restitution * currentMat.restitution);
  52. contactMaterial.contactEquationStiffness = 1e10;
  53. contactMaterial.contactEquationRegularizationTime = 10;
  54. this._world.addContactMaterial(contactMaterial);
  55. }
  56. return currentMat;
  57. };
  58. BABYLON.PhysicsEngine.prototype._setGravity = function (gravity) {
  59. this.gravity = gravity || new BABYLON.Vector3(0, -9.82, 0);
  60. this._world.gravity.set(this.gravity.x, this.gravity.z, this.gravity.y);
  61. };
  62. BABYLON.PhysicsEngine.prototype._checkWithEpsilon = function (value) {
  63. return value < BABYLON.PhysicsEngine.Epsilon ? BABYLON.PhysicsEngine.Epsilon : value;
  64. };
  65. BABYLON.PhysicsEngine.prototype._registerMesh = function (mesh, options, onlyShape) {
  66. var shape = null;
  67. var initialRotation;
  68. if (mesh.rotationQuaternion) {
  69. initialRotation = mesh.rotationQuaternion.clone();
  70. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  71. }
  72. this._unregisterMesh(mesh);
  73. mesh.computeWorldMatrix(true);
  74. switch (options.impostor) {
  75. case BABYLON.PhysicsEngine.SphereImpostor:
  76. var bbox = mesh.getBoundingInfo().boundingBox;
  77. var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
  78. var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
  79. var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
  80. shape = new CANNON.Sphere(Math.max(this._checkWithEpsilon(radiusX), this._checkWithEpsilon(radiusY), this._checkWithEpsilon(radiusZ)) / 2);
  81. break;
  82. case BABYLON.PhysicsEngine.BoxImpostor:
  83. var bbox = mesh.getBoundingInfo().boundingBox;
  84. var min = bbox.minimumWorld;
  85. var max = bbox.maximumWorld;
  86. var box = max.subtract(min).scale(0.5);
  87. shape = new CANNON.Box(new CANNON.Vec3(this._checkWithEpsilon(box.x), this._checkWithEpsilon(box.z), this._checkWithEpsilon(box.y)));
  88. break;
  89. case BABYLON.PhysicsEngine.PlaneImpostor:
  90. shape = new CANNON.Plane();
  91. break;
  92. case BABYLON.PhysicsEngine.MeshImpostor:
  93. var rawVerts = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  94. var rawFaces = mesh.getIndices();
  95. var verts = [], faces = [];
  96. mesh.computeWorldMatrix(true);
  97. // Get vertices
  98. for (var i = 0; i < rawVerts.length; i += 3) {
  99. var transformed = BABYLON.Vector3.Zero();
  100. BABYLON.Vector3.TransformNormalFromFloatsToRef(rawVerts[i], rawVerts[i + 1], rawVerts[i + 2], mesh.getWorldMatrix(), transformed);
  101. verts.push(new CANNON.Vec3(transformed.x, transformed.z, transformed.y));
  102. }
  103. // Get faces
  104. for (var j = 0; j < rawFaces.length; j += 3) {
  105. faces.push([rawFaces[j], rawFaces[j + 2], rawFaces[j + 1]]);
  106. }
  107. // Construct polyhedron
  108. shape = new CANNON.ConvexPolyhedron(verts, faces);
  109. break;
  110. }
  111. if (onlyShape) {
  112. return shape;
  113. }
  114. var material = this._addMaterial(options.friction, options.restitution);
  115. var body = new CANNON.RigidBody(options.mass, shape, material);
  116. if (initialRotation) {
  117. body.quaternion.x = initialRotation.x;
  118. body.quaternion.z = initialRotation.y;
  119. body.quaternion.y = initialRotation.z;
  120. body.quaternion.w = -initialRotation.w;
  121. }
  122. body.position.set(mesh.position.x, mesh.position.z, mesh.position.y);
  123. this._world.add(body);
  124. this._registeredMeshes.push({ mesh: mesh, body: body, material: material });
  125. return body;
  126. };
  127. BABYLON.PhysicsEngine.prototype._registerCompound = function (options) {
  128. var compoundShape = new CANNON.Compound();
  129. var initialMesh = options.parts[0].mesh;
  130. var initialPosition = initialMesh.position;
  131. for (var index = 0; index < options.parts.length; index++) {
  132. var mesh = options.parts[index].mesh;
  133. var shape = this._registerMesh(mesh, options.parts[index], true);
  134. if (index == 0) { // Parent
  135. compoundShape.addChild(shape, new CANNON.Vec3(0, 0, 0));
  136. } else {
  137. compoundShape.addChild(shape, new CANNON.Vec3(mesh.position.x, mesh.position.z, mesh.position.y));
  138. }
  139. }
  140. var material = this._addMaterial(options.friction, options.restitution);
  141. var body = new CANNON.RigidBody(options.mass, compoundShape, material);
  142. body.position.set(initialPosition.x, initialPosition.z, initialPosition.y);
  143. this._world.add(body);
  144. for (var index = 0; index < options.parts.length; index++) {
  145. var mesh = options.parts[index].mesh;
  146. this._registeredMeshes.push({ mesh: mesh, body: body, material: material, isChild: index != 0 });
  147. }
  148. body.parts = options.parts;
  149. return body;
  150. };
  151. BABYLON.PhysicsEngine.prototype._unbindBody = function (body) {
  152. for (var index = 0; index < this._registeredMeshes.length; index++) {
  153. var registeredMesh = this._registeredMeshes[index];
  154. if (registeredMesh.body === body) {
  155. registeredMesh.body = null;
  156. }
  157. }
  158. };
  159. BABYLON.PhysicsEngine.prototype._unregisterMesh = function (mesh) {
  160. for (var index = 0; index < this._registeredMeshes.length; index++) {
  161. var registeredMesh = this._registeredMeshes[index];
  162. if (registeredMesh.mesh === mesh) {
  163. // Remove body
  164. if (registeredMesh.body) {
  165. this._world.remove(registeredMesh.body);
  166. this._unbindBody(registeredMesh.body);
  167. }
  168. this._registeredMeshes.splice(index, 1);
  169. return;
  170. }
  171. }
  172. };
  173. BABYLON.PhysicsEngine.prototype._applyImpulse = function (mesh, force, contactPoint) {
  174. var worldPoint = new CANNON.Vec3(contactPoint.x, contactPoint.z, contactPoint.y);
  175. var impulse = new CANNON.Vec3(force.x, force.z, force.y);
  176. for (var index = 0; index < this._registeredMeshes.length; index++) {
  177. var registeredMesh = this._registeredMeshes[index];
  178. if (registeredMesh.mesh === mesh) {
  179. registeredMesh.body.applyImpulse(impulse, worldPoint);
  180. return;
  181. }
  182. }
  183. };
  184. BABYLON.PhysicsEngine.prototype._createLink = function (mesh1, mesh2, pivot1, pivot2) {
  185. var body1, body2;
  186. for (var index = 0; index < this._registeredMeshes.length; index++) {
  187. var registeredMesh = this._registeredMeshes[index];
  188. if (registeredMesh.mesh === mesh1) {
  189. body1 = registeredMesh.body;
  190. } else if (registeredMesh.mesh === mesh2) {
  191. body2 = registeredMesh.body;
  192. }
  193. }
  194. if (!body1 || !body2) {
  195. return;
  196. }
  197. 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));
  198. this._world.addConstraint(constraint);
  199. };
  200. BABYLON.PhysicsEngine.prototype.dispose = function () {
  201. while (this._registeredMeshes.length) {
  202. this._unregisterMesh(this._registeredMeshes[0].mesh);
  203. }
  204. };
  205. // Statics
  206. BABYLON.PhysicsEngine.IsSupported = function () {
  207. return window.CANNON !== undefined;
  208. };
  209. BABYLON.PhysicsEngine.NoImpostor = 0;
  210. BABYLON.PhysicsEngine.SphereImpostor = 1;
  211. BABYLON.PhysicsEngine.BoxImpostor = 2;
  212. BABYLON.PhysicsEngine.PlaneImpostor = 3;
  213. BABYLON.PhysicsEngine.CompoundImpostor = 4;
  214. BABYLON.PhysicsEngine.MeshImpostor = 4;
  215. BABYLON.PhysicsEngine.Epsilon = 0.001;
  216. })();