Search Unity

AR Location issue

Discussion in 'AR/VR (XR) Discussion' started by denali95, Dec 20, 2017.

  1. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    I'm using GPS values to make an object exist in a certain Lat Lng. To put it into Unity coordinates, I'm using this function:

    Code (CSharp):
    1. /// <param name="lat">Latitude</param>
    2.     /// <param name="lon">Longitude</param>
    3.     /// <param name="h">height.</param>
    4.     Vector3 WGS84ToCartesian(float lat, float lon, float h)
    5.     {              
    6.         float cosLat = Mathf.Cos(lat * Mathf.PI / 180.0f);
    7.         float sinLat = Mathf.Sin(lat * Mathf.PI / 180.0f);
    8.         float cosLon = Mathf.Cos(lon * Mathf.PI / 180.0f);
    9.         float sinLon = Mathf.Sin(lon * Mathf.PI / 180.0f);
    10.  
    11.         float f = 1.0f / 298.257224f;
    12.         float C = 1.0f / Mathf.Sqrt(cosLat * cosLat + (1 - f) * (1 - f) * sinLat * sinLat);
    13.         float S = (1.0f - f) * (1.0f - f) * C;
    14.         float earthRadius = 6378137.0f;
    15.  
    16.         float x = (earthRadius * C + h) * cosLat * cosLon;
    17.         float y = (earthRadius * C + h) * cosLat * sinLon;
    18.         float z = (earthRadius * S + h) * sinLat;
    19.  
    20.         return new Vector3 (x, y, z);
    21.     }
    22. }
    The problem is, it seems to be almost at a rotation. When I move away from the object, rather than seeming to move backward in the z direction, it seems to go up in the y as well, making it climb into the sky. Is this because of the curvature of the earth? Btw, I'm also using altitude for height.