babylon.cannonJSPlugin.js 13 KB

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