Fix animation transition around the unit circle in Android compass example

For example if the previous angle was 269 and the new angle is -84, then the current code would rotate the needle in a clockwise direction. This fix would add 360 to -84, so the new angle would be 276, which would result in the correct behaviour of the needle rotating anti-clockwise.
This commit is contained in:
Kristian Sloth Lauszus 2017-01-27 00:05:14 +01:00 committed by Gabriel Pettier
parent d06ea4da25
commit e658c65cef
1 changed files with 9 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import kivy
kivy.require('1.7.0')
from jnius import autoclass
from math import floor
from kivy.app import App
from kivy.properties import NumericProperty
from kivy.clock import Clock
@ -48,6 +49,14 @@ class CompassApp(App):
# calculate the angle
needle_angle = Vector(x, y).angle((0, 1)) + 90.
# fix animation transition around the unit circle
if (self.needle_angle % 360) - needle_angle > 180:
needle_angle += 360
elif (self.needle_angle % 360) - needle_angle < -180:
needle_angle -= 360
# add the number of revolutions to the result
needle_angle += 360 * floor(self.needle_angle / 360.)
# animate the needle
if self._anim:
self._anim.stop(self)