2015-11-25 184 views
1

我从一些软件中得到一个变换矩阵,我必须提取轴旋转。我不确定确切的名称,但我想要有与Matrix4d.rotX(a)相反的功能。从矩阵4d提取旋转

比方说,我有这样的代码:

Matrix4d matrix = new Matrix4d(); 
matrix.setIdentity(); 
Matrix4d m = new Matrix4d(); 
m.rotX(a); 
matrix.mul(m); 
m.rotY(b); 
matrix.mul(m); 
m.rotZ(c); 
matrix.mul(m); 

我希望能够得到的值,b和c的矩阵。会有人有想法吗?我在3D中几乎没有任何经验,我查看了java vecmath包,但找不到任何东西。

我试着用下面的:

public static float[] getRotation(Matrix4f matrix) { 
     float Yaw; 
     float Pitch; 
     float Roll; 
     if (Math.abs(matrix.m00 + 1.0f) < 0.01f) 
     { 
      Yaw = (float) Math.atan2(matrix.m02, matrix.m23); 
      Pitch = 0; 
      Roll = 0; 

     }else if (Math.abs(matrix.m00 -1.0f) < 0.01f) 
     { 
      Yaw = (float) Math.atan2(matrix.m02, matrix.m23); 
      Pitch = 0; 
      Roll = 0; 
     }else 
     { 

      Yaw = (float) Math.atan2(-matrix.m20,matrix.m00); 
      Pitch = (float) Math.asin(matrix.m10); 
      Roll = (float) Math.atan2(-matrix.m12,matrix.m11); 
     } 
     return new float[] { Yaw, Pitch, Roll}; 
    } 

public static float[] GetRotation(Matrix4f matrix) { 
     float Yaw; 
     float Pitch; 
     float Roll; 
     if (matrix.m00 == 1.0f) 
     { 
      Yaw = (float) Math.atan2(matrix.m20, matrix.m32); 
      Pitch = 0; 
      Roll = 0; 

     }else if (matrix.m00 == -1.0f) 
     { 
      Yaw = (float) Math.atan2(matrix.m20, matrix.m32); 
      Pitch = 0; 
      Roll = 0; 
     }else 
     { 

      Yaw = (float) Math.atan2(-matrix.m02,matrix.m00); 
      Pitch = (float) Math.asin(matrix.m01); 
      Roll = (float) Math.atan2(-matrix.m21,matrix.m11); 
     } 
     return new float[] { Yaw, Pitch, Roll}; 
    } 

但价值是错误的

+0

相关:http://gamedev.st ackexchange.com/questions/50963/how-to-extract-euler-angles-from-transformation-matrix –

+0

我已经尝试过,但无法让它工作。 – kelto

+0

你还没有分享你的代码,所以我不能提供任何提示错误的地方。 –

回答

0

我找到了答案,这里是代码,使其工作:

public static float[] getRotation(Matrix4f matrix) { 
      float x,y,z; 
      Matrix3f m = new Matrix3f(); 

      matrix.get(m); 
      if(Math.abs(m.m02 - 1) < 0.0000001) { 
       x = (float) Math.atan2(-m.m10,m.m11); 
       y = (float) (-3.1415926535897931/2); 
       z = 0.0f; 
      } else if(Math.abs(m.m02 + 1)< 0.0000001) { 
       x = (float) Math.atan2(m.m10,m.m11); 
       y = (float) (3.1415926535897931/2); 
       z = 0.0f; 
      } else { 
       x = (float) Math.atan2(m.m12,m.m22); 
       y = (float) Math.atan2(-m.m02, Math.sqrt(m.m12 * m.m12 + m.m22 * m.m22)); 
       z = (float) Math.atan2(m.m01,m.m00); 
      } 

      return new float[] {x,y,z}; 
} 
+0

它与此示例类似:https://github.com/gouessej/Ardor3D/blob/master/ardor3d-math/src/main/java/com/ardor3d/math/Matrix3.java#L1072您将极点上的奇点然后是一般情况。这是解决着名的万向节锁问题的非线性解决方案。我希望你能理解这个源代码。 – gouessej