Jump to content


- - - - -

Extract Yaw angle from Matrix


  • You cannot reply to this topic
6 replies to this topic

#1 Crazys

    New Member

  • Members
  • Pip
  • 3 posts

Posted 02 May 2006 - 02:13 AM

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; 

}



#2 juhnu

    Valued Member

  • Members
  • PipPipPip
  • 292 posts

Posted 02 May 2006 - 02:24 AM

Why do you need this?

#3 Crazys

    New Member

  • Members
  • Pip
  • 3 posts

Posted 02 May 2006 - 02:37 AM

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?

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 02 May 2006 - 04:40 AM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 Crazys

    New Member

  • Members
  • Pip
  • 3 posts

Posted 02 May 2006 - 06:46 AM

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.

#6 kusma

    Valued Member

  • Members
  • PipPipPip
  • 163 posts

Posted 02 May 2006 - 09: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.

#7 roel

    Senior Member

  • Members
  • PipPipPipPip
  • 697 posts

Posted 02 May 2006 - 09: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)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users