我正在制作飞机游戏,我需要获得飞机的偏航。下面是设置俯仰和滚动的工作代码,但我无法弄清楚偏航。
private void _CalculateRollAndPitchAngles()
{
// Calculate roll & pitch angles
// Calculate the flat forward direction (with no y component).
Vector3 flatForward = transform.forward;
flatForward.y = 0;
// If the flat forward vector is non-zero (which would only happen if the plane was pointing exactly straight upwards)
if (flatForward.sqrMagnitude > 0)
{
flatForward.Normalize();
// calculate current pitch angle
Vector3 localFlatForward = transform.InverseTransformDirection(flatForward);
m_pitchDegrees = Mathf.Atan2(localFlatForward.y, localFlatForward.z) * Mathf.Rad2Deg;
// calculate current roll angle
Vector3 flatRight = Vector3.Cross(Vector3.up, flatForward);
Vector3 localFlatRight = transform.InverseTransformDirection(flatRight);
m_rollDegrees = Mathf.Atan2(localFlatRight.y, localFlatRight.x) * Mathf.Rad2Deg;
}
}
答案 0 :(得分:0)
继续评论之后,您可以使用here中的公式并将Y-Z交换计算出来,将转换矩阵(或其旋转部分)直接转换为欧拉角:
roll = atan2(M[1][2], M[1][1])
pitch = atan2(-M[1][0], sqrt(M[0][1] * M[0][1] + M[0][0] * M[0][0]))
yaw = atan2(M[2][0], M[0][0])