Search Unity

GPS float precision issue!

Discussion in 'Editor & General Support' started by kenjichanhkg, Oct 21, 2014.

  1. kenjichanhkg

    kenjichanhkg

    Joined:
    May 5, 2014
    Posts:
    6
    Hi all,

    Why does unity use float instead of double? I'm getting just 112.123 (longitude), about 3-4 decimals. That's real bad. I lost many decimals/GPS accuracy just because of the data type Unity's chosen.

    For iOS, in iPhone_Sensors.mm, I've found these lines.
    Unity casts a double into a float.

    Code (CSharp):
    1. UnitySetLastLocation(double timestamp,
    2.                     float latitude,
    3.                     float longitude,
    4.                     float altitude,
    5.                     float horizontalAccuracy,
    6.                     float verticalAccuracy);
    7.  
    8. - (void)locationManager:(CLLocationManager *)manager
    9.     didUpdateToLocation:(CLLocation *)newLocation
    10.           fromLocation:(CLLocation *)oldLocation
    11. {
    12.     gLocationServiceStatus.locationStatus = kLocationServiceRunning;
    13.     UnitySetLastLocation([newLocation.timestamp timeIntervalSince1970],
    14.                         newLocation.coordinate.latitude,
    15.                         newLocation.coordinate.longitude,
    16.                         newLocation.altitude,
    17.                         newLocation.horizontalAccuracy,
    18.                         newLocation.verticalAccuracy);
    19. }
    I believe that's very very very easy to fix. Right?

    Can anyone or the Unity Team have a look at the issue?

    Thanks!
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Unity uses floats internally. If you need doubles, well, you've just found them.
     
  3. kenjichanhkg

    kenjichanhkg

    Joined:
    May 5, 2014
    Posts:
    6
    Do you see the problem here? GPS coordinates are NOT in-game coordinates, they have nothing to do with what Unity uses internally. I am not asking for doubles. I am asking for solutions that can provide more accuracy than just 3-4 decimals, e.g. 114.12345678 becomes 144.123

    How many meters are lost in there!?

    We could use UInt32, for example, multiply by 100000000, so we have 11412345678. Whatever ways not losing the information in those numbers are better than float in this case.
     
  4. kenjichanhkg

    kenjichanhkg

    Joined:
    May 5, 2014
    Posts:
    6
    GPS coordinates with that much inaccuracy are completely useless. Imagine GPS nowadays with 100-1000m error.
     
  5. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Yes, I know by the time these values get into Unity they are floats and useless. iOS gives you the doubles that you need. (CLLocationDegrees is typedef-ef to double.) So, until some time in the future that this is fixed, there is a solution you can use today. Will that work for you?
     
  6. kenjichanhkg

    kenjichanhkg

    Joined:
    May 5, 2014
    Posts:
    6
    There is a solution that I can use today? Yes, thank you! What is it?
     
  7. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Get the values that are picked up in iPhone_Sensors.mm, convert them into a string, and pass them into Unity. See:

    http://docs.unity3d.com/Manual/PluginsForIOS.html

    "Unity iOS supports limited native-to-managed callback functionality via UnitySendMessage:"
     
  8. kenjichanhkg

    kenjichanhkg

    Joined:
    May 5, 2014
    Posts:
    6
    Yup. That will do it. Thanks!
    But I think future release should fix it.
     
  9. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  10. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  11. j0schm03

    j0schm03

    Joined:
    Jun 21, 2018
    Posts:
    16
    Is there a similar solution for Android?
     
  12. Max-Bot

    Max-Bot

    Joined:
    Sep 25, 2013
    Posts:
    83
    Yes, solution exists for IOS and Android.
    Hope it helps to struggling with this problem: Accura GPS for Unity
     
  13. karsnen

    karsnen

    Joined:
    Feb 9, 2014
    Posts:
    65
    Max-Bot likes this.
  14. Max-Bot

    Max-Bot

    Joined:
    Sep 25, 2013
    Posts:
    83
    On the other hand it saves more time to use.
    I believe it will be updated with time flow.
     
    Last edited: Aug 2, 2018
  15. neomarian

    neomarian

    Joined:
    May 11, 2012
    Posts:
    43
  16. FunRobDev

    FunRobDev

    Joined:
    Jun 3, 2017
    Posts:
    11
    I've run some float precision tests in the longitude latitude coordinate range (-180, +180). I found out that float precision is enough for ~0.94m at the edge of the world map. But this is the worst case. So for general use of GPS I wouldn't worry too much.

    Details:

    Float's precision around values of +180 is 0.000016
    Float's precision around values of +1 is 0.00000006

    For example in North-West USA, Portland, float's coordinates have the accuracy of 0.1 meters.

    So if you don't want to send missiles to mice with your app, float is just fine :)

    How did I tested?

    I opened the two links below, first compared them by pressing TAB in browser (you can do that easily), but I took screenshots, compared the shift at the edge of the screenshots (or grid is also good above the screenshot on a second layer), When I've seen the amount of shift, I went back to google map and measured that distance. North-West USA, Portland:
    https://www.google.hu/maps/@45.4963671,-122.6707551,21z
    https://www.google.hu/maps/@45.4963687,-122.6707567,21z
    You can see the coordinates above, I added the worst case float precision 0.000016 to the second one. Note that precision is better here because coordinates are not around 180 but only ~122 and ~45, but I took the worst case.

    I made this kind of thest in North-Canada and Japan where coordinates are big. If you live in a country when coordinates are smaller gps accuracy with float is even better. E.g. in Paris float precision is 0.01m or less.

    And if you are interested, here are my c# test for float precision:
    Code (CSharp):
    1.     public void Test1()
    2.     {
    3.         double step = 0.000016;  // float fails badly with 0.000015 , but 0.000016 is good!
    4.         RunFloatTest(-181, step);
    5.         RunFloatTest(-001, step);
    6.         RunFloatTest(+179, step);
    7.     }
    8.     private void RunFloatTest(double start, double step)
    9.     {
    10.         double end = start + 2.0;
    11.         float prevfloat = float.MinValue;
    12.         float f;
    13.         long sameCount = 0;
    14.         long count = 0;
    15.         for (double d = start; d <= end; d += step)
    16.         {
    17.             f = (float)d;
    18.             if (f == prevfloat) sameCount++;
    19.             prevfloat = f;
    20.             count++;
    21.         }
    22.         Mydebug.LogFormat("Samecount: {0} ({1}%)", sameCount, (double)sameCount / count * 100.0);
    23.     }
     
    Last edited: May 8, 2020
    thorikawa likes this.
  17. asa989

    asa989

    Joined:
    Dec 18, 2015
    Posts:
    52
    We really need to have the double. It would make the AR experience even better.
     
  18. tmSitz

    tmSitz

    Joined:
    Jun 10, 2019
    Posts:
    1


    I develop an outdoor GPS based MMO AR game using Unity. It uses the default location system. In general players get 3m accuracy in most wooded conditions and 10m indoors. Since it is a territory control game fought with paintball guns, each "Control Point" has a "10 meter gps center". Cross the 10m circle and the player begins capturing that location for their side. They have to stay within 25 meters for 3 minutes. This has been the best overall distance and timing setup for the game mechanics to work in the real world.
     
  19. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,163