Game Development Community

Code translate

by Robert Pinter · in General Discussion · 03/06/2013 (10:06 pm) · 2 replies

Hi,

Can somebody translat it from XNA to Torque3D please?

I did already, but I think I do something wrong.

XNA Code

view plaincopy to clipboardprint?

    Vector3 s;  
                Quaternion r;  
                Vector3 t;  
                if (!worldTransform.Decompose(out s, out r, out t))  
                    return;  
      
                var currentPos = positionResolver(currentJointID);  
                var nextPos = positionResolver(nextJointID);  
      
                var shoulderToElbowDir = Vector3.Normalize(nextPos - currentPos);  
      
                var angle = (float)Math.Acos(Vector3.Dot(Vector3.Right, shoulderToElbowDir));  
                var axis = Vector3.Normalize(Vector3.Cross(Vector3.Right, shoulderToElbowDir));  
      
                worldTransform = Matrix.CreateScale(s) * Matrix.CreateFromAxisAngle(axis, angle) * Matrix.CreateTranslation(t);

My translation

view plaincopy to clipboardprint?

    MatrixF mScale(true);  
                mScale.scale(WorldTransform->getScale());  
                  
                MatrixF mPosition(true);  
                mPosition.setPosition(WorldTransform->getPosition());  
                  
              
                //Calculate Joint Direction           
                Point3F shoulderToElbowDir = mNormalize(NextPos - CurrentPos);  
                Point3F RightVec = MatrixF(true).getRightVector();  
                //Calculate Angle                         
                F32 angle = acos(mDot(RightVec, shoulderToElbowDir));  
                //Calculate Axis  
                Point3F axis = mNormalize(mCross(RightVec, shoulderToElbowDir));  
                //Create AngAxis rotation  
                AngAxisF aa(axis,angle);  
                MatrixF RotMat;  
                aa.setMatrix(&RotMat);  
      
               WorldTransform = &(mScale * RotMat * mPosition);

Thanks

#1
03/07/2013 (6:24 am)
In the C++ code you have:
Point3F shoulderToElbowDir = mNormalize(NextPos - CurrentPos);
Where is NextPos and CurrentPos defined?
#2
03/07/2013 (5:33 pm)
Hey Lukas,

So the the original story is, I have implemented Microsoft Kinect SDK into the Torque 1.2 engine and after I successfully represent my motion by circles (for joints) and lines (for bones) in Torque, I did a function which is controlling an avatar, but I lost with the right axis and angle calculation.

So in the first step the 3d coordinates are coming from the Kinect skeleton frame joint position.

And this 3d coordinate must be convert from 4d (kinect) to Point3F and the Y and Z axis must be exchange because the Kinect coordinate system uses Y up and the Torque uses Z up.

Point3F UserTracking::PosV4toP3F(Vector4 KinectPos)
{

	return Point3F(KinectPos.x,KinectPos.z,KinectPos.y);

}

So I have a HandleJoint function which takes the WorldTransformMatrix and CurrentPos and NextPos and called on this way within the skeleton frame. The WorldTransformMatrix is the original BindPos Matrix.

switch (Bones)
					{
					case SHOULDER_RIGHT:
						{
							HandleJoint(WorldTransform,PosV4toP3F(pSkel->SkeletonPositions[NUI_SKELETON_POSITION_SHOULDER_RIGHT]),PosV4toP3F(pSkel->SkeletonPositions[NUI_SKELETON_POSITION_ELBOW_RIGHT]));


The HandleJoint looks like as below

void UserTracking::HandleJoint(MatrixF *&WorldTransform, Point3F CurrentPos, Point3F NextPos)
{
			
			MatrixF mScale;
			//mScale.scale(c);
			
            MatrixF mPosition(true);
			mPosition.setPosition(WorldTransform->getPosition());
			
		
			//Calculate Joint Direction			
            Point3F shoulderToElbowDir = mNormalize(NextPos - CurrentPos);
			Point3F RightVec = MatrixF(true).getRightVector();
			//Calculate Angle						
            F32 angle = acos(mDot(RightVec, shoulderToElbowDir));
			//Calculate Axis
            Point3F axis = mNormalize(mCross(RightVec, shoulderToElbowDir));
			//Create AngAxis rotation
			AngAxisF aa(axis,angle);
			MatrixF RotMat(true);
			aa.setMatrix(&RotMat);

           WorldTransform=&(RotMat.mul(mPosition));
		   WorldTransform->mul(Point4F(WorldTransform->getScale().x,WorldTransform->getScale().y,WorldTransform->getScale().z,1));
}



In the XNA it works fine, as mine is working and the skeleton is moving, but the bones are moving on wired mode, stretching, twisting on wrong way.

So I am sure the angle and axis calculation is wrong, hard to debug.