2012-03-23 8 views
0

回転が動作していますが、オブジェクトを90度回転させようとしましたが、回転していましたが、oX軸上で左に予期せぬ変換が行われます。私はquaterionのw、x、y、z成分のための正規化メソッドを追加し、見つけたコードミスを修正しました。java quaternion 3D rotation implementation

これは私が使用する方法である:

public class Point3DRotationQuaternions 
    { 
    public static ArrayList<Float> rotation3D(ArrayList<Float> points, double angle, int x, int y, int z) 
    { 
    ArrayList<Float> newpoints = points; 

    for (int i=0;i<points.size();i+=3) 
    { 
     float x_old = points.get(i).floatValue(); 
     float y_old = points.get(i+1).floatValue(); 
     float z_old = points.get(i+2).floatValue(); 
     double[] initial = {1,0,0,0}; 
     double[] total = new double[4]; 
     double[] local = new double[4]; 

     //components for local quaternion 
     //w 
     local[0] = Math.cos(0.5 * angle); 
     //x 
     local[1] = x * Math.sin(0.5 * angle); 
     //y 
     local[2] = y * Math.sin(0.5 * angle); 
     //z 
     local[3] = z * Math.sin(0.5 * angle); 
     //local = magnitude(local); 

     //components for final quaternion Q1*Q2 
     //w = w1w2 - x1x2 - y1y2 - z1z2 
     total[0] = local[0] * initial[0] - local[1] * initial[1] - local[2] * initial[2] - local[3] * initial[3]; 
     //x = w1x2 + x1w2 + y1z2 - z1y2 
     total[1] = local[0] * initial[1] + local[1] * initial[0] + local[2] * initial[3] - local[3] * initial[2]; 
     //y = w1y2 - x1z2 + y1w2 + z1x2 
     total[2] = local[0] * initial[2] - local[1] * initial[3] + local[2] * initial[0] + local[3] * initial[1]; 
     //z = w1z2 + x1y2 - y1x2 + z1w2 
     total[3] = local[0] * initial[3] + local[1] * initial[2] - local[2] * initial[1] + local[3] * initial[0]; 

     //new x,y,z of the 3d point using rotation matrix made from the final quaternion 
     float x_new = (float)((1 - 2 * total[2] * total[2] - 2 * total[3] * total[3]) * x_old 
         + (2 * total[1] * total[2] - 2 * total[0] * total[3]) * y_old 
         + (2 * total[1] * total[3] + 2 * total[0] * total[2]) * z_old); 
     float y_new = (float) ((2 * total[1] * total[2] + 2 * total[0] * total[3]) * x_old 
         + (1 - 2 * total[1] * total[1] - 2 * total[3] * total[3]) * y_old 
         + (2 * total[2] * total[3] + 2 * total[0] * total[1]) * z_old); 
     float z_new = (float) ((2 * total[1] * total[3] - 2 * total[0] * total[2]) * x_old 
         + (2 * total[2] * total[3] - 2 * total[0] * total[1]) * y_old 
         + (1 - 2 * total[1] * total[1] - 2 * total[2] * total[2]) * z_old); 
     newpoints.set(i, x_new); 
     newpoints.set(i+1, y_new); 
     newpoints.set(i+2, z_new); 

    } 

    return newpoints; 
    } 
} 

public static void main(String args[]) 
{ 

    ArrayList<Float> list = new ArrayList<>(); 
    list.add(new Float(0)); 
    list.add(new Float(0)); 
    list.add(new Float(-11)); 
    ArrayList<Float> list1 = Point3DRotationQuaternions.rotation3D(list, Math.toRadians(90), 0, 1, 0); 
    for (int i=0;i<list1.size();i++) 
     System.out.print(list1.get(i)+" "); 
} 

今の呼び出しは、このrotation3D(points, Math.toRadians(90), 0, 1, 0)ようになります。 これらのメソッドを書くために私はthis articleを使用しました。

これらは私が回転しようとするフィギュアのための頂点です:

//下のベース

古い頂点:

0.0 0.0 -9.0

0.0 0.0 -11.0

20.0 0.0 -11.0

20.0 0.0 -9.0

新しい頂点:

-9.0 0.0 -1.9984014E-15

-11.0 0.0 -2.4424907E-15

-11.0 0.0 -20.0

-9.0 0.0 -20.0

//トップベース

古い頂点:

0.0 20.0 -11.0

0.0 20.0 -9.0

20.0 20.0 -9.0

20.0 20.0 -11.0

新しい頂点:

-11.0 20.0 -2.4424907E-15

-9.0 20.0-1.9984014e-15

-9.0 20.0 -20.0

-11.0 20.0 -20.0

//

古い頂点に直面:

0.0 20.0 -9.0

0.0 0.0 -9.0

20.0 0.0 -9.0

20.0 20.0 -9。0

新しい頂点:

-9.0 20.0 -1.9984014E-15

-9.0 0.0 -1.9984014E-15

-9.0 0.0 -20.0

-9.0 20.0 -20.0

//バック

古い頂点:

個の

20.0 20.0 -9.0

20.0 0.0 -9.0

20.0 0.0 -11.0

20.0 20.0 -11.0

新しい頂点: -9.0 20.0 -20.0

- 9.0 0.0 -20.0

-11.0 0.0 -20.0

-11.0 20.0 -20.0

//右側

古い頂点:

0.0 0.0 -11.0

0.0 20.0 -11.0

20.0 20.0 -11.0

20.0 0.0 -11.0

新しい頂点:

-11.0 0.0 -2.4424907E-15

-11.0 20.0 -2.4424907E-15

-11.0 20.0 -20.0

-11.0 0.0 -20.0

//左ベース

古い頂点:

0.0 20.0 -9.0

0.0 20.0 -11.0

0.0 0.0 -11.0

0.0 0.0 -9.0

新しい頂点:

-9.0 20.0 -1.9984014E-15

-11.0 20.0 -2。4424907E-15

-11.0 0.0 -2.4424907E-15

-9.0 0.0 -1.9984014E-15

+1

50ラジアンの回転が必要ですか?それとも、度が必要ですか? –

+0

度は私が欲しいものです。 – MRM

+1

'Math'関数は角度ではなく、ラジアン単位の角度をとります。 'Math.toRadians() 'を使って変換できます。 –

答えて

2

あなたはtotal[0] = ... 4回をやっています。

+0

ああ...はい!愚かな私... – MRM

+0

私はそれを修正したが、それはまた、望ましくない翻訳をします。 – MRM