babylon.oimoJSPlugin.js 14 KB

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