# How to obtain FRONT, UP vec from yaw pitch roll angles

## Recommended Posts

WiredCat    1451
hello there,

I want to create basic flight sim, but i have a problem with calculating vectors from pitch , roll, yaw angles

I use a rotation formula from one of books

For non zero CENTER_POINT

[quote]void __fastcall TachoGLModel::RotateModel(int dimension, float angle)
{

int i;

for (i = 0; i < VBO_V.Length; i++) {
p = vectors_add(VBO_V[i], reverse_point(CENTER_POINT)); //punkt przeniesiony na srodek bryly

if (dimension == 0) { //XZ
VBO_V[i].x = cos(angle*imopi)*p.x - sin(angle*imopi)*p.z;
VBO_V[i].z = sin(angle*imopi)*p.x + cos(angle*imopi)*p.z;
}

if (dimension == 1) { //XY
VBO_V[i].x = cos(angle*imopi)*p.x - sin(angle*imopi)*p.y;
VBO_V[i].y = sin(angle*imopi)*p.x + cos(angle*imopi)*p.y;
}

if (dimension == 2) { //ZY
VBO_V[i].z = cos(angle*imopi)*p.z - sin(angle*imopi)*p.y;
VBO_V[i].y = sin(angle*imopi)*p.z + cos(angle*imopi)*p.y;
}

}

}

[/quote]

And everything rotates correctly, but when i changed this code i cant calculate proper vectors, I don't know what i am doing wrong:

if i rotate model with these angles by glRotate everything works fine - i use:

/* XZ */ glRotatef(PHYSIC_MODEL->superpointglop,0.0,1.0,0.0); // YAW angle
/* ZY */ glRotatef(PHYSIC_MODEL->heading,1.0,0.0,0.0); // PITCH angle
/* XY */ glRotatef(PHYSIC_MODEL->glop,0.0,0.0,1.0); // ROLL angle

so i know that i must first rotate by ROLL ANGLE, then by PITCH angle and then by YAW angle but the calculations are wrong :S

can anyody help me with calculations?

and i almost forgot imopi equals to

const float pi = 3.1415926535897932384626433832795;

const float imopi = 0.017453292519943295769236907684886; => pi / 180.0f

[quote]void __fastcall WATEK_Z_RYSOWANIEM::Draw()
{
PHYSIC_MODEL->ProcessFrame();

glPushMatrix();

gluLookAt(kold.x,kold.y,kold.z,kold.x+ksp.x,
kold.y-ksp.y,kold.z-ksp.z,0,1,0);

glTranslatef(PHYSIC_MODEL->pos.x,PHYSIC_MODEL->pos.y,PHYSIC_MODEL->pos.z);
glPushMatrix();
/* XZ */ glRotatef(PHYSIC_MODEL->superpointglop,0.0,1.0,0.0); // YAW angle
/* ZY */ glRotatef(PHYSIC_MODEL->heading,1.0,0.0,0.0); // PITCH angle
/* XY */ glRotatef(PHYSIC_MODEL->glop,0.0,0.0,1.0); // ROLL angle

PHYSIC_MODEL->FRONT.glop = 0.0f;
PHYSIC_MODEL->FRONT.CALCULATE_VEC();

PHYSIC_MODEL->UP.glop = 0.0f;
PHYSIC_MODEL->UP.CALCULATE_VEC();

PHYSIC_MODEL->RIGHT.glop = 90.0f;
PHYSIC_MODEL->RIGHT.CALCULATE_VEC();

//XY -> ZY -> XZ
float angle;

//SO THERE IS A PROBLEM---SO THERE IS A PROBLEM---SO THERE IS A PROBLEM---SO THERE IS A PROBLEM---SO THERE IS A PROBLEM---SO THERE IS A PROBLEM-
angle = PHYSIC_MODEL->glop; //roll
PHYSIC_MODEL->UP.vec.x = cos(angle*imopi)*PHYSIC_MODEL->UP.vec.x - sin(angle*imopi)*PHYSIC_MODEL->UP.vec.y;
PHYSIC_MODEL->UP.vec.y = sin(angle*imopi)*PHYSIC_MODEL->UP.vec.x + cos(angle*imopi)*PHYSIC_MODEL->UP.vec.y;

PHYSIC_MODEL->UP.vec.z = cos(angle*imopi)*PHYSIC_MODEL->UP.vec.z - sin(angle*imopi)*PHYSIC_MODEL->UP.vec.y;
PHYSIC_MODEL->UP.vec.y = sin(angle*imopi)*PHYSIC_MODEL->UP.vec.z + cos(angle*imopi)*PHYSIC_MODEL->UP.vec.y;

angle = PHYSIC_MODEL->superpointglop; //yaw
PHYSIC_MODEL->UP.vec.x = cos(angle*imopi)*PHYSIC_MODEL->UP.vec.x - sin(angle*imopi)*PHYSIC_MODEL->UP.vec.z;
PHYSIC_MODEL->UP.vec.z = sin(angle*imopi)*PHYSIC_MODEL->UP.vec.x + cos(angle*imopi)*PHYSIC_MODEL->UP.vec.z;

angle = PHYSIC_MODEL->glop;//roll
PHYSIC_MODEL->FRONT.vec.x = cos(angle*imopi)*PHYSIC_MODEL->FRONT.vec.x - sin(angle*imopi)*PHYSIC_MODEL->FRONT.vec.y;
PHYSIC_MODEL->FRONT.vec.y = sin(angle*imopi)*PHYSIC_MODEL->FRONT.vec.x + cos(angle*imopi)*PHYSIC_MODEL->FRONT.vec.y;
PHYSIC_MODEL->FRONT.vec.z = cos(angle*imopi)*PHYSIC_MODEL->FRONT.vec.z - sin(angle*imopi)*PHYSIC_MODEL->FRONT.vec.y;
PHYSIC_MODEL->FRONT.vec.y = sin(angle*imopi)*PHYSIC_MODEL->FRONT.vec.z + cos(angle*imopi)*PHYSIC_MODEL->FRONT.vec.y;
angle = PHYSIC_MODEL->superpointglop;//yaw
PHYSIC_MODEL->FRONT.vec.x = cos(angle*imopi)*PHYSIC_MODEL->FRONT.vec.x - sin(angle*imopi)*PHYSIC_MODEL->FRONT.vec.z;
PHYSIC_MODEL->FRONT.vec.z = sin(angle*imopi)*PHYSIC_MODEL->FRONT.vec.x + cos(angle*imopi)*PHYSIC_MODEL->FRONT.vec.z;

angle = PHYSIC_MODEL->glop;//roll
PHYSIC_MODEL->RIGHT.vec.x = cos(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.x - sin(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.y;
PHYSIC_MODEL->RIGHT.vec.y = sin(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.x + cos(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.y;
PHYSIC_MODEL->RIGHT.vec.z = cos(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.z - sin(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.y;
PHYSIC_MODEL->RIGHT.vec.y = sin(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.z + cos(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.y;
angle = PHYSIC_MODEL->superpointglop;//yaw
PHYSIC_MODEL->RIGHT.vec.x = cos(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.x - sin(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.z;
PHYSIC_MODEL->RIGHT.vec.z = sin(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.x + cos(angle*imopi)*PHYSIC_MODEL->RIGHT.vec.z;

//SO THERE IS A PROBLEM---SO THERE IS A PROBLEM---SO THERE IS A PROBLEM---SO THERE IS A PROBLEM---SO THERE IS A PROBLEM---SO THERE IS A PROBLEM-

glColor3f(1,1,1);
model->DrawSimpleModel();

glPopMatrix(); glPopMatrix(); glPopMatrix();

glPushMatrix();

glBegin(GL_LINES);
glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,0.0f,0.0f); //front
glVertex3f(PHYSIC_MODEL->FRONT.vec.x,PHYSIC_MODEL->FRONT.vec.y,PHYSIC_MODEL->FRONT.vec.z);
//up
glColor3f(0.0f,1.0f,0.0f);glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(PHYSIC_MODEL->UP.vec.x,PHYSIC_MODEL->UP.vec.y,PHYSIC_MODEL->UP.vec.z);
//right
glColor3f(0.0f,0.0f,1.0f);glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(PHYSIC_MODEL->RIGHT.vec.x,PHYSIC_MODEL->RIGHT.vec.y,PHYSIC_MODEL->RIGHT.vec.z);

glEnd();

glPopMatrix();
}

[/quote]

##### Share on other sites
mhagain    13430
These vectors are just parts (the top-left 3x3 portion) of a rotation matrix for the respective angles, so calculate the rotation matrix and you can pull out the vectors from it.

##### Share on other sites
WiredCat    1451
LOL man! it works

BIG THANKS

heres a code for others

[quote]

float rotz[16]; angle = PHYSIC_MODEL->glop; // PITCH
PHYSIC_MODEL->PITCH_MATRIX.pointer[0] = cos(angle*imopi);
PHYSIC_MODEL->PITCH_MATRIX.pointer[1] = -sin(angle*imopi);
PHYSIC_MODEL->PITCH_MATRIX.pointer[2] = 0.0f;
PHYSIC_MODEL->PITCH_MATRIX.pointer[3] = 0.0f;
PHYSIC_MODEL->PITCH_MATRIX.pointer[4] = sin(angle*imopi);
PHYSIC_MODEL->PITCH_MATRIX.pointer[5] = cos(angle*imopi);
PHYSIC_MODEL->PITCH_MATRIX.pointer[6] = 0.0f;
PHYSIC_MODEL->PITCH_MATRIX.pointer[7] = 0.0f;
PHYSIC_MODEL->PITCH_MATRIX.pointer[8] = 0.0f;
PHYSIC_MODEL->PITCH_MATRIX.pointer[9] = 0.0f;
PHYSIC_MODEL->PITCH_MATRIX.pointer[10] = 1.0f;
PHYSIC_MODEL->PITCH_MATRIX.pointer[11] = 0.0f;
PHYSIC_MODEL->PITCH_MATRIX.pointer[12] = 0.0f;
PHYSIC_MODEL->PITCH_MATRIX.pointer[13] = 0.0f;
PHYSIC_MODEL->PITCH_MATRIX.pointer[14] = 0.0f;
PHYSIC_MODEL->PITCH_MATRIX.pointer[15] = 1.0f;

PHYSIC_MODEL->ROLL_MATRIX.pointer[0] = 1.0f;
PHYSIC_MODEL->ROLL_MATRIX.pointer[1] = 0.0f;
PHYSIC_MODEL->ROLL_MATRIX.pointer[2] = 0.0f;
PHYSIC_MODEL->ROLL_MATRIX.pointer[3] = 0.0f;
PHYSIC_MODEL->ROLL_MATRIX.pointer[4] = 0.0f;
PHYSIC_MODEL->ROLL_MATRIX.pointer[5] = cos(angle*imopi);
PHYSIC_MODEL->ROLL_MATRIX.pointer[6] = -sin(angle*imopi);
PHYSIC_MODEL->ROLL_MATRIX.pointer[7] = 0.0f;
PHYSIC_MODEL->ROLL_MATRIX.pointer[8] = 0.0f;
PHYSIC_MODEL->ROLL_MATRIX.pointer[9] = sin(angle*imopi);
PHYSIC_MODEL->ROLL_MATRIX.pointer[10] = cos(angle*imopi);
PHYSIC_MODEL->ROLL_MATRIX.pointer[11] = 0.0f;
PHYSIC_MODEL->ROLL_MATRIX.pointer[12] = 0.0f;
PHYSIC_MODEL->ROLL_MATRIX.pointer[13] = 0.0f;
PHYSIC_MODEL->ROLL_MATRIX.pointer[14] = 0.0f;
PHYSIC_MODEL->ROLL_MATRIX.pointer[15] = 1.0f;

angle = PHYSIC_MODEL->superpointglop; // YAW

PHYSIC_MODEL->YAW_MATRIX.pointer[0] = cos(angle*imopi);
PHYSIC_MODEL->YAW_MATRIX.pointer[1] = 0.0f;
PHYSIC_MODEL->YAW_MATRIX.pointer[2] = sin(angle*imopi);
PHYSIC_MODEL->YAW_MATRIX.pointer[3] = 0.0f;
PHYSIC_MODEL->YAW_MATRIX.pointer[4] = 0.0f;
PHYSIC_MODEL->YAW_MATRIX.pointer[5] = 1.0f;
PHYSIC_MODEL->YAW_MATRIX.pointer[6] = 0.0;
PHYSIC_MODEL->YAW_MATRIX.pointer[7] = 0.0f;
PHYSIC_MODEL->YAW_MATRIX.pointer[8] = -sin(angle*imopi);
PHYSIC_MODEL->YAW_MATRIX.pointer[9] = 0.0f;
PHYSIC_MODEL->YAW_MATRIX.pointer[10] = cos(angle*imopi);
PHYSIC_MODEL->YAW_MATRIX.pointer[11] = 0.0f;
PHYSIC_MODEL->YAW_MATRIX.pointer[12] = 0.0f;
PHYSIC_MODEL->YAW_MATRIX.pointer[13] = 0.0f;
PHYSIC_MODEL->YAW_MATRIX.pointer[14] = 0.0f;
PHYSIC_MODEL->YAW_MATRIX.pointer[15] = 1.0f;

// Z X Y

//PITCH -> ROLL -> YAW
MatrixMulti4x4(PHYSIC_MODEL->PITCH_MATRIX.pointer,PHYSIC_MODEL->ROLL_MATRIX.pointer,PHYSIC_MODEL->BASE_ROTATION_MATRIX2);
MatrixMulti4x4(PHYSIC_MODEL->BASE_ROTATION_MATRIX2.pointer,PHYSIC_MODEL->YAW_MATRIX.pointer,PHYSIC_MODEL->BASE_ROTATION_MATRIX);
//

PHYSIC_MODEL->UP.vec.x = PHYSIC_MODEL->BASE_ROTATION_MATRIX.pointer[1];
PHYSIC_MODEL->UP.vec.y = PHYSIC_MODEL->BASE_ROTATION_MATRIX.pointer[5];
PHYSIC_MODEL->UP.vec.z = PHYSIC_MODEL->BASE_ROTATION_MATRIX.pointer[9];

PHYSIC_MODEL->UP.vec = vector_multiple(PHYSIC_MODEL->UP.vec,10000.0f);

[/quote]