# How to convert Euler angles to directional vector?

### Question

I have pitch, roll, and yaw angles. How would I convert these to a directional vector?

It'd be especially cool if you can show me a quaternion and/or matrix representation of this!

1
45
9/19/2016 7:36:08 PM

Unfortunately there are different conventions on how to define these things (and roll, pitch, yaw are not quite the same as Euler angles), so you'll have to be careful.

If we define pitch=0 as horizontal (z=0) and yaw as counter-clockwise from the x axis, then the direction vector will be

```x = cos(yaw)*cos(pitch)
y = sin(yaw)*cos(pitch)
z = sin(pitch)
```

Note that I haven't used roll; this is direction unit vector, it doesn't specify attitude. It's easy enough to write a rotation matrix that will carry things into the frame of the flying object (if you want to know, say, where the left wing-tip is pointing), but it's really a good idea to specify the conventions first. Can you tell us more about the problem?

EDIT: (I've been meaning to get back to this question for two and a half years.)

For the full rotation matrix, if we use the convention above and we want the vector to yaw first, then pitch, then roll, in order to get the final coordinates in the world coordinate frame we must apply the rotation matrices in the reverse order.

First roll:

``````| 1    0          0      |
| 0 cos(roll) -sin(roll) |
| 0 sin(roll)  cos(roll) |
``````

then pitch:

``````| cos(pitch) 0 -sin(pitch) |
|     0      1      0      |
| sin(pitch) 0  cos(pitch) |
``````

then yaw:

``````| cos(yaw) -sin(yaw) 0 |
| sin(yaw)  cos(yaw) 0 |
|    0         0     1 |
``````

Combine them, and the total rotation matrix is:

``````| cos(yaw)cos(pitch) -cos(yaw)sin(pitch)sin(roll)-sin(yaw)cos(roll) -cos(yaw)sin(pitch)cos(roll)+sin(yaw)sin(roll)|
| sin(yaw)cos(pitch) -sin(yaw)sin(pitch)sin(roll)+cos(yaw)cos(roll) -sin(yaw)sin(pitch)cos(roll)-cos(yaw)sin(roll)|
| sin(pitch)          cos(pitch)sin(roll)                            cos(pitch)sin(roll)|
``````

So for a unit vector that starts at the x axis, the final coordinates will be:

``````x = cos(yaw)cos(pitch)
y = sin(yaw)cos(pitch)
z = sin(pitch)
``````

And for the unit vector that starts at the y axis (the left wing-tip), the final coordinates will be:

``````x = -cos(yaw)sin(pitch)sin(roll)-sin(yaw)cos(roll)
y = -sin(yaw)sin(pitch)sin(roll)+cos(yaw)cos(roll)
z =  cos(pitch)sin(roll)
``````
72
11/6/2018 5:42:30 PM

There are six different ways to convert three Euler Angles into a Matrix depending on the Order that they are applied:

``````typedef float Matrix;
struct EulerAngle { float X,Y,Z; };

// Euler Order enum.
enum EEulerOrder
{
ORDER_XYZ,
ORDER_YZX,
ORDER_ZXY,
ORDER_ZYX,
ORDER_YXZ,
ORDER_XZY
};

Matrix EulerAnglesToMatrix(const EulerAngle &inEulerAngle,EEulerOrder EulerOrder)
{
// Convert Euler Angles passed in a vector of Radians
// into a rotation matrix.  The individual Euler Angles are
// processed in the order requested.
Matrix Mx;

const FLOAT    Sx    = sinf(inEulerAngle.X);
const FLOAT    Sy    = sinf(inEulerAngle.Y);
const FLOAT    Sz    = sinf(inEulerAngle.Z);
const FLOAT    Cx    = cosf(inEulerAngle.X);
const FLOAT    Cy    = cosf(inEulerAngle.Y);
const FLOAT    Cz    = cosf(inEulerAngle.Z);

switch(EulerOrder)
{
case ORDER_XYZ:
Mx.M=Cy*Cz;
Mx.M=-Cy*Sz;
Mx.M=Sy;
Mx.M=Cz*Sx*Sy+Cx*Sz;
Mx.M=Cx*Cz-Sx*Sy*Sz;
Mx.M=-Cy*Sx;
Mx.M=-Cx*Cz*Sy+Sx*Sz;
Mx.M=Cz*Sx+Cx*Sy*Sz;
Mx.M=Cx*Cy;
break;

case ORDER_YZX:
Mx.M=Cy*Cz;
Mx.M=Sx*Sy-Cx*Cy*Sz;
Mx.M=Cx*Sy+Cy*Sx*Sz;
Mx.M=Sz;
Mx.M=Cx*Cz;
Mx.M=-Cz*Sx;
Mx.M=-Cz*Sy;
Mx.M=Cy*Sx+Cx*Sy*Sz;
Mx.M=Cx*Cy-Sx*Sy*Sz;
break;

case ORDER_ZXY:
Mx.M=Cy*Cz-Sx*Sy*Sz;
Mx.M=-Cx*Sz;
Mx.M=Cz*Sy+Cy*Sx*Sz;
Mx.M=Cz*Sx*Sy+Cy*Sz;
Mx.M=Cx*Cz;
Mx.M=-Cy*Cz*Sx+Sy*Sz;
Mx.M=-Cx*Sy;
Mx.M=Sx;
Mx.M=Cx*Cy;
break;

case ORDER_ZYX:
Mx.M=Cy*Cz;
Mx.M=Cz*Sx*Sy-Cx*Sz;
Mx.M=Cx*Cz*Sy+Sx*Sz;
Mx.M=Cy*Sz;
Mx.M=Cx*Cz+Sx*Sy*Sz;
Mx.M=-Cz*Sx+Cx*Sy*Sz;
Mx.M=-Sy;
Mx.M=Cy*Sx;
Mx.M=Cx*Cy;
break;

case ORDER_YXZ:
Mx.M=Cy*Cz+Sx*Sy*Sz;
Mx.M=Cz*Sx*Sy-Cy*Sz;
Mx.M=Cx*Sy;
Mx.M=Cx*Sz;
Mx.M=Cx*Cz;
Mx.M=-Sx;
Mx.M=-Cz*Sy+Cy*Sx*Sz;
Mx.M=Cy*Cz*Sx+Sy*Sz;
Mx.M=Cx*Cy;
break;

case ORDER_XZY:
Mx.M=Cy*Cz;
Mx.M=-Sz;
Mx.M=Cz*Sy;
Mx.M=Sx*Sy+Cx*Cy*Sz;
Mx.M=Cx*Cz;
Mx.M=-Cy*Sx+Cx*Sy*Sz;
Mx.M=-Cx*Sy+Cy*Sx*Sz;
Mx.M=Cz*Sx;
Mx.M=Cx*Cy+Sx*Sy*Sz;
break;
}
return(Mx);
}
``````

FWIW, some CPU's can compute Sin & Cos simultaneously (for example fsincos on x86). If you do this, you can make it a bit faster with three calls rather than 6 to compute the initial sin & cos values.

Update: There are actually 12 ways depending if you want right-handed or left-handed results -- you can change the "handedness" by negating the angles.