From e658c65cef474ee2675cb2c7b017db7f743194c6 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Fri, 27 Jan 2017 00:05:14 +0100 Subject: [PATCH] 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. --- examples/android/compass/main.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/examples/android/compass/main.py b/examples/android/compass/main.py index 02c4007db..06163ad05 100644 --- a/examples/android/compass/main.py +++ b/examples/android/compass/main.py @@ -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)