PDA

View Full Version : Extract Yaw angle from Matrix


Crazys
05-01-2006, 07:13 PM
I have an interesting problem that I cannot seem to figure out ...

A unit vector V is the result of applying first a pitch and then yaw to the vector (0,0,1). Pitch is always between –89 degrees and 89 degrees.

I am writing a function which determines the yaw angle component (in radians) of this transform, given the transformed vector.

The problem with my code is that asin() returns a value from -PI/2 to PI/2 which is ambiguous for cases like 45 degrees and 135 degrees. How can I resolve this ambiguity?

Here is my code ...


float ExtractYaw(Vec &transformedVec)

{

/*
After applying pitch (of angle theta) to V the resulting vector is

| 0 |
| -sin(theta) |
| cos(theta) |
| 1 |

After applying yaw (of angle phi) to the above vector the resulting vector is

| sin(phi) * cos(theta) |
| -sin(theta) |
| cos(phi) * cos(theta) |
| 1 |

We can now solve for theta ...
*/

float theta = -asin(transformedVec.y);

// We can now use theta to solve for phi by using either x or z
// (I use x)

float cosTheta = cos(theta);
float phi = asin(transformedVec.x / cosTheta);

return phi;
}

juhnu
05-01-2006, 07:24 PM
Why do you need this?

Crazys
05-01-2006, 07:37 PM
I had this on a test I took, I didn't get it right and now I'm trying to figure out how to solve it. Any ideas?

Reedbeta
05-01-2006, 09:40 PM
It's not completely clear what yaw and pitch mean in your specific coordinate system - I'll assume you're considering +y to be up, so that a pitch is a rotation about the x axis and a yaw is a rotation about the y axis. In that case, to find the angle of rotation about y, all you need to compute is atan2(x, z). Visualize it as if you were looking down from above, with the x axis pointing up and the z axis pointing right (assuming right-handed coordinate system; if not, interchange x and z). You want to "smash" the vector into the x-z plane (i.e. ignore the y component) and then measure its angle from the +z axis. Basic trig tells us the tangent of this angle is x/z. Calling atan2(x, z) will compute this and also take care of rotating the angle into the correct quadrant if x and z are not both positive.

Crazys
05-01-2006, 11:46 PM
Reedbeta, thanks for your response. I had tried that, and it works better than my asin() method since it returns a value from -PI to PI, however if the original angle, phi, is outside of this range don't I get the same ambiguity?

Take for example 179 degrees and -181 degrees dont they both yield the same result when plugged into atan2()? If I am not mistaken, how can I determine what was the actual angle of rotation?

An as I type that I realize that ... a rotation by 179 degrees is the same as a rotation by -181 degrees ...

Thanks for your help.

kusma
05-02-2006, 02:02 AM
as you realized, you cannot as a matrix only represent the directions of the different axes in a coordinate system. 179 and -181 should therefore give the exact same matrix.

roel
05-02-2006, 02:04 AM
Ofcourse you can't distinguish whether it was 179 or -181 degrees, this information isn't contained in the matrix. (and it isn't very interesting either)