Search code examples
touchkivyangle

Kivy Angle Examples


Angle rotation only works on the first example

I want to understand. Why one example with the angle animation call works and the other fails.

From what I can gather, this has to do with the Builder.load_string.

Working Example

#working example
from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.image import Image

from kivy.graphics import Rotate
from kivy.properties import NumericProperty

from math import atan2, degrees

from kivy.animation import Animation

Builder.load_string('''                                                                                                                                        
<PlayerImage>:                                                                                                                                                 
    canvas.before:                                                                                                                                             
        PushMatrix                                                                                                                                             
        Rotate:                                                                                                                                                
            angle: self.angle                                                                                                                                  
            axis: (0, 0, 1)                                                                                                                                    
            origin: self.center                                                                                                                                
    canvas.after:                                                                                                                                              
        PopMatrix                                                                                                                                              
''')

class PlayerImage(Image):
    angle = NumericProperty(0)

    def on_touch_down(self, touch):
        Animation.cancel_all(self)
        angle = degrees(atan2(touch.y - self.center_y, 
                              touch.x - self.center_x))

        Animation(center=touch.pos, angle=angle).start(self)

root = Builder.load_string('''                                                                                                                                 
Widget:                                                                                                                                                        
    PlayerImage:                                                                                                                                               
        source: 'images/example.png'                                                                                                                                 
        allow_stretch: True                                                                                                                                    
        keep_ratio: False                                                                                                                                    
''')

runTouchApp(root)

Non Working Example

from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.image import Image

from kivy.graphics import Rotate
from kivy.properties import NumericProperty

from math import atan2, degrees

from kivy.animation import Animation

class PlayerImage(Image):
    angle = NumericProperty(0)

    def on_touch_down(self, touch):
        Animation.cancel_all(self)
        angle = degrees(atan2(touch.y - self.center_y, 
                              touch.x - self.center_x))

        Animation(center=touch.pos, angle=angle).start(self)


root = Builder.load_string('''                                                                                                                                 
Widget:                                                                                                                                                        
    PlayerImage:                                                                                                                                                
        source: 'images/example.png'                                                                                                                                 
        allow_stretch: True                                                                                                                                    
        keep_ratio: False
        canvas.before:                                                                                                                                             
            PushMatrix                                                                                                                                             
                Rotate:                                                                                                                                                
                    angle: self.angle                                                                                                                                  
                    axis: (0, 0, 1)                                                                                                                                    
                    origin: self.center                                                                                                                                
        canvas.after:                                                                                                                                              
            PopMatrix                                                                                                                                  
''')

runTouchApp(root)

Solution

  • The second KVlang block is incorrect, there is excess indentation on the Rotate (and, for stylistic purposes, a missing ":" after the PushMatrix).