babylon.physicsEngine.js 11 KB

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