CS 201 - Computer Science I - Spring 2009
Project 4


Loyola College > Department of Computer Science > Dr. James Glenn > CS 201 > Projects > Project 4

Due

Tuesday, April 14th at 11:59pm. Projects submitted after the due date will be assessed a 20% penalty per day. Projects will not be accepted more than four days late.

Objectives

Introduction

For this project you will simulate the flight of a spaceship and the flight of a missile fired at the ship. The objective is to determine the angle at which to fire the missile so that it comes closest to the ship. The simulation will start with the initial positions and velocities of the ship and the missile and then compute the subsequent positions every tenth of a second thereafter, stopping when the closest approach has been reached.

Assignment

Part 1

Write a program called BallisticsTest1 that asks the user to enter the initial x- and y-coordinates of the cannon that will fire the missile, the missile's speed, the initial x- and y-coordinates of the ship, the initial x- and y-components of the ship's velocity, the gravitational acceleration, and the angle to fire the missile at (with 0 degrees being straight up and angles increasing clockwise). The output of this part of the program should like the following (take care to ask for the input values in this order):

Enter cannon position (x y):
25 0
Enter missile speed:
20
Enter ship position (x y):
0 20
Enter ship velocity (x y):
10 10
Enter gravity:
-2
Enter angle (degrees):
0

Next, simulate the flight of the rocket by repeating the following steps for time 0.0, time 0.1, time 0.2, etc.:

You should produce a table showing the ship's position and velocity, the missile's position, and the distance between them at each point in time. Your table should look like the following.
00.0 (+000.00, +020.00) (+010.00, +010.00) (+025.00, +000.00) 032.02
00.1 (+001.00, +021.02) (+010.00, +010.20) (+025.00, +002.00) 030.62
00.2 (+002.00, +022.06) (+010.00, +010.40) (+025.00, +004.00) 029.24
00.3 (+003.00, +023.12) (+010.00, +010.60) (+025.00, +006.00) 027.88
00.4 (+004.00, +024.20) (+010.00, +010.80) (+025.00, +008.00) 026.52
00.5 (+005.00, +025.30) (+010.00, +011.00) (+025.00, +010.00) 025.18
00.6 (+006.00, +026.42) (+010.00, +011.20) (+025.00, +012.00) 023.85
00.7 (+007.00, +027.56) (+010.00, +011.40) (+025.00, +014.00) 022.54
00.8 (+008.00, +028.72) (+010.00, +011.60) (+025.00, +016.00) 021.23
00.9 (+009.00, +029.90) (+010.00, +011.80) (+025.00, +018.00) 019.94
01.0 (+010.00, +031.10) (+010.00, +012.00) (+025.00, +020.00) 018.66
01.1 (+011.00, +032.32) (+010.00, +012.20) (+025.00, +022.00) 017.39
01.2 (+012.00, +033.56) (+010.00, +012.40) (+025.00, +024.00) 016.14
01.3 (+013.00, +034.82) (+010.00, +012.60) (+025.00, +026.00) 014.89
01.4 (+014.00, +036.10) (+010.00, +012.80) (+025.00, +028.00) 013.66
01.5 (+015.00, +037.40) (+010.00, +013.00) (+025.00, +030.00) 012.44
01.6 (+016.00, +038.72) (+010.00, +013.20) (+025.00, +032.00) 011.23
01.7 (+017.00, +040.06) (+010.00, +013.40) (+025.00, +034.00) 010.04
01.8 (+018.00, +041.42) (+010.00, +013.60) (+025.00, +036.00) 008.85
01.9 (+019.00, +042.80) (+010.00, +013.80) (+025.00, +038.00) 007.68
02.0 (+020.00, +044.20) (+010.00, +014.00) (+025.00, +040.00) 006.53
02.1 (+021.00, +045.62) (+010.00, +014.20) (+025.00, +042.00) 005.39
02.2 (+022.00, +047.06) (+010.00, +014.40) (+025.00, +044.00) 004.29
02.3 (+023.00, +048.52) (+010.00, +014.60) (+025.00, +046.00) 003.22
02.4 (+024.00, +050.00) (+010.00, +014.80) (+025.00, +048.00) 002.24
02.5 (+025.00, +051.50) (+010.00, +015.00) (+025.00, +050.00) 001.50
02.6 (+026.00, +053.02) (+010.00, +015.20) (+025.00, +052.00) 001.43
02.7 (+027.00, +054.56) (+010.00, +015.40) (+025.00, +054.00) 002.08

Your table should end when the distance reaches zero (or within 10-10 of 0), starts to increase, or the time reaches 10.0 seconds. See below for an example

Enter cannon position (x y):
25 0
Enter missile speed:
25
Enter ship position (x y):
0 25
Enter ship velocity (x y):
25 0
Enter gravity:
0
Enter angle (degrees):
0
00.0 (+000.00, +025.00) (+025.00, +000.00) (+025.00, +000.00) 035.36
00.1 (+002.50, +025.00) (+025.00, +000.00) (+025.00, +002.50) 031.82
00.2 (+005.00, +025.00) (+025.00, +000.00) (+025.00, +005.00) 028.28
00.3 (+007.50, +025.00) (+025.00, +000.00) (+025.00, +007.50) 024.75
00.4 (+010.00, +025.00) (+025.00, +000.00) (+025.00, +010.00) 021.21
00.5 (+012.50, +025.00) (+025.00, +000.00) (+025.00, +012.50) 017.68
00.6 (+015.00, +025.00) (+025.00, +000.00) (+025.00, +015.00) 014.14
00.7 (+017.50, +025.00) (+025.00, +000.00) (+025.00, +017.50) 010.61
00.8 (+020.00, +025.00) (+025.00, +000.00) (+025.00, +020.00) 007.07
00.9 (+022.50, +025.00) (+025.00, +000.00) (+025.00, +022.50) 003.54
01.0 (+025.00, +025.00) (+025.00, +000.00) (+025.00, +025.00) 000.00

You can use the Position and Velocity classes described in the documentation to do the calculations for you. Instances of both classes can be used with System.out.println to produce output as in the tables shown above.

Part 2

Write a new program called BallisticsTest2 that works like the first one, but instead of producing the table for one angle, it does the calculations for every angle between -80 degrees and 80 degrees, increasing by 10 degrees at a time. As it runs through all the angles, it should keep track of the angle that produces the shortest minimum distance between the missile and the ship, breaking ties in favor of combinations that hit the target soonest. It should display the best angle at the end. For example, using the same values as in the first example (omitting the angle) should produce the following output.
Enter cannon position (x y):
25 0
Enter missile speed:
20
Enter ship position (x y):
0 20
Enter ship velocity (x y):
10 10
Enter gravity:
-2
-80 025.37
-70 023.28
-60 020.90
-50 018.25
-40 015.37
-30 012.23
-20 008.87
-10 005.19
0 001.43
10 002.00
20 007.06
30 025.95
40 031.77
50 032.02
60 032.02
70 032.02
80 032.02
Best angle = 0

Part 3

Copy your code from Part 2 into the findShootingAngle method in the Cannon class. You will have to modify your code in the following ways.

When this part is complete you should be able to compile and run (as an applet) the LiberationApplet class. You can control the ship's movement with the arrow keys (left and right rotate, up fires the engine); the space bar drops a bomb. Before adding your code, the enemy cannons will only fire straight up. After adding your code, they should score direct hits on the player's ship (although someone needs to write the collision detection code to make things blow up...).

Files

The Java archive contains among other files.

Advice

Grading

Submissions

Submit by e-mail the source code (.java files) for your BallisticsTest1, BallisticsTest2, and Cannon.