# Axis to Angles

27 replies to this topic

### #1mysticman

Member

• Members
• 95 posts

Posted 28 June 2005 - 05:13 PM

Do you excuse my slowness, but is there a formula that converts the axis euler in the classical angles [[PITCH] [YAW] [ROLL]] ? :sad:

Do you know which the correct sequence of rotation is?
That is, RX RY RZ or RZ RX RY? :rolleyes:

thanks!

### #2anubis

Senior Member

• Members
• 2225 posts

Posted 28 June 2005 - 07:39 PM

first question : why do you need to get that information ?
If Prolog is the answer, what is the question ?

### #3mysticman

Member

• Members
• 95 posts

Posted 28 June 2005 - 08:14 PM

anubis said:

first question : why do you need to get that information ?

I have need to know how they rotate some polygons of a model in physical way, not from matrix or from glRotatef () of opengl, in this way that I can update, in base to the rotated vertexes, the new normal, tangent and binormal, which are passed then to the shaders arb.
For the surfaces of the bsp, I don't have to do this, but for the models and sub-models unfortunately I don't see (I don't know) other solution (this always in base to my scarce knowledge). :blush:

### #4anubis

Senior Member

• Members
• 2225 posts

Posted 28 June 2005 - 09:47 PM

this sounds fishy to me... extracting angles from matrices is a pretty unusual operation. anyway...i'd figure you should be able to get the rotation angles bye using the atan2 function.

you should basically get your angles like this :

pitch = asin(m21)
roll = atan2(-m01, m11)

something along those lines anyway... i might be completely wrong. it's almost 1 am, which is kind of the wrong time to get my head around math problems :)

edit : i think this is actually right now
If Prolog is the answer, what is the question ?

### #5anubis

Senior Member

• Members
• 2225 posts

Posted 28 June 2005 - 10:05 PM

unregardless of the correctnes getting these angles from a matrix is error prone... gimbal lock... jadajada
If Prolog is the answer, what is the question ?

### #6anubis

Senior Member

• Members
• 2225 posts

Posted 28 June 2005 - 10:08 PM

oh yeah... and i assume rotation order is x y z
If Prolog is the answer, what is the question ?

### #7mysticman

Member

• Members
• 95 posts

Posted 28 June 2005 - 10:13 PM

this is the matrix where the 3 axis and the translation are set.

obj_m[0] = e->axis[0][0];
obj_m[1] = e->axis[0][1];
obj_m[2] = e->axis[0][2];
obj_m[4] = e->axis[1][0];
obj_m[5] = e->axis[1][1];
obj_m[6] = e->axis[1][2];
obj_m[8] = e->axis[2][0];
obj_m[9] = e->axis[2][1];
obj_m[10] = e->axis[2][2];
obj_m[3] = 0;
obj_m[7] = 0;
obj_m[11] = 0;
obj_m[12] = e->origin[0];
obj_m[13] = e->origin[1];
obj_m[14] = e->origin[2];
obj_m[15] = 1.0;


Which can it be, in explicit way, the formula for to reconstruct the angles PITCH YAW ROLL ?

### #8anubis

Senior Member

• Members
• 2225 posts

Posted 28 June 2005 - 10:15 PM

i modified the thing for you so the names are more meaningful :)
If Prolog is the answer, what is the question ?

### #9mysticman

Member

• Members
• 95 posts

Posted 28 June 2005 - 10:17 PM

once that I get the pitch,yaw,roll I can rotate the polygons in this way:

 _RotateX(inVertsArray[i], vout, DEG2RAD(aout[ROLL]));
VectorCopy(vout, inVertsArray[i]);
VectorCopy(vout, inVertsArray[i]);
VectorCopy(vout, inVertsArray[i]);

inline void _RotateX(vec3_t pin, float *pout, float angle)
{
pout[0] = pin[0];
pout[1] = (sin(angle)*pin[1]) + (cos(angle)*pin[2]);
pout[2] = (cos(angle)*pin[1]) - (sin(angle)*pin[2]);
}
inline void _RotateY(vec3_t pin, float *pout, float angle)
{
pout[0] = (sin(angle)*pin[0]) + (cos(angle)*pin[2]);
pout[1] = pin[1];
pout[2] = (cos(angle)*pin[0]) - (sin(angle)*pin[2]);
}
inline void _RotateZ(vec3_t pin, float *pout, float angle)
{
pout[0] = (cos(angle)*pin[0]) - (sin(angle)*pin[1]);
pout[1] = (sin(angle)*pin[0]) + (cos(angle)*pin[1]);
pout[2] = pin[2];
}


### #10mysticman

Member

• Members
• 95 posts

Posted 28 June 2005 - 10:51 PM

Gulp! asin2 () I don't find it! :blush:

### #11Ed Mack

Senior Member

• Members
• 1239 posts

Posted 29 June 2005 - 05:11 AM

<cmath> has the following defined:

 asin(float __x);
asin(long double __x);


### #12anubis

Senior Member

• Members
• 2225 posts

Posted 29 June 2005 - 05:36 AM

corrected...
If Prolog is the answer, what is the question ?

### #13mysticman

Member

• Members
• 95 posts

Posted 29 June 2005 - 06:56 AM

if I rotate with head in this way:
head = atan2(-axis[1][0], axis[1][1]);
...
...
VectorCopy(vout, inVertsArray[i]);


it works, but as soon as I put the others (I have tried different order of rotation) the gimbal lock happens... :dry:

I am desperate and discouraged, I don't know more than thing to do.
I have also tried this function, taken by the sdk of rtcw, but....

void AxisToAngles( vec3_t axis[3], vec3_t angles ) {
vec3_t right, roll_angles, tvec;

// first get the pitch and yaw from the forward vector
VecToAngles( axis[0], angles );

// now get the roll from the right vector
VectorCopy( axis[1], right );
// get the angle difference between the tmpAxis[2] and axis[2] after they have been reverse-rotated
RotatePointAroundVector( tvec, axis_identity[2], right, -angles[YAW] );
RotatePointAroundVector( right, axis_identity[1], tvec, -angles[PITCH] );
// now find the angles, the PITCH is effectively our ROLL
VecToAngles( right, roll_angles );
roll_angles[PITCH] = AngleNormalize180( roll_angles[PITCH] );
// if the yaw is more than 90 degrees difference, we should adjust the pitch
if (DotProduct( right, axis_identity[1] ) < 0) {
if (roll_angles[PITCH] < 0)
roll_angles[PITCH] = -90 + (-90 - roll_angles[PITCH]);
else
roll_angles[PITCH] = 90 + ( 90 - roll_angles[PITCH]);
}

angles[ROLL] = -roll_angles[PITCH];
}


... I don't know if it is correct... :sad:

### #14mysticman

Member

• Members
• 95 posts

Posted 29 June 2005 - 07:23 AM

pitch = 0 :cool: no problem

pitch over -45.00 :rolleyes: no problem

head = -(float)( (atan2(e->axis[1][0], e->axis[1][1])) );
pitch = (float)(atan2(e->axis[2][2]+e->axis[1][2], e->axis[0][2]));
roll = (float)(atan2(e->axis[2][2]+e->axis[0][2], e->axis[1][2]));

for( i = 0; i < mesh->numverts; i++) {

_RotateX(inVertsArray[i], vout, roll);
VectorCopy(vout, inVertsArray[i]);
_RotateY(inVertsArray[i], vout, pitch);
VectorCopy(vout, inVertsArray[i]);
VectorCopy(vout, inVertsArray[i]);
...
...


as you see, the problem is in the pitch down over the 45 degrees. :sad:

...besides if I change order of rotation, gimbal lock appears :blink:

### #15mysticman

Member

• Members
• 95 posts

Posted 29 June 2005 - 05:27 PM

anubis said:

unregardless of the correctnes getting these angles from a matrix is error prone... gimbal lock... jadajada

I have spoken of matrix, but it is not necessary nevertheless.

Considering that I have:
axis[0] = vector FORWARD
axis[1] = vector RIGHT
axis[2] = vector UP

which would the formula be for extracting pitch, yaw, roll?

I have tried of everything, but I don't know whether to reach the solution.
Help me. :unsure:

### #16Reedbeta

DevMaster Staff

• 5311 posts
• LocationSanta Clara, CA

Posted 29 June 2005 - 07:00 PM

If you have normal, tangent, binormal vectors defined in local coordinates, you can just apply the same rotation matrix to them that you apply to the vertices of the model.

If in local coordinates the model faces +X, with +Y to its left and +Z up, and you have the desired forward, right, up vectors in world coordinates, then you can easily compute the rotation matrix:
  [ fwd.x, -right.x, up.x ]
R = [ fwd.y, -right.y, up.y ]
[ fwd.z, -right.z, up.z ]
If the local coordinates have the model facing +Y, with +X to the right and +Z up, then it is:
  [ right.x, fwd.x, up.x ]
R = [ right.y, fwd.y, up.y ]
[ right.z, fwd.z, up.z ]
This matrix will transform your normals/tangents/binormals into world space. There is no trigonometry needed.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #17mysticman

Member

• Members
• 95 posts

Posted 29 June 2005 - 08:05 PM

Using an animate model, I calculate to every frame (animation) the TBN, putting them in relative arrays.
Every frame can involve translation or rotation of the polygons of the model.
Once that me performed the lerping between the old frame and the new one, rotates and I moves the model with this function:

void R_RotateForEntity( entity_t *e )
{
mat4x4_t obj_m;

if ( e->scale != 1.0f && e->model && (e->model->type == mod_brush) ) {
obj_m[0] = e->axis[0][0] * e->scale;
obj_m[1] = e->axis[0][1] * e->scale;
obj_m[2] = e->axis[0][2] * e->scale;
obj_m[4] = e->axis[1][0] * e->scale;
obj_m[5] = e->axis[1][1] * e->scale;
obj_m[6] = e->axis[1][2] * e->scale;
obj_m[8] = e->axis[2][0] * e->scale;
obj_m[9] = e->axis[2][1] * e->scale;
obj_m[10] = e->axis[2][2] * e->scale;
}
else { // model
obj_m[0] = e->axis[0][0];
obj_m[1] = e->axis[0][1];
obj_m[2] = e->axis[0][2];
obj_m[4] = e->axis[1][0];
obj_m[5] = e->axis[1][1];
obj_m[6] = e->axis[1][2];
obj_m[8] = e->axis[2][0];
obj_m[9] = e->axis[2][1];
obj_m[10] = e->axis[2][2];
}

obj_m[3] = 0;
obj_m[7] = 0;
obj_m[11] = 0;
obj_m[12] = e->origin[0];
obj_m[13] = e->origin[1];
obj_m[14] = e->origin[2];
obj_m[15] = 1.0;

Matrix4_MultiplyFast( r_worldview_matrix, obj_m, r_modelview_matrix );

}


The problem is that to the shaders the rotated vertexes arrive and correctly moved only, while for TBN it is everything confused, the light is not correct on the models.
While if I do as I have said above (you see images), the light correctly works, but I has the problem to perform the correct rotation as if it was done from matrix or from glrotatef.

Using the system that you recommend me, as do I do?

You excuse if I am pedantic.... :sad:

### #18Reedbeta

DevMaster Staff

• 5311 posts
• LocationSanta Clara, CA

Posted 29 June 2005 - 09:07 PM

It looks to me as if the rotation matrix you are creating is the transpose of the one you want. Try replacing the e->axis[0][1] with e->axis[1][0], switch the indices on all of them.

Also, does the qglLoadMatrixf function transpose the matrix before sending it to glLoadMatrix? Remember that OpenGL accepts matrices in column-major order (first column, then second column, etc), while they are usually stored in memory in row-major order.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #19mysticman

Member

• Members
• 95 posts

Posted 29 June 2005 - 09:27 PM

The code that I have put is normally that of qfusion and it works... :unsure:

### #20mysticman

Member

• Members
• 95 posts

Posted 29 June 2005 - 10:20 PM

OK! Well! I have succeeded there, I had all the formulas that served me to course of hand.
I thank you for your time for me. I have learned that I need of the others to discover little by little myself. Thanks of everything still.

Now touches to the sub-models.... but this is another adventure! :lol:

excuse me for my bad english! :blush:

the code is:

 vec3_t axis[3];

Matrix_Transpose(e->axis, axis);

for( i = 0; i < mesh->numverts; i++) {
// Rotation
Matrix_TransformVector(axis, inVertsArray[i], vout);
// Translation
// Final result
VectorCopy(vout, inVertsArray[i]);
}

R_GetCurrentNormal( mesh->numverts, inVertsArray, mesh->stcoords, mesh->numtris, mesh->indexes);
R_BuildTangentVectors( mesh->numverts, inVertsArray, mesh->stcoords, mesh->numtris, mesh->indexes, inSVectorsArray, inTVectorsArray );
...
...


thanks again! :wub:

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users