Angry Dots [30 points] EEL 2161 Computer Methods

Angry Dots [30 points]
EEL 2161 Computer Methods, Spring 2015
In this assignment, you will explore some of the fundamentals behind the Angry
Birds video game, and in the process, practice many of the topics we are talking
about in class, including functions, loops, and plotting.
Please read this entire document before starting the assignment, as each
part of the assignment builds on the previous one.
PART 1: Trajectories
Part 1 focuses on how to use Matlab to calculate the trajectory of an angry bird.
A trajectory is the path an object follows over time. For example, the dotted lines
in the figure below represent possible trajectories of an angry bird.
Let’s define a few variables we are going to need to describe our trajectory.
• v0 is the initial velocity (speed) of the object,
• Θ is the angle at which the object is fired,
• (xt, yt) is the 2D position of the object at time t
• g is the strength of gravity (on Earth, g = 9.8 m/s2)
Also, assume that
• the beginning of the trajectory occurs at time t = 0,
• the ground is at y=0;
• When t = 0, (x0, y0) = (0,0)
With these variables defined, we can now define the following mathematical
equations that describe the position of an object as a function of time.
xt = v0 * cos (Θ) * t
yt = v0 * sin(Θ) * t – 0.5 * g * t2
Build It
1. Write a function called trajectory() that returns the 2D position (xt, yt)
of an object at a given time t. Pass the object’s initial velocity v0, angle Θ,
current time t, and gravity (g) as input arguments. If the user does not
specify gravity as an input, your function should assume g = 9.8 m/s2.
2. Write a function called peakheight() that returns the height of the
object when it reaches the peak of the trajectory (the highest height the
object reaches).
3. Write a function called timeflight() that returns the total duration of
the object’s flight (the time at which the object hits the ground).
4. Write a function called range() that returns the total horizontal distance
the object travels.
Hint: Equations for peakheight(), timeflight() and range() can be
found by rearranging the trajectory equations above.
PART 2: Plotting trajectories
In Part 2, you will generate three figures, each showing trajectories under various
conditions. Make sure the trajectories appear smooth in the figures and that the
axes with numbers are shown in the figures.
Figure 1 (Vary firing angle): Plot 5 second trajectories for an object with initial
velocity 10 m/s. Generate a single figure that shows the trajectories for all of the
following angles: 0, pi/8, pi/4, 3pi/8, pi/2, 5pi/8, 3pi/4, 7pi/8, pi
Figure 2 (Vary initial velocity): Plot 5 second trajectories for an object with firing
angle pi/4. Generate a single figure that shows the trajectories for the following
initial velocities: 0, 1, 2, 3, 4, …, 18, 19, 20 m/s.
Figure 3 (Vary gravity): Plot 5 second trajectories for an object with firing angle
pi/4 and initial velocity 10 m/s. Generate a single figure that shows the
trajectories for all of the following gravity: -20, -15, -10, -5, 0, 5, 10, 15, 20 m/s2
Hint: use figure and hold to create new figures and ensure they do not get
overwritten.
PART 3: Angry Dots
In Part 3, you will use Matlab’s plot functionality to generate animations of
trajectories. You will ask the user for an initial velocity and angle, and it will play
an animation of the trajectory and optionally save the result to a video file.
An animation creates the perception of movement by rapidly changing pictures
one after the other. As long as consecutive images are similar to each other, and
the images change fast enough, our brains will perceive it as an animation rather
than as separate images.
Part 3a: Write a function called playTrajectory(), which takes an initial
velocity and initial angle as input arguments. It should also take two optional
arguments. The first should be set to True if you want the animation to show all
points in the trajectory. False means show one point at a time. The second
optional argument is the name of the file to use when saving the video. If no
filename is specified, do not save a video file. playTrajectory() should use
plot() to plot points in the trajectory to the figure.
The animation should play from t = 0 to t = timeflight(v0, angle), the time
at which the objects hits the ground. In addition, make sure the trajectory is not
drawn off screen by setting the x and y axes based on the peak height and range
of the trajectory (hint: use the axes command).
Hint: Use getframe() to capture each frame of the animation (as shown here:
http://goo.gl/WmxwQW). Once captured, use movie2avi to save the movie to
an avi video file.
Part 3b: Write a script angrydots.m that runs until the user types CTRL-C
(CMD-C on Mac). It should ask the user for an initial velocity, and an initial angle,
and then call playTrajectory() to generate the corresponding animation.
When done with the animation, angrydots.m should again ask the user for an
initial velocity and angle.
What to Turn In
1. All of the .m files produced for this project
2. Jpg or png files of Figures 1-3.
3. Movie files for 3 different trajectories (any you want) of one dot
showing
4. Movie files for 3 different trajectories (any you want) of all dots
showing