babylon.cannonJSPlugin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. for (var i = 0; i < rawVerts.length; i += 3) {
  114. var transformed = BABYLON.Vector3.Zero();
  115. BABYLON.Vector3.TransformNormalFromFloatsToRef(rawVerts[i], rawVerts[i + 1], rawVerts[i + 2], mesh.getWorldMatrix(), transformed);
  116. verts.push(new CANNON.Vec3(transformed.x, transformed.z, transformed.y));
  117. }
  118. for (var j = 0; j < rawFaces.length; j += 3) {
  119. faces.push([rawFaces[j], rawFaces[j + 2], rawFaces[j + 1]]);
  120. }
  121. var shape = new CANNON.ConvexPolyhedron(verts, faces);
  122. if (!options) {
  123. return shape;
  124. }
  125. return this._createRigidBodyFromShape(shape, mesh, options.mass, options.friction, options.restitution);
  126. };
  127. CannonJSPlugin.prototype._addMaterial = function (friction, restitution) {
  128. var index;
  129. var mat;
  130. for (index = 0; index < this._physicsMaterials.length; index++) {
  131. mat = this._physicsMaterials[index];
  132. if (mat.friction === friction && mat.restitution === restitution) {
  133. return mat;
  134. }
  135. }
  136. var currentMat = new CANNON.Material();
  137. currentMat.friction = friction;
  138. currentMat.restitution = restitution;
  139. this._physicsMaterials.push(currentMat);
  140. for (index = 0; index < this._physicsMaterials.length; index++) {
  141. mat = this._physicsMaterials[index];
  142. var contactMaterial = new CANNON.ContactMaterial(mat, currentMat, mat.friction * currentMat.friction, mat.restitution * currentMat.restitution);
  143. contactMaterial.contactEquationStiffness = 1e10;
  144. contactMaterial.contactEquationRegularizationTime = 10;
  145. this._world.addContactMaterial(contactMaterial);
  146. }
  147. return currentMat;
  148. };
  149. CannonJSPlugin.prototype._createRigidBodyFromShape = function (shape, mesh, mass, friction, restitution) {
  150. var initialRotation = null;
  151. if (mesh.rotationQuaternion) {
  152. initialRotation = mesh.rotationQuaternion.clone();
  153. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  154. }
  155. // The delta between the mesh position and the mesh bounding box center
  156. var bbox = mesh.getBoundingInfo().boundingBox;
  157. var deltaPosition = mesh.position.subtract(bbox.center);
  158. var material = this._addMaterial(friction, restitution);
  159. var body = new CANNON.RigidBody(mass, shape, material);
  160. if (initialRotation) {
  161. body.quaternion.x = initialRotation.x;
  162. body.quaternion.z = initialRotation.y;
  163. body.quaternion.y = initialRotation.z;
  164. body.quaternion.w = -initialRotation.w;
  165. }
  166. body.position.set(bbox.center.x, bbox.center.z, bbox.center.y);
  167. this._world.add(body);
  168. this._registeredMeshes.push({ mesh: mesh, body: body, material: material, delta: deltaPosition });
  169. return body;
  170. };
  171. CannonJSPlugin.prototype.registerMeshesAsCompound = function (parts, options) {
  172. var compoundShape = new CANNON.Compound();
  173. for (var index = 0; index < parts.length; index++) {
  174. var mesh = parts[index].mesh;
  175. var shape = this.registerMesh(mesh, parts[index].impostor);
  176. if (index == 0) {
  177. compoundShape.addChild(shape, new CANNON.Vec3(0, 0, 0));
  178. }
  179. else {
  180. compoundShape.addChild(shape, new CANNON.Vec3(mesh.position.x, mesh.position.z, mesh.position.y));
  181. }
  182. }
  183. var initialMesh = parts[0].mesh;
  184. var body = this._createRigidBodyFromShape(compoundShape, initialMesh, options.mass, options.friction, options.restitution);
  185. body.parts = parts;
  186. return body;
  187. };
  188. CannonJSPlugin.prototype._unbindBody = function (body) {
  189. for (var index = 0; index < this._registeredMeshes.length; index++) {
  190. var registeredMesh = this._registeredMeshes[index];
  191. if (registeredMesh.body === body) {
  192. registeredMesh.body = null;
  193. registeredMesh.delta = 0;
  194. }
  195. }
  196. };
  197. CannonJSPlugin.prototype.unregisterMesh = function (mesh) {
  198. for (var index = 0; index < this._registeredMeshes.length; index++) {
  199. var registeredMesh = this._registeredMeshes[index];
  200. if (registeredMesh.mesh === mesh) {
  201. // Remove body
  202. if (registeredMesh.body) {
  203. this._world.remove(registeredMesh.body);
  204. this._unbindBody(registeredMesh.body);
  205. }
  206. this._registeredMeshes.splice(index, 1);
  207. return;
  208. }
  209. }
  210. };
  211. CannonJSPlugin.prototype.applyImpulse = function (mesh, force, contactPoint) {
  212. var worldPoint = new CANNON.Vec3(contactPoint.x, contactPoint.z, contactPoint.y);
  213. var impulse = new CANNON.Vec3(force.x, force.z, force.y);
  214. for (var index = 0; index < this._registeredMeshes.length; index++) {
  215. var registeredMesh = this._registeredMeshes[index];
  216. if (registeredMesh.mesh === mesh) {
  217. registeredMesh.body.applyImpulse(impulse, worldPoint);
  218. return;
  219. }
  220. }
  221. };
  222. CannonJSPlugin.prototype.createLink = function (mesh1, mesh2, pivot1, pivot2) {
  223. var body1 = null, body2 = null;
  224. for (var index = 0; index < this._registeredMeshes.length; index++) {
  225. var registeredMesh = this._registeredMeshes[index];
  226. if (registeredMesh.mesh === mesh1) {
  227. body1 = registeredMesh.body;
  228. }
  229. else if (registeredMesh.mesh === mesh2) {
  230. body2 = registeredMesh.body;
  231. }
  232. }
  233. if (!body1 || !body2) {
  234. return false;
  235. }
  236. 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));
  237. this._world.addConstraint(constraint);
  238. return true;
  239. };
  240. CannonJSPlugin.prototype.dispose = function () {
  241. while (this._registeredMeshes.length) {
  242. this.unregisterMesh(this._registeredMeshes[0].mesh);
  243. }
  244. };
  245. CannonJSPlugin.prototype.isSupported = function () {
  246. return window.CANNON !== undefined;
  247. };
  248. return CannonJSPlugin;
  249. })();
  250. BABYLON.CannonJSPlugin = CannonJSPlugin;
  251. })(BABYLON || (BABYLON = {}));
  252. //# sourceMappingURL=babylon.cannonJSPlugin.js.map