Browse Source

changed some function signatures to make things more consistent within the Bone class

Adam Bowman 8 years ago
parent
commit
b5ad5331fd

+ 129 - 56
src/Bones/babylon.bone.ts

@@ -171,54 +171,84 @@
             return true;
         }
 
-        public translate (vec: Vector3): void {
+        public translate (vec: Vector3, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             var lm = this.getLocalMatrix();
 
-            lm.m[12] += vec.x;
-            lm.m[13] += vec.y;
-            lm.m[14] += vec.z;
+            if(space == Space.LOCAL){
+
+                lm.m[12] += vec.x;
+                lm.m[13] += vec.y;
+                lm.m[14] += vec.z;
+
+            }else{
+
+                this._skeleton.computeAbsoluteTransforms();
+
+                var tmat = Tmp.Matrix[0];
+                var vec = Tmp.Vector3[0];
+
+                if (mesh) {
+                    tmat.copyFrom(this._parent.getAbsoluteTransform());
+                    tmat.multiplyToRef(mesh.getWorldMatrix(), tmat);
+                }else {
+                    tmat.copyFrom(this._parent.getAbsoluteTransform());
+                }
+
+                tmat.invert();
+                Vector3.TransformCoordinatesToRef(vec, tmat, vec);
+
+                lm.m[12] += vec.x;
+                lm.m[13] += vec.y;
+                lm.m[14] += vec.z;
+
+            }
 
             this.markAsDirty();
 	        
         }
 
-        public setPosition (position: Vector3): void {
+        public setPosition (position: Vector3, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             var lm = this.getLocalMatrix();
 
-            lm.m[12] = position.x;
-            lm.m[13] = position.y;
-            lm.m[14] = position.z;
+            if(space == Space.LOCAL){
 
-            this.markAsDirty();
-	        
-        }
+                lm.m[12] = position.x;
+                lm.m[13] = position.y;
+                lm.m[14] = position.z;
 
-        public setAbsolutePosition (position: Vector3, mesh: AbstractMesh = null): void {
+            }else{
 
-            this._skeleton.computeAbsoluteTransforms();
+                this._skeleton.computeAbsoluteTransforms();
 
-            var tmat = Tmp.Matrix[0];
-            var vec = Tmp.Vector3[0];
+                var tmat = Tmp.Matrix[0];
+                var vec = Tmp.Vector3[0];
 
-            if (mesh) {
-                tmat.copyFrom(this._parent.getAbsoluteTransform());
-                tmat.multiplyToRef(mesh.getWorldMatrix(), tmat);
-            }else {
-                tmat.copyFrom(this._parent.getAbsoluteTransform());
-            }
+                if (mesh) {
+                    tmat.copyFrom(this._parent.getAbsoluteTransform());
+                    tmat.multiplyToRef(mesh.getWorldMatrix(), tmat);
+                }else {
+                    tmat.copyFrom(this._parent.getAbsoluteTransform());
+                }
 
-            tmat.invert();
-            Vector3.TransformCoordinatesToRef(position, tmat, vec);
+                tmat.invert();
+                Vector3.TransformCoordinatesToRef(position, tmat, vec);
 
-            var lm = this.getLocalMatrix();
-            lm.m[12] = vec.x;
-            lm.m[13] = vec.y;
-            lm.m[14] = vec.z;
+                lm.m[12] = vec.x;
+                lm.m[13] = vec.y;
+                lm.m[14] = vec.z;
+
+            }
 
             this.markAsDirty();
-			
+	        
+        }
+
+        public setAbsolutePosition(position:Vector3, mesh?: AbstractMesh){
+
+            this.setPosition(position, Space.WORLD, mesh);
+
         }
 
         public setScale (x: number, y: number, z: number, scaleChildren = false): void {
@@ -291,7 +321,7 @@
 
         }
 
-        public setYawPitchRoll (yaw: number, pitch: number, roll: number, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        public setYawPitchRoll (yaw: number, pitch: number, roll: number, space = Space.LOCAL, mesh?: AbstractMesh): void {
 	
             var rotMat = Tmp.Matrix[0];
             Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, rotMat);
@@ -306,7 +336,7 @@
             
         }
 
-        public rotate (axis: Vector3, amount: number, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        public rotate (axis: Vector3, amount: number, space = Space.LOCAL, mesh?: AbstractMesh): void {
             
             var rmat = Tmp.Matrix[0];
             rmat.m[12] = 0;
@@ -319,7 +349,7 @@
             
         }
 
-        public setAxisAngle (axis: Vector3, angle: number, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        public setAxisAngle (axis: Vector3, angle: number, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             var rotMat = Tmp.Matrix[0];
             Matrix.RotationAxisToRef(axis, angle, rotMat);
@@ -332,7 +362,7 @@
 
         }
 
-        public setRotationMatrix (rotMat: Matrix, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        public setRotationMatrix (rotMat: Matrix, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             var rotMatInv = Tmp.Matrix[0];
             
@@ -347,7 +377,7 @@
 
         }
 
-        private _rotateWithMatrix (rmat: Matrix, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        private _rotateWithMatrix (rmat: Matrix, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             var lmat = this.getLocalMatrix();
             var lx = lmat.m[12];
@@ -396,7 +426,7 @@
 
         }
 
-        private _getNegativeRotationToRef(rotMatInv: Matrix, space = Space.LOCAL, mesh: AbstractMesh = null): void {
+        private _getNegativeRotationToRef(rotMatInv: Matrix, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
             if (space == Space.WORLD) {
                 var scaleMatrix = Tmp.Matrix[2];
@@ -445,32 +475,60 @@
             
         }
 
-        public getAbsolutePosition (mesh: AbstractMesh = null): Vector3 {
+        public getPosition (space = Space.LOCAL, mesh?: AbstractMesh): Vector3 {
 
             var pos = Vector3.Zero();
 
-            this.getAbsolutePositionToRef(mesh, pos);
+            this.getPositionToRef(pos, space, mesh);
 
             return pos;
 
         }
 
-        public getAbsolutePositionToRef (mesh: AbstractMesh = null, result: Vector3): void {
+        public getPositionToRef (result: Vector3, space = Space.LOCAL, mesh?: AbstractMesh): void {
 
-            this._skeleton.computeAbsoluteTransforms();
-            
-            var tmat = Tmp.Matrix[0];
+            if(space == Space.LOCAL){
+
+                var lm = this.getLocalMatrix();
+
+                result.x = lm.m[12];
+                result.y = lm.m[13];
+                result.z = lm.m[14];
 
-            if (mesh) {
-                tmat.copyFrom(this.getAbsoluteTransform());
-                tmat.multiplyToRef(mesh.getWorldMatrix(), tmat);
             }else{
-                tmat = this.getAbsoluteTransform();
+                
+                this._skeleton.computeAbsoluteTransforms();
+                
+                var tmat = Tmp.Matrix[0];
+
+                if (mesh) {
+                    tmat.copyFrom(this.getAbsoluteTransform());
+                    tmat.multiplyToRef(mesh.getWorldMatrix(), tmat);
+                }else{
+                    tmat = this.getAbsoluteTransform();
+                }
+
+                result.x = tmat.m[12];
+                result.y = tmat.m[13];
+                result.z = tmat.m[14];
+
             }
 
-            result.x = tmat.m[12];
-            result.y = tmat.m[13];
-            result.z = tmat.m[14];
+        }
+
+        public getAbsolutePosition (mesh?: AbstractMesh): Vector3 {
+
+            var pos = Vector3.Zero();
+
+            this.getPositionToRef(pos, Space.WORLD, mesh);
+
+            return pos;
+
+        }
+
+        public getAbsolutePositionToRef (result: Vector3, mesh?: AbstractMesh) {
+
+            this.getPositionToRef(result, Space.WORLD, mesh);
 
         }
 
@@ -533,7 +591,7 @@
 
         }
 
-        public getDirectionToRef (localAxis: Vector3, result: Vector3, mesh?: AbstractMesh): void {
+        public getDirectionToRef (result: Vector3, localAxis: Vector3, mesh?: AbstractMesh): void {
 
             this._skeleton.computeAbsoluteTransforms();
             
@@ -551,26 +609,41 @@
 
         }
 
-        public getRotation(mesh: AbstractMesh): Quaternion {
+        public getRotation(space = Space.LOCAL, mesh?: AbstractMesh): Quaternion {
 
             var result = Quaternion.Identity();
 
-            this.getRotationToRef(mesh, result);
+            this.getRotationToRef(result, space, mesh);
 
             return result;
 
         }
 
-        public getRotationToRef(mesh: AbstractMesh, result: Quaternion): void{
+        public getRotationToRef( result: Quaternion, space = Space.LOCAL, mesh?: AbstractMesh): void{
+
+            if(space == Space.LOCAL){
 
-            var mat = Tmp.Matrix[0];
-            var amat = this.getAbsoluteTransform();
-            var wmat = mesh.getWorldMatrix();
+                this.getLocalMatrix().decompose(Tmp.Vector3[0], result, Tmp.Vector3[1]);
+
+            }else{
 
-            amat.multiplyToRef(wmat, mat);
+                var mat = Tmp.Matrix[0];
+                var amat = this.getAbsoluteTransform();
 
-            mat.decompose(Tmp.Vector3[0], result, Tmp.Vector3[1]);
+                if(mesh){
 
+                    var wmat = mesh.getWorldMatrix();
+                    amat.multiplyToRef(wmat, mat);
+
+                    mat.decompose(Tmp.Vector3[0], result, Tmp.Vector3[1]);
+
+                }else{
+
+                    amat.decompose(Tmp.Vector3[0], result, Tmp.Vector3[1]);
+
+                }
+
+            }
         }
 
     }

+ 4 - 4
src/Bones/babylon.boneIKController.ts

@@ -65,9 +65,9 @@ module BABYLON {
             
                 mesh.computeWorldMatrix(true);
 
-                var pos1 = this._bone2.children[0].getAbsolutePosition(mesh);
-                var pos2 = this._bone2.getAbsolutePosition(mesh);
-                var pos3 = this._bone1.getAbsolutePosition(mesh);
+                var pos1 = this._bone2.children[0].getPosition(Space.WORLD, mesh);
+                var pos2 = this._bone2.getPosition(Space.WORLD, mesh);
+                var pos3 = this._bone1.getPosition(Space.WORLD, mesh);
 
                 this._bone1Length = Vector3.Distance(pos1, pos2);
                 this._bone2Length = Vector3.Distance(pos2, pos3);
@@ -111,7 +111,7 @@ module BABYLON {
             var mat1 = this._tmpMat1;
             var mat2 = this._tmpMat2;
 
-            bone1.getAbsolutePositionToRef(this.mesh, bonePos);
+            bone1.getPositionToRef(bonePos, Space.WORLD, this.mesh);
 
             poleTarget.subtractToRef(bonePos, upAxis);
 

+ 1 - 1
src/Bones/babylon.boneLookController.ts

@@ -42,7 +42,7 @@ module BABYLON {
             var mat1 = this._tmpMat1;
             var mat2 = this._tmpMat2;
 
-            bone.getAbsolutePositionToRef(this.mesh, bonePos);
+            bone.getPositionToRef(bonePos, Space.WORLD, this.mesh);
 
             target.subtractToRef(bonePos, zaxis);
             zaxis.normalize();

+ 2 - 2
src/Debug/babylon.skeletonViewer.ts

@@ -90,8 +90,8 @@
                     points = [Vector3.Zero(), Vector3.Zero()];
                     this._debugLines[boneNum] = points;
                 }
-                childBone.getAbsolutePositionToRef(this.mesh, points[0]);
-				parentBone.getAbsolutePositionToRef(this.mesh, points[1]);
+                childBone.getPositionToRef(points[0], Space.WORLD, this.mesh);
+				parentBone.getPositionToRef(points[1], Space.WORLD, this.mesh);
                 boneNum++;
             }
         }

+ 2 - 2
src/babylon.node.ts

@@ -346,12 +346,12 @@
         public getDirection(localAxis:BABYLON.Vector3): BABYLON.Vector3 {
             var result = BABYLON.Vector3.Zero();
 
-            this.getDirectionToRef(localAxis, result);
+            this.getDirectionToRef(result, localAxis);
             
             return result;
         }
 
-        public getDirectionToRef(localAxis:BABYLON.Vector3, result:BABYLON.Vector3): void {
+        public getDirectionToRef(result:BABYLON.Vector3, localAxis:BABYLON.Vector3): void {
             BABYLON.Vector3.TransformNormalToRef(localAxis, this.getWorldMatrix(), result);
         }