How to Convert Euler Angles to Directional Vector

How to convert Euler angles to directional vector?

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)

How to convert direction vector to euler angles?

Let's see if I understand correctly. This is about the orientation of a rigid body in three dimensional space, like an air plane during flight. The nose of that airplane points towards the direction vector

D=(XD,YD,ZD) .

Towards the roof is the up vector

U=(XU,YU,ZU) .

Then heading H would be the direction vector D projected onto the earth surface:

H=(XD,YD,0) ,

with an associated angle

angle_H=atan2(YD,XD) .

Pitch P would be the up/down angle of the nose with respect to the horizon, if the direction vector D is normalized you get it from

ZD=sin(angle_P)

resulting in

angle_P=asin(ZD) .

Finally, for the bank angle we consider the direction of the wings, assuming the wings are perpendicular to the body. If the plane flies straight towards D, the wings point perpendicular to D and parallel to the earth surface:

W0 = ( -YD, XD, 0 )

This would be a bank angle of 0. The expected Up Vector would be perpendicular to W0 and perpendicular to D

U0 = W0 × D

with × denoting the cross product. U equals U0 if the bank angle is zero, otherwise the angle between U and U0 is the bank angle angle_B, which can be calculated from

cos(angle_B) = Dot(U0,U) / abs(U0) / abs(U)
sin(angle_B) = Dot(W0,U) / abs(W0) / abs(U) .

Here 'abs' calculates the length of the vector. From that you get the bank angle as

angle_B = atan2( Dot(W0,U) / abs(W0), Dot(U0,U) / abs(U0) ) .

The normalization factors cancel each other if U and D are normalized.

Euler Coordinate to Directional Vector

Build the rotation matrices for yaw, pitch and roll, and multiply them. The resulting product is a matrix with the rotated coordinate system base vectors as its columns. However the vectors are not guaranteed to be orthogonal. In fact it may happen that two or all three of them become colinear due to an effect called gimbal lock. Gimbal lock is also the reason why you shouldn't use Euler rotations at all.

How to convert Euler Angles to Front, Up, Right vectors

Here is a full working example. It isn't very C++-like. You would probably want to use a real matrix class, but it should be ok for demonstration purposes. One thing that isn't clear from your question is the rotation order, but that can easily be changed.

#include <iostream>
#include <cmath>
#include <cstdlib>

typedef float Float;
typedef Float Axis[3];
typedef Axis Axes[3];

static void copy(const Axes &from,Axes &to)
{
for (size_t i=0; i!=3; ++i) {
for (size_t j=0; j!=3; ++j) {
to[i][j] = from[i][j];
}
}
}

static void mul(Axes &mat,Axes &b)
{
Axes result;
for (size_t i=0; i!=3; ++i) {
for (size_t j=0; j!=3; ++j) {
Float sum = 0;
for (size_t k=0; k!=3; ++k) {
sum += mat[i][k]*b[k][j];
}
result[i][j] = sum;
}
}
copy(result,mat);
}

static void getAxes(Axes &result,Float yaw,Float pitch,Float roll)
{
Float x = -pitch;
Float y = yaw;
Float z = -roll;
Axes matX = {
{1, 0, 0 },
{0, cos(x),sin(x)},
{0,-sin(x),cos(x)}
};
Axes matY = {
{cos(y),0,-sin(y)},
{ 0,1, 0},
{sin(y),0, cos(y)}
};
Axes matZ = {
{ cos(z),sin(z),0},
{-sin(z),cos(z),0},
{ 0, 0,1}
};
Axes axes = {
{1,0,0},
{0,1,0},
{0,0,1}
};

mul(axes,matX);
mul(axes,matY);
mul(axes,matZ);

copy(axes,result);
}


static void showAxis(const char *desc,const Axis &axis,Float sign)
{
std::cout << " " << desc << " = (";
for (size_t i=0; i!=3; ++i) {
if (i!=0) {
std::cout << ",";
}
std::cout << axis[i]*sign;
}
std::cout << ")\n";
}

static void showAxes(const char *desc,Axes &axes)
{
std::cout << desc << ":\n";
showAxis("front",axes[2],1);
showAxis("right",axes[0],-1);
showAxis("up",axes[1],1);
}

int main(int,char**)
{
Axes axes;
std::cout.setf(std::ios::fixed);
std::cout.precision(1);
getAxes(axes,0,0,0);
showAxes("yaw=0, pitch=0, roll=0",axes);
getAxes(axes,M_PI/2,0,0);
showAxes("yaw=90, pitch=0, roll=0",axes);
getAxes(axes,0,M_PI/2,0);
showAxes("yaw=0, pitch=90, roll=0",axes);
getAxes(axes,0,0,M_PI/2);
showAxes("yaw=0, pitch=0, roll=90",axes);
return 0;
}


Related Topics



Leave a reply



Submit