babylon.oimoJSPlugin.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. // register mesh
  21. switch (impostor) {
  22. case BABYLON.PhysicsEngine.SphereImpostor:
  23. var initialRotation = null;
  24. if (mesh.rotationQuaternion) {
  25. initialRotation = mesh.rotationQuaternion.clone();
  26. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  27. mesh.computeWorldMatrix(true);
  28. }
  29. var bbox = mesh.getBoundingInfo().boundingBox;
  30. var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
  31. var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
  32. var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
  33. var size = Math.max(
  34. this._checkWithEpsilon(radiusX),
  35. this._checkWithEpsilon(radiusY),
  36. this._checkWithEpsilon(radiusZ)) / 2;
  37. // The delta between the mesh position and the mesh bounding box center
  38. var deltaPosition = mesh.position.subtract(bbox.center);
  39. // Transform delta position with the rotation
  40. if (initialRotation) {
  41. var m = new BABYLON.Matrix();
  42. initialRotation.toRotationMatrix(m);
  43. deltaPosition = BABYLON.Vector3.TransformCoordinates(deltaPosition, m);
  44. }
  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. // Restore rotation
  55. if (initialRotation) {
  56. body.setQuaternion(initialRotation);
  57. }
  58. this._registeredMeshes.push({
  59. mesh: mesh,
  60. body: body,
  61. delta: deltaPosition
  62. });
  63. break;
  64. case BABYLON.PhysicsEngine.PlaneImpostor:
  65. case BABYLON.PhysicsEngine.BoxImpostor:
  66. initialRotation = null;
  67. if (mesh.rotationQuaternion) {
  68. initialRotation = mesh.rotationQuaternion.clone();
  69. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  70. mesh.computeWorldMatrix(true);
  71. }
  72. bbox = mesh.getBoundingInfo().boundingBox;
  73. var min = bbox.minimumWorld;
  74. var max = bbox.maximumWorld;
  75. var box = max.subtract(min);
  76. var sizeX = this._checkWithEpsilon(box.x);
  77. var sizeY = this._checkWithEpsilon(box.y);
  78. var sizeZ = this._checkWithEpsilon(box.z);
  79. // The delta between the mesh position and the mesh boudning box center
  80. deltaPosition = mesh.position.subtract(bbox.center);
  81. // Transform delta position with the rotation
  82. if (initialRotation) {
  83. m = new BABYLON.Matrix();
  84. initialRotation.toRotationMatrix(m);
  85. deltaPosition = BABYLON.Vector3.TransformCoordinates(deltaPosition, m);
  86. }
  87. body = new OIMO.Body({
  88. type: 'box',
  89. size: [sizeX, sizeY, sizeZ],
  90. pos: [bbox.center.x, bbox.center.y, bbox.center.z],
  91. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD],
  92. move: options.mass != 0,
  93. config: [options.mass, options.friction, options.restitution],
  94. world: this._world
  95. });
  96. if (initialRotation) {
  97. body.setQuaternion(initialRotation);
  98. }
  99. this._registeredMeshes.push({
  100. mesh: mesh,
  101. body: body,
  102. delta: deltaPosition
  103. });
  104. break;
  105. }
  106. return body;
  107. }
  108. public registerMeshesAsCompound(parts: PhysicsCompoundBodyPart[], options: PhysicsBodyCreationOptions): any {
  109. var types = [],
  110. sizes = [],
  111. positions = [],
  112. rotations = [];
  113. var initialMesh = parts[0].mesh;
  114. for (var index = 0; index < parts.length; index++) {
  115. var part = parts[index];
  116. var bodyParameters = this._createBodyAsCompound(part, options, initialMesh);
  117. types.push(bodyParameters.type);
  118. sizes.push.apply(sizes, bodyParameters.size);
  119. positions.push.apply(positions, bodyParameters.pos);
  120. rotations.push.apply(rotations, bodyParameters.rot);
  121. }
  122. var body = new OIMO.Body({
  123. type: types,
  124. size: sizes,
  125. pos: positions,
  126. rot: rotations,
  127. move: options.mass != 0,
  128. config: [options.mass, options.friction, options.restitution],
  129. world: this._world
  130. });
  131. this._registeredMeshes.push({
  132. mesh: initialMesh,
  133. body: body
  134. });
  135. return body;
  136. }
  137. private _createBodyAsCompound(part: PhysicsCompoundBodyPart, options: PhysicsBodyCreationOptions, initialMesh: AbstractMesh): any {
  138. var bodyParameters = null;
  139. var mesh = part.mesh;
  140. // We need the bounding box/sphere info to compute the physics body
  141. mesh.computeWorldMatrix();
  142. switch (part.impostor) {
  143. case BABYLON.PhysicsEngine.SphereImpostor:
  144. var bbox = mesh.getBoundingInfo().boundingBox;
  145. var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
  146. var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
  147. var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
  148. var size = Math.max(
  149. this._checkWithEpsilon(radiusX),
  150. this._checkWithEpsilon(radiusY),
  151. this._checkWithEpsilon(radiusZ)) / 2;
  152. bodyParameters = {
  153. type: 'sphere',
  154. /* bug with oimo : sphere needs 3 sizes in this case */
  155. size: [size, -1, -1],
  156. pos: [mesh.position.x, mesh.position.y, mesh.position.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. case BABYLON.PhysicsEngine.PlaneImpostor:
  161. case BABYLON.PhysicsEngine.BoxImpostor:
  162. bbox = mesh.getBoundingInfo().boundingBox;
  163. var min = bbox.minimumWorld;
  164. var max = bbox.maximumWorld;
  165. var box = max.subtract(min);
  166. var sizeX = this._checkWithEpsilon(box.x);
  167. var sizeY = this._checkWithEpsilon(box.y);
  168. var sizeZ = this._checkWithEpsilon(box.z);
  169. var relativePosition = mesh.position;
  170. bodyParameters = {
  171. type: 'box',
  172. size: [sizeX, sizeY, sizeZ],
  173. pos: [relativePosition.x, relativePosition.y, relativePosition.z],
  174. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD]
  175. };
  176. break;
  177. }
  178. return bodyParameters;
  179. }
  180. public unregisterMesh(mesh: AbstractMesh): void {
  181. for (var index = 0; index < this._registeredMeshes.length; index++) {
  182. var registeredMesh = this._registeredMeshes[index];
  183. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  184. if (registeredMesh.body) {
  185. this._world.removeRigidBody(registeredMesh.body.body);
  186. this._unbindBody(registeredMesh.body);
  187. }
  188. this._registeredMeshes.splice(index, 1);
  189. return;
  190. }
  191. }
  192. }
  193. private _unbindBody(body: any): void {
  194. for (var index = 0; index < this._registeredMeshes.length; index++) {
  195. var registeredMesh = this._registeredMeshes[index];
  196. if (registeredMesh.body === body) {
  197. registeredMesh.body = null;
  198. }
  199. }
  200. }
  201. /**
  202. * Update the body position according to the mesh position
  203. * @param mesh
  204. */
  205. public updateBodyPosition = function (mesh: AbstractMesh): void {
  206. for (var index = 0; index < this._registeredMeshes.length; index++) {
  207. var registeredMesh = this._registeredMeshes[index];
  208. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  209. var body = registeredMesh.body.body;
  210. mesh.computeWorldMatrix(true);
  211. var center = mesh.getBoundingInfo().boundingBox.center;
  212. body.setPosition(center.x, center.y, center.z);
  213. body.setRotation(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z);
  214. return;
  215. }
  216. // Case where the parent has been updated
  217. if (registeredMesh.mesh.parent === mesh) {
  218. mesh.computeWorldMatrix(true);
  219. registeredMesh.mesh.computeWorldMatrix(true);
  220. var absolutePosition = registeredMesh.mesh.getAbsolutePosition();
  221. var absoluteRotation = mesh.rotation;
  222. body = registeredMesh.body.body;
  223. body.setPosition(absolutePosition.x, absolutePosition.y, absolutePosition.z);
  224. body.setRotation(absoluteRotation.x, absoluteRotation.y, absoluteRotation.z);
  225. return;
  226. }
  227. }
  228. }
  229. public applyImpulse(mesh: AbstractMesh, force: Vector3, contactPoint: Vector3): void {
  230. for (var index = 0; index < this._registeredMeshes.length; index++) {
  231. var registeredMesh = this._registeredMeshes[index];
  232. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  233. // Get object mass to have a behaviour similar to cannon.js
  234. var mass = registeredMesh.body.body.massInfo.mass;
  235. // The force is scaled with the mass of object
  236. registeredMesh.body.body.applyImpulse(contactPoint.scale(OIMO.INV_SCALE), force.scale(OIMO.INV_SCALE * mass));
  237. return;
  238. }
  239. }
  240. }
  241. public createLink(mesh1: AbstractMesh, mesh2: AbstractMesh, pivot1: Vector3, pivot2: Vector3, options?: any): boolean {
  242. var body1 = null,
  243. body2 = null;
  244. for (var index = 0; index < this._registeredMeshes.length; index++) {
  245. var registeredMesh = this._registeredMeshes[index];
  246. if (registeredMesh.mesh === mesh1) {
  247. body1 = registeredMesh.body.body;
  248. } else if (registeredMesh.mesh === mesh2) {
  249. body2 = registeredMesh.body.body;
  250. }
  251. }
  252. if (!body1 || !body2) {
  253. return false;
  254. }
  255. if (!options) {
  256. options = {};
  257. }
  258. new OIMO.Link({
  259. type: options.type,
  260. body1: body1,
  261. body2: body2,
  262. min: options.min,
  263. max: options.max,
  264. axe1: options.axe1,
  265. axe2: options.axe2,
  266. pos1: [pivot1.x, pivot1.y, pivot1.z],
  267. pos2: [pivot2.x, pivot2.y, pivot2.z],
  268. collision: options.collision,
  269. spring: options.spring,
  270. world: this._world
  271. });
  272. return true;
  273. }
  274. public dispose(): void {
  275. this._world.clear();
  276. while (this._registeredMeshes.length) {
  277. this.unregisterMesh(this._registeredMeshes[0].mesh);
  278. }
  279. }
  280. public isSupported(): boolean {
  281. return OIMO !== undefined;
  282. }
  283. private _getLastShape(body: any): any {
  284. var lastShape = body.shapes;
  285. while (lastShape.next) {
  286. lastShape = lastShape.next;
  287. }
  288. return lastShape;
  289. }
  290. public runOneStep(time: number): void {
  291. this._world.step();
  292. // Update the position of all registered meshes
  293. var i = this._registeredMeshes.length;
  294. var m;
  295. while (i--) {
  296. var body = this._registeredMeshes[i].body.body;
  297. var mesh = this._registeredMeshes[i].mesh;
  298. var delta = this._registeredMeshes[i].delta;
  299. if (!body.sleeping) {
  300. if (body.shapes.next) {
  301. var parentShape = this._getLastShape(body);
  302. mesh.position.x = parentShape.position.x * OIMO.WORLD_SCALE;
  303. mesh.position.y = parentShape.position.y * OIMO.WORLD_SCALE;
  304. mesh.position.z = parentShape.position.z * OIMO.WORLD_SCALE;
  305. var mtx = BABYLON.Matrix.FromArray(body.getMatrix());
  306. if (!mesh.rotationQuaternion) {
  307. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  308. }
  309. mesh.rotationQuaternion.fromRotationMatrix(mtx);
  310. mesh.computeWorldMatrix();
  311. } else {
  312. m = body.getMatrix();
  313. mtx = BABYLON.Matrix.FromArray(m);
  314. // Body position
  315. var bodyX = mtx.m[12],
  316. bodyY = mtx.m[13],
  317. bodyZ = mtx.m[14];
  318. if (!delta) {
  319. mesh.position.x = bodyX;
  320. mesh.position.y = bodyY;
  321. mesh.position.z = bodyZ;
  322. } else {
  323. mesh.position.x = bodyX + delta.x;
  324. mesh.position.y = bodyY + delta.y;
  325. mesh.position.z = bodyZ + delta.z;
  326. }
  327. if (!mesh.rotationQuaternion) {
  328. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  329. }
  330. mesh.rotationQuaternion.fromRotationMatrix(mtx);
  331. mesh.computeWorldMatrix();
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }