From 8adf2941785a76ee2c4a8e786e904bc96a60a60e Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 24 Jun 2022 11:38:13 -0400 Subject: [PATCH] Fix coordinates --- NewHorizons/Utility/CoordinateUtilities.cs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/NewHorizons/Utility/CoordinateUtilities.cs b/NewHorizons/Utility/CoordinateUtilities.cs index d4267cef..497cbb5e 100644 --- a/NewHorizons/Utility/CoordinateUtilities.cs +++ b/NewHorizons/Utility/CoordinateUtilities.cs @@ -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); } } }