0

so I have an AxisAngle4f object with a vector3D as axis and an angle, how do I get rotation angle for each of x,y,z axes?

r_d26
  • 19
  • 2

1 Answers1

0
  1. create 4x4 homogenous transform matrix representing your rotation

    first see Understanding 4x4 homogenous transform matrices so basically you want 3 basis vectors and origin of unit matrix then rotate each by your rotation (for that you can use this or glRotate or whatever). Here C++ example:

    void rotate3d(float alfa,float *axis,float *point)
         {
         float p[3],q[3],c=cos(alfa),s=sin(alfa);
         //Euler Rodrigues' rotation formula
         vector_mul(q,point,c);
         vector_mul(p,axis,point);
         vector_mul(p,p,s);
         vector_add(p,p,q);
         vector_mul(q,axis,vector_mul(axis,point)*(1.0-c));
         vector_add(point,p,q);
         }
    

    The vector math functions are described (with source) in the link above. Just change the double into float as you are using those. So it boils up to something like this in C++:

    float X[3] = { 1.0,0.0,0.0 };
    float Y[3] = { 0.0,1.0,0.0 };
    float Z[3] = { 0.0,0.0,1.0 };
    float O[3] = { 0.0,0.0,0.0 };
    float M[16];
    float AxisAngle4f[4]={x,y,z,angle};
    rotate3d(AxisAngle4f[3],AxisAngle4f,X);
    rotate3d(AxisAngle4f[3],AxisAngle4f,Y);
    rotate3d(AxisAngle4f[3],AxisAngle4f,Z);
    rotate3d(AxisAngle4f[3],AxisAngle4f,O);
    M[0]=X[0]; M[4]=Y[0]; M[ 8]=Z[0]; M[12]=O[0];
    M[1]=X[1]; M[5]=Y[1]; M[ 9]=Z[1]; M[13]=O[1];
    M[2]=X[2]; M[6]=Y[2]; M[10]=Z[2]; M[14]=O[2];
    M[3]= 0.0; M[7]= 0.0; M[11]= 0.0; M[15]= 1.0;
    

    Where M is OpenGL style direct matrix representing your rotation.

  2. convert M into your Euler angles

    see Is there a way to calculate 3D rotation on X and Y axis from a 4x4 matrix on how (again change to floats)...

    const float deg=M_PI/180.0;
    const float rad=180.0/M_PI;
    // variables
    float e[3],m[16];
    int euler_cfg[_euler_cfgs];
    // init angles
    e[0]=10.0*deg;
    e[1]=20.0*deg;
    e[2]=30.0*deg;
    // compute coresponding rotation matrix with your environment
    m = some_rotate_of yours(e)
    // cross match e,m -> euler_cfg
    matrix2euler_init(e,m,euler_cfg);
    
    // now we can convert M into e
    matrix2euler(e,M,euler_cfg);
    // e holds your euler angles you want
    

    The init of euler_cfg is needed just once then you can use matrix2euler at will.

Spektre
  • 49,595
  • 11
  • 110
  • 380