
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.




include c:\cxpl\codes;          \intrinsic 'code' declarations
 
proc    Ball(X0, Y0, R, C);     \Draw a filled circle
int     X0, Y0, R, C;           \center coordinates, radius, color
int     X, Y;
for Y:= -R to R do
    for X:= -R to R do
        if X*X + Y*Y <= R*R then Point(X+X0, Y+Y0, C);
 
 
def     L  = 2.0,               \pendulum arm length (meters)
        G  = 9.81,              \acceleration due to gravity (meters/second^2)
        Pi = 3.14,
        DT = 1.0/72.0;          \delta time = screen refresh rate (seconds)
def     X0=640/2, Y0=480/2;     \anchor point = center coordinate
real    S, V, A, T;             \arc length, velocity, acceleration, theta angle
int     X, Y;                   \ball coordinates
 
[SetVid($101);                  \set 640x480x8 graphic display mode
T:= Pi*0.75;  V:= 0.0;          \starting angle and velocity
S:= T*L;
repeat  A:= -G*Sin(T);
        V:= V + A*DT;
        S:= S + V*DT;
        T:= S/L;
        X:= X0 + fix(L*100.0*Sin(T));   \100 scales to fit screen
        Y:= Y0 + fix(L*100.0*Cos(T));
        Move(X0, Y0); Line(X, Y, 7);    \draw pendulum
        Ball(X, Y, 10, $E\yellow\);
        while port($3DA) & $08 do [];   \wait for vertical retrace to go away
        repeat until port($3DA) & $08;  \wait for vertical retrace signal
        Move(X0, Y0); Line(X, Y, 0);    \erase pendulum
        Ball(X, Y, 10, 0\black\);
until   KeyHit;                         \keystroke terminates program
SetVid(3);                              \restore normal text screen
]