babylon.oimoJSPlugin.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var OimoJSPlugin = (function () {
  4. function OimoJSPlugin() {
  5. this._registeredMeshes = [];
  6. /**
  7. * Update the body position according to the mesh position
  8. * @param mesh
  9. */
  10. this.updateBodyPosition = function (mesh) {
  11. for (var index = 0; index < this._registeredMeshes.length; index++) {
  12. var registeredMesh = this._registeredMeshes[index];
  13. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  14. var body = registeredMesh.body.body;
  15. mesh.computeWorldMatrix(true);
  16. var center = mesh.getBoundingInfo().boundingBox.center;
  17. body.setPosition(new OIMO.Vec3(center.x, center.y, center.z));
  18. body.setRotation(new OIMO.Vec3(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z));
  19. body.sleeping = false;
  20. return;
  21. }
  22. // Case where the parent has been updated
  23. if (registeredMesh.mesh.parent === mesh) {
  24. mesh.computeWorldMatrix(true);
  25. registeredMesh.mesh.computeWorldMatrix(true);
  26. var absolutePosition = registeredMesh.mesh.getAbsolutePosition();
  27. var absoluteRotation = mesh.rotation;
  28. body = registeredMesh.body.body;
  29. body.setPosition(new OIMO.Vec3(absolutePosition.x, absolutePosition.y, absolutePosition.z));
  30. body.setRotation(new OIMO.Vec3(absoluteRotation.x, absoluteRotation.y, absoluteRotation.z));
  31. body.sleeping = false;
  32. return;
  33. }
  34. }
  35. };
  36. }
  37. OimoJSPlugin.prototype._checkWithEpsilon = function (value) {
  38. return value < BABYLON.PhysicsEngine.Epsilon ? BABYLON.PhysicsEngine.Epsilon : value;
  39. };
  40. OimoJSPlugin.prototype.initialize = function (iterations) {
  41. this._world = new OIMO.World();
  42. this._world.clear();
  43. };
  44. OimoJSPlugin.prototype.setGravity = function (gravity) {
  45. this._world.gravity = gravity;
  46. };
  47. OimoJSPlugin.prototype.registerMesh = function (mesh, impostor, options) {
  48. var body = null;
  49. this.unregisterMesh(mesh);
  50. mesh.computeWorldMatrix(true);
  51. var initialRotation = null;
  52. if (mesh.rotationQuaternion) {
  53. initialRotation = mesh.rotationQuaternion.clone();
  54. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  55. mesh.computeWorldMatrix(true);
  56. }
  57. var bbox = mesh.getBoundingInfo().boundingBox;
  58. // The delta between the mesh position and the mesh bounding box center
  59. var deltaPosition = mesh.position.subtract(bbox.center);
  60. // Transform delta position with the rotation
  61. if (initialRotation) {
  62. var m = new BABYLON.Matrix();
  63. initialRotation.toRotationMatrix(m);
  64. deltaPosition = BABYLON.Vector3.TransformCoordinates(deltaPosition, m);
  65. }
  66. switch (impostor) {
  67. case BABYLON.PhysicsEngine.SphereImpostor:
  68. var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
  69. var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
  70. var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
  71. var size = Math.max(this._checkWithEpsilon(radiusX), this._checkWithEpsilon(radiusY), this._checkWithEpsilon(radiusZ)) / 2;
  72. body = new OIMO.Body({
  73. type: 'sphere',
  74. size: [size],
  75. pos: [bbox.center.x, bbox.center.y, bbox.center.z],
  76. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD],
  77. move: options.mass != 0,
  78. config: [options.mass, options.friction, options.restitution],
  79. world: this._world
  80. });
  81. break;
  82. case BABYLON.PhysicsEngine.PlaneImpostor:
  83. case BABYLON.PhysicsEngine.CylinderImpostor:
  84. case BABYLON.PhysicsEngine.BoxImpostor:
  85. var min = bbox.minimumWorld;
  86. var max = bbox.maximumWorld;
  87. var box = max.subtract(min);
  88. var sizeX = this._checkWithEpsilon(box.x);
  89. var sizeY = this._checkWithEpsilon(box.y);
  90. var sizeZ = this._checkWithEpsilon(box.z);
  91. body = new OIMO.Body({
  92. type: 'box',
  93. size: [sizeX, sizeY, sizeZ],
  94. pos: [bbox.center.x, bbox.center.y, bbox.center.z],
  95. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD],
  96. move: options.mass != 0,
  97. config: [options.mass, options.friction, options.restitution],
  98. world: this._world
  99. });
  100. break;
  101. }
  102. //If quaternion was set as the rotation of the object
  103. if (initialRotation) {
  104. //We have to access the rigid body's properties to set the quaternion.
  105. //The setQuaternion function of Oimo only sets the newOrientation that is only set after an impulse is given or a collision.
  106. body.body.orientation = new OIMO.Quat(initialRotation.w, initialRotation.x, initialRotation.y, initialRotation.z);
  107. //update the internal rotation matrix
  108. body.body.syncShapes();
  109. }
  110. this._registeredMeshes.push({
  111. mesh: mesh,
  112. body: body,
  113. delta: deltaPosition
  114. });
  115. return body;
  116. };
  117. OimoJSPlugin.prototype.registerMeshesAsCompound = function (parts, options) {
  118. var types = [], sizes = [], positions = [], rotations = [];
  119. var initialMesh = parts[0].mesh;
  120. for (var index = 0; index < parts.length; index++) {
  121. var part = parts[index];
  122. var bodyParameters = this._createBodyAsCompound(part, options, initialMesh);
  123. types.push(bodyParameters.type);
  124. sizes.push.apply(sizes, bodyParameters.size);
  125. positions.push.apply(positions, bodyParameters.pos);
  126. rotations.push.apply(rotations, bodyParameters.rot);
  127. }
  128. var body = new OIMO.Body({
  129. type: types,
  130. size: sizes,
  131. pos: positions,
  132. rot: rotations,
  133. move: options.mass != 0,
  134. config: [options.mass, options.friction, options.restitution],
  135. world: this._world
  136. });
  137. this._registeredMeshes.push({
  138. mesh: initialMesh,
  139. body: body
  140. });
  141. return body;
  142. };
  143. OimoJSPlugin.prototype._createBodyAsCompound = function (part, options, initialMesh) {
  144. var bodyParameters = null;
  145. var mesh = part.mesh;
  146. // We need the bounding box/sphere info to compute the physics body
  147. mesh.computeWorldMatrix();
  148. switch (part.impostor) {
  149. case BABYLON.PhysicsEngine.SphereImpostor:
  150. var bbox = mesh.getBoundingInfo().boundingBox;
  151. var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
  152. var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
  153. var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
  154. var size = Math.max(this._checkWithEpsilon(radiusX), this._checkWithEpsilon(radiusY), this._checkWithEpsilon(radiusZ)) / 2;
  155. bodyParameters = {
  156. type: 'sphere',
  157. /* bug with oimo : sphere needs 3 sizes in this case */
  158. size: [size, -1, -1],
  159. pos: [mesh.position.x, mesh.position.y, mesh.position.z],
  160. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD]
  161. };
  162. break;
  163. case BABYLON.PhysicsEngine.PlaneImpostor:
  164. case BABYLON.PhysicsEngine.BoxImpostor:
  165. bbox = mesh.getBoundingInfo().boundingBox;
  166. var min = bbox.minimumWorld;
  167. var max = bbox.maximumWorld;
  168. var box = max.subtract(min);
  169. var sizeX = this._checkWithEpsilon(box.x);
  170. var sizeY = this._checkWithEpsilon(box.y);
  171. var sizeZ = this._checkWithEpsilon(box.z);
  172. var relativePosition = mesh.position;
  173. bodyParameters = {
  174. type: 'box',
  175. size: [sizeX, sizeY, sizeZ],
  176. pos: [relativePosition.x, relativePosition.y, relativePosition.z],
  177. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD]
  178. };
  179. break;
  180. }
  181. return bodyParameters;
  182. };
  183. OimoJSPlugin.prototype.unregisterMesh = function (mesh) {
  184. for (var index = 0; index < this._registeredMeshes.length; index++) {
  185. var registeredMesh = this._registeredMeshes[index];
  186. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  187. if (registeredMesh.body) {
  188. this._world.removeRigidBody(registeredMesh.body.body);
  189. this._unbindBody(registeredMesh.body);
  190. }
  191. this._registeredMeshes.splice(index, 1);
  192. return;
  193. }
  194. }
  195. };
  196. OimoJSPlugin.prototype._unbindBody = function (body) {
  197. for (var index = 0; index < this._registeredMeshes.length; index++) {
  198. var registeredMesh = this._registeredMeshes[index];
  199. if (registeredMesh.body === body) {
  200. registeredMesh.body = null;
  201. }
  202. }
  203. };
  204. OimoJSPlugin.prototype.applyImpulse = function (mesh, force, contactPoint) {
  205. for (var index = 0; index < this._registeredMeshes.length; index++) {
  206. var registeredMesh = this._registeredMeshes[index];
  207. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  208. // Get object mass to have a behaviour similar to cannon.js
  209. var mass = registeredMesh.body.body.massInfo.mass;
  210. // The force is scaled with the mass of object
  211. registeredMesh.body.body.applyImpulse(contactPoint.scale(OIMO.INV_SCALE), force.scale(OIMO.INV_SCALE * mass));
  212. return;
  213. }
  214. }
  215. };
  216. OimoJSPlugin.prototype.createLink = function (mesh1, mesh2, pivot1, pivot2, options) {
  217. var body1 = null, body2 = null;
  218. for (var index = 0; index < this._registeredMeshes.length; index++) {
  219. var registeredMesh = this._registeredMeshes[index];
  220. if (registeredMesh.mesh === mesh1) {
  221. body1 = registeredMesh.body.body;
  222. }
  223. else if (registeredMesh.mesh === mesh2) {
  224. body2 = registeredMesh.body.body;
  225. }
  226. }
  227. if (!body1 || !body2) {
  228. return false;
  229. }
  230. if (!options) {
  231. options = {};
  232. }
  233. new OIMO.Link({
  234. type: options.type,
  235. body1: body1,
  236. body2: body2,
  237. min: options.min,
  238. max: options.max,
  239. axe1: options.axe1,
  240. axe2: options.axe2,
  241. pos1: [pivot1.x, pivot1.y, pivot1.z],
  242. pos2: [pivot2.x, pivot2.y, pivot2.z],
  243. collision: options.collision,
  244. spring: options.spring,
  245. world: this._world
  246. });
  247. return true;
  248. };
  249. OimoJSPlugin.prototype.dispose = function () {
  250. this._world.clear();
  251. while (this._registeredMeshes.length) {
  252. this.unregisterMesh(this._registeredMeshes[0].mesh);
  253. }
  254. };
  255. OimoJSPlugin.prototype.isSupported = function () {
  256. return OIMO !== undefined;
  257. };
  258. OimoJSPlugin.prototype._getLastShape = function (body) {
  259. var lastShape = body.shapes;
  260. while (lastShape.next) {
  261. lastShape = lastShape.next;
  262. }
  263. return lastShape;
  264. };
  265. OimoJSPlugin.prototype.runOneStep = function (time) {
  266. this._world.step();
  267. // Update the position of all registered meshes
  268. var i = this._registeredMeshes.length;
  269. var m;
  270. while (i--) {
  271. var body = this._registeredMeshes[i].body.body;
  272. var mesh = this._registeredMeshes[i].mesh;
  273. var delta = this._registeredMeshes[i].delta;
  274. if (!body.sleeping) {
  275. if (body.shapes.next) {
  276. var parentShape = this._getLastShape(body);
  277. mesh.position.x = parentShape.position.x * OIMO.WORLD_SCALE;
  278. mesh.position.y = parentShape.position.y * OIMO.WORLD_SCALE;
  279. mesh.position.z = parentShape.position.z * OIMO.WORLD_SCALE;
  280. var mtx = BABYLON.Matrix.FromArray(body.getMatrix());
  281. if (!mesh.rotationQuaternion) {
  282. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  283. }
  284. mesh.rotationQuaternion.fromRotationMatrix(mtx);
  285. mesh.computeWorldMatrix();
  286. }
  287. else {
  288. m = body.getMatrix();
  289. mtx = BABYLON.Matrix.FromArray(m);
  290. // Body position
  291. var bodyX = mtx.m[12], bodyY = mtx.m[13], bodyZ = mtx.m[14];
  292. if (!delta) {
  293. mesh.position.x = bodyX;
  294. mesh.position.y = bodyY;
  295. mesh.position.z = bodyZ;
  296. }
  297. else {
  298. mesh.position.x = bodyX + delta.x;
  299. mesh.position.y = bodyY + delta.y;
  300. mesh.position.z = bodyZ + delta.z;
  301. }
  302. if (!mesh.rotationQuaternion) {
  303. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  304. }
  305. BABYLON.Quaternion.FromRotationMatrixToRef(mtx, mesh.rotationQuaternion);
  306. mesh.computeWorldMatrix();
  307. }
  308. }
  309. }
  310. };
  311. return OimoJSPlugin;
  312. })();
  313. BABYLON.OimoJSPlugin = OimoJSPlugin;
  314. })(BABYLON || (BABYLON = {}));
  315. //# sourceMappingURL=babylon.oimoJSPlugin.js.map