babylon.oimoJSPlugin.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. module BABYLON {
  2. declare var OIMO;
  3. export class OimoJSPlugin implements IPhysicsEnginePlugin {
  4. private _world;
  5. private _registeredMeshes = [];
  6. private _checkWithEpsilon(value: number): number {
  7. return value < BABYLON.PhysicsEngine.Epsilon ? BABYLON.PhysicsEngine.Epsilon : value;
  8. }
  9. public initialize(iterations?: number): void {
  10. this._world = new OIMO.World();
  11. this._world.clear();
  12. }
  13. public setGravity(gravity: Vector3): void {
  14. this._world.gravity = gravity;
  15. }
  16. public registerMesh(mesh: AbstractMesh, impostor: number, options: PhysicsBodyCreationOptions): any {
  17. var body = null;
  18. this.unregisterMesh(mesh);
  19. mesh.computeWorldMatrix(true);
  20. var initialRotation = null;
  21. if (mesh.rotationQuaternion) {
  22. initialRotation = mesh.rotationQuaternion.clone();
  23. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  24. mesh.computeWorldMatrix(true);
  25. }
  26. var bbox = mesh.getBoundingInfo().boundingBox;
  27. // The delta between the mesh position and the mesh bounding box center
  28. var deltaPosition = mesh.position.subtract(bbox.center);
  29. // Transform delta position with the rotation
  30. if (initialRotation) {
  31. var m = new BABYLON.Matrix();
  32. initialRotation.toRotationMatrix(m);
  33. deltaPosition = BABYLON.Vector3.TransformCoordinates(deltaPosition, m);
  34. }
  35. // register mesh
  36. switch (impostor) {
  37. case BABYLON.PhysicsEngine.SphereImpostor:
  38. var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
  39. var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
  40. var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
  41. var size = Math.max(
  42. this._checkWithEpsilon(radiusX),
  43. this._checkWithEpsilon(radiusY),
  44. this._checkWithEpsilon(radiusZ)) / 2;
  45. body = new OIMO.Body({
  46. type: 'sphere',
  47. size: [size],
  48. pos: [bbox.center.x, bbox.center.y, bbox.center.z],
  49. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD],
  50. move: options.mass != 0,
  51. config: [options.mass, options.friction, options.restitution],
  52. world: this._world
  53. });
  54. break;
  55. case PhysicsEngine.PlaneImpostor:
  56. //Oimo "fakes" a cylinder as a box, so why don't we!
  57. case PhysicsEngine.CylinderImpostor:
  58. case PhysicsEngine.BoxImpostor:
  59. var min = bbox.minimumWorld;
  60. var max = bbox.maximumWorld;
  61. var box = max.subtract(min);
  62. var sizeX = this._checkWithEpsilon(box.x);
  63. var sizeY = this._checkWithEpsilon(box.y);
  64. var sizeZ = this._checkWithEpsilon(box.z);
  65. body = new OIMO.Body({
  66. type: 'box',
  67. size: [sizeX, sizeY, sizeZ],
  68. pos: [bbox.center.x, bbox.center.y, bbox.center.z],
  69. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD],
  70. move: options.mass != 0,
  71. config: [options.mass, options.friction, options.restitution],
  72. world: this._world
  73. });
  74. break;
  75. }
  76. //If quaternion was set as the rotation of the object
  77. if (initialRotation) {
  78. //We have to access the rigid body's properties to set the quaternion.
  79. //The setQuaternion function of Oimo only sets the newOrientation that is only set after an impulse is given or a collision.
  80. body.body.orientation = new OIMO.Quat(initialRotation.w, initialRotation.x, initialRotation.y, initialRotation.z);
  81. //update the internal rotation matrix
  82. body.body.syncShapes();
  83. }
  84. this._registeredMeshes.push({
  85. mesh: mesh,
  86. body: body,
  87. delta: deltaPosition
  88. });
  89. return body;
  90. }
  91. public registerMeshesAsCompound(parts: PhysicsCompoundBodyPart[], options: PhysicsBodyCreationOptions): any {
  92. var types = [],
  93. sizes = [],
  94. positions = [],
  95. 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. private _createBodyAsCompound(part: PhysicsCompoundBodyPart, options: PhysicsBodyCreationOptions, initialMesh: AbstractMesh): any {
  121. var bodyParameters = null;
  122. var mesh = part.mesh;
  123. // We need the bounding box/sphere info to compute the physics body
  124. mesh.computeWorldMatrix();
  125. switch (part.impostor) {
  126. case BABYLON.PhysicsEngine.SphereImpostor:
  127. var bbox = mesh.getBoundingInfo().boundingBox;
  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(
  132. this._checkWithEpsilon(radiusX),
  133. this._checkWithEpsilon(radiusY),
  134. this._checkWithEpsilon(radiusZ)) / 2;
  135. bodyParameters = {
  136. type: 'sphere',
  137. /* bug with oimo : sphere needs 3 sizes in this case */
  138. size: [size, -1, -1],
  139. pos: [mesh.position.x, mesh.position.y, mesh.position.z],
  140. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD]
  141. };
  142. break;
  143. case BABYLON.PhysicsEngine.PlaneImpostor:
  144. case BABYLON.PhysicsEngine.BoxImpostor:
  145. bbox = mesh.getBoundingInfo().boundingBox;
  146. var min = bbox.minimumWorld;
  147. var max = bbox.maximumWorld;
  148. var box = max.subtract(min);
  149. var sizeX = this._checkWithEpsilon(box.x);
  150. var sizeY = this._checkWithEpsilon(box.y);
  151. var sizeZ = this._checkWithEpsilon(box.z);
  152. var relativePosition = mesh.position;
  153. bodyParameters = {
  154. type: 'box',
  155. size: [sizeX, sizeY, sizeZ],
  156. pos: [relativePosition.x, relativePosition.y, relativePosition.z],
  157. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD]
  158. };
  159. break;
  160. }
  161. return bodyParameters;
  162. }
  163. public unregisterMesh(mesh: AbstractMesh): void {
  164. for (var index = 0; index < this._registeredMeshes.length; index++) {
  165. var registeredMesh = this._registeredMeshes[index];
  166. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  167. if (registeredMesh.body) {
  168. this._world.removeRigidBody(registeredMesh.body.body);
  169. this._unbindBody(registeredMesh.body);
  170. }
  171. this._registeredMeshes.splice(index, 1);
  172. return;
  173. }
  174. }
  175. }
  176. private _unbindBody(body: any): void {
  177. for (var index = 0; index < this._registeredMeshes.length; index++) {
  178. var registeredMesh = this._registeredMeshes[index];
  179. if (registeredMesh.body === body) {
  180. registeredMesh.body = null;
  181. }
  182. }
  183. }
  184. /**
  185. * Update the body position according to the mesh position
  186. * @param mesh
  187. */
  188. public updateBodyPosition = function (mesh: AbstractMesh): void {
  189. for (var index = 0; index < this._registeredMeshes.length; index++) {
  190. var registeredMesh = this._registeredMeshes[index];
  191. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  192. var body = registeredMesh.body.body;
  193. mesh.computeWorldMatrix(true);
  194. var center = mesh.getBoundingInfo().boundingBox.center;
  195. body.setPosition(center.x, center.y, center.z);
  196. body.setRotation(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z);
  197. return;
  198. }
  199. // Case where the parent has been updated
  200. if (registeredMesh.mesh.parent === mesh) {
  201. mesh.computeWorldMatrix(true);
  202. registeredMesh.mesh.computeWorldMatrix(true);
  203. var absolutePosition = registeredMesh.mesh.getAbsolutePosition();
  204. var absoluteRotation = mesh.rotation;
  205. body = registeredMesh.body.body;
  206. body.setPosition(absolutePosition.x, absolutePosition.y, absolutePosition.z);
  207. body.setRotation(absoluteRotation.x, absoluteRotation.y, absoluteRotation.z);
  208. return;
  209. }
  210. }
  211. }
  212. public applyImpulse(mesh: AbstractMesh, force: Vector3, contactPoint: Vector3): void {
  213. for (var index = 0; index < this._registeredMeshes.length; index++) {
  214. var registeredMesh = this._registeredMeshes[index];
  215. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  216. // Get object mass to have a behaviour similar to cannon.js
  217. var mass = registeredMesh.body.body.massInfo.mass;
  218. // The force is scaled with the mass of object
  219. registeredMesh.body.body.applyImpulse(contactPoint.scale(OIMO.INV_SCALE), force.scale(OIMO.INV_SCALE * mass));
  220. return;
  221. }
  222. }
  223. }
  224. public createLink(mesh1: AbstractMesh, mesh2: AbstractMesh, pivot1: Vector3, pivot2: Vector3, options?: any): boolean {
  225. var body1 = null,
  226. body2 = null;
  227. for (var index = 0; index < this._registeredMeshes.length; index++) {
  228. var registeredMesh = this._registeredMeshes[index];
  229. if (registeredMesh.mesh === mesh1) {
  230. body1 = registeredMesh.body.body;
  231. } else if (registeredMesh.mesh === mesh2) {
  232. body2 = registeredMesh.body.body;
  233. }
  234. }
  235. if (!body1 || !body2) {
  236. return false;
  237. }
  238. if (!options) {
  239. options = {};
  240. }
  241. new OIMO.Link({
  242. type: options.type,
  243. body1: body1,
  244. body2: body2,
  245. min: options.min,
  246. max: options.max,
  247. axe1: options.axe1,
  248. axe2: options.axe2,
  249. pos1: [pivot1.x, pivot1.y, pivot1.z],
  250. pos2: [pivot2.x, pivot2.y, pivot2.z],
  251. collision: options.collision,
  252. spring: options.spring,
  253. world: this._world
  254. });
  255. return true;
  256. }
  257. public dispose(): void {
  258. this._world.clear();
  259. while (this._registeredMeshes.length) {
  260. this.unregisterMesh(this._registeredMeshes[0].mesh);
  261. }
  262. }
  263. public isSupported(): boolean {
  264. return OIMO !== undefined;
  265. }
  266. private _getLastShape(body: any): any {
  267. var lastShape = body.shapes;
  268. while (lastShape.next) {
  269. lastShape = lastShape.next;
  270. }
  271. return lastShape;
  272. }
  273. public runOneStep(time: number): void {
  274. this._world.step();
  275. // Update the position of all registered meshes
  276. var i = this._registeredMeshes.length;
  277. var m;
  278. while (i--) {
  279. var body = this._registeredMeshes[i].body.body;
  280. var mesh = this._registeredMeshes[i].mesh;
  281. var delta = this._registeredMeshes[i].delta;
  282. if (!body.sleeping) {
  283. if (body.shapes.next) {
  284. var parentShape = this._getLastShape(body);
  285. mesh.position.x = parentShape.position.x * OIMO.WORLD_SCALE;
  286. mesh.position.y = parentShape.position.y * OIMO.WORLD_SCALE;
  287. mesh.position.z = parentShape.position.z * OIMO.WORLD_SCALE;
  288. var mtx = BABYLON.Matrix.FromArray(body.getMatrix());
  289. if (!mesh.rotationQuaternion) {
  290. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  291. }
  292. mesh.rotationQuaternion.fromRotationMatrix(mtx);
  293. mesh.computeWorldMatrix();
  294. } else {
  295. m = body.getMatrix();
  296. mtx = BABYLON.Matrix.FromArray(m);
  297. // Body position
  298. var bodyX = mtx.m[12],
  299. bodyY = mtx.m[13],
  300. bodyZ = mtx.m[14];
  301. if (!delta) {
  302. mesh.position.x = bodyX;
  303. mesh.position.y = bodyY;
  304. mesh.position.z = bodyZ;
  305. } else {
  306. mesh.position.x = bodyX + delta.x;
  307. mesh.position.y = bodyY + delta.y;
  308. mesh.position.z = bodyZ + delta.z;
  309. }
  310. if (!mesh.rotationQuaternion) {
  311. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  312. }
  313. Quaternion.FromRotationMatrixToRef(mtx, mesh.rotationQuaternion);
  314. mesh.computeWorldMatrix();
  315. }
  316. }
  317. }
  318. }
  319. }
  320. }