Created
November 9, 2025 21:34
-
-
Save StagPoint/029002b91336db8c685a8e5a906cbfbf to your computer and use it in GitHub Desktop.
Converts a direction vector to spherical coordinates (for the Godot game engine)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Godot; | |
| public static class MathUtils | |
| { | |
| /// <summary> | |
| /// Converts a normalized direction vector into spherical coordinates | |
| /// </summary> | |
| /// <param name="cartCoords">The normalized direction vector to convert</param> | |
| /// <param name="outPolar">The rotation angle (in radians) of the direction vector</param> | |
| /// <param name="outElevation">The elevation angle (in radians) of the direction vector</param> | |
| public static void DirectionToSpherical( Vector3 cartCoords, out float outPolar, out float outElevation ) | |
| { | |
| if( cartCoords.X == 0 ) | |
| { | |
| cartCoords.X = float.Epsilon; | |
| } | |
| outPolar = Mathf.Atan2( -cartCoords.Z, cartCoords.X ); | |
| outPolar = Mathf.Wrap( outPolar, 0, Mathf.Tau ); | |
| outElevation = Mathf.Asin( cartCoords.Y ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment