Vector3 到纬度经度

Vector3 to Latitude Longitude

我正在尝试对球体上的一个点进行光线投射,然后将该点转换为其适当的纬度和经度。我遇到了这个问题,它与我在堆栈交换中所做的几乎完全相关

3D coordinates on a sphere to Latitude and Longitude

使用了这个函数

public static LatLon FromVector3(Vector3 position, float sphereRadius)
    {
        float lat = (float)Math.Acos(position.Y / sphereRadius); //theta
        float lon = (float)Math.Atan(position.X / position.Z); //phi
        return new LatLon(lat, lon);
    }

但是,该函数总是返回一个介于 0 到 3(纬度)和 -1 到 1(经度)之间的值。

我使用的程序是 Unity,有人知道为什么我会得到这些不正确的值吗?我将光线投射命中世界矢量和球体半径作为我的参数传递,球体以世界 0,0,0 为中心。

更新代码

public static Vector2 GetLatLonFromVector3 (Vector3 position, float sphereRadius){
        float lat = (float)Mathf.Acos(position.y / sphereRadius); //theta
        float lon = (float)Mathf.Atan2(position.x, position.z); //phi
        lat *= Mathf.Rad2Deg;
        lon *= Mathf.Rad2Deg;
        return new Vector2(lat, lon);
    }

我现在 运行 遇到的问题是值没有被它们所在的象限正确反映。

Acos 和 atan return 以弧度表示的值,因此从 -π (-3.1...) 到 π (3.1...)。为了转换为度数,除以 π 并乘以 180。Unity 甚至有一个有用的常量 Mathf.Rad2Deg 来简化它。

public static LatLon FromVector3(Vector3 position, float sphereRadius)
{
    float lat = (float)Math.Acos(position.Y / sphereRadius); //theta
    float lon = (float)Math.Atan(position.X / position.Z); //phi
    return new LatLon(lat * Mathf.Rad2Deg, lon * Mathf.Rad2Deg);
}