如何从两个3D方向矢量获得X和Y旋转(零Z旋转)

问题描述 投票:0回答:1

我有两个3D方向(归一化的)向量A和B。我正在寻找将Euler角旋转成B的欧拉角。我知道它有很多解决方案,因为仅使用两个轴就可以将法向向量旋转到任何地方,例如X和Y或滚动和倾斜。我必须找到Z旋转为零的解决方案。

我想创建一个这样的函数:

Vector3 dir1 (0, 1, 0);
Vector3 someRotation(Pi / 4, Pi / 4, 0);
Vector3 dir2 = dir1.rotateXYZ(someRotation);

Vector3 xyRotation = dir1.eulerToDirection(dir2);

// now I expect that the eulerToDirection fv calculated the X, Y rotation from the vectors at Z = 0
// so xyRotation.x == Pi / 4 && xyRotation.y == Pi / 4 && xyRotation.z == 0 is true
// aside from the floating point error

实际上,some rotation并不总是在Z处为0。仅用于示例

math graphics 3d
1个回答
0
投票

首先使用atan2(z2-z1, y2-y1)查找围绕y和z对齐的X轴旋转的角度。然后,使用刚旋转的向量和最终向量之间的点积的acos。这将是围绕Y旋转所需的角度。根据实现旋转的方式,可能需要翻转一些符号。

© www.soinside.com 2019 - 2024. All rights reserved.