babylon.oimoJSPlugin.js 16 KB

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