
One good way of making an animation is by simulating a physical system and illustrating the variables in that system using a dynamically changing graphical display.

The classic such physical system is a simple gravity pendulum.



Task

Create a simple physical model of a pendulum and animate it.




global RODLEN, GRAVITY, DT
global velocity, acceleration, angle, posX, posY
 
on startMovie
 
    -- window properties
    _movie.stage.title = "Pendulum"
    _movie.stage.titlebarOptions.visible = TRUE
    _movie.stage.rect = rect(0, 0, 400, 400)
    _movie.centerStage = TRUE
    _movie.puppetTempo(30)
 
    RODLEN = 180
    GRAVITY = -9.8
    DT = 0.03
 
    velocity = 0.0
    acceleration = 0.0
    angle = PI/3
    posX = 200 - sin(angle) * RODLEN
    posY = 100 + cos(angle) * RODLEN    
    paint()
 
    -- show the window
    _movie.stage.visible = TRUE
end
 
on enterFrame
    acceleration = GRAVITY * sin(angle)
    velocity = velocity + acceleration * DT
    angle = angle + velocity * DT
    posX = 200 - sin(angle) * rodLen
    posY = 100 + cos(angle) * rodLen
    paint()
end
 
on paint
    img = _movie.stage.image
    img.fill(img.rect, rgb(255,255,255))
    img.fill(point(200-5, 100-5), point(200+5, 100+5), [#shapeType:#oval,#color:rgb(0,0,0)])
    img.draw(point(200, 100), point(posX, posY), [#color:rgb(0,0,0)])
    img.fill(point(posX-20, posY-20), point(posX+20, posY+20), [#shapeType:#oval,#lineSize:1,#bgColor:rgb(0,0,0),#color:rgb(255,255,0)])
end