浏览代码

Merge pull request #268 from Temechon/patch-12

Update oimo.js and cannon.js
deltakosh 11 年之前
父节点
当前提交
f8a755e426
共有 1 个文件被更改,包括 36 次插入10 次删除
  1. 36 10
      Babylon/Physics/Plugins/babylon.oimoJSPlugin.ts

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

@@ -35,18 +35,23 @@ module BABYLON {
                         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);
+
                     body = new OIMO.Body({
                         type: 'sphere',
                         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],
                         move: options.mass != 0,
                         config: [options.mass, options.friction, options.restitution],
                         world: this._world
                     });
                     this._registeredMeshes.push({
-                        mesh: mesh,
-                        body: body
+                        mesh:       mesh,
+                        body:       body,
+                        delta:  deltaPosition
                     });
                     break;
 
@@ -59,10 +64,14 @@ module BABYLON {
                     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
+                    var deltaPosition = mesh.position.subtract(bbox.center);
+
                     body = new OIMO.Body({
                         type: 'box',
                         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],
                         move: options.mass != 0,
                         config: [options.mass, options.friction, options.restitution],
@@ -70,8 +79,9 @@ module BABYLON {
                     });
 
                     this._registeredMeshes.push({
-                        mesh: mesh,
-                        body: body
+                        mesh:   mesh,
+                        body:   body,
+                        delta:  deltaPosition
                     });
                     break;
 
@@ -193,7 +203,9 @@ module BABYLON {
                 var registeredMesh = this._registeredMeshes[index];
                 if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
                     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);
                     return;
                 }
@@ -289,6 +301,8 @@ module BABYLON {
 
                 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) {
@@ -306,9 +320,21 @@ module BABYLON {
                     } else {
                         m = body.getMatrix();
                         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) {
                             mesh.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 1);