babylon.physicsEngine.js 10 KB

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