Browse Source

Merge pull request #3195 from abow/fix_setparent

fixed #3187
David Catuhe 7 years ago
parent
commit
8f752185e4
1 changed files with 42 additions and 17 deletions
  1. 42 17
      src/Mesh/babylon.transformNode.ts

+ 42 - 17
src/Mesh/babylon.transformNode.ts

@@ -425,18 +425,20 @@ module BABYLON {
         }        
         }        
 
 
         /**
         /**
-         * Defines the passed mesh as the parent of the current mesh.  
-         * Returns the AbstractMesh.  
+         * Defines the passed node as the parent of the current node.  
+         * Returns the TransformNode.
          */
          */
-        public setParent(mesh: Nullable<AbstractMesh>): TransformNode {
-            var parent = (<AbstractMesh>mesh);
-
-            if (mesh == null) {
-
+        public setParent(node: Nullable<TransformNode>): TransformNode {
+            
+            if (node == null) {
                 var rotation = Tmp.Quaternion[0];
                 var rotation = Tmp.Quaternion[0];
                 var position = Tmp.Vector3[0];
                 var position = Tmp.Vector3[0];
                 var scale = Tmp.Vector3[1];
                 var scale = Tmp.Vector3[1];
-
+                
+                if(this.parent && (<TransformNode>this.parent).computeWorldMatrix){
+                    (<TransformNode>this.parent).computeWorldMatrix(true);
+                }
+                this.computeWorldMatrix(true);              
                 this.getWorldMatrix().decompose(scale, rotation, position);
                 this.getWorldMatrix().decompose(scale, rotation, position);
 
 
                 if (this.rotationQuaternion) {
                 if (this.rotationQuaternion) {
@@ -448,18 +450,41 @@ module BABYLON {
                 this.position.x = position.x;
                 this.position.x = position.x;
                 this.position.y = position.y;
                 this.position.y = position.y;
                 this.position.z = position.z;
                 this.position.z = position.z;
-
             } else {
             } else {
-
+                var rotation = Tmp.Quaternion[0];
                 var position = Tmp.Vector3[0];
                 var position = Tmp.Vector3[0];
-                var m1 = Tmp.Matrix[0];
-
-                parent.getWorldMatrix().invertToRef(m1);
-                Vector3.TransformCoordinatesToRef(this.position, m1, position);
-
-                this.position.copyFrom(position);
+                var scale = Tmp.Vector3[1];
+                var m0 = Tmp.Matrix[0];
+                var m1 = Tmp.Matrix[1];
+                var invParentMatrix = Tmp.Matrix[2];
+                
+                node.computeWorldMatrix(true);
+                node.getWorldMatrix().decompose(scale, rotation, position);
+                
+                rotation.toRotationMatrix(m0);
+                m1.setTranslation(position);   
+                m1.multiplyToRef(m0, m0);     
+                m0.invertToRef(invParentMatrix);
+                
+                this.getWorldMatrix().multiplyToRef(invParentMatrix, m0);        
+                m0.decompose(scale, rotation, position);
+                
+                if (this.rotationQuaternion) {
+                    this.rotationQuaternion.copyFrom(rotation);
+                } else {
+                    rotation.toEulerAnglesToRef(this.rotation);
+                }
+                
+                node.getWorldMatrix().invertToRef(invParentMatrix);
+                this.getWorldMatrix().multiplyToRef(invParentMatrix, m0);
+                m0.decompose(scale, rotation, position);
+                
+                this.position.x = position.x;
+                this.position.y = position.y;
+                this.position.z = position.z;
             }
             }
-            this.parent = parent;
+            
+            this.parent = node;
             return this;
             return this;
         }       
         }