babylon.oimoJSPlugin.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var OimoJSPlugin = (function () {
  4. function OimoJSPlugin(iterations) {
  5. this.name = "OimoJSPlugin";
  6. this._tmpImpostorsArray = [];
  7. this._tmpPositionVector = BABYLON.Vector3.Zero();
  8. this.world = new OIMO.World(1 / 60, 2, iterations, true);
  9. this.world.clear();
  10. //making sure no stats are calculated
  11. this.world.isNoStat = true;
  12. }
  13. OimoJSPlugin.prototype.setGravity = function (gravity) {
  14. this.world.gravity.copy(gravity);
  15. };
  16. OimoJSPlugin.prototype.setTimeStep = function (timeStep) {
  17. this.world.timeStep = timeStep;
  18. };
  19. OimoJSPlugin.prototype.executeStep = function (delta, impostors) {
  20. var _this = this;
  21. impostors.forEach(function (impostor) {
  22. impostor.beforeStep();
  23. });
  24. this.world.step();
  25. impostors.forEach(function (impostor) {
  26. impostor.afterStep();
  27. //update the ordered impostors array
  28. _this._tmpImpostorsArray[impostor.mesh.uniqueId] = impostor;
  29. });
  30. //check for collisions
  31. var contact = this.world.contacts;
  32. while (contact !== null) {
  33. if (contact.touching && !contact.body1.sleeping && !contact.body2.sleeping) {
  34. contact = contact.next;
  35. continue;
  36. }
  37. //is this body colliding with any other? get the impostor
  38. var mainImpostor = this._tmpImpostorsArray[+contact.body1.name];
  39. var collidingImpostor = this._tmpImpostorsArray[+contact.body2.name];
  40. if (!mainImpostor || !collidingImpostor) {
  41. contact = contact.next;
  42. continue;
  43. }
  44. mainImpostor.onCollide({ body: collidingImpostor.physicsBody });
  45. collidingImpostor.onCollide({ body: mainImpostor.physicsBody });
  46. contact = contact.next;
  47. }
  48. };
  49. OimoJSPlugin.prototype.applyImpulse = function (impostor, force, contactPoint) {
  50. var mass = impostor.physicsBody.massInfo.mass;
  51. impostor.physicsBody.applyImpulse(contactPoint.scale(OIMO.INV_SCALE), force.scale(OIMO.INV_SCALE * mass));
  52. };
  53. OimoJSPlugin.prototype.applyForce = function (impostor, force, contactPoint) {
  54. BABYLON.Tools.Warn("Oimo doesn't support applying force. Using impule instead.");
  55. this.applyImpulse(impostor, force, contactPoint);
  56. };
  57. OimoJSPlugin.prototype.generatePhysicsBody = function (impostor) {
  58. var _this = this;
  59. //parent-child relationship. Does this impostor has a parent impostor?
  60. if (impostor.parent) {
  61. if (impostor.physicsBody) {
  62. this.removePhysicsBody(impostor);
  63. //TODO is that needed?
  64. impostor.forceUpdate();
  65. }
  66. return;
  67. }
  68. impostor.mesh.computeWorldMatrix(true);
  69. if (impostor.isBodyInitRequired()) {
  70. if (!impostor.mesh.rotationQuaternion) {
  71. impostor.mesh.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(impostor.mesh.rotation.y, impostor.mesh.rotation.x, impostor.mesh.rotation.z);
  72. }
  73. var bodyConfig = {
  74. name: impostor.mesh.uniqueId,
  75. //Oimo must have mass, also for static objects.
  76. config: [impostor.getParam("mass") || 1, impostor.getParam("friction"), impostor.getParam("restitution")],
  77. size: [],
  78. type: [],
  79. pos: [],
  80. rot: [],
  81. move: impostor.getParam("mass") !== 0,
  82. //Supporting older versions of Oimo
  83. world: this.world
  84. };
  85. var impostors = [impostor];
  86. function addToArray(parent) {
  87. parent.getChildMeshes().forEach(function (m) {
  88. if (m.physicsImpostor) {
  89. impostors.push(m.physicsImpostor);
  90. m.physicsImpostor._init();
  91. }
  92. });
  93. }
  94. addToArray(impostor.mesh);
  95. function checkWithEpsilon(value) {
  96. return Math.max(value, BABYLON.PhysicsEngine.Epsilon);
  97. }
  98. impostors.forEach(function (i) {
  99. //get the correct bounding box
  100. var oldQuaternion = i.mesh.rotationQuaternion;
  101. var rot = new OIMO.Euler().setFromQuaternion({ x: impostor.mesh.rotationQuaternion.x, y: impostor.mesh.rotationQuaternion.y, z: impostor.mesh.rotationQuaternion.z, s: impostor.mesh.rotationQuaternion.w });
  102. i.mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  103. i.mesh.computeWorldMatrix(true);
  104. var bbox = i.mesh.getBoundingInfo().boundingBox;
  105. if (i === impostor) {
  106. impostor.mesh.position.subtractToRef(impostor.mesh.getBoundingInfo().boundingBox.center, _this._tmpPositionVector);
  107. //Can also use Array.prototype.push.apply
  108. bodyConfig.pos.push(bbox.center.x);
  109. bodyConfig.pos.push(bbox.center.y);
  110. bodyConfig.pos.push(bbox.center.z);
  111. //tmp solution
  112. bodyConfig.rot.push(rot.x / (OIMO.degtorad || OIMO.TO_RAD));
  113. bodyConfig.rot.push(rot.y / (OIMO.degtorad || OIMO.TO_RAD));
  114. bodyConfig.rot.push(rot.z / (OIMO.degtorad || OIMO.TO_RAD));
  115. }
  116. else {
  117. bodyConfig.pos.push(i.mesh.position.x);
  118. bodyConfig.pos.push(i.mesh.position.y);
  119. bodyConfig.pos.push(i.mesh.position.z);
  120. //tmp solution until https://github.com/lo-th/Oimo.js/pull/37 is merged
  121. bodyConfig.rot.push(0);
  122. bodyConfig.rot.push(0);
  123. bodyConfig.rot.push(0);
  124. }
  125. // register mesh
  126. switch (i.type) {
  127. case BABYLON.PhysicsEngine.SphereImpostor:
  128. var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
  129. var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
  130. var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
  131. var size = Math.max(checkWithEpsilon(radiusX), checkWithEpsilon(radiusY), checkWithEpsilon(radiusZ)) / 2;
  132. bodyConfig.type.push('sphere');
  133. //due to the way oimo works with compounds, add 3 times
  134. bodyConfig.size.push(size);
  135. bodyConfig.size.push(size);
  136. bodyConfig.size.push(size);
  137. break;
  138. case BABYLON.PhysicsEngine.PlaneImpostor:
  139. //TODO Oimo now supports cylinder!
  140. case BABYLON.PhysicsEngine.CylinderImpostor:
  141. case BABYLON.PhysicsEngine.BoxImpostor:
  142. default:
  143. var min = bbox.minimumWorld;
  144. var max = bbox.maximumWorld;
  145. var box = max.subtract(min);
  146. var sizeX = checkWithEpsilon(box.x);
  147. var sizeY = checkWithEpsilon(box.y);
  148. var sizeZ = checkWithEpsilon(box.z);
  149. bodyConfig.type.push('box');
  150. bodyConfig.size.push(sizeX);
  151. bodyConfig.size.push(sizeY);
  152. bodyConfig.size.push(sizeZ);
  153. break;
  154. }
  155. //actually not needed, but hey...
  156. i.mesh.rotationQuaternion = oldQuaternion;
  157. });
  158. impostor.physicsBody = new OIMO.Body(bodyConfig).body; //this.world.add(bodyConfig);
  159. }
  160. else {
  161. this._tmpPositionVector.copyFromFloats(0, 0, 0);
  162. }
  163. impostor.setDeltaPosition(this._tmpPositionVector);
  164. //this._tmpPositionVector.addInPlace(impostor.mesh.getBoundingInfo().boundingBox.center);
  165. //this.setPhysicsBodyTransformation(impostor, this._tmpPositionVector, impostor.mesh.rotationQuaternion);
  166. };
  167. OimoJSPlugin.prototype.removePhysicsBody = function (impostor) {
  168. //impostor.physicsBody.dispose();
  169. //Same as : (older oimo versions)
  170. this.world.removeRigidBody(impostor.physicsBody);
  171. };
  172. OimoJSPlugin.prototype.generateJoint = function (impostorJoint) {
  173. var mainBody = impostorJoint.mainImpostor.physicsBody;
  174. var connectedBody = impostorJoint.connectedImpostor.physicsBody;
  175. if (!mainBody || !connectedBody) {
  176. return;
  177. }
  178. var jointData = impostorJoint.joint.jointData;
  179. var options = jointData.nativeParams || {};
  180. var type;
  181. var nativeJointData = {
  182. body1: mainBody,
  183. body2: connectedBody,
  184. axe1: options.axe1 || (jointData.mainAxis ? jointData.mainAxis.asArray() : null),
  185. axe2: options.axe2 || (jointData.connectedAxis ? jointData.connectedAxis.asArray() : null),
  186. pos1: options.pos1 || (jointData.mainPivot ? jointData.mainPivot.asArray() : null),
  187. pos2: options.pos2 || (jointData.connectedPivot ? jointData.connectedPivot.asArray() : null),
  188. min: options.min,
  189. max: options.max,
  190. collision: options.collision || jointData.collision,
  191. spring: options.spring,
  192. //supporting older version of Oimo
  193. world: this.world
  194. };
  195. switch (impostorJoint.joint.type) {
  196. case BABYLON.PhysicsJoint.BallAndSocketJoint:
  197. type = "jointBall";
  198. break;
  199. case BABYLON.PhysicsJoint.SpringJoint:
  200. BABYLON.Tools.Warn("Oimo.js doesn't support Spring Constraint. Simulating using DistanceJoint instead");
  201. var springData = jointData;
  202. nativeJointData.min = springData.length || nativeJointData.min;
  203. //Max should also be set, just make sure it is at least min
  204. nativeJointData.max = Math.max(nativeJointData.min, nativeJointData.max);
  205. case BABYLON.PhysicsJoint.DistanceJoint:
  206. type = "jointDistance";
  207. nativeJointData.max = jointData.maxDistance;
  208. break;
  209. case BABYLON.PhysicsJoint.PrismaticJoint:
  210. type = "jointPrisme";
  211. break;
  212. case BABYLON.PhysicsJoint.SliderJoint:
  213. type = "jointSlide";
  214. break;
  215. case BABYLON.PhysicsJoint.WheelJoint:
  216. type = "jointWheel";
  217. break;
  218. case BABYLON.PhysicsJoint.HingeJoint:
  219. default:
  220. type = "jointHinge";
  221. break;
  222. }
  223. nativeJointData.type = type;
  224. impostorJoint.joint.physicsJoint = new OIMO.Link(nativeJointData).joint; //this.world.add(nativeJointData);
  225. };
  226. OimoJSPlugin.prototype.removeJoint = function (joint) {
  227. joint.joint.physicsJoint.dispose();
  228. };
  229. OimoJSPlugin.prototype.isSupported = function () {
  230. return OIMO !== undefined;
  231. };
  232. OimoJSPlugin.prototype.setTransformationFromPhysicsBody = function (impostor) {
  233. if (!impostor.physicsBody.sleeping) {
  234. //TODO check that
  235. if (impostor.physicsBody.shapes.next) {
  236. var parentShape = this._getLastShape(impostor.physicsBody);
  237. impostor.mesh.position.x = parentShape.position.x * OIMO.WORLD_SCALE;
  238. impostor.mesh.position.y = parentShape.position.y * OIMO.WORLD_SCALE;
  239. impostor.mesh.position.z = parentShape.position.z * OIMO.WORLD_SCALE;
  240. }
  241. else {
  242. impostor.mesh.position.copyFrom(impostor.physicsBody.getPosition());
  243. }
  244. impostor.mesh.rotationQuaternion.copyFrom(impostor.physicsBody.getQuaternion());
  245. }
  246. };
  247. OimoJSPlugin.prototype.setPhysicsBodyTransformation = function (impostor, newPosition, newRotation) {
  248. var body = impostor.physicsBody;
  249. body.position.init(newPosition.x * OIMO.INV_SCALE, newPosition.y * OIMO.INV_SCALE, newPosition.z * OIMO.INV_SCALE);
  250. body.orientation.init(newRotation.w, newRotation.x, newRotation.y, newRotation.z);
  251. body.syncShapes();
  252. body.awake();
  253. };
  254. OimoJSPlugin.prototype._getLastShape = function (body) {
  255. var lastShape = body.shapes;
  256. while (lastShape.next) {
  257. lastShape = lastShape.next;
  258. }
  259. return lastShape;
  260. };
  261. OimoJSPlugin.prototype.setLinearVelocity = function (impostor, velocity) {
  262. impostor.physicsBody.linearVelocity.init(velocity.x, velocity.y, velocity.z);
  263. };
  264. OimoJSPlugin.prototype.setAngularVelocity = function (impostor, velocity) {
  265. impostor.physicsBody.angularVelocity.init(velocity.x, velocity.y, velocity.z);
  266. };
  267. OimoJSPlugin.prototype.sleepBody = function (impostor) {
  268. impostor.physicsBody.sleep();
  269. };
  270. OimoJSPlugin.prototype.wakeUpBody = function (impostor) {
  271. impostor.physicsBody.awake();
  272. };
  273. OimoJSPlugin.prototype.dispose = function () {
  274. this.world.clear();
  275. };
  276. return OimoJSPlugin;
  277. })();
  278. BABYLON.OimoJSPlugin = OimoJSPlugin;
  279. })(BABYLON || (BABYLON = {}));