如何使用joml将标准化向量旋转到点



这就是我对如何将矢量旋转到点的理解:

矢量A=(0,0,-1)
矢量B=(15,164,16)

步骤1:归一化B
步骤2:计算A和归一化B之间的角度
第3步:计算A与归一化B 的叉积

然后,将A绕着我们在步骤3中计算的轴旋转,乘以在步骤2中计算的角度(以弧度为单位),应该会得到B的归一化矢量。

然而,尝试用joml做这件事并没有得到正确的结果。这是我的代码:

Vector3f vecA = new Vector3f(0.0f, 0.0f, -1.0f);
System.out.println("Vector A: " + vecA.toString(FORMAT));
Vector3f vecB = new Vector3f(0, 0.5f, 1.0f).normalize();
System.out.println("Vector B: " + vecB.toString(FORMAT));
float angle = (float) Math.acos(vecA.dot(vecB));
System.out.println("Angle between the two: " + angle + "(" + Math.toDegrees(angle) + "°)");
Vector3f rotationAxis = new Vector3f();
vecA.cross(vecB, rotationAxis);
Vector3f rotatedVector = new Vector3f();
vecA.rotateAxis(angle, rotationAxis.x, rotationAxis.y, rotationAxis.z, rotatedVector).normalize();
System.out.println("Rotated Vector: " + rotatedVector.toString(FORMAT));

这将产生以下输出:

Vector A: (0 0 -1)
Vector B: (0 0.44721 0.89443)
Angle between the two: 2.6779451(153.43495411905388°)
Rotated Vector: (0 0.82566 0.56416)

根据上面的计算,旋转后的矢量不应该等于矢量B的输出吗?

您必须规范化rotationAxis-向量,因为轴由单位向量表示(请参见例如。https://en.wikipedia.org/wiki/Axis%E2%80%93angle_representation)。

因此,只需更换

vecA.cross(vecB, rotationAxis);

带有

vecA.cross(vecB, rotationAxis).normalize();

它起作用:

Vector A: (0 0 -1)
Vector B: (0 0.44721 0.89443)
Angle between the two: 2.6779451(153.43495411905388°)
Rotated Vector: (0 0.44721 0.89443)

最新更新