我在游戏中使用四元数来计算一些基本角度。
现在我试图获得一个四元数的角度,给定一个轴。因此,给定四元数和新轴。我可以将投影角度恢复吗?
对我来说,这似乎是计算两个向量之间的有符号角度的可靠方法。代码是用Unity3D C#编写的,但并不一定是这种情况。这更像是一种通用的方法。
public static float DirectionalAngle(Vector3 from, Vector3 to, Vector3 up)
{
// project the vectors on the plane with up as a normal
Vector3 f = Vector3.Exclude(up, from);
Vector3 t = Vector3.Exclude(up, to);
// creates an angle-axis rotation
Quaternion q = Quaternion.FromToRotation(f, t);
// do something to get the angle out of the quaternion,
// given the new axis.
// TODO: Make this method. Doesn't exist.
Quaternion q2 = Quaternion.GetAngleFromAxis(q, Vector3.right);
return q2.Angle(q2);
}
我总能得到四元数的角度和轴,以及x,y,z,w和欧拉角。但我没有办法从轴上的四元数中获得投影角度。
答案 0 :(得分:1)
我找到了!我唯一要做的就是旋转四元数,使其朝向局部空间。对于我测试过的所有飞机来说,这种方法似乎都是合理的。
public static float DirectionalAngle(Vector3 from, Vector3 to, Vector3 up)
{
// project the vectors on the plane with up as a normal
Vector3 f = Vector3.Exclude(up, from);
Vector3 t = Vector3.Exclude(up, to);
// rotate the vectors into y-up coordinates
Quaternion toZ = Quaternion.FromToRotation(up, Vector3.up);
f = toZ * f;
t = toZ * t;
// calculate the quaternion in between f and t.
Quaternion fromTo = Quaternion.FromToRotation(f,t);
// return the eulers.
return fromTo.eulerAngles.y;
}