babylon.cannonJSPlugin.js 13 KB

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