123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- module BABYLON {
- declare var OIMO;
- export class OimoJSPlugin implements IPhysicsEnginePlugin {
- private _world;
- private _registeredMeshes = [];
- private _checkWithEpsilon(value: number): number {
- return value < BABYLON.PhysicsEngine.Epsilon ? BABYLON.PhysicsEngine.Epsilon : value;
- }
- public initialize(iterations?: number): void {
- this._world = new OIMO.World();
- this._world.clear();
- }
- public setGravity(gravity: Vector3): void {
- this._world.gravity = gravity;
- }
- public registerMesh(mesh: AbstractMesh, impostor: number, options: PhysicsBodyCreationOptions): any {
- var body = null;
- this.unregisterMesh(mesh);
- mesh.computeWorldMatrix(true);
- // register mesh
- switch (impostor) {
- case BABYLON.PhysicsEngine.SphereImpostor:
- var initialRotation = null;
- if (mesh.rotationQuaternion) {
- initialRotation = mesh.rotationQuaternion.clone();
- mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
- mesh.computeWorldMatrix(true);
- }
- var bbox = mesh.getBoundingInfo().boundingBox;
- var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
- var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
- var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
- var size = Math.max(
- this._checkWithEpsilon(radiusX),
- this._checkWithEpsilon(radiusY),
- this._checkWithEpsilon(radiusZ)) / 2;
- // The delta between the mesh position and the mesh bounding box center
- var deltaPosition = mesh.position.subtract(bbox.center);
- // Transform delta position with the rotation
- if (initialRotation) {
- var m = new BABYLON.Matrix();
- initialRotation.toRotationMatrix(m);
- deltaPosition = BABYLON.Vector3.TransformCoordinates(deltaPosition, m);
- }
- body = new OIMO.Body({
- type: 'sphere',
- size: [size],
- pos: [bbox.center.x, bbox.center.y, bbox.center.z],
- rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD],
- move: options.mass != 0,
- config: [options.mass, options.friction, options.restitution],
- world: this._world
- });
- // Restore rotation
- if (initialRotation) {
- body.setQuaternion(initialRotation);
- }
- this._registeredMeshes.push({
- mesh: mesh,
- body: body,
- delta: deltaPosition
- });
- break;
- case BABYLON.PhysicsEngine.PlaneImpostor:
- case BABYLON.PhysicsEngine.BoxImpostor:
- initialRotation = null;
- if (mesh.rotationQuaternion) {
- initialRotation = mesh.rotationQuaternion.clone();
- mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
- mesh.computeWorldMatrix(true);
- }
- bbox = mesh.getBoundingInfo().boundingBox;
- var min = bbox.minimumWorld;
- var max = bbox.maximumWorld;
- var box = max.subtract(min);
- var sizeX = this._checkWithEpsilon(box.x);
- var sizeY = this._checkWithEpsilon(box.y);
- var sizeZ = this._checkWithEpsilon(box.z);
- // The delta between the mesh position and the mesh boudning box center
- deltaPosition = mesh.position.subtract(bbox.center);
- // Transform delta position with the rotation
- if (initialRotation) {
- m = new BABYLON.Matrix();
- initialRotation.toRotationMatrix(m);
- deltaPosition = BABYLON.Vector3.TransformCoordinates(deltaPosition, m);
- }
- body = new OIMO.Body({
- type: 'box',
- size: [sizeX, sizeY, sizeZ],
- pos: [bbox.center.x, bbox.center.y, bbox.center.z],
- rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD],
- move: options.mass != 0,
- config: [options.mass, options.friction, options.restitution],
- world: this._world
- });
- if (initialRotation) {
- body.setQuaternion(initialRotation);
- }
- this._registeredMeshes.push({
- mesh: mesh,
- body: body,
- delta: deltaPosition
- });
- break;
- }
- return body;
- }
- public registerMeshesAsCompound(parts: PhysicsCompoundBodyPart[], options: PhysicsBodyCreationOptions): any {
- var types = [],
- sizes = [],
- positions = [],
- rotations = [];
- var initialMesh = parts[0].mesh;
- for (var index = 0; index < parts.length; index++) {
- var part = parts[index];
- var bodyParameters = this._createBodyAsCompound(part, options, initialMesh);
- types.push(bodyParameters.type);
- sizes.push.apply(sizes, bodyParameters.size);
- positions.push.apply(positions, bodyParameters.pos);
- rotations.push.apply(rotations, bodyParameters.rot);
- }
- var body = new OIMO.Body({
- type: types,
- size: sizes,
- pos: positions,
- rot: rotations,
- move: options.mass != 0,
- config: [options.mass, options.friction, options.restitution],
- world: this._world
- });
- this._registeredMeshes.push({
- mesh: initialMesh,
- body: body
- });
- return body;
- }
- private _createBodyAsCompound(part: PhysicsCompoundBodyPart, options: PhysicsBodyCreationOptions, initialMesh: AbstractMesh): any {
- var bodyParameters = null;
- var mesh = part.mesh;
- // We need the bounding box/sphere info to compute the physics body
- mesh.computeWorldMatrix();
- switch (part.impostor) {
- case BABYLON.PhysicsEngine.SphereImpostor:
- var bbox = mesh.getBoundingInfo().boundingBox;
- var radiusX = bbox.maximumWorld.x - bbox.minimumWorld.x;
- var radiusY = bbox.maximumWorld.y - bbox.minimumWorld.y;
- var radiusZ = bbox.maximumWorld.z - bbox.minimumWorld.z;
- var size = Math.max(
- this._checkWithEpsilon(radiusX),
- this._checkWithEpsilon(radiusY),
- this._checkWithEpsilon(radiusZ)) / 2;
- bodyParameters = {
- type: 'sphere',
- /* bug with oimo : sphere needs 3 sizes in this case */
- size: [size, -1, -1],
- pos: [mesh.position.x, mesh.position.y, mesh.position.z],
- rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD]
- };
- break;
- case BABYLON.PhysicsEngine.PlaneImpostor:
- case BABYLON.PhysicsEngine.BoxImpostor:
- bbox = mesh.getBoundingInfo().boundingBox;
- var min = bbox.minimumWorld;
- var max = bbox.maximumWorld;
- var box = max.subtract(min);
- var sizeX = this._checkWithEpsilon(box.x);
- var sizeY = this._checkWithEpsilon(box.y);
- var sizeZ = this._checkWithEpsilon(box.z);
- var relativePosition = mesh.position;
- bodyParameters = {
- type: 'box',
- size: [sizeX, sizeY, sizeZ],
- pos: [relativePosition.x, relativePosition.y, relativePosition.z],
- rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD]
- };
- break;
- }
- return bodyParameters;
- }
- public unregisterMesh(mesh: AbstractMesh): void {
- for (var index = 0; index < this._registeredMeshes.length; index++) {
- var registeredMesh = this._registeredMeshes[index];
- if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
- if (registeredMesh.body) {
- this._world.removeRigidBody(registeredMesh.body.body);
- this._unbindBody(registeredMesh.body);
- }
- this._registeredMeshes.splice(index, 1);
- return;
- }
- }
- }
- private _unbindBody(body: any): void {
- for (var index = 0; index < this._registeredMeshes.length; index++) {
- var registeredMesh = this._registeredMeshes[index];
- if (registeredMesh.body === body) {
- registeredMesh.body = null;
- }
- }
- }
- /**
- * Update the body position according to the mesh position
- * @param mesh
- */
- public updateBodyPosition = function (mesh: AbstractMesh): void {
- for (var index = 0; index < this._registeredMeshes.length; index++) {
- var registeredMesh = this._registeredMeshes[index];
- if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
- var body = registeredMesh.body.body;
- mesh.computeWorldMatrix(true);
- var center = mesh.getBoundingInfo().boundingBox.center;
- body.setPosition(center.x, center.y, center.z);
- body.setRotation(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z);
- return;
- }
- // Case where the parent has been updated
- if (registeredMesh.mesh.parent === mesh) {
- mesh.computeWorldMatrix(true);
- registeredMesh.mesh.computeWorldMatrix(true);
- var absolutePosition = registeredMesh.mesh.getAbsolutePosition();
- var absoluteRotation = mesh.rotation;
- body = registeredMesh.body.body;
- body.setPosition(absolutePosition.x, absolutePosition.y, absolutePosition.z);
- body.setRotation(absoluteRotation.x, absoluteRotation.y, absoluteRotation.z);
- return;
- }
- }
- }
- public applyImpulse(mesh: AbstractMesh, force: Vector3, contactPoint: Vector3): void {
- for (var index = 0; index < this._registeredMeshes.length; index++) {
- var registeredMesh = this._registeredMeshes[index];
- if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
- // Get object mass to have a behaviour similar to cannon.js
- var mass = registeredMesh.body.body.massInfo.mass;
- // The force is scaled with the mass of object
- registeredMesh.body.body.applyImpulse(contactPoint.scale(OIMO.INV_SCALE), force.scale(OIMO.INV_SCALE * mass));
- return;
- }
- }
- }
- public createLink(mesh1: AbstractMesh, mesh2: AbstractMesh, pivot1: Vector3, pivot2: Vector3, options?: any): boolean {
- var body1 = null,
- body2 = null;
- for (var index = 0; index < this._registeredMeshes.length; index++) {
- var registeredMesh = this._registeredMeshes[index];
- if (registeredMesh.mesh === mesh1) {
- body1 = registeredMesh.body.body;
- } else if (registeredMesh.mesh === mesh2) {
- body2 = registeredMesh.body.body;
- }
- }
- if (!body1 || !body2) {
- return false;
- }
- if (!options) {
- options = {};
- }
- new OIMO.Link({
- type: options.type,
- body1: body1,
- body2: body2,
- min: options.min,
- max: options.max,
- axe1: options.axe1,
- axe2: options.axe2,
- pos1: [pivot1.x, pivot1.y, pivot1.z],
- pos2: [pivot2.x, pivot2.y, pivot2.z],
- collision: options.collision,
- spring: options.spring,
- world: this._world
- });
- return true;
- }
- public dispose(): void {
- this._world.clear();
- while (this._registeredMeshes.length) {
- this.unregisterMesh(this._registeredMeshes[0].mesh);
- }
- }
- public isSupported(): boolean {
- return OIMO !== undefined;
- }
- private _getLastShape(body: any): any {
- var lastShape = body.shapes;
- while (lastShape.next) {
- lastShape = lastShape.next;
- }
- return lastShape;
- }
- public runOneStep(time: number): void {
- this._world.step();
- // Update the position of all registered meshes
- var i = this._registeredMeshes.length;
- var m;
- while (i--) {
- var body = this._registeredMeshes[i].body.body;
- var mesh = this._registeredMeshes[i].mesh;
- var delta = this._registeredMeshes[i].delta;
- if (!body.sleeping) {
- if (body.shapes.next) {
- var parentShape = this._getLastShape(body);
- mesh.position.x = parentShape.position.x * OIMO.WORLD_SCALE;
- mesh.position.y = parentShape.position.y * OIMO.WORLD_SCALE;
- mesh.position.z = parentShape.position.z * OIMO.WORLD_SCALE;
- var mtx = BABYLON.Matrix.FromArray(body.getMatrix());
- if (!mesh.rotationQuaternion) {
- mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
- }
- mesh.rotationQuaternion.fromRotationMatrix(mtx);
- mesh.computeWorldMatrix();
- } else {
- m = body.getMatrix();
- mtx = BABYLON.Matrix.FromArray(m);
- // Body position
- var bodyX = mtx.m[12],
- bodyY = mtx.m[13],
- bodyZ = mtx.m[14];
- if (!delta) {
- mesh.position.x = bodyX;
- mesh.position.y = bodyY;
- mesh.position.z = bodyZ;
- } else {
- mesh.position.x = bodyX + delta.x;
- mesh.position.y = bodyY + delta.y;
- mesh.position.z = bodyZ + delta.z;
- }
- if (!mesh.rotationQuaternion) {
- mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
- }
- mesh.rotationQuaternion.fromRotationMatrix(mtx);
- mesh.computeWorldMatrix();
- }
- }
- }
- }
- }
- }
|