Converting (lat, lon) to Unity Vector3 target position on globe shaped map
I'd like to set a POI on the globe shaped map provided in the "Globe" example. I know how to do the conversion on a standard flat map but how do I accomplish this on the globe?
@DanielRakh The conversion formula should look something like this: https://gist.github.com/david-rhodes/131413e24355b0a5f0afd0c6b6e77e38.
@david-rhodes great thanks! And the _radius is EarthRadius = 6378137 provided in Conversions?
@DanielRakh No, the radius needs to be in Unity units. For example, if you wanted to project onto a default sphere, the radius would be .5.
Hmm doesn't seem to be working for me. I'm using the Globe scene provided setting the Earth Radius in GlobeTerrainFactory to 100. Also converting my coordinates to radians...what am I missing?
float ltR = lt * Mathf.Deg2Rad;
float lnR = ln * Mathf.Deg2Rad;
float _radius = 100f;
float xPos = (_radius) * Mathf.Cos(ltR) * Mathf.Cos(lnR);
float zPos = (_radius) * Mathf.Cos(ltR) * Mathf.Sin(lnR);
float yPos = (_radius) * Mathf.Sin(ltR);
@DanielRakh IIRC, there's a bug in the SphereTerrainFactory such that the radius field there is not the actual radius in Unity. Create a 3d sphere and scale it up until it roughly encompasses your globe, and use that as your radius for these calculations. We'll work on fixing that bug soon and adding these conversions to the library.
If you want to use latitude and longitude in degrees, you can use this formula.
Excerpt from my Position.cs class:
public static GameObject OnGlobe(GameObject marker, string type, List<Vector2> coordinates)
{
var threePosition = coordinates.Select(coordinate => Quaternion.AngleAxis(coordinate.y,-Vector3.up)
* Quaternion.AngleAxis(coordinate.x, -Vector3.right)
* new Vector3(0, 0, 1) * _globeRadius
+ _globe.transform.position).ToList();
...
This should give you a world space coordinate for whatever you're using.
If you're looking for help calculating the radius, you can create a sphere collider (which can help you see it visually) and try using this:
_globeRadius = _globe.GetComponentInChildren<SphereCollider> ().radius * _globe.transform.lossyScale.z;
Thanks for sharing @ahokinson!
@ahokinson thanks for the formula!
I'm still having some trouble though...now I'm getting the POI to actually show up on the globe so thats great but I think the longitude is off. I'm putting in the coordinates for New York City but getting an indicator to show up somewhere in Italy?
float _globeRadius = _map.GetComponentInChildren<SphereCollider>().radius * _map.transform.lossyScale.z;
var pos = Quaternion.AngleAxis(-74.0059f,-Vector3.up) * Quaternion.AngleAxis(40.7128f, -Vector3.right) * new Vector3(0, 0, 1) * _globeRadius + _map.transform.position;
GameObject current = Instantiate(mapObject);
current.transform.position = new Vector3(pos.x, pos.y, pos.z);
current.transform.localScale = new Vector3(0.005f, 0.005f, 0.005f);
Any idea on whats going on?
@DanielRakh Looks like a 90 degree offset. As far as I remember, you should aim to have the prime meridian facing Z-forward. Otherwise, just add a 90 degree offset to your result position.
@DanielRakh,
I've had similar issues with offsets as @david-rhodes suggested.
I would suggest being very careful with your hierarchy. This is what mine looks like.

This is mostly done to ensure rotations are where they need to be (most often 0). If you plan to have your globe spinning, I would also recommend keeping the map object stationary and rotating a camera around it.
Here is an example of how I use the same positioning and rotating.
Figured it out. Thank you both for the help!
@DanielRakh @ahokinson Globe conversions method and example: https://github.com/mapbox/mapbox-unity-sdk/pull/249. The incorrect radius issue is also resolved in this PR, so you no longer need a sphere collider. You can simply get radius from the FlatSphereTerrainFactory.
Oh, and if you want to rotate the globe instead of the camera, you can simply rotate the converted geocoordinate Vector3 by the globe's rotation:
var rotatedPosition = _globeTransform.rotation * position;
@david-rhodes How do you go about offsetting the result position. I have the globe continuously spinning.
@Tonykidv2 Isn't it what this comment says?
You can simply rotate the converted geocoordinate Vector3 by the globe's rotation:
var rotatedPosition = _globeTransform.rotation * position;
@apavani
private Vector3 GPSLocation()
{
Vector3 result;
float ltR = Lat * Mathf.Deg2Rad;
float lnR = Lon * Mathf.Deg2Rad;
float xPos = (radius * transform.lossyScale.z) * Mathf.Cos(ltR) * Mathf.Cos(lnR);
float zPos = (radius * transform.lossyScale.z) * Mathf.Cos(ltR) * Mathf.Sin(lnR);
float yPos = (radius * transform.lossyScale.z) * Mathf.Sin(ltR);
Vector3 earthPositon = PlanetTracker.transform.position;
result = new Vector3(xPos + earthPositon.x, yPos + earthPositon.y, zPos + earthPositon.z);
return result;
}
Well here is my code. In my application earth is rotating and every so often a new GPS coordinate will be enter and this is called but this function doesn't take into account how much the globe has rotated.
this
var rotatedPosition = _globeTransform.rotation * position;
doesn't seem to work.