瀏覽代碼

Merge pull request #363 from Temechon/patch-21

Update babylon.oimoJSPlugin.ts
David Catuhe 10 年之前
父節點
當前提交
237cc552b9
共有 1 個文件被更改,包括 40 次插入0 次删除
  1. 40 0
      Babylon/Physics/Plugins/babylon.oimoJSPlugin.ts

+ 40 - 0
Babylon/Physics/Plugins/babylon.oimoJSPlugin.ts

@@ -26,6 +26,14 @@ module BABYLON {
             // 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;
@@ -39,6 +47,13 @@ module BABYLON {
                     // 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],
@@ -48,6 +63,12 @@ module BABYLON {
                         config: [options.mass, options.friction, options.restitution],
                         world: this._world
                     });
+
+                    // Restore rotation
+                    if (initialRotation) {
+                        body.setQuaternion(initialRotation);
+                    }
+                    
                     this._registeredMeshes.push({
                         mesh: mesh,
                         body: body,
@@ -57,6 +78,14 @@ module BABYLON {
 
                 case BABYLON.PhysicsEngine.PlaneImpostor:
                 case BABYLON.PhysicsEngine.BoxImpostor:
+                    
+                    var 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;
@@ -68,6 +97,13 @@ module BABYLON {
                     // The delta between the mesh position and the mesh boudning 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: 'box',
                         size: [sizeX, sizeY, sizeZ],
@@ -78,6 +114,10 @@ module BABYLON {
                         world: this._world
                     });
 
+                    if (initialRotation) {
+                        body.setQuaternion(initialRotation);
+                    }
+
                     this._registeredMeshes.push({
                         mesh: mesh,
                         body: body,