Angle jump in reverse direction at points 179 degree to -179 degree and -179 to 179 degree while making compass app in android studio

24 views Asked by At

I am trying to make a compass app. But it has a problem when azimuth change from 179 degree to -179 degree and vice versa, compass image moving around 360 degree to come at -179 degree from 179 degree.

I tried to detect significant angle change and then adjust azimuth angle but failed

float angleDifference = Math.abs(azimuthInDegree-currentDeegre);

            if (angleDifference>300f) {

                if (azimuthInDegree > currentDeegre) {
                    azimuthInDegree = 360f - azimuthInDegree;
        }
                if (azimuthInDegree < currentDeegre) {
                    azimuthInDegree = -azimuthInDegree-360f;

           }
            }
1

There are 1 answers

0
Anil Pandey On

Done after some experiments


                if (azimuthInDegree > currentDeegre ) {
                    azimuthInDegree = azimuthInDegree-360f;
        }
                if (azimuthInDegree < currentDeegre ) {
                    azimuthInDegree = 360f + azimuthInDegree;

           }
            }