
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.




REAL :: msec=10, Lrod=1, dBob=0.03, g=9.81, Theta(2), dTheta(2)
BobMargins = ALIAS(ls, rs, ts, bs) ! box margins to draw the bob
 
 
Theta = (1, 0)        ! initial angle and velocity
start_t = TIME()
 
DO i = 1, 1E100       ! "forever"
   end_t = TIME()     ! to integrate in real-time sections:
   DIFFEQ(Callback="pendulum", T=end_t, Y=Theta, DY=dTheta, T0=start_t)
   xBob = (SIN(Theta(1)) + 1) / 2
   yBob = COS(Theta(1)) - dBob
 
   ! create or clear window and draw pendulum bob at (xBob, yBob):
   WINDOW(WIN=wh, LeftSpace=0, RightSpace=0, TopSpace=0, BottomSpace=0, Up=999)
   BobMargins = (xBob-dBob, 1-xBob-dBob, yBob-dBob, 1-yBob-dBob)
   WINDOW(WIN=wh, LeftSpace=ls, RightSpace=rs, TopSpace=ts, BottomSpace=bs)
   WRITE(WIN=wh, DeCoRation='EL=4, BC=4') ! flooded red ellipse as bob
 
   ! draw the rod hanging from the center of the window:
   WINDOW(WIN=wh, LeftSpace=0.5, TopSpace=0, RightSpace=rs+dBob)
   WRITE(WIN=wh, DeCoRation='LI=0 0; 1 1, FC=4.02') ! red pendulum rod
 
   SYSTEM(WAIT=msec)
   start_t = end_t
ENDDO
 
END
 
SUBROUTINE pendulum                 ! Theta" = - (g/Lrod) * SIN(Theta)
  dTheta(1) = Theta(2)              ! Theta' = Theta(2)  substitution
  dTheta(2) = -g/Lrod*SIN(Theta(1)) ! Theta" = Theta(2)' = -g/Lrod*SIN(Theta(1))
END