babylon.oimoJSPlugin.js 13 KB

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