Unity3d tutorial- Calculating direction based on two latitude longitude points




In this article we are going to see how to calculate direction based on two latitude longitude points,there are many codes available, this is best code I have seen so far.



Sample C# code:



                        double lat1=13.09628;
double lng1=80.19778;
                      double lat2=13.090427;
                      double lng2=80.207480;

double latDelta = (lat2 - lat1);
double lonDelta = (lng2 - lng1);

double y = Math.Sin(lonDelta)  * Math.Cos(lat2);


double x = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2)*                                      Math.Cos(lonDelta);

double angle = Math.Atan2(y, x); //not finished here yet
        double brng = angle * 180/Math.PI;

   brng = (brng + 360) % 360;
   brng = 360 - brng; // count degrees counter-clockwise - remove to make clockwise



The above code is completely written in C# ,the code is similar to C program(the math functions are same)

we must give current location with target location that we want to calculate direction,this far most best way calculate direction in 360 degree(compass).

The variable brng is our calculated angle.


Comments