whi*_*olf 1 geometry latitude-longitude unity-game-engine
我正在尝试对球体上的一个点进行光线投射,然后将该点转换为其适当的纬度和经度。我遇到了这个问题,它与我在堆栈交换上所做的事情几乎完全相关
使用这个函数
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);
}
Run Code Online (Sandbox Code Playgroud)
但是,该函数始终返回 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);
}
Run Code Online (Sandbox Code Playgroud)
我现在遇到的问题是它们所在的象限没有正确反映这些值。
Acos 和 atan 返回以弧度为单位的值,因此从 -\xcf\x80 (-3.1...) 到 \xcf\x80 (3.1...)。要转换为度数,请除以 \xcf\x80 并乘以 180。Unity 甚至有一个有用的常量 Mathf.Rad2Deg 来简化此操作。
\n\npublic static LatLon FromVector3(Vector3 position, float sphereRadius)\n{\n float lat = (float)Math.Acos(position.Y / sphereRadius); //theta\n float lon = (float)Math.Atan(position.X / position.Z); //phi\n return new LatLon(lat * Mathf.Rad2Deg, lon * Mathf.Rad2Deg);\n}\nRun Code Online (Sandbox Code Playgroud)\n