5

So I'm trying to use the haversine formula in Unity to get the distance between two different points (latitude and longitud given). The code is working (no errors) but I keep gettting a wrong result. I followed the entire formula so I don't really know where the math/code problem is. Any idea?

Here's the code:

 public float lat1 = 42.239616f;
 public float lat2 = -8.72304f;
 public float lon1 = 42.239659f;
 public float lon2 = -8.722305f;

 void operacion(){
 float R = 6371000; // metres
 float omega1 = ((lat1/180)*Mathf.PI);
 float omega2 = ((lat2/180)*Mathf.PI);
 float variacionomega1 = (((lat2 - lat1)/180)*Mathf.PI);
 float variacionomega2 = (((lon2 - lon1) / 180) * Mathf.PI);
 float a = Mathf.Sin(variacionomega1/2) * Mathf.Sin(variacionomega1/2) +
             Mathf.Cos(omega1) * Mathf.Cos(omega2) *
             Mathf.Sin(variacionomega2/2) * Mathf.Sin(variacionomega2/2);
 float c = 2 * Mathf.Atan2(Mathf.Sqrt(a), Mathf.Sqrt(1-a));

 float d = R * c;
 }
  • 2
    While Adilson's result may solve your problem, it is worth noting that `6371000` should be `6372797.560856` for better precision. – Programmer Aug 23 '16 at 21:58
  • @Programmer Thanks for the answer. I take the advice as it should make it more precise however, I want to clarify the fact that the difference between the given result and the correct result is huge. – Manuel Estévez Aug 23 '16 at 22:21
  • Well its a comment not answer. Check Adilson's updated answer. That should solve your problem. If not let me know. – Programmer Aug 23 '16 at 22:53
  • @Programmer As you see, I already tried Adilson's answer but, for now, it isn't working. Any idea? – Manuel Estévez Aug 23 '16 at 23:09
  • Ok. Do you want the distance in Meters or in arc radius? Also what result are expecting? What result are you currently getting? – Programmer Aug 23 '16 at 23:15
  • @Programmer For what I'm trying to achieve I think it's best to get the distance in Meters. Well, the points given are close (50-60m) and as I saw in the correct result given by that formula it is something close to that, in fact, the distance is 0,06086km, which is a really good result (the desired result). However, using this code I get results like 7670,64km. By the way, I'm following and cheking if the result is good by using the website I told Adilson in the comments (don't if that helps in any way). – Manuel Estévez Aug 23 '16 at 23:39
  • Just tested this and this is what I found out. **1**.The code in your answer is working. **2**.The code in your question and the one in the answer will both produce the-same result. **3**.You are feeding the values in the wrong places on that website. That website is expecting `lat1` and `long1` followed by `lat2` and `long2`. You are feeding it something else. I got the-same result on both sites which is `7668481`. Again the code is in your question is fine. The one provided by Adilson is fine too. – Programmer Aug 24 '16 at 00:06
  • @Programmer Yes, I recently realised as you see in the Adilson's answer comments. It seems it's working correctly now. Thanks for the answer and for your time! – Manuel Estévez Aug 24 '16 at 00:08
  • Ok. Good. Happy coding! – Programmer Aug 24 '16 at 00:09

1 Answers1

5

I think this line is incorrect:

float c = 2 * Mathf.Atan2(Mathf.Sqrt(a), Mathf.Sqrt(1-a));

UPDATED:

The correct way would be:

float c = 2 * Mathf.Asin(Mathf.Sqrt(a));
Adilson de Almeida Jr
  • 2,761
  • 21
  • 37
  • Hey, thanks for the answer! I tried replacing that line but Unity gives the error "CS1501: No overload for method `Asin' takes `2' arguments". – Manuel Estévez Aug 23 '16 at 22:18
  • Hi. I tried the update and while it works now (no errors) the result is, however, wrong. I don't really have many ideas of why it isn't working already as I'm following the formula. By the way, if it helps in any way, I'm checking if the result is correct by using the website http://www.movable-type.co.uk/scripts/latlong.html Thanks for the answer! – Manuel Estévez Aug 23 '16 at 23:08
  • I don´t have Unity API here, but replacing Mathf, by Math on that code I ended with a distance of 7668480 meters, while filling these values on the page you pointed out, I also ended with 7668 km. What coordinates have you used, and what distance did you got? – Adilson de Almeida Jr Aug 23 '16 at 23:37
  • I got nearly the same distance you got using the coordinates I wrote at the beggining of the code (lat1, lat2,...). But if I use the website the distance I get is around 0,060km, which is really close to the actual distance between the two points. – Manuel Estévez Aug 23 '16 at 23:44
  • Hmmm... Maybe you are inverting the coordinates on the page, or on your code. The values on your code, means you are looking for the distance between (42.239616, 42.239659) (which means somewhere on Georgia), and (-8.72304, -8.722305) (a point on Atlantic Ocean close to Afrca West Coast). If you exchange the values of lat2 and lon1 you will get 2 close coordinates of Vigo. – Adilson de Almeida Jr Aug 23 '16 at 23:54
  • Ok, so turns out I'm really dumb. I accidentally changed the coordinates, as they are refering to the Vigo ones and I kept revising the code over and over without noticing that. It seems it now works correctly. Thanks for the answers and, of course, for your time! – Manuel Estévez Aug 24 '16 at 00:05