Roll, Pitch, and Yaw of a camera
by Nathan Bowhay - ESAL · in Torque 3D Professional · 08/23/2011 (4:12 pm) · 4 replies
I am trying to get the roll pitch and yaw of a camera. I have managed to get the pitch and yaw by simply doing this:
Since the transform has the angle in angle axis form, I just needed to use the conversion function.
I also needed to look at the yaw and if it was greater than 180 multiply the pitch by -1 because for some reason it flips and I want absolute roll pitch yaw.
So that gives me yaw and pitch, but how do I get roll?
At first I thought that Euler was the same as Yaw Pitch Roll, but it seems that AngleAxisToEuler gives me rotations on axis when it doesn't appear there is any rotation.
I also found this: sunday-lab.blogspot.com/2008/04/get-pitch-yaw-roll-from-quaternion.html and tried using it so I just used the Quaternion ctor to convert from angle axis to quaternion and then to yaw, pitch roll, but again the rotations seemed to be off.
I also found www.garagegames.com/community/forums/viewthread/59072 , but the function seems to take 2 args for mAtan and only one for mSqrt.
Hopefully some can shed some light on on all this.
%transform = %camera.getTransform(); %AngleAxis = getWords(%transform, 3); %AngleAxis = setWord(%AngleAxis, 3, mRadToDeg(getWord(%AngleAxis, 3))); %rot = AngleAxisToPitchYaw(%AngleAxis);
Since the transform has the angle in angle axis form, I just needed to use the conversion function.
I also needed to look at the yaw and if it was greater than 180 multiply the pitch by -1 because for some reason it flips and I want absolute roll pitch yaw.
So that gives me yaw and pitch, but how do I get roll?
At first I thought that Euler was the same as Yaw Pitch Roll, but it seems that AngleAxisToEuler gives me rotations on axis when it doesn't appear there is any rotation.
I also found this: sunday-lab.blogspot.com/2008/04/get-pitch-yaw-roll-from-quaternion.html and tried using it so I just used the Quaternion ctor to convert from angle axis to quaternion and then to yaw, pitch roll, but again the rotations seemed to be off.
I also found www.garagegames.com/community/forums/viewthread/59072 , but the function seems to take 2 args for mAtan and only one for mSqrt.
Hopefully some can shed some light on on all this.
#2
08/25/2011 (11:53 am)
Yeah that doesn't get the correct roll either, hmm. If anyone knows how to get the local roll rotation that would be awesome. I really need to brush up on rotations.
#3
09/27/2011 (3:54 pm)
I also posted on gamedev with some more info: www.gamedev.net/topic/609224-roll-pitch-yaw-rotation-from-angle-axis/page__view_...
#4
11/08/2011 (11:20 am)
Ok I think I got the function converted correctly and it works, not sure how much different it is then what Cris said, Maybe I messed something up the first time.DefineConsoleFunction( TransformToRollPitchYaw, Point3F, ( TransformF trans),,
"@brief Converts the transform's rotation to Roll, Pitch, Yaw.\n\n"
"@param trans Transform to be converted.\n"
"@return Roll, Pitch, and Yaw as a Point3F.\n"
"@ingroup Math" )
{
// Get current object transform matrix
MatrixF objTx = trans.getMatrix();
// 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(-mAtan2(vec.z, mSqrt(vec.x*vec.x + vec.y*vec.y)), mDot(xv,Point3F(0,0,1)), -mAtan2(-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
return Point3F(roll, pitch, yaw);
}
Associate Chris Haigler
Jester Dance
DefineEngineFunction( GetEulerAngles, EulerF, (Point3F pos1, AngAxisF aa1),, "Retrieves Euler from transformation matrix." ) { MatrixF objTx(true); aa1.setMatrix(&objTx); objTx.setColumn(3, pos1); VectorF vec; objTx.getColumn(1, &vec); Point3F xv; objTx.getColumn(0, &xv); Point3F rot(-mAtan2(vec.z, mSqrt(vec.x*vec.x + vec.y*vec.y)), mDot(xv,Point3F(0,0,1)), -mAtan2(-vec.x,vec.y) ); F32 pitch = mRadToDeg(rot.x); F32 yaw = mRadToDeg(rot.z); F32 roll = mRadToDeg(rot.y); Point3F ret(pitch, (yaw + 180.0f), roll); return ret; }Usage will be something like: GetEulerAngles(<objectId>.position, <objectId>.rotation);