2016-04-19 26 views
-2

私はゲームエンジンを作成するためのチュートリアルに従っていましたが、3D回転行列を計算する際に、行列が正しく計算されていないと思われる問題がありました。私はで終わる回転行列Java

Rotation Matrix: 

1.0 0.0 0.0 0.0 
0.0 1.0 0.0 0.0 
0.0 0.0 1.0 0.0 
0.0 0.0 0.0 1.0 

しかし:私はx、y、z軸上の0度行列を回転しようとすると、私は次のようになります回転行列のための単位行列を取得する必要がありますここで

Rotation Matrix: 

1.0 1.0 1.0 1.0 
1.0 1.0 1.0 1.0 
1.0 1.0 1.0 1.0 
1.0 1.0 1.0 1.0 

はチュートリアルから直接回転行列を計算するためのコードです:

public class Matrix4f 
{ 
    private float[][] m; 

    //generate a 4X4 array as my inital matrix which will represent homogeneous 
    //coordinates: x, y, z, w 
    public Matrix4f() 
    { 
     m = new float[4][4]; 
    } 

    public Matrix4f initRotation(float x, float y, float z) 
    { 
     //generate rotation matrices for x, y, and z 
     Matrix4f rx = new Matrix4f(); 
     Matrix4f ry = new Matrix4f(); 
     Matrix4f rz = new Matrix4f(); 

     //convert x,y, and z to radians for angle calculations 
     x = (float)Math.toRadians(x); 
     y = (float)Math.toRadians(y); 
     z = (float)Math.toRadians(z); 

     //calculate rotation matrices for x, y, z 

     rz.m[0][0] = (float)Math.cos(z);rz.m[0][1] = (float)Math.sin(z);rz.m[0][2] = 0;    rz.m[0][3] = 0; 
     rz.m[1][0] = -(float)Math.sin(z);rz.m[1][1] = (float)Math.cos(z);rz.m[1][2] = 0;     rz.m[1][3] = 0; 
     rz.m[2][0] = 0;     rz.m[2][1] = 0;     rz.m[2][2] = 1;     rz.m[2][3] = 0; 
     rz.m[3][0] = 0;     rz.m[3][1] = 0;     rz.m[3][2] = 0;     rz.m[3][3] = 1; 

     rx.m[0][0] = 1;     rx.m[0][1] = 0;     rx.m[0][2] = 0;     rx.m[0][3] = 0; 
     rx.m[1][0] = 0;     rx.m[1][1] = (float)Math.cos(x);rx.m[1][2] = -(float)Math.sin(x);rx.m[1][3] = 0; 
     rx.m[2][0] = 0;     rx.m[2][1] = -(float)Math.sin(x);rx.m[2][2] = (float)Math.cos(x);rx.m[2][3] = 0; 
     rx.m[3][0] = 0;     rx.m[3][1] = 0;     rx.m[3][2] = 0;     rx.m[3][3] = 1; 

     ry.m[0][0] = (float)Math.cos(y);ry.m[0][1] = 0;     ry.m[0][2] = -(float)Math.sin(y);ry.m[0][3] = 0; 
     ry.m[1][0] = 0;     ry.m[1][1] = 1;     ry.m[1][2] = 0;     ry.m[1][3] = 0; 
     ry.m[2][0] = (float)Math.sin(y);ry.m[2][1] = 0;     ry.m[2][2] = (float)Math.cos(y);ry.m[2][3] = 0; 
     ry.m[3][0] = 0;     ry.m[3][1] = 0;     ry.m[3][2] = 0;     ry.m[3][3] = 1; 

     //calculate the final rotation matrix by multiplying the 3 rotation matrices together 
     m = rz.mul(ry.mul(rx)).getM(); 

     //a simple way to print out the full matrix 
     System.out.println("Rotation Matrix:"); 
     for(int i = 0; i < 4; i++) 
     { 
      System.out.println(""); 
      for(int j = 0; j < 4; j++) 
      { 
       System.out.print(m[i][j] + " "); 
      } 
     } 
     System.out.println(""); 

     return this; 
    } 

    //defining the multiplication operation for matrices 
    public Matrix4f mul(Matrix4f r) 
    { 
     Matrix4f res = new Matrix4f(); 

     for(int i = 0; i < 4; i++) 
     { 
      for(int j = 0; j < 4; j++) 
      { 
       res.set(i, j, m[i][0]*r.get(0, i) + 
           m[i][1]*r.get(1, i) + 
           m[i][2]*r.get(2, i) + 
           m[i][3]*r.get(3, i)); 
      } 
     } 

     return res; 

    } 

    //get the matrix in array form 
    public float[][] getM() 
    { 
     return m; 
    } 

    //get an individual element of the matrix 
    public float get(int x, int y) 
    { 
     return m[x][y]; 
    } 

    //set the whole matrix equal to a matrix m 
    public void setM(float[][] m) 
    { 
     this.m = m; 
    } 

    //set an individual element of the matrix to a value 
    public void set(int x, int y, float value) 
    { 
     m[x][y] = value; 
    } 

} 

その後、基本的に私はそうのような主要な方法でそれを実行しようとするとこのようになります行列:

Rotation Matrix: 

1.0 0.0 0.0 0.0 
0.0 1.0 0.0 0.0 
0.0 0.0 1.0 0.0 
0.0 0.0 0.0 1.0 

EDITを:いくつかのより多くのデバッグの後、私は後に、問題を解決した

(new Matrix4f()).initRotation(0, 0, 0); 

私は回転行列の操作に応じて、私が取得する必要があるにもかかわらず

Rotation Matrix: 

1.0 1.0 1.0 1.0 
1.0 1.0 1.0 1.0 
1.0 1.0 1.0 1.0 
1.0 1.0 1.0 1.0 

を取得しますより多くのデバッグは、行列のmulメソッドで問題になる。私は怠惰に謝ります。私はしばらくの間プログラミングしてきましたが、実際にこの種のことはしばしばやっていませんが、私のプログラムでは本当に不満を抱いていました。

行列乗算アルゴリズムのための補正は、次のとおり

public Matrix4f mul(Matrix4f r) 
    { 
     Matrix4f res = new Matrix4f(); 

     for(int i = 0; i < 4; i++) 
     { 
      for(int j = 0; j < 4; j++) 
      { 
       res.set(i, j, m[i][0]*r.get(0, j) + 
           m[i][1]*r.get(1, j) + 
           m[i][2]*r.get(2, j) + 
           m[i][3]*r.get(3, j)); 
      } 
     } 

     return res; 

    } 

代わりに:

公共Matrix4f MUL(Matrix4f R) {Matrix4f RES =新しいMatrix4f()。

for(int i = 0; i < 4; i++) 
    { 
     for(int j = 0; j < 4; j++) 
     { 
      res.set(i, j, m[i][0]*r.get(0, i) + 
          m[i][1]*r.get(1, i) + 
          m[i][2]*r.get(2, i) + 
          m[i][3]*r.get(3, i)); 
     } 
    } 

    return res; 

} 
+0

デバッグを行う必要があります。 –

+0

私は1トンのデバッグをしました。あなたはもっと多くの情報を求めていますか?私はそれを供給することを嬉しく思うでしょう。 –

+0

動作が期待と異なる場合の計算/行を特定し、[最小限のテストケース](http://stackoverflow.com/help/mcve)を作成する必要があります。 –

答えて

2
  • あなたの乗算機能は論理的に間違っていると思います。あなたの乗算関数では、すべてのセルの最初の生と列を乗算することになります。以下のようにiをjに変更すると、乗算が正しく計算されます。

    public Matrix4f mul(Matrix4f r) { 
    Matrix4f res = new Matrix4f(); 
    for(int i = 0; i < 4; i++) 
    { 
        for(int j = 0; j < 4; j++) 
        { 
         res.set(i, j, m[i][0]*r.get(0, j) + 
             m[i][1]*r.get(1, j) + 
             m[i][2]*r.get(2, j) + 
             m[i][3]*r.get(3, j)); 
        } 
    } 
    
    return res; 
    
    } 
    
  • 第二に、あなたは、乗算機能にブレークポイントを使用してコードをデバッグしようとすることができます。任意の標準IDE(Eclipse)デバッグ機能を使用してデバッグできます。実行のステップごとに正しいアイデアが得られ、コードを自分で修正できるようになります。

希望します。

+0

私と同じことが分かった+1 – maskacovnik

1

私はデバッグをたくさんやった、と私はあなたが悪いのインデックスを持って考え出し:

res.set(i, j, m[i][0]*r.get(0, i) + 
    m[i][1]*r.get(1, i) + 
    m[i][2]*r.get(2, i) + 
    m[i][3]*r.get(3, i)); 

あなたは、両方の行列にインデックスiを持っている - あなたはそれをこのように修正する必要があります。

res.set(i, j, m[i][0]*r.get(0, j) + 
    m[i][1]*r.get(1, j) + 
    m[i][2]*r.get(2, j) + 
    m[i][3]*r.get(3, j)); 

次の計算で転置行列を受け取った場合(単位行列では、それを計算する方法はありません) - スワップiおよびj 希望する;)

関連する問題