Euler angles convertion to matrix

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Euler angles convertion to matrix

Postby fafahh » Wed Jan 20, 2010 9:37 am

hi,
I'm trying to use NewtonSetEulerAngle and NewtonGetEulerAngle to pass my Euler angles to the matrix, but the results are strange.
here is my code
Code: Select all
#include <iostream>

#include "Newton.h"

using namespace std;

// Structure matrix
struct matrix
{
    float matrice [4][4];

    bool operator== ( const matrix & other)
    {
        if ( (matrice[0][0] == other.matrice[0][0]) &&
                (matrice[0][1] == other.matrice[0][1]) &&
                (matrice[0][2] == other.matrice[0][2]) &&
                (matrice[0][3] == other.matrice[0][3]) &&
                (matrice[1][0] == other.matrice[1][0]) &&
                (matrice[1][1] == other.matrice[1][1]) &&
                (matrice[1][2] == other.matrice[1][2]) &&
                (matrice[1][3] == other.matrice[1][3]) &&
                (matrice[2][0] == other.matrice[2][0]) &&
                (matrice[2][1] == other.matrice[2][1]) &&
                (matrice[2][2] == other.matrice[2][2]) &&
                (matrice[2][3] == other.matrice[2][3]) &&
                (matrice[3][0] == other.matrice[3][0]) &&
                (matrice[3][1] == other.matrice[3][1]) &&
                (matrice[3][2] == other.matrice[3][2]) &&
                (matrice[3][3] == other.matrice[3][3]) )
            return true;
        else
            return false;
    }

    static matrix getIdentity ()
    {
        matrix matrice;

        for (int x = 0 ; x < 4 ; ++x)
            for (int y = 0 ; y < 4 ; y++)
            {
                if (x == y)
                    matrice.matrice [x][y] = 1.0f;
                else
                    matrice.matrice [x][y] = 0.0f;
            }
        return matrice;
    }

    void show ()
    {
        std::cout << "[" << matrice[0][0] << "] [" << matrice[0][1] << "] [" << matrice[0][2] << "] [" << matrice[0][3] << "]\n";
        std::cout << "[" << matrice[1][0] << "] [" << matrice[1][1] << "] [" << matrice[1][2] << "] [" << matrice[1][3] << "]\n";
        std::cout << "[" << matrice[2][0] << "] [" << matrice[2][1] << "] [" << matrice[2][2] << "] [" << matrice[2][3] << "]\n";
        std::cout << "[" << matrice[3][0] << "] [" << matrice[3][1] << "] [" << matrice[3][2] << "] [" << matrice[3][3] << "]\n";
    }
};

int main()
{
    matrix mat4 = matrix::getIdentity ();
    float angles[3] = { 0, -1.6, 0 };

    NewtonSetEulerAngle( &angles[0], &mat4.matrice[0][0]);

    mat4.show ();

    float anglesRep[3];
    NewtonGetEulerAngle( &mat4.matrice[0][0], &anglesRep[0]);

    cout << "anglesRep[0]= " << anglesRep[0] << endl;
    cout << "anglesRep[1]= " << anglesRep[1] << endl;
    cout << "anglesRep[2]= " << anglesRep[2] << endl;

    return 0;


}


I don't get the same angles before and after :?:
In fact everything is ok if angles are less than 1.57 (pi/2) or greater than -1.57 (-pi/2). It seems that these functions work in a [-pi/2, pi/2] range.

Can you explain me how to use these functions and where i made a mistake.
Thank's for reading.
bye.
fafahh
 
Posts: 2
Joined: Wed Jan 20, 2010 9:18 am

Re: Euler angles convertion to matrix

Postby JernejL » Wed Jan 20, 2010 10:29 am

Conversion of angles from matrix to angles will always result in loss of data and gimbal lock kind of problems. 3 euler angles are not adequate to reprisent proper 3D rotation so this is normal behavior - euler angles are not intended for such a purpose.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Euler angles convertion to matrix

Postby Julio Jerez » Wed Jan 20, 2010 2:49 pm

yes this s is right the yaw angle will alwy be in teh intevale [-pi/2 and pi/2]
The funtion GetEuler will make such teh teh matrix genertae formn teh angle will be teh same, even of eth angle are not.

There is not general way to get detrministic results when extration angle form matrices,
this come from the fact that from each tupple of Eulers angle there are other Tuplese that map to teh same rotation matrix.
so for making the problem deterministic another piece of information is need it, like for example the value of the prevoius rotation, or that one axis will repect the cuadrant into wich it projects,

what are you dwoing? maybe I can help you to get an specific solution for you.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Euler angles convertion to matrix

Postby fafahh » Thu Jan 21, 2010 5:05 pm

hi,
thank's for your answer.
I'll try to explain what i want to do :
I made a CAD soft with 3d visualisation with OpenGL, for now each objet owns it's position and rotation within vec3
(vector of 3 floats), and in the drawing loop it use glRotate and glTranslate to set the OpenGL MODELVIEW matrix for
each objet within glPushMatrix and glPopMatrix. It work well.Here is part of my loop :

Code: Select all
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glTranslatef (mPosition.x, mPosition.y, mPosition.z);
glRotatef(mRotation.x,1,0,0);
glRotatef(mRotation.y,0,1,0);
glRotatef(mRotation.z,0,0,1);

...

glPopMatrix ();


Now I want to integrate Newton in my project so I made this :
Code: Select all
matrix matrice;
NewtonBodyGetMatrix (mpBody, &matrice.matrice [0][0]);

glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glMultMatrixf (&matrice.matrice [0][0]);

mPosition.x = matrice.matrice[3][0];
mPosition.y = matrice.matrice[3][2];
mPosition.z = matrice.matrice[3][1];

float angles[3];
NewtonGetEulerAngle ( &matrice.matrice [0][0], angles);

mRotation.x = angles[0]/M_PI*180.0f;
mRotation.y = angles[1]/M_PI*180.0f;
mRotation.z = angles[2]/M_PI*180.0f;


It display correctly the objets but the mRotation updated is wrong because of what you've just exlain me.

I think I must use Quaternion, but I don't know them and I'll have to rewrite somme part of my soft.

For now I'll focus on Newton tutorial to unterstand well how it work before plug it to my soft CAD.
And exuse me but I have another question about Nexton tutorial, I try to compile Tutorail_101_GettingStarted with
Newton 2.16, and it don't compile because NewtonCreate take 2 arguments in the main function but it's declared
taking no argument in Newton.h. I try the same tutorial with Newton 1.53 but JointLibrary.h and
NewtonDebuggerServer.h don't exist in this version.

Besides I don't have Ms Visual, I use CodeBlocks with MinGW, I don't use SDL and glut, I use a lib homemade (Win32 +
OpenGL) to make the UI of my project.

PS : i'm very beginner with Newton.
PS : excuse my poor english.

Thank's again for your interest.
Bye
fafahh
 
Posts: 2
Joined: Wed Jan 20, 2010 9:18 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 399 guests

cron