SUNY Geneseo Department of Computer Science
CSci 120, Spring 2014
Prof. Doug Baldwin
Due Wednesday, April 2
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.
Recall that whatever you turn in for this project has to be a script written for this project and expressed in your own words. This rule is doubly significant for this project: First, it means that while you are welcome to share ideas within study or other groups, ultimately everyone has to turn in a distinct solution that reflects their own understanding of how to solve the problem. Second, there are lots of examples and demonstrations of Matlab animation on the Internet, and while you are welcome to look at them for inspiration, you must turn in your own script, written in a way that shows you understand how it works; if you do look at Internet examples, you must cite them in comments within your script.
When you finish this exercise, email the “.m” file containing your script to Nick (nsl3@geneseo.edu). Please make the subject line of your email be
Project 2 - YourName
where YourName should be replaced with your own first and last name. Your email should be sent by 11:59 on the due date above.