babylon.oimoJSPlugin.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 < PhysicsEngine.Epsilon ? 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. this.unregisterMesh(mesh);
  18. if (!mesh.rotationQuaternion) {
  19. mesh.rotationQuaternion = Quaternion.RotationYawPitchRoll(mesh.rotation.y, mesh.rotation.x, mesh.rotation.z);
  20. }
  21. mesh.computeWorldMatrix(true);
  22. var bbox = mesh.getBoundingInfo().boundingBox;
  23. // The delta between the mesh position and the mesh bounding box center
  24. var deltaPosition = mesh.position.subtract(bbox.center);
  25. //calculate rotation to fit Oimo's needs (Euler...)
  26. var rot = OIMO.MatrixToEuler(mesh.getWorldMatrix().asArray());
  27. var bodyConfig : any = {
  28. pos: [bbox.center.x, bbox.center.y, bbox.center.z],
  29. rot: rot,
  30. move: options.mass != 0,
  31. config: [options.mass, options.friction, options.restitution],
  32. world: this._world
  33. };
  34. // register mesh
  35. switch (impostor) {
  36. case PhysicsEngine.SphereImpostor:
  37. var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
  38. var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
  39. var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
  40. var size = Math.max(
  41. this._checkWithEpsilon(radiusX),
  42. this._checkWithEpsilon(radiusY),
  43. this._checkWithEpsilon(radiusZ)) / 2;
  44. bodyConfig.type = 'sphere';
  45. bodyConfig.size = [size];
  46. break;
  47. case PhysicsEngine.PlaneImpostor:
  48. //Oimo "fakes" a cylinder as a box, so why don't we!
  49. case PhysicsEngine.CylinderImpostor:
  50. case PhysicsEngine.BoxImpostor:
  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. bodyConfig.type = 'box';
  58. bodyConfig.size = [sizeX, sizeY, sizeZ];
  59. break;
  60. }
  61. var body = new OIMO.Body(bodyConfig);
  62. //We have to access the rigid body's properties to set the quaternion.
  63. //The setQuaternion function of Oimo only sets the newOrientation that is only set after an impulse is given or a collision.
  64. //body.body.orientation = new OIMO.Quat(mesh.rotationQuaternion.w, mesh.rotationQuaternion.x, mesh.rotationQuaternion.y, mesh.rotationQuaternion.z);
  65. //TEST
  66. //body.body.resetQuaternion(new OIMO.Quat(mesh.rotationQuaternion.w, mesh.rotationQuaternion.x, mesh.rotationQuaternion.y, mesh.rotationQuaternion.z));
  67. //update the internal rotation matrix
  68. //body.body.syncShapes();
  69. this._registeredMeshes.push({
  70. mesh: mesh,
  71. body: body,
  72. delta: deltaPosition
  73. });
  74. return body;
  75. }
  76. public registerMeshesAsCompound(parts: PhysicsCompoundBodyPart[], options: PhysicsBodyCreationOptions): any {
  77. var types = [],
  78. sizes = [],
  79. positions = [],
  80. rotations = [];
  81. var initialMesh = parts[0].mesh;
  82. for (var index = 0; index < parts.length; index++) {
  83. var part = parts[index];
  84. var bodyParameters = this._createBodyAsCompound(part, options, initialMesh);
  85. types.push(bodyParameters.type);
  86. sizes.push.apply(sizes, bodyParameters.size);
  87. positions.push.apply(positions, bodyParameters.pos);
  88. //Hack for Oimo's rotation. Quaternion will be used later.
  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 mesh = part.mesh;
  108. // We need the bounding box/sphere info to compute the physics body
  109. mesh.computeWorldMatrix();
  110. var rot = OIMO.MatrixToEuler(mesh.getWorldMatrix().asArray());
  111. var bodyParameters : any = {
  112. pos: [mesh.position.x, mesh.position.y, mesh.position.z],
  113. rot: rot
  114. };
  115. switch (part.impostor) {
  116. case PhysicsEngine.SphereImpostor:
  117. var bbox = mesh.getBoundingInfo().boundingBox;
  118. var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
  119. var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
  120. var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
  121. var size = Math.max(
  122. this._checkWithEpsilon(radiusX),
  123. this._checkWithEpsilon(radiusY),
  124. this._checkWithEpsilon(radiusZ)) / 2;
  125. bodyParameters.type = 'sphere';
  126. bodyParameters.size = [size, size, size];
  127. break;
  128. case PhysicsEngine.PlaneImpostor:
  129. case PhysicsEngine.CylinderImpostor:
  130. case PhysicsEngine.BoxImpostor:
  131. bbox = mesh.getBoundingInfo().boundingBox;
  132. var min = bbox.minimumWorld;
  133. var max = bbox.maximumWorld;
  134. var box = max.subtract(min);
  135. var sizeX = this._checkWithEpsilon(box.x);
  136. var sizeY = this._checkWithEpsilon(box.y);
  137. var sizeZ = this._checkWithEpsilon(box.z);
  138. bodyParameters.type = 'sphere';
  139. bodyParameters.size = [sizeX, sizeY, sizeZ];
  140. break;
  141. }
  142. return bodyParameters;
  143. }
  144. public unregisterMesh(mesh: AbstractMesh): void {
  145. for (var index = 0; index < this._registeredMeshes.length; index++) {
  146. var registeredMesh = this._registeredMeshes[index];
  147. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  148. if (registeredMesh.body) {
  149. this._world.removeRigidBody(registeredMesh.body.body);
  150. this._unbindBody(registeredMesh.body);
  151. }
  152. this._registeredMeshes.splice(index, 1);
  153. return;
  154. }
  155. }
  156. }
  157. private _unbindBody(body: any): void {
  158. for (var index = 0; index < this._registeredMeshes.length; index++) {
  159. var registeredMesh = this._registeredMeshes[index];
  160. if (registeredMesh.body === body) {
  161. registeredMesh.body = null;
  162. }
  163. }
  164. }
  165. /**
  166. * Update the body position according to the mesh position
  167. * @param mesh
  168. */
  169. public updateBodyPosition = function (mesh: AbstractMesh): void {
  170. for (var index = 0; index < this._registeredMeshes.length; index++) {
  171. var registeredMesh = this._registeredMeshes[index];
  172. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  173. var body = registeredMesh.body.body;
  174. mesh.computeWorldMatrix(true);
  175. var center = mesh.getBoundingInfo().boundingBox.center;
  176. body.setPosition(new OIMO.Vec3(center.x, center.y, center.z));
  177. body.setQuaternion(mesh.rotationQuaternion);
  178. body.sleeping = false;
  179. return;
  180. }
  181. // Case where the parent has been updated
  182. if (registeredMesh.mesh.parent === mesh) {
  183. mesh.computeWorldMatrix(true);
  184. registeredMesh.mesh.computeWorldMatrix(true);
  185. var absolutePosition = registeredMesh.mesh.getAbsolutePosition();
  186. body = registeredMesh.body.body;
  187. body.setPosition(new OIMO.Vec3(absolutePosition.x, absolutePosition.y, absolutePosition.z));
  188. body.setQuaternion(mesh.rotationQuaternion);
  189. body.sleeping = false;
  190. return;
  191. }
  192. }
  193. }
  194. public applyImpulse(mesh: AbstractMesh, force: Vector3, contactPoint: Vector3): void {
  195. for (var index = 0; index < this._registeredMeshes.length; index++) {
  196. var registeredMesh = this._registeredMeshes[index];
  197. if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
  198. // Get object mass to have a behaviour similar to cannon.js
  199. var mass = registeredMesh.body.body.massInfo.mass;
  200. // The force is scaled with the mass of object
  201. registeredMesh.body.body.applyImpulse(contactPoint.scale(OIMO.INV_SCALE), force.scale(OIMO.INV_SCALE * mass));
  202. return;
  203. }
  204. }
  205. }
  206. public createLink(mesh1: AbstractMesh, mesh2: AbstractMesh, pivot1: Vector3, pivot2: Vector3, options?: any): boolean {
  207. var body1 = null,
  208. body2 = null;
  209. for (var index = 0; index < this._registeredMeshes.length; index++) {
  210. var registeredMesh = this._registeredMeshes[index];
  211. if (registeredMesh.mesh === mesh1) {
  212. body1 = registeredMesh.body.body;
  213. } else if (registeredMesh.mesh === mesh2) {
  214. body2 = registeredMesh.body.body;
  215. }
  216. }
  217. if (!body1 || !body2) {
  218. return false;
  219. }
  220. if (!options) {
  221. options = {};
  222. }
  223. new OIMO.Link({
  224. type: options.type,
  225. body1: body1,
  226. body2: body2,
  227. min: options.min,
  228. max: options.max,
  229. axe1: options.axe1,
  230. axe2: options.axe2,
  231. pos1: [pivot1.x, pivot1.y, pivot1.z],
  232. pos2: [pivot2.x, pivot2.y, pivot2.z],
  233. collision: options.collision,
  234. spring: options.spring,
  235. world: this._world
  236. });
  237. return true;
  238. }
  239. public dispose(): void {
  240. this._world.clear();
  241. while (this._registeredMeshes.length) {
  242. this.unregisterMesh(this._registeredMeshes[0].mesh);
  243. }
  244. }
  245. public isSupported(): boolean {
  246. return OIMO !== undefined;
  247. }
  248. public getWorldObject() : any {
  249. return this._world;
  250. }
  251. private _getLastShape(body: any): any {
  252. var lastShape = body.shapes;
  253. while (lastShape.next) {
  254. lastShape = lastShape.next;
  255. }
  256. return lastShape;
  257. }
  258. public runOneStep(time: number): void {
  259. this._world.step();
  260. // Update the position of all registered meshes
  261. var i = this._registeredMeshes.length;
  262. var m;
  263. while (i--) {
  264. var body = this._registeredMeshes[i].body.body;
  265. var mesh = this._registeredMeshes[i].mesh;
  266. if(!this._registeredMeshes[i].delta) {
  267. this._registeredMeshes[i].delta = Vector3.Zero();
  268. }
  269. if (!body.sleeping) {
  270. //TODO check that
  271. if (body.shapes.next) {
  272. var parentShape = this._getLastShape(body);
  273. mesh.position.x = parentShape.position.x * OIMO.WORLD_SCALE;
  274. mesh.position.y = parentShape.position.y * OIMO.WORLD_SCALE;
  275. mesh.position.z = parentShape.position.z * OIMO.WORLD_SCALE;
  276. } else {
  277. mesh.position.copyFrom(body.getPosition()).addInPlace(this._registeredMeshes[i].delta);
  278. }
  279. mesh.rotationQuaternion.copyFrom(body.getQuaternion());
  280. mesh.computeWorldMatrix();
  281. }
  282. }
  283. }
  284. }
  285. }