SUNY Geneseo Department of Mathematics
Math 230 01
Fall 2014
Prof. Doug Baldwin
Complete by Monday, November 17
Grade by Wednesday, November 19
This exercise consolidates your understanding of Matlab programming, and introduces you to some Matlab and mathematical elements for computer animation.
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.
There are several simple operations you can use to change a shape. All of them start
with the vertices of the shape (i.e., the points you give to plot
to draw
the shape) stored in vectors or some other convenient form. Typically these vertices
also start at some convenient place in the coordinate system, for example centered on
the origin (e.g., if the shape in question were a square, you might start with the
points (-1,-1), (1,-1), (1,1), (-1,1), and (-1,-1)). Here, then, are some common
transformations:
All of these transformations lend themselves very well to vector or matrix operations in Matlab.
Once you have updated your shape’s vertices for the next image in your animation,
simply call plot
with the updated vertices to draw it. If the image you
draw contains multiple lines or shapes, you will have to redraw all of them, even ones
that don’t change. 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 element of 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:
x = initial X coordinates of vertices
y = initial Y coordinates of vertices
while animation isn’t finished
use plot(x,y) to draw shape
update shape’s position, size, orientation, etc.
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.)
Write a Matlab script that animates 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.
You are welcome to vary this 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.
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.