Skip to content

Instantly share code, notes, and snippets.

@inoook
Last active May 9, 2018 09:58
Show Gist options
  • Save inoook/9261394ddcf5954112c5a6504643af1a to your computer and use it in GitHub Desktop.
Save inoook/9261394ddcf5954112c5a6504643af1a to your computer and use it in GitHub Desktop.
public static Vector3 SetLatLonPosition(float r, float lat, float lon)
{
float phi = (90-lat)*Mathf.Deg2Rad;
float theta = (lon)*Mathf.Deg2Rad;
float x = r * Mathf.Sin(phi) * Mathf.Cos(theta);
float z = r * Mathf.Sin(phi) * Mathf.Sin(theta);
float y = r * Mathf.Cos(phi);
Vector3 pos = new Vector3(x,y,z);
return pos;
}
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment