Mar
1
2016

Latitude Longitude Position On 3D Sphere (V2)

latitude_longitude_unity_1

This is just a c# conversion and few updated notes for the old post : http://unitycoder.com/blog/2011/08/09/latitude-longitude-on-3d-sphere/

Instructions
– Attach the script below to your sphere (Unity sphere UV mapping is not suitable so I downloaded this sphere model and scaled transform to 5)
– Add earth texture map to the sphere (its included in the old version package)
– Add a marker sphere, scale it to 0.3 or so
– Assign marker sphere to “marker” field on the script
– Set lat long values in the inspector (default value is for London, get more values from here www.findlatitudeandlongitude.com )
– Note: Rotate your earth sphere by 180 degrees in Y axis (otherwise marker goes to wrong position) *Probably not needed
– The hit play to test it.

Resources:
– Map texture was taken from somewhere here : http://visibleearth.nasa.gov/view_cat.php?categoryID=1484
– Original math code is from: http://www.actionscript.org/forums/index.php#post722957 *link broken..

 

Script source


using UnityEngine;
public class LatLong : MonoBehaviour
{
public Transform marker; // marker object
public float radius = 5; // globe ball radius (unity units)
public float latitude = 51.5072f; // lat
public float longitude = 0.1275f; // long
// Use this for initialization
void Start()
{
// calculation code taken from
// @miquael http://www.actionscript.org/forums/showthread.php3?p=722957#post722957
// convert lat/long to radians
latitude = Mathf.PI * latitude / 180;
longitude = Mathf.PI * longitude / 180;
// adjust position by radians
latitude -= 1.570795765134f; // subtract 90 degrees (in radians)
// and switch z and y (since z is forward)
float xPos = (radius) * Mathf.Sin(latitude) * Mathf.Cos(longitude);
float zPos = (radius) * Mathf.Sin(latitude) * Mathf.Sin(longitude);
float yPos = (radius) * Mathf.Cos(latitude);
// move marker to position
marker.position = new Vector3(xPos, yPos, zPos);
}
}

view raw

LatLong.cs

hosted with ❤ by GitHub


15 Comments + Add Comment

  • Hi again, I would love to see your final project
    So I downloaded the white sphere.7z
    I copied it in, dragged the old texture on to it
    sized it to 5,5,5
    added a marker sphere, attached it to script

    Mine seems to be in the right place if I do not rotate the sphere by 180?

    • oh i see, actually i did remove the empty parent from the downloaded sphere, that was probably causing the rotation.

      • Actually, regarding the rotation of the sphere along the Y-axis, I found that I had to rotate mine by the amount that the ‘seam’ in the wrapped texture was off from Y == 0.In my case, that was -84.4.
        I was unable to get access to the linked dropbox sphere so I made my own in Blender, UV-mapped an Earth texture to it and exported it to .obj. In Unity, I added the same Earth texture and it used the Blender UV to seamlessly map to the sphere. I added a single pixel column to the edge of the texture in Photoshop so I could see the seam on the sphere. Then I rotated the sphere on the Y-axis until the seam lined up with where Y == 0 using the gridlines.
        Close enough for what I need it for…
        https://dl.dropboxusercontent.com/u/306957072/rotated_texture_seam.png
        https://dl.dropboxusercontent.com/u/306957072/rotated_texture_seam_running.png

  • Hi mate. Have you tested the script with Unity 5? I’ve download the older version package (I couldn’t find the V2 one) and just run the scene_lat_long. The marker appeared by far away London. I’ve then tested the scripts of the current version with no luck either. The model and scene is the one you provided by your V1 package; hence there is no chance to have an incompatible earth model. I need to be able to find correctly a 3d point on earth sphere for my project but so far I had no luck with the maths. What else could be the problem? Any help will be much appreciated.

  • HI,

    latitude -= 1.570795765134f

    From where did you get the above value?

    • I think it was in the original code already.. (but the link is broken)

      • Yes it was in the original code. May I please know on what basis the value is set?

  • Haversine formula: giving great-circle distances between two points on a sphere from their longitudes and latitudes
    https://rosettacode.org/wiki/Haversine_formula#C.23

  • Hi,

    The sphere model links no longer work. They take you to the forum with the post, but the links in the forum are dead… Any chance you can upload them to GitHub or somewhere else?

  • […] The following is my code for this conversion, this code is based on the code here […]

Leave a comment to Bill Rigas

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.