babylon.oimoJSPlugin.ts 13 KB

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