babylon.oimoJSPlugin.ts 14 KB

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