Browse Source

Update oimo.js

Mesh position and bounding box position taken into account. Now the physics body is at the bounding box position and not at the mesh position (generally the same in all cases)
Temechon 11 năm trước cách đây
mục cha
commit
abe0781ae7

+ 36 - 10
Babylon/Physics/Plugins/babylon.oimoJSPlugin.ts

@@ -35,18 +35,23 @@ module BABYLON {
                         this._checkWithEpsilon(radiusX),
                         this._checkWithEpsilon(radiusX),
                         this._checkWithEpsilon(radiusY),
                         this._checkWithEpsilon(radiusY),
                         this._checkWithEpsilon(radiusZ))/2;
                         this._checkWithEpsilon(radiusZ))/2;
+
+                    // The delta between the mesh position and the mesh bounding box center
+                    var deltaPosition = mesh.position.subtract(bbox.center);
+
                     body = new OIMO.Body({
                     body = new OIMO.Body({
                         type: 'sphere',
                         type: 'sphere',
                         size: [size],
                         size: [size],
-                        pos: [mesh.position.x, mesh.position.y, mesh.position.z],
+                        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],
                         rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD],
                         move: options.mass != 0,
                         move: options.mass != 0,
                         config: [options.mass, options.friction, options.restitution],
                         config: [options.mass, options.friction, options.restitution],
                         world: this._world
                         world: this._world
                     });
                     });
                     this._registeredMeshes.push({
                     this._registeredMeshes.push({
-                        mesh: mesh,
-                        body: body
+                        mesh:       mesh,
+                        body:       body,
+                        delta:  deltaPosition
                     });
                     });
                     break;
                     break;
 
 
@@ -59,10 +64,14 @@ module BABYLON {
                     var sizeX = this._checkWithEpsilon(box.x);
                     var sizeX = this._checkWithEpsilon(box.x);
                     var sizeY = this._checkWithEpsilon(box.y);
                     var sizeY = this._checkWithEpsilon(box.y);
                     var sizeZ = this._checkWithEpsilon(box.z);
                     var sizeZ = this._checkWithEpsilon(box.z);
+
+                    // The delta between the mesh position and the mesh boudning box center
+                    var deltaPosition = mesh.position.subtract(bbox.center);
+
                     body = new OIMO.Body({
                     body = new OIMO.Body({
                         type: 'box',
                         type: 'box',
                         size: [sizeX, sizeY, sizeZ],
                         size: [sizeX, sizeY, sizeZ],
-                        pos: [mesh.position.x, mesh.position.y, mesh.position.z],
+                        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],
                         rot: [mesh.rotation.x / OIMO.TO_RAD, mesh.rotation.y / OIMO.TO_RAD, mesh.rotation.z / OIMO.TO_RAD],
                         move: options.mass != 0,
                         move: options.mass != 0,
                         config: [options.mass, options.friction, options.restitution],
                         config: [options.mass, options.friction, options.restitution],
@@ -70,8 +79,9 @@ module BABYLON {
                     });
                     });
 
 
                     this._registeredMeshes.push({
                     this._registeredMeshes.push({
-                        mesh: mesh,
-                        body: body
+                        mesh:   mesh,
+                        body:   body,
+                        delta:  deltaPosition
                     });
                     });
                     break;
                     break;
 
 
@@ -193,7 +203,9 @@ module BABYLON {
                 var registeredMesh = this._registeredMeshes[index];
                 var registeredMesh = this._registeredMeshes[index];
                 if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
                 if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
                     var body = registeredMesh.body.body;
                     var body = registeredMesh.body.body;
-                    body.setPosition(mesh.position.x, mesh.position.y, mesh.position.z);
+
+                    var center = mesh.getBoundingInfo().boundingBox.center;
+                    body.setPosition(center.x, center.y, center.z);
                     body.setOrientation(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z);
                     body.setOrientation(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z);
                     return;
                     return;
                 }
                 }
@@ -289,6 +301,8 @@ module BABYLON {
 
 
                 var body = this._registeredMeshes[i].body.body;
                 var body = this._registeredMeshes[i].body.body;
                 var mesh = this._registeredMeshes[i].mesh;
                 var mesh = this._registeredMeshes[i].mesh;
+                var delta  = this._registeredMeshes[i].delta;
+
                 if (!body.sleeping) {
                 if (!body.sleeping) {
 
 
                     if (body.shapes.next) {
                     if (body.shapes.next) {
@@ -306,9 +320,21 @@ module BABYLON {
                     } else {
                     } else {
                         m = body.getMatrix();
                         m = body.getMatrix();
                         mtx = BABYLON.Matrix.FromArray(m);
                         mtx = BABYLON.Matrix.FromArray(m);
-                        mesh.position.x = mtx.m[12];
-                        mesh.position.y = mtx.m[13];
-                        mesh.position.z = mtx.m[14];
+
+                        // 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) {
                         if (!mesh.rotationQuaternion) {
                             mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);
                             mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);