SUNY Geneseo Department of Mathematics

Project 2—Animation

Math 230 02
Spring 2015
Prof. Doug Baldwin

Complete by Monday, April 13
Grade by Thursday, April 16

Purpose

This exercise consolidates your understanding of Matlab programming, and introduces you to some Matlab elements for computer animation.

Background

The basic idea underlying computer animation (or any other animation) is to draw a series of still images, one after another, each differing from the one before in only small ways. If these images are shown at a rate of about 30 per second (or faster), the human eye will interpret them as a smoothly moving sequence. For example, a series of images of a circle, each with the circle slightly to the right of where it was in the previous image, shown at a fast enough rate, will look to a person like a single circle moving rightward.

Matlab has many ways to produce an image, the simplest of which is the plot command. To build an animation around plot, you need to be able to do two main things: change the position, size, orientation, or other features of a plotted shape, and draw the changed shape 30 or more times per second.

In the matrices lab you learned how to use vectors to represent the vertices (i.e., corners) of a shape, and to use matrix multiplication to rotate, dilate, or shear that shape. Furthermore, although not covered in the matrix lab, you can move a shape simply by adding the amount you want to move by to its x and y coordinates. By alternating such transformations with plotting, you can create a series of pictures of a changing shape. If the image you draw contains multiple lines or shapes, you will have to redraw all of them every time you plot the image, even shapes that haven’t changed. In this case, be sure to stop holding figure contents (i.e., hold off) before starting to redraw the image, and to turn holding back on (hold on) after you plot the first shape in the image.

The key idea behind updating and redrawing an image 30 times per second is to put the update and redraw operations inside a loop. However, if this is all you do, Matlab will probably be fast enough to draw more than 30 images per second. There’s nothing terribly wrong with this, aside from making it hard to control how fast things seem to move in the animation, and probably making them move at different speeds on different computers. However, to get better control over the drawing rate, you can use Matlab’s pause command. This command causes a program to temporarily stop for some period of time, and then continue. The basic form of pause is

pause( t )

where t is the number of seconds you want your program to wait. For example, to wait for 1/30th of a second, you could say

pause( 1/30 );

Putting all of the above ideas together, the skeleton of a basic animation script in Matlab could look something like this:

    cords = 2-row-by-n-column matrix of vertex coordinates, x in row 1, y in row 2
    while animation isn’t finished
        use plot( coords(1,:), coords(2,:) ) to draw shape
        multiply coords by transformation matrices and/or add motions to it.
        pause( 1/30 );
    end

(Note that there are more sophisticated ways of doing animation in Matlab, which can produce smoother-running animations with less code, but the techniques described above work for simple animations, and are easy to code using ideas we have already explored in this course.)

Activity

Write a Matlab script that performs an animation similar to a ball bouncing around inside a rectangular “cell.” Whenever the ball hits a side of the cell, it should bounce off that side back into the cell. Your animation should stop after the ball has bounced some set number of times. You can either build this number into your script, or prompt the user for it.

I specified this exercise as “similar to” a ball bouncing in a cell for the sake of having something concrete to describe. However, you are welcome to vary the assignment in your own ways, as long as you stay within the basic spirit of a shape moving around in a limited region. For example, the shape needn’t be a ball, it could rotate or change size as it moves, the rules for changing direction could be more complicated than just bouncing off of “walls” (e.g., gravity pulling a satellite through its orbit around a planet, a shape following a trajectory given by some set of parametric equations), etc.

Follow-Up

I will grade this exercise in a face-to-face meeting with you. During this meeting I will look at your solution, ask you any questions I have about it, answer questions you have, etc. Please bring a written solution to the exercise to your meeting, as that will speed the process along.

Sign up for a meeting via Google calendar. If you worked in a group on this exercise, the whole group should schedule a single meeting with me. Please make the meeting 15 minutes long, and schedule it to finish before the end of the “Grade By” date above.