I am just starting to experiment with scenes and I have a question. I have drawn a complex shape (e.g. a car) using lines, rects and ellipses on the scene. If I want to animate the whole shape with a sequence of rotate, scale, translate, how should I go able that? I tried layers, but it does not support drawings functions on the layer itself. Appreciate any pointers.
Forum Archive
Animation of complex objects
echen
Jan 09, 2016 - 02:37
ccc
Jan 09, 2016 - 05:25
Layer.draw() does exist along with a rotation property and animations. Example code would help for this question.
echen
Jan 09, 2016 - 07:35
thanks I guess my problem is I am not getting the concept right. Love to see a tutorial, but cannot find one. In the following sample, how do I animate the trasnformations (in the touch_began function) so that the drawing in the draw() function will move, scale and flip to my touch point ? Using layer.draw(), how do I go about doing this?
# coding: utf-8
from scene import *
import ui
class MyScene(Scene):
def draw(self):
startx = 20
starty = 20
length = 100
width = 200
#simple shape
# begin location
fill(.5,.5,.5)
rect(startx, starty, width, length )
fill(0,1,0)
rect(startx*2, starty, width/2, length/2)
fill(1,0,0)
ellipse(startx*2, starty*2, 10,10)
ellipse(startx*8, starty*2, 10,10)
def touch_began(self, touch):
#end location
print touch.location.x, touch.location.y
push_matrix()
scale(1.5, 1.5)
translate(touch.location.x, touch.location.y)
rotate(180)
pop_matrix()
class SceneViewer(ui.View):
def __init__(self, in_scene):
self.present('fullscreen')
self.scene_view = SceneView(frame=self.bounds)
self.scene_view.scene = in_scene
self.add_subview(self.scene_view)
SceneViewer(MyScene())
brumm
Jan 09, 2016 - 14:36
here are other examples.
# coding: utf-8
from scene import *
import ui
class MyScene(Scene):
def setup(self):
startx = 20
starty = 20
length = 100
width = 200
self.rect1 = Rect(startx, starty, width, length)
self.rect2 = Rect(startx*2, starty, width/2, length/2)
self.elli1 = Rect(startx*2, starty*2, 10, 10)
self.elli2 = Rect(startx*8, starty*2, 10, 10)
self.rect1_layer = Layer(self.rect1)
self.rect1_layer.background = Color(.5,.5,.5) #grey
self.add_layer(self.rect1_layer)
self.rect2_layer = Layer(self.rect2)
self.rect2_layer.background = Color(0,1,0) #green
self.add_layer(self.rect2_layer)
def draw(self):
background(0,0,1)
fill(0,1,1) #cyan
rect(*self.rect1)
self.rect2_layer.update(self.dt)
self.rect2_layer.draw()
fill(1,0,0) #red
ellipse(*self.elli1)
ellipse(*self.elli2)
self.rect1_layer.update(self.dt)
self.rect1_layer.draw()
def touch_began(self, touch):
self.rect1_layer.animate('alpha', 0.0, duration=1.0, autoreverse=False) # grey => cyan
self.rect2_layer.animate('rotation', 180.0, duration=2.0, autoreverse=False)
self.elli1 = Rect(touch.location.x, touch.location.y, 10, 10)
# Pythonista docu: Animatable attributes are frame, scale_x, scale_y, rotation, background, stroke, stroke_weight, tint and alpha.
class SceneViewer(ui.View):
def __init__(self, in_scene):
self.present('fullscreen')
self.scene_view = SceneView(frame=self.bounds)
self.scene_view.scene = in_scene
self.add_subview(self.scene_view)
SceneViewer(MyScene())