# CS 488: Assignment 2

## 'Astaroidz'

Out: 2/11/03

Due:3/11/03 at 2:00pm

In this second assignment you will get to play with 2D geometric transforms (scale, rotate, translate) by implementing a simplified version of the late 70s game of Asteroids. It will also introduce the routines to draw text on the screen. This is a much more realistic example of how computer graphics programs are written. It gives us pretty much everything we need prior to making the jump into 3D graphics in the next assignment.

Your program will read in two datafiles. The first called ship.txt describes your spaceship. The second called ast.txt describes the asteroid. Each of the files contains a set of floating point (X,Y) vertices (of unlimited number) which when connected by lines in sequence will draw the appropriate shape. You will be using lines on this assignment rather than filled polygons for all of the objects on the screen. GL_LINE_LOOP will come in handy here. The center of both the ship shape and the asteroid shape is (0,0) in their own coordinate spaces.

e.g. a very simple triangular-ish spaceship could be formed as:

0.0 12.0
-9.0 -9.0
0.0 -4.0
9.0 -9.0

A simple asteroid could be formed as:

0.0 15.0
-9.0 20.0
-14.0 1.0
-5.0 -18.0
15.0 -14.0
20.0 -5.0
15.0 8.0
18.0 18.0
6.0 22.0

The ship starts out with its center at 0,0 in world space. The ship is initially not moving or rotating, The user can rotate the ship counterclockwise and clockwise using the j and l keys respectively. The k key is used to add thrust in the direction the spaceship is facing - that is, given that the spaceship is currently moving in some direction, by pressing the k key we add velocity in the current direction the ship is facing. We will assume that +Y in the ships data file is towards the front of the ship. That may speed the ship up, it may slow the ship down, it may move the ship in a different direction. Note that the ship is not necessarily moving in the direction that it is facing.

The i key shoots a small square missile (2 units wide and tall) in the direction the ship is facing (which again is not necessarily the direction that the ship is moving). Only three missiles can be on the screen at one time. A missile moves in the direction it was fired until it hits an asteroid or the edge of the screen and then it explodes. How you indicate this explosion is up to you. If the ship manages to shoot the missile through itself (which is quite possible) then the missile will pass through the ship, not explode. If the ship manages to shoot the missile through another missile (which is also quite possible) then the missile will pass through the other missile, not explode.

There will be n asteroids on the screen. Each asteroid starts out at a random point on the screen (though not so close to the center that the player dies immediately.) The asteroids start out moving in a random direction at a constant speed and each slowly rotating either clockwise or counterclockwise. If an asteroid gets hit by a missile it will break into 2 small asteroids each half as big as its parent (but the same shape) moving in random directions, with one rotating clockwise and the other counterclockwise. If a small asteroid is hit by a missile then it is destroyed. How you indicate this is up to you. Asteroids do not hit each other - they pass through each other.

When drawing the asteroids you can make use of glPushMatrix, glPopMatrix, glTranslate, glScale and glRotate to make things easier. When drawing the ship you can _not_ make use of these functions. You must compute the the locations of the lines making up the spaceship on your own. This will help you understand what the openGL routines are doing for you and appreciate them more.

You will keep 2 textual displays on the screen - one showing the number of spaceships that have been destroyed, and another showing the number of asteroids (large and small combined) that have been destroyed. You should use the glutStrokeCharacter routine for this - though you will probably want to write yourself something slightly higher level that calls that function so you can deal with strings rather than individual characters. We are using this function rather than the bitmap routines because these fonts will scale very nicely as you change the size of the window. Another hint here - the characters are really really big by default so you will need to scale them down.

If an asteroid strikes the player's ship then the player's ship is destroyed. How you indicate this is up to you. The player then gets a new ship in the center of the screen and the game continues. If the player destroys all the asteroids (large and small) then a new batch of asteroids appears and the game continues.

Detecting collisions is the probably the hardest part of the assignment as the spaceship and the asteroids can be concave wacky shapes. We will simplify the problem by defining a 'collision radius' for the asteroid - which is the average distance from each of its vertices to its (0,0) center. If the center (0,0) of the spaceship strays within this radius, then we declare that there has been a collision. If the center of the missile strays within this radius, then we declare that there has been a collision. Note that this radius will change for each asteroid model loaded in, and that it will be smaller for the smaller version of the asteroid.

As in the first assignment the window edge as a hard boundary (which is not the way the actual asteroids worked, but it makes things a bit easier.) Therefor the ship and the asteroids (large and small) bounce off the edge of the screen as the ball bounced in assignment 1, they do not wrap around as they did in the real game of Asteroids.

The program should accept a set of command line parameters. If these values are not changed via the command line then the program should use the built in defaults:

• name of the spaceship file - default ship.txt
• name of the asteroid file - default ast.txt
• ship colour (R G B) - default light blue
• asteroid colour (R G B) - default red
• missile colour (R G B) - default yellow
• text colour (R G B) - default white
• asteroid speed - default 40 units per second
• spaceship maximum speed - default 80 units per second
• spaceship turn rate - default 60 degrees per second
• missile speed - default 100 units per second
• number of asteroids - default 3

Your program should be well commented and be a good example of literate programming.

As with assignment 1, your program will be submitted electronically and you need to be sure that it compiles and runs on the SGI O2s in the CS lab.

Unlike assignment 1 where everthing was in absolute coordines, here you have each object in its own coordinate system and use the transforms to put them in the right place at the right size in the right orientation. I would start with the ship - read it in, draw it, spin it, and move it. Then once you have that figured out do the same for an asteroid. Then work on the collision routines. Finally deal with the textual display.

This assignment will make use of some trigonometry. If you havent used sines and cosines recently, I would start thinking about it early. Remember that some routines use degrees and others use radians. I would avoid using fcos, fsin, ftan etc, and instead use cosf, sinf, tanf, etc - they seem more stable.

We will still use a window size of 500,500 and a logical space ranging from -200 to 200.

Again, I would also highly recommend that you understand the code you are writing - you never know when you might need to reproduce it (hint hint).

last modification 2/9/2003