babylon.oimoJSPlugin.js 13 KB

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