Pitch/Yaw to Unit Vector

This is a discussion on Pitch/Yaw to Unit Vector within the C Programming forums, part of the General Programming Boards category; I've been looking around for a specific formula to create a Unit Vector representing the direction created by a given ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    Pitch/Yaw to Unit Vector

    I've been looking around for a specific formula to create a Unit Vector representing the direction created by a given Pitch/Yaw.

    The formula I see commonly is:
    Code:
    v = [ cos(y)*cos(p), sin(y), cos(y)*sin(p) ]
    Where v is the resulting unit vector, y is the yaw and p is the pitch.

    However, when plugging in the most simple test angles to make sure it's legitimate, I get a discrepancy. For example:

    Code:
    pitch = 45.0
    yaw = 45.0
    v = [ cos(45.0)*cos(45.0), sin(45.0), cos(45.0)*sin(45.0) ]
    v = [ 0.7071*0.7071, 0.7071, 0.7071*0.7071 ]
    v = [0.5, 0.7071, 0.5]
    Obviously the unit vector [0.5, 0.7071, 0.5] does not represent the pitch/yaw of 45.0/45.0 because any vector with orientations of 45 have matching values (i.e [3, 3, 3]).

    Is there a standard formula to convert Pitch/Yaw values into a Unit Vector? Am I doing something wrong with the formula above?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Math.h trigonometric functions take angles in radians as params.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean like something you'd find in a search engine, if you typed in "pitch yaw vector conversion"?

    Conversion between quaternions and Euler angles - Wikipedia, the free encyclopedia
    Convert Vector to Euler (Cardan) Angles - GameDev.Net Discussion Forums

    It would be silly of me to keep pasting in links at this point.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Denethor2000 View Post
    Obviously the unit vector [0.5, 0.7071, 0.5] does not represent the pitch/yaw of 45.0/45.0 because any vector with orientations of 45 have matching values (i.e [3, 3, 3]).
    If that was true, you would be right to be worried. Since it isn't, you don't need to be worried. (After all, if you did no yaw at all, you would have to have a 0 in the z coordinate, yes? So the fact that [0.707 0.707 0] doesn't have all three elements the same wouldn't worry you.)

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Quote Originally Posted by tabstop View Post
    If that was true, you would be right to be worried. Since it isn't, you don't need to be worried. (After all, if you did no yaw at all, you would have to have a 0 in the z coordinate, yes? So the fact that [0.707 0.707 0] doesn't have all three elements the same wouldn't worry you.)
    Sorry for being vague in my wording. What I mean is that a vector with a pitch of 45.0 and yaw of 45.0, of any length, will always have equal elements, just like a vector for a 2d object with an orientation of 45.0 will always have equal elements. I did not mean to say pitch OR yaw, rather pitch AND yaw.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Quote Originally Posted by claudiu View Post
    Math.h trigonometric functions take angles in radians as params.
    I'm aware of this and this is not a factor in my problem.

    Quote Originally Posted by quzah View Post
    You mean like something you'd find in a search engine, if you typed in "pitch yaw vector conversion"?

    Conversion between quaternions and Euler angles - Wikipedia, the free encyclopedia
    Convert Vector to Euler (Cardan) Angles - GameDev.Net Discussion Forums

    It would be silly of me to keep pasting in links at this point.


    Quzah.
    Thanks for the links, however the first explains the conversion between quaternions and euler angles and the second explains the conversion from vector to euler angles. Neither address my problem which involves the conversion from euler angles to unit vectors. I am aware of the existence of google and I have attempted to utilize it before asking on a message board.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Denethor2000 View Post
    Sorry for being vague in my wording. What I mean is that a vector with a pitch of 45.0 and yaw of 45.0, of any length, will always have equal elements, just like a vector for a 2d object with an orientation of 45.0 will always have equal elements. I did not mean to say pitch OR yaw, rather pitch AND yaw.
    A vector with a pitch and yaw AND ROLL of 45 degrees will have all three equal. A vector with two 45 degrees and the third something will have two that match and the missing one not.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Quote Originally Posted by tabstop View Post
    A vector with a pitch and yaw AND ROLL of 45 degrees will have all three equal. A vector with two 45 degrees and the third something will have two that match and the missing one not.
    A 3-element vector representing 3D direction does not include the roll. I believe the 4-element vector that includes the roll is called a quaternion, but I could be mistaken.

    The 3 elements in a vector representing a 3D direction represent the X, Y and Z of the vector's head relative to the vector's tail, which is relatively 0,0,0.
    Last edited by Denethor2000; 05-05-2010 at 06:49 PM.

  9. #9
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    The problem is that you are visualizing the rotation wrong - 45/45 doesn't mean an even rotation like 45 in 2D. The x and y rotations compound in the x and z directions (just like the equation says). I did a quick bit in Blender to show this effect. See the attached images. The block on the left is not rotated, the second has a x-rot of 45, and the third is rotated 45 in the x-axis then 45 in the y-axis.
    Attached Images Attached Images    
    Consider this post signed

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Denethor2000 View Post
    A 3-element vector representing 3D direction does not include the roll. I believe the 4-element vector that includes the roll is called a quaternion, but I could be mistaken.

    The 3 elements in a vector representing a 3D direction represent the X, Y and Z of the vector's head relative to the vector's tail, which is relatively 0,0,0.
    I guess the question then is "what do you think this vector represents"? If you are trying to get at the axis of the rotation, then yes you do need three angles to rotate around. If you are trying to say "this thing is the rotation" then you have a matrix, not a vector.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Ahh I think I understand the error in my thinking, and I'm glad to see someone has come and pointed it out for me too!

    The length of the XY vector [0.5, 0.5] is actually 0.7071, meaning the third parameter (Z) would actually have to be equal to 0.7071 and not 0.5.

    Thanks bernt.

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Quote Originally Posted by tabstop View Post
    I guess the question then is "what do you think this vector represents"? If you are trying to get at the axis of the rotation, then yes you do need three angles to rotate around. If you are trying to say "this thing is the rotation" then you have a matrix, not a vector.
    To clarify, I have a point (the origin of my 3d object) and am calculating a directional vector representing the velocity of my object that I can simply add together for the next step in a physics loop.

    Pseudocode:
    Code:
    float[3] myPoint = {0.0, 0.0, 0.0};
    float[3] myVelocity;
    
    void addForce(float pitch, float yaw, float power) {
        float[3] newVelocity;
        newVelocity[0] = cos(pitch)*cos(yaw)*power;
        newVelocity[1] = cos(pitch)*sin(yaw)*power;
        newVelocity[2] = sin(pitch)*power;
    
        myVelocity[0] += newVelocity[0];
        myVelocity[1] += newVelocity[1];
        myVelocity[2] += newVelocity[2];
    }
    
    void main(){
        while(1==1)
        {
            myPoint[0] += myVelocity[0];
            myPoint[1] += myVelocity[1];
            myPoint[2] += myVelocity[2];
        }
    }
    I didn't include air resistance or gravity for simplicity's sake. I use float[3] instead of vector objects because I'm coding in a c-based language that does not support bulk copy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21