babylon.cannonJSPlugin.js 12 KB

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