1

I am using a Github script (Unity GPS Convertor) to convert unity co-ordinate system x,z into latitude and longitude. It is actually working fine but the problem is, when I go farther from the origin (0,0,0) my conversion is no more accurate. I deeply try to study this topic and found a valuable blog about unity coordinate limitation which states that

Floating Point Accuracy Unity allows you to place objects anywhere within the limitations of the float-based coordinate system. The limitation for the X, Y and Z Position Transform is 7 significant digits, with a decimal place anywhere within those 7 digits; in effect you could place an object at 12345.67 or 12.34567, for just two examples.

With this system, the further away from the origin (0.000000 - absolute zero) you get, the more floating-point precision you lose. For example, accepting that one unit (1u) equals one meter (1m), an object at 1.234567 has a floating point accuracy to 6 decimal places (a micrometer), while an object at 76543.21 can only have two decimal places (a centimeter), and is thus less accurate.

It means that if I put any object at 10000 position of x then, the floating point accuracy will only be 2 decimal places? My Current environment is very large and even one object position is at 90000 of x coordinate in unity position. So my question is that how can I convert the lat-long correctly keeping in mind this floating point accuracy problem. The Unity GPS Convertor library is fine but it based on float.

Remember: My Project is a very large 3D environment where someone can provide lat/long then, I move my camera at that position. This require conversion which i have discussed in this caution that is not accurate. The environment is very large so I am also shifting the origin but whenever user try to set lat/long of the player/camera, I stop origin shifting and move back everything on its original position in order to carryout right conversion. Then, I again start origin shifting.

Edit: Floating Origin Code:

 void Update()
{
    //As camera distance reached at certain limit set the environment parent position according to the inverse of camera local position
    if (AlllowShifting && CompareDistance())
    {
        ChangeEnvironmentPositionAtZero();//TODO Uncomment
    }
}



public void ChangeEnvironmentPositionAtZero() {

    Vector3 cameraInversePosition = new Vector3(camera.localPosition.x * -1f, camera.localPosition.y * -1f, camera.localPosition.z * -1f);//
    parent.transform.position = cameraInversePosition;

}

bool CompareDistance()
{

    distanceNow = Vector3.Distance(this.transform.position, origin.transform.position);
    return distanceNow > cameraDistanceLimit;

}

Note: All the objects are inside one main parent. The position is changing of the parent. While you can find Unity Co-ordinate conversion to GPS here.

Muhammad Faizan Khan
  • 2,020
  • 3
  • 29
  • 66
  • Longitude is naturally restricted to the range -180 to +180, in which you have precision to within about 7.6 millionths of a degree. How are you ending up out at 10 000? It sounds like your issue isn't the conversion itself, but your choice to allow gameplay that far from the origin. – DMGregory May 10 '19 at 13:00
  • floats are designed to handle small values at high precision or larger values at low precision. They're small and fast, but not always precise enough. Doubles offer the same precision at every size, but use twice as much bits and are slower. Since you need the precision though, they're the clear winners. Rewrite the library to use doubles, shouldn't be hard – Bálint May 10 '19 at 13:20
  • 2
    @Bálint "Doubles offer the same precision at every size" is not true. "double" is short for double-precision floating point, so it has the same precision-vs-magnitude trade-off behaviour as the single-precision version we commonly call "float", it just uses a larger mantissa so you can go vastly further from zero before the precision loss becomes troublesome. – DMGregory May 10 '19 at 13:37
  • @dmgregory 10000 is the X position ofunity coordinate system. – Muhammad Faizan Khan May 10 '19 at 17:37
  • 1
    That still leaves us with the question: why are you trying to do something that far from Unity's origin? Once we know that, we can suggest ways you can do it closer to the origin. – DMGregory May 10 '19 at 17:43
  • Simple. Actually it is a very large 3d environment where someone can provide lat long then I move my camera at that position. This require conversion. Remember the environment is very large so I am also shifting the origin but whenever user try to set lat n long I stop origin shifting and move back everything on its original position in order to carryout right conversion. – Muhammad Faizan Khan May 10 '19 at 17:50
  • I am also doing origin shifting when player reached at specific distance. – Muhammad Faizan Khan May 13 '19 at 15:23
  • @dmgregory if longitude restricted to 180 then maybe we require a different way to add model in unity as longitude value is not much vast. – Muhammad Faizan Khan May 13 '19 at 15:27
  • Sounds like you should show us your code for applying and undoing origin shifting. It looks like that might be the source of your problem, if you're tying to do the camera transition in a kind of absolute coordinate system. – DMGregory May 14 '19 at 10:58
  • The source is given in the question but i will post my specific code when it will accessible to me. – Muhammad Faizan Khan May 14 '19 at 14:53
  • @DMGregory I have shared the floating origin code. Maybe it will helpful. – Muhammad Faizan Khan May 15 '19 at 07:19

0 Answers0