babylon.oimoJSPlugin.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. switch (part.impostor) {
  141. case BABYLON.PhysicsEngine.SphereImpostor:
  142. var bbox = mesh.getBoundingInfo().boundingBox;
  143. var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
  144. var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
  145. var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
  146. var size = Math.max(
  147. this._checkWithEpsilon(radiusX),
  148. this._checkWithEpsilon(radiusY),
  149. this._checkWithEpsilon(radiusZ)) / 2;
  150. bodyParameters = {
  151. type: 'sphere',
  152. /* bug with oimo : sphere needs 3 sizes in this case */
  153. size: [size, -1, -1],
  154. pos: [mesh.position.x, mesh.position.y, mesh.position.z],
  155. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD]
  156. };
  157. break;
  158. case BABYLON.PhysicsEngine.PlaneImpostor:
  159. case BABYLON.PhysicsEngine.BoxImpostor:
  160. bbox = mesh.getBoundingInfo().boundingBox;
  161. var min = bbox.minimumWorld;
  162. var max = bbox.maximumWorld;
  163. var box = max.subtract(min);
  164. var sizeX = this._checkWithEpsilon(box.x);
  165. var sizeY = this._checkWithEpsilon(box.y);
  166. var sizeZ = this._checkWithEpsilon(box.z);
  167. var relativePosition = mesh.position;
  168. bodyParameters = {
  169. type: 'box',
  170. size: [sizeX, sizeY, sizeZ],
  171. pos: [relativePosition.x, relativePosition.y, relativePosition.z],
  172. rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD]
  173. };
  174. break;
  175. }
  176. return bodyParameters;
  177. }
  178. public unregisterMesh(mesh: AbstractMesh): void {
  179. for (var index = 0; index < this._registeredMeshes.length; index++) {
  180. var registeredMesh = this._registeredMeshes[index];
  181. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  182. if (registeredMesh.body) {
  183. this._world.removeRigidBody(registeredMesh.body.body);
  184. this._unbindBody(registeredMesh.body);
  185. }
  186. this._registeredMeshes.splice(index, 1);
  187. return;
  188. }
  189. }
  190. }
  191. private _unbindBody(body: any): void {
  192. for (var index = 0; index < this._registeredMeshes.length; index++) {
  193. var registeredMesh = this._registeredMeshes[index];
  194. if (registeredMesh.body === body) {
  195. registeredMesh.body = null;
  196. }
  197. }
  198. }
  199. /**
  200. * Update the body position according to the mesh position
  201. * @param mesh
  202. */
  203. public updateBodyPosition = function (mesh: AbstractMesh): void {
  204. for (var index = 0; index < this._registeredMeshes.length; index++) {
  205. var registeredMesh = this._registeredMeshes[index];
  206. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  207. var body = registeredMesh.body.body;
  208. mesh.computeWorldMatrix(true);
  209. var center = mesh.getBoundingInfo().boundingBox.center;
  210. body.setPosition(center.x, center.y, center.z);
  211. body.setOrientation(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z);
  212. return;
  213. }
  214. // Case where the parent has been updated
  215. if (registeredMesh.mesh.parent === mesh) {
  216. mesh.computeWorldMatrix(true);
  217. registeredMesh.mesh.computeWorldMatrix(true);
  218. var absolutePosition = registeredMesh.mesh.getAbsolutePosition();
  219. var absoluteRotation = mesh.rotation;
  220. body = registeredMesh.body.body;
  221. body.setPosition(absolutePosition.x, absolutePosition.y, absolutePosition.z);
  222. body.setOrientation(absoluteRotation.x, absoluteRotation.y, absoluteRotation.z);
  223. return;
  224. }
  225. }
  226. }
  227. public applyImpulse(mesh: AbstractMesh, force: Vector3, contactPoint: Vector3): void {
  228. for (var index = 0; index < this._registeredMeshes.length; index++) {
  229. var registeredMesh = this._registeredMeshes[index];
  230. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  231. // Get object mass to have a behaviour similar to cannon.js
  232. var mass = registeredMesh.body.body.massInfo.mass;
  233. // The force is scaled with the mass of object
  234. registeredMesh.body.body.applyImpulse(contactPoint.scale(OIMO.INV_SCALE), force.scale(OIMO.INV_SCALE * mass));
  235. return;
  236. }
  237. }
  238. }
  239. public createLink(mesh1: AbstractMesh, mesh2: AbstractMesh, pivot1: Vector3, pivot2: Vector3, options?: any): boolean {
  240. var body1 = null,
  241. body2 = null;
  242. for (var index = 0; index < this._registeredMeshes.length; index++) {
  243. var registeredMesh = this._registeredMeshes[index];
  244. if (registeredMesh.mesh === mesh1) {
  245. body1 = registeredMesh.body.body;
  246. } else if (registeredMesh.mesh === mesh2) {
  247. body2 = registeredMesh.body.body;
  248. }
  249. }
  250. if (!body1 || !body2) {
  251. return false;
  252. }
  253. if (!options) {
  254. options = {};
  255. }
  256. new OIMO.Link({
  257. type: options.type,
  258. body1: body1,
  259. body2: body2,
  260. min: options.min,
  261. max: options.max,
  262. axe1: options.axe1,
  263. axe2: options.axe2,
  264. pos1: [pivot1.x, pivot1.y, pivot1.z],
  265. pos2: [pivot2.x, pivot2.y, pivot2.z],
  266. collision: options.collision,
  267. spring: options.spring,
  268. world: this._world
  269. });
  270. return true;
  271. }
  272. public dispose(): void {
  273. this._world.clear();
  274. while (this._registeredMeshes.length) {
  275. this.unregisterMesh(this._registeredMeshes[0].mesh);
  276. }
  277. }
  278. public isSupported(): boolean {
  279. return OIMO !== undefined;
  280. }
  281. private _getLastShape(body: any): any {
  282. var lastShape = body.shapes;
  283. while (lastShape.next) {
  284. lastShape = lastShape.next;
  285. }
  286. return lastShape;
  287. }
  288. public runOneStep(time: number): void {
  289. this._world.step();
  290. // Update the position of all registered meshes
  291. var i = this._registeredMeshes.length;
  292. var m;
  293. while (i--) {
  294. var body = this._registeredMeshes[i].body.body;
  295. var mesh = this._registeredMeshes[i].mesh;
  296. var delta = this._registeredMeshes[i].delta;
  297. if (!body.sleeping) {
  298. if (body.shapes.next) {
  299. var parentShape = this._getLastShape(body);
  300. mesh.position.x = parentShape.position.x * OIMO.WORLD_SCALE;
  301. mesh.position.y = parentShape.position.y * OIMO.WORLD_SCALE;
  302. mesh.position.z = parentShape.position.z * OIMO.WORLD_SCALE;
  303. var mtx = BABYLON.Matrix.FromArray(body.getMatrix());
  304. if (!mesh.rotationQuaternion) {
  305. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  306. }
  307. mesh.rotationQuaternion.fromRotationMatrix(mtx);
  308. mesh.computeWorldMatrix();
  309. } else {
  310. m = body.getMatrix();
  311. mtx = BABYLON.Matrix.FromArray(m);
  312. // Body position
  313. var bodyX = mtx.m[12],
  314. bodyY = mtx.m[13],
  315. bodyZ = mtx.m[14];
  316. if (!delta) {
  317. mesh.position.x = bodyX;
  318. mesh.position.y = bodyY;
  319. mesh.position.z = bodyZ;
  320. } else {
  321. mesh.position.x = bodyX + delta.x;
  322. mesh.position.y = bodyY + delta.y;
  323. mesh.position.z = bodyZ + delta.z;
  324. }
  325. if (!mesh.rotationQuaternion) {
  326. mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
  327. }
  328. mesh.rotationQuaternion.fromRotationMatrix(mtx);
  329. mesh.computeWorldMatrix();
  330. }
  331. }
  332. }
  333. }
  334. }
  335. }