2015-11-25 182 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