Fix coordinates

This commit is contained in:
Nick 2022-06-24 11:38:13 -04:00
parent c760805c50
commit 8adf294178

View File

@ -7,20 +7,17 @@ namespace NewHorizons.Utility
// Using the phi and theta convention used on https://mathworld.wolfram.com/SphericalCoordinates.html (Mathematics not physics convention)
public static Vector3 CartesianToSpherical(Vector3 v)
{
// y is up in unity
var x = v.y;
var y = v.z;
var z = v.x;
// Y is up in unity
float dist = Mathf.Sqrt(x * x + y * y + z * z);
float dist = Mathf.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
// theta
float longitude = 180f;
if (x > 0) longitude = Mathf.Rad2Deg * Mathf.Atan(y / x);
if (x < 0) longitude = Mathf.Rad2Deg * (Mathf.Atan(y / x) + Mathf.PI);
if (v.x > 0) longitude = Mathf.Rad2Deg * Mathf.Atan(v.z / v.x);
if (v.x < 0) longitude = Mathf.Rad2Deg * (Mathf.Atan(v.z / v.x) + Mathf.PI);
// phi
float latitude = (Mathf.Rad2Deg * Mathf.Acos(z / dist));
float latitude = (Mathf.Rad2Deg * Mathf.Acos(v.y / dist));
return new Vector3(longitude, latitude, dist);
}
@ -35,10 +32,10 @@ namespace NewHorizons.Utility
var phi = Mathf.Deg2Rad * latitude;
var x = r * Mathf.Cos(theta) * Mathf.Sin(phi);
var y = r * Mathf.Sin(theta) * Mathf.Sin(phi);
var z = r * Mathf.Cos(phi);
var z = r * Mathf.Sin(theta) * Mathf.Sin(phi);
var y = r * Mathf.Cos(phi);
return new Vector3(z, x, y);
return new Vector3(x, y, z);
}
}
}