Purpose
This exercise consolidates your understanding of Matlab programming, and introduces you to some Matlab and mathematical 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.
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:
- To move the shape a units in the x direction and b units in the y, simply add a to every vertex’s x coordinate and b to every y coordinate. You can easily create a moving shape by making the initial shape the first image you draw, then adding a and b to the x and y coordinates to get the second image, adding a and b to the result of the first additions to get the third image, adding a and b again to get the fourth image, and so forth. If the values of a and b change at any point in this sequence, the shape will change its direction and/or speed of motion.
- To expand points away from the origin by some factor s, simply multiply x and y coordinates by s. If s is less than 1, the effect will be to collapse the shape towards the origin. For a shape centered on the origin, such multiplications have the effect of making it bigger or smaller. For shapes not centered on the origin, such multiplication will both increase (or decrease) the overall size of the shape and move it farther from (or closer to) the origin. You can also multiply x and y by different scale factors to get different amounts of expansion or shrinkage in different dimensions.
- To rotate the shape counterclockwise through angle Θ, calculate x and y coordinates of rotated vertices as follows: let any vertex’s unrotated coordinates be (x,y), and calculate the rotated x as x cosΘ - y sinΘ, and the rotated y as x sinΘ + y cosΘ. Remember that this rotation is always around the origin, so unless the shape you transform is centered at the origin, the rotation will appear to be an orbit around the origin rather than a rotation around the center of the shape. This might or might not be what you want.
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.)
Activity
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.
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.