来自 javax.vectmath.matrix3d 的旋转
Rotations from javax.vectmath.matrix3d
时隔许久,再次尝试使用JAVA。我正在使用 vectmath 包,我想用它来旋转带有旋转矩阵的 3d 矢量。所以我写道:
double x=2, y=0.12;
Matrix3d rotMat = new Matrix3d(1,0,0, 0,1,0, 0,0,1); //identity matrix
rotMat.rotX(x); //rotation on X axis
rotMat.rotY(y); // rotation on Y axis
System.out.println("rot :\n" + rotMat); // diagonal shouldn't have 1 value on it
结果:
rot :
0.9928086358538663, 0.0, 0.11971220728891936
0.0, 1.0, 0.0
-0.11971220728891936, 0.0, 0.9928086358538663
不幸的是,它没有给我预期的结果。就好像他忽略了第一次旋转(围绕 X)而只进行了第二次旋转(围绕 Y)。
如果我评论 rotMat.rotX(x);它给了我相同的结果。
我怀疑打印或变量管理有误。
谢谢
方法 rotX
和 rotY
set/overwrite 矩阵元素,因此您随后对 rotY(y)
的调用取消了对 rotX(x)
的调用。这样的事情应该有效:
Vector3d vector = ... //a vector to be transformed
double x=2, y=0.12;
Matrix3d rotMat = new Matrix3d(1,0,0, 0,1,0, 0,0,1); //identity matrix
rotMat.rotX(x); //rotation on X axis
rotMat.transform(vector);
rotMat.rotY(y); // rotation on Y axis
rotMat.transform(vector);
// the vector should now have both x and y rotation
// transformations applied
System.out.println("Rotated vector :\n" + vector);
时隔许久,再次尝试使用JAVA。我正在使用 vectmath 包,我想用它来旋转带有旋转矩阵的 3d 矢量。所以我写道:
double x=2, y=0.12;
Matrix3d rotMat = new Matrix3d(1,0,0, 0,1,0, 0,0,1); //identity matrix
rotMat.rotX(x); //rotation on X axis
rotMat.rotY(y); // rotation on Y axis
System.out.println("rot :\n" + rotMat); // diagonal shouldn't have 1 value on it
结果:
rot :
0.9928086358538663, 0.0, 0.11971220728891936
0.0, 1.0, 0.0
-0.11971220728891936, 0.0, 0.9928086358538663
不幸的是,它没有给我预期的结果。就好像他忽略了第一次旋转(围绕 X)而只进行了第二次旋转(围绕 Y)。 如果我评论 rotMat.rotX(x);它给了我相同的结果。
我怀疑打印或变量管理有误。
谢谢
方法 rotX
和 rotY
set/overwrite 矩阵元素,因此您随后对 rotY(y)
的调用取消了对 rotX(x)
的调用。这样的事情应该有效:
Vector3d vector = ... //a vector to be transformed
double x=2, y=0.12;
Matrix3d rotMat = new Matrix3d(1,0,0, 0,1,0, 0,0,1); //identity matrix
rotMat.rotX(x); //rotation on X axis
rotMat.transform(vector);
rotMat.rotY(y); // rotation on Y axis
rotMat.transform(vector);
// the vector should now have both x and y rotation
// transformations applied
System.out.println("Rotated vector :\n" + vector);