Tuesday, May 1, 2018

c# - Unity Co-ordinate system to latitude longitude conversion floating point precision lost


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.





No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...