Game Development Community

dev|Pro Game Development Curriculum

Retrieve Roll/Pitch/Yaw From Transform Matrix

by Damian Sloane · 04/04/2007 (3:46 pm) · 4 comments

In my code I wanted to retrieve the the roll, pitch and yaw of the camera in a client-side script for use in an artifical horizion/compass.

After some piecing together of various code in the forums I managed to cobble together the following console function.

ConsoleFunction( GetEulerAngles, const char*, 2, 2, "(MatrixF xfrm) Retrieve euler angles from transformation matrix.")
{
   Point3F pos1;
   AngAxisF aa1(Point3F(0,0,0),0);
   dSscanf(argv[1], "%g %g %g %g %g %g %g", &pos1.x, &pos1.y, &pos1.z, &aa1.axis.x, &aa1.axis.y, &aa1.axis.z, &aa1.angle);

   MatrixF objTx(true);
   aa1.setMatrix(&objTx);
   objTx.setColumn(3, pos1);
   
   // Get rotations from transform matrix   
   
   VectorF vec;   
   objTx.getColumn(1,&vec);   
   
   // Get X-vector for roll calculation   
   
   Point3F xv;   
   objTx.getColumn(0,&xv);   
   
   // Calculate PRH (x = pitch, y = roll, z = heading)   
   
   Point3F rot(-mAtan(vec.z, mSqrt(vec.x*vec.x + vec.y*vec.y)), mDot(xv,Point3F(0,0,1)), -mAtan(-vec.x,vec.y) );      
   
   // Set up vars   
   
   F32 pitch = mRadToDeg(rot.x);	 // Pitch   
   F32 yaw = mRadToDeg(rot.z);		 // Heading   
   F32 roll = mRadToDeg(rot.y);		 // Roll

   char* ret = Con::getReturnBuffer(256);
   dSprintf(ret, 255, "%g %g %g", pitch, yaw + 180, roll);
   return ret;
}

Simply drop the code into your engine code. I stuck it in "math/mathTypes.cc" for need of a better location, but i'll tidy that up later.

Re-compile the engine and drop the following into a client side scirpt

%transform = localClientConnection.player.getEyeTransform();   
%angles = getEulerAngles(%transform);

%pitch = getWord(%angles, 0); 
%yaw = getWord(%angles, 1); 
%roll = getWord(%angles, 2);

Hopefully its helpful to somebody.

About the author

Recent Blogs


#1
04/07/2007 (1:13 pm)
handy, thanks.
#2
06/21/2007 (5:50 pm)
and very educational for the matrix-ignorant
#3
08/30/2007 (10:58 am)
How did you figure out how to get roll (or y)?
Camera transform matrix makes no sense to me,
especially that if you use toEuler function it will
give completely different results....
Thank You for the resource
#4
09/04/2007 (3:02 pm)
The pitch is not working correctly for me. The yaw and roll fields always return the same value that i set the heading to, but the pitch does not.

I'm setting the values by multiplying the quat by (0 0 1 y) then ( 0 1 0 p ) then ( 0 0 1 r).

Any thoughts?