現在旋轉工作,我試着旋轉一個物體90度,它旋轉它,但也使一個意想不到的翻譯在左側的OX軸。我爲quaterion的w,x,y,z組件添加了規範化方法,並更正了我發現的任何代碼錯誤。java四元數三維旋轉實現
這是我使用的方法:
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 -20.0 0.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 -20.0 0.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
您確定要旋轉50弧度?或者你想學位嗎? – 2012-03-23 17:15:06
度是我想要的。 – MRM 2012-03-23 17:24:36
'Math'函數以弧度表示角度,而不是度數。你可以通過'Math.toRadians()'進行轉換。 – 2012-03-23 17:41:59