- Home /
GPS coordinates (lat/long/altitude) to Unity 3D coordinates (x, y, z)
Hi,
I'm trying to put the Unity camera into the "real" 3D space by reading the GPS coordinates and converting them to Unity 3D coordinates, and then setting the camera position to those 3D coordinates. I'm also trying to put some objects into the "real" space around the user, so they show up when the user points his mobile device at those objects (I'm using the gyroscope to detect the rotation).
The problem I'm having is that I can't seem to find a method to convert those GPS coordinates to Unity 3D coordinates so they match up (I know they can't match up 100%, but in my case they don't match even slightly). I've tried a few methods that were mentioned here, I even tried converting the method found here to C# (the function called llhxyz, along with its related functions that calculate the constants), but even that didn't give me satisfying results. I am also aware that the ECEF XYZ coordinates are not equivalent to Unity's XYZ, but even switching them around doesn't help.
Can anyone push me in the right direction or tell me what I'm doing wrong?
Perhaps you need to increase your accuracy.
I'm not sure if these issues still exists but its worth reading:
http://answers.unity3d.com/questions/759854/unity3d-location-services-not-updating-regularly.html
Hi,
I'm having exactly the same problem.
Did you find a solution for this ?
@schpinn l.O, Did you succeed to do what you want ? COuld you tell me how to do all of this ? I would like to do exactly the same thing and it is urgent thanks for all my mail :mmmklmail@gmail.com
Answer by Paulius-Liekis · Jul 22, 2015 at 12:11 PM
First of all there is no answer "how to convert lat/long to Unity coordinates", because Unity coordinates can be anything. There is no official rule how you convert real world, to Unity coordinates. It all depends what you choose your Unity coordinates to be.
The most simple thing would be to treat lat/long/altitude as coordinates on a sphere (that's what they are) and just render such sphere with user position in Unity. If that's not what you're after, then you have to modify to what Unity coordinates in your project mean.
Thanks for your answer. I chose that 1 Unity unit corresponds to 1 real word meter (it seemed to me that would be the easiest for conversion).
Well... when you say 1 Unity unit = 1 meter, you're kinda implying that your coordinate system is orthogonal. And lat-long is not - it's spherical. So you can not just assign lat-long-atlitude to xyz and expect it to work (well you can in local area, but don't start thinking about that). Read about how to convert spherical coordinate system to orthogonal. It should be something along those lines:
// this gives you a point on a spehere
const float kRadiusOfEarth = 6000;
Vector3 pos = new Quaternion(lat, long, 0) * new Vector3(0, 0, kRadiusOfEarth + altitude);
Again, thanks for answering.
I didn't try to just assign lat/long/alt to XYZ - as I said in my original question, I tried various methods of converting lat/long/alt to XYZ (most of them from this here forum), I even converted this JS function to C# (llhxyz):
http://www.oc.nps.edu/oc2902w/coord/geodesy.js
That function doesn't even use a sphere, but a WGS84 ellipsoid (the same one the GPS uses), and I still didn't get acceptable results.
Here's what I did - I converted the GPS coordinates of the mobile phone, put the camera at those XYZ coordinates (all while continually pinging the GPS and moving the camera to the new coordinates), used the gyroscope to deter$$anonymous$$e how the phone is rotated (and rotated the camera in the same way), put an object at a known coordinate around me (I tried both with a coordinate from Google $$anonymous$$aps, and with one I obtained manually from my phone's GPS), pointed my phone at where the object should be and it was way off.
I'm thinking I'm probably missing some vital piece of information, or I'm not understanding the problem I'm having correctly. How would you approach the issue I'm trying to solve?
EDIT: I'm not at my work computer right now so I don't have the Unity project, but do you think the formulas you gave would give acceptable results? Also, is everything correct with my approach?
EDIT: I'm not at my work computer right now so I don't have the Unity project, but do you think the formulas you gave would give acceptable results?
No, I wouldn't take that code for granted :)
I'm thinking I'm probably missing some vital piece of information, or I'm not understanding the problem I'm having correctly. How would you approach the issue I'm trying to solve?
One step at a time. Seriously.
$$anonymous$$y guess is that your positioning code (i.e. transform of lat long) is correct, as long as translated the code correctly. You can test it by simply supplying some known points on the globe and measuring distance - you can compare that against what google maps give you based on same coordinates.
$$anonymous$$ake sure that your positioning system provides you values on correct axes, i.e. some coordinate systems have Y up (like Unity), but some have Z up. Some use right handed coordinate system, some left handed. The fix: just swap two coordinates and change one sign if necessary.
$$anonymous$$ake sure you know how to use gyro values. They give you orientation of gyro in screen space of device, and you have to transform it into device orientation "on a plane of the world". I get annoyed every time I have to solve this problem, because you face same problems as in 2nd step. As I said input is device orientation specific, so I would advice to lock device orientation of LandscapeLeft only (or one of those).
Transform "device orientation on a plane of the world" into "device orientation on a sphere".
If you do all 4 correctly, then it should work. I have done it in the past, getting all those details right was hard :)
Your answer
Follow this Question
Related Questions
How can I get a city name given a latitude & longitude 1 Answer
Using external GPS device unity application -1 Answers
GPS doesn't work in AGPS mode 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers