【问题标题】:Rotating a globe in Unity while keeping it the right way up在 Unity 中旋转地球,同时保持正确的方向
【发布时间】:2021-08-13 19:39:41
【问题描述】:

问题来了:

我有一个统一的地球,通过右键单击并拖动鼠标,它可以围绕赤道和与相机水平的平行轴旋转(在这种情况下是绝对 x 轴,保持两极在th YZ 平面),这工作正常,没有问题。

旋转地球的另一种方法是点击标签,它会自动旋转地球以使标签在相机上居中。地球以全球坐标 0,0 为中心开始。这可行,但是标签离本初子午线越远(因此从 0 纬度向东或向西越远),两极旋转远离其预期轴的距离越远(而不是沿 YZ 平面保持)。有没有人知道如何实现这一点?

以下是相关代码:

public class LabelBehaviour : MonoBehaviour
{
    [...]
    public void HomeIn()
    {
        globe.transform.rotation = Quaternion.identity;
        Vector3 fromDirection = transform.position;
        Vector3 toDirection = mainCamera.transform.position;
        InputController.Instance.rotateTo = Quaternion.FromToRotation(fromDirection, toDirection);
    }
}

//InputController class is attached to the globe object
public class InputController : MonoBehaviour {
    [...]
    void Update()
    {
        if(Quaternion.Angle(rotateTo,transform.rotation) > .1f)
            //rotate towards position in 1.5s
            rotateTime = Mathf.Min(rotateTime + Time.deltaTime * .75f,1);
            transform.rotation = Quaternion.Slerp(rotateFrom, rotateTo, rotateTime);
            if (Quaternion.Angle(rotateTo, transform.rotation) <= .1f)
            {
                rotateTo = transform.rotation;
                rotateFrom = transform.rotation;
                rotateTime = 0;
            }
            UpdateLabelRotations();
        }
    }
}

【问题讨论】:

  • 请不要将答案编辑到问题中。相反,请考虑self-answering。您应该有机会从这个问题中删除您的自我回答,并将其添加到下面作为答案。如果一段时间后您不这样做,那么应该有人代表您这样做,将问题恢复到添加答案之前的版本,将答案发布为社区 wiki,并将您作为作者。

标签: unity3d rotation linear-algebra quaternions


【解决方案1】:

经过一整天的数学运算,结果证明很简单:

因为我知道标签的球坐标(例如纬度/经度),我可以算出我需要的对应欧拉角,这里是相关代码:

InputController.Instance.rotateTo = Quaternion.Euler(
    xzAngle * Mathf.Cos(Mathf.Deg2Rad * yAngle), 
    yAngle, 
    xzAngle * Mathf.Sin(Mathf.Deg2Rad * yAngle)
);

虽然是几天后发布的,但对于 90 度经度倍数以外的点,这似乎不是特别准确

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多