Latitude and Longitutde

I have an earth model and i want an object to move according to the latitudes and longitudes on the model.

I have the formal for converting lats and longs to ECEF XYZ but it exceeds the floating -point precision in unity and my model is pretty small, i am using it because i wanted an offline map. anyways i am using this formula, i tried changing the radius to my model radius but it does not work.

public const float EarthRadius = 6.371f;
{
    float cosLat = Mathf.Cos(latitude * Mathf.Deg2Rad);
    float sinLat = Mathf.Sin(latitude * Mathf.Deg2Rad);
    float cosLon = Mathf.Cos(longitude * Mathf.Deg2Rad);
    float sinLon = Mathf.Sin(longitude * Mathf.Deg2Rad);

    float radius = EarthRadius / Mathf.Sqrt(1.0f - 0.00669437999014f * sinLat * sinLon);

    float x = (radius + altitude) * cosLat * cosLon;
    float y = (radius + altitude) * cosLat * sinLon;
    float z = (radius * (1.0f - 0.00669437999014f) + altitude) * sinLat;

    return new Vector3(x, y, z);
}

How can I solve this conversion to get correct coordinates?

It does not have to be precise. I just want my object to move to that latitudes and longitudes.

Thank you for your help.

I toyed with {latitude,longitude}Vector3 conversion in the past. This code approximates Earth to a sphere so won’t give you 1:1.

Link: Geographical (spherical approximation) to cartesian coordinates, Unity3d · GitHub

Well, if it doesn’t have to be precise, why do you use a formula that includes the eccentricity of earth? This is a tiny change due to the earth not being a perfect sphere. Is your model actually a proper representation of earth that includes this tiny change? If not and your model is an actual sphere your eccentricity “correction” would just make the object float or sink a tiny bit.

Apart from that your corrected “radius” calculation is wrong. Multiplying the sin or “lat” with the sin or “lon” makes no sense. This should be the square of the sin of “lat”. So it should be

float radius = EarthRadius / Mathf.Sqrt(1.0f - 0.00669437999014f * sinLat * sinLat);

You also want to store the eccentricity in a variable. It makes the code much more readable and you don’t have to duplicate magic numbers in your code.

Without that eccentricity correction the code simplifies to

{
    float cosLat = Mathf.Cos(latitude * Mathf.Deg2Rad);
    float sinLat = Mathf.Sin(latitude * Mathf.Deg2Rad);
    float cosLon = Mathf.Cos(longitude * Mathf.Deg2Rad);
    float sinLon = Mathf.Sin(longitude * Mathf.Deg2Rad);

    float radius = EarthRadius + altitude;

    float x = radius * cosLat * cosLon;
    float y = radius * cosLat * sinLon;
    float z = radius * sinLat;

    return new Vector3(x, y, z);
}

Though it usually makes more sense to do the calculations with double precision and just cast to float in the end. Doesn’t make a huge difference, but can simplify the code a bit

public const double EarthRadius = 6.371d;
public static Vector3 LatLon2XYZ(double latitude, double, longitude, double altitude = 0d)
{
    (double sinLat, double cosLat) = System.Math.SinCos(latitude * Math.PI / 180d);
    (double sinLon, double cosLon) = System.Math.SinCos(longitude * Math.PI / 180d);
    double radius = EarthRadius + altitude;

    float x = (float)(radius * cosLat * cosLon);
    float y = (float)(radius * cosLat * sinLon);
    float z = (float)(radius * sinLat);

    return new Vector3(x, y, z);
}

Keep in mind that the altitude has to be given in the same “unit” as your radius. So for the “real” earth the radius is 6371000 meters so the altitude is given in meters as well. When you scale down your earth model to only have a radius of 6.371 units in worldspace, one unit of altitude would be equal to 1 million meters or 1000 km. So keep that in mind when you work with “actual” altitude values. Sometimes it might make sense to use real world numbers and just scale the result at the end by a certain scale factor. Though that’s all up to your usecase and where the values come from.

If your “earth” model is really just about 12 units in diameter, using an altitude for features “on” earth probably doesn’t make much sense anyways. It’s a good lesson on the scale of earth ^^.