Docsity

Popping Pigs: A Simple Angry Birds Simulation - Prof. Charles V. Stewart, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Data Structures — CSci 1200
Homework 1
Popping Pigs
Overview
Many of you — perhaps most of you — have played Angry Birds by now, or know someone who
has. In the simplest version of the game, birds are fired from a slingshot in the direction of pigs,
and when the birds strike the pigs, the pigs explode. This game, like many video games, requires
a simulation of the geometry and physics of movement, intersection and collision. Homework 1
investigates a very simple version of this.
This homework is worth 60 points toward your homework grade, and is due Tuesday Febru-
ary 1, 2011 at 11:59:59 pm. See the course syllabus for discussion of the late policy. See the
course website for discussion of homework submission and homework grading. See the academic
integrity statement for discussion of what collaboration is and is not acceptable.
You may use any technique discussed in Lectures 1 or 2 or Lab 1 of the course as well as methods
discussed on the course website. If you wish, you may also use structs or classes, but that is not at
all necesssary for this assignment.
Specifics
You are given a series of pigs, represented as non-interesting circles. Circles are specified as three
floating point values giving the x location and y location of the pig center, and the pig radius. Birds,
which have no radius, fly in a straight line (not a parabolic arc) starting from the (0,0) location,
which means a bird need only be specified by an angle θ(with the positive x axis being 0 and
the angle formed by a counter-clockwise rotation). Whenever a bird’s path touches or intersects
the circle representing a pig, the pig is “popped”, and the bird flies on, without being diverted,
potentially popping other pigs. Stating this mathematically, which is what’s needed to simulate
the game, any pig whose circle intersects a bird’s ray of points (tcosθ , t sin θ), for t≥0, is popped.
We will not worry about the order in which the pigs are popped by a particular bird.
The input will come from a single text file. The first line of the file will give an integer value
n, being the number of pigs. You may assume without checking, that n≤100 (although if you use
vectors instead of arrays, you need not make this assumption). This will be followed by nlines,
with each line containing three float values — the x, y and r values of the pig circles. After these
nlines you will have some number of float values, each representing the direction of a bird. Thus,
for example, if the input file is
3
1.5 6.5 1.4
9.6 4.6 2.6
7.5 8.9 1.8
45
60
75
23
There are three pigs and then four birds shot in directions 45◦, 60◦, 75◦and 23◦.
Three different types of output will come from the program, written to an output file:
pf3

Partial preview of the text

Download Popping Pigs: A Simple Angry Birds Simulation - Prof. Charles V. Stewart and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures — CSci 1200

Homework 1

Popping Pigs

Overview

Many of you — perhaps most of you — have played Angry Birds by now, or know someone who has. In the simplest version of the game, birds are fired from a slingshot in the direction of pigs, and when the birds strike the pigs, the pigs explode. This game, like many video games, requires a simulation of the geometry and physics of movement, intersection and collision. Homework 1 investigates a very simple version of this. This homework is worth 60 points toward your homework grade, and is due Tuesday Febru- ary 1, 2011 at 11:59:59 pm. See the course syllabus for discussion of the late policy. See the course website for discussion of homework submission and homework grading. See the academic integrity statement for discussion of what collaboration is and is not acceptable. You may use any technique discussed in Lectures 1 or 2 or Lab 1 of the course as well as methods discussed on the course website. If you wish, you may also use structs or classes, but that is not at all necesssary for this assignment.

Specifics

You are given a series of pigs, represented as non-interesting circles. Circles are specified as three floating point values giving the x location and y location of the pig center, and the pig radius. Birds, which have no radius, fly in a straight line (not a parabolic arc) starting from the (0,0) location, which means a bird need only be specified by an angle θ (with the positive x axis being 0 and the angle formed by a counter-clockwise rotation). Whenever a bird’s path touches or intersects the circle representing a pig, the pig is “popped”, and the bird flies on, without being diverted, potentially popping other pigs. Stating this mathematically, which is what’s needed to simulate the game, any pig whose circle intersects a bird’s ray of points (t cos θ, t sin θ), for t ≥ 0, is popped. We will not worry about the order in which the pigs are popped by a particular bird. The input will come from a single text file. The first line of the file will give an integer value n, being the number of pigs. You may assume without checking, that n ≤ 100 (although if you use vectors instead of arrays, you need not make this assumption). This will be followed by n lines, with each line containing three float values — the x, y and r values of the pig circles. After these n lines you will have some number of float values, each representing the direction of a bird. Thus, for example, if the input file is

3 1.5 6.5 1. 9.6 4.6 2. 7.5 8.9 1. 45 60 75 23

There are three pigs and then four birds shot in directions 45◦, 60◦, 75◦^ and 23◦. Three different types of output will come from the program, written to an output file:

  • Check to see if any of the circles intersect. Check all pairs of circles, and if any pairs do inter- sect, there should be an error message output and the game (program) should not continue. The format of this error message will be shown in the examples. You may assume without checking that no pig circle encloses the (0, 0) point. This means that the birds aren’t shot from inside a pig!
  • When a bird pops a pig, an output message should be generated. Birds will be identified by their directions (45, 60, 75 or 23 in the above example), and pigs by their indices (0, 1 or 2 in the above example). Follow the example output format posted on the course web site. Note that once a bird pops a pig, a subsequent bird can not pop the same pig.
  • At the end of the program, a message should be output indicating either that all pigs were popped, or how many pigs remained. Once again, see the examples on the course web site.

In summary, your program should run with a single command-line of the form

pop_pigs data-file out-file

where data-file is the name of the file containing the pig and bird information. Except for command-line argument errors, all output should go to out-file. It is important to try to match our output as precisely as possible.

Popping Pigs and Intersecting Pigs

We don’t necessarily expect that you could come up with the equations and functions for the bird- pig (line-circle) intersection or the pig-pig (circle-circle) intersection on your own. The function, pops pig, that tells if a bird pops a pig, is posted on the course web site. You may use it exactly as is. You may also write your own version, of course. Determining if two circles intersect is easy. You just need to calculate the distance between the center points and compare it to the sum of the two circle radii. If the distance is less than the sum, the circles intersect.

Step-By-Step Program Development Guide

For this assignment, we provide you with the following outline of steps for developing, writing and testing this program. You should follow similar steps in future assignments. If you go to office hours or advising for help on Homework 1, you must show your progress in attemping to follow these steps.

  1. Implement and test two functions: the one given above to determine if a bird pops a pig, and a second to test if two pigs (circles) intersect. Write a very simple main program that provides hand-specified values to test the functions. Save a copy of the result, then delete the test main function you wrote and continue on. An example of this is already given for you on-line in checking the pops pig function.
  2. Implement and test the code in the main function to check the command-line arguments, open the input file, and read the pigs and birds. (Look carefully at the links for “Misc Programming Info” on the course website and at the final example from Lecture 2 for aid in reading command-line arguments.) Add code after reading the data to output it. Save a copy of the resulting code here as well.