babylon.cannonJSPlugin.js 11 KB

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