Skip to content

Instantly share code, notes, and snippets.

@StagPoint
Created November 9, 2025 21:34
Show Gist options
  • Select an option

  • Save StagPoint/029002b91336db8c685a8e5a906cbfbf to your computer and use it in GitHub Desktop.

Select an option

Save StagPoint/029002b91336db8c685a8e5a906cbfbf to your computer and use it in GitHub Desktop.
Converts a direction vector to spherical coordinates (for the Godot game engine)
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