# Thread: Pitch/Yaw to Unit Vector

1. ## 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. Math.h trigonometric functions take angles in radians as params.

3. 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.

4. Originally Posted by Denethor2000
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. Originally Posted by tabstop
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. Originally Posted by claudiu
Math.h trigonometric functions take angles in radians as params.
I'm aware of this and this is not a factor in my problem.

Originally Posted by quzah
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. Originally Posted by Denethor2000
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. Originally Posted by tabstop
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.

9. 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.

10. Originally Posted by Denethor2000
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. 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. Originally Posted by tabstop
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.