CS 201 - Computer Science I - Fall 2008
Project 4


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

Due

Wednesday, November 26th 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


This applet requires Java 2 and so will not work with some browsers.
Use Z, X, C, S, and space to control the red ship and the arrow keys and keypad 0 to control the blue ship.
We are adding rockets to Space War. A rocket is the same as a missile, except rockets have an engine that provides additional acceleration after they have been launched. Once a rocket's engine is turned on it can be turned off but not restarted. In the applet shown above, the blue ship can launch a rocket with the '0' key on the keypad (make sure Num Lock is on); the red ship launches a rocket with the space bar.

We want the rocket's engine to be turned on and then off automatically so that it gets as close to the target ship as possible. When we had constant velocity missiles and targets we were able to write equations that would determine exactly how close a missile would get to its target. The rockets' acceleration complicates these equations, so instead we will simulate the flight of a rocket to determine when the best time to use the engine is.

Assignment

Part 1

Write a program called BallisticsTest1 that asks the user to enter the initial x- and y-coordinates of the target ship, then its direction (in degrees clockwise from right) and speed. Next, ask the user to enter the initial coordinates of the rocket, and its direction and speed. Finally, ask the user to enter the rocket's acceleration and the time to turn the engine on and then off. The output of this part of the program should like the following.

Enter initial position of the target (x y):
0 0
Enter initial velocity of the target (speed dir):
3 0
Enter initial position of the rocket (x y):
50 100
Enter initial velocity of the rocket (speed dir):
2 270
Enter acceleration of the rocket:
1
Enter start and end of rocket engine burn (start end):
5 15

Next, simulate the flight of the rocket by repeating the following steps for time 0, time 1, time 2, etc.:

You should produce a table showing the rocket's position and velocity, the target's position, and the distance between them at each point in time. Your table should look like the following.
 0 (+050.00, +100.00) (-000.00, -002.00) (+000.00, +000.00) 111.80339887498948
 1 (+050.00, +098.00) (-000.00, -002.00) (+003.00, +000.00) 108.6876257906115
 2 (+050.00, +096.00) (-000.00, -002.00) (+006.00, +000.00) 105.60303025955268
 3 (+050.00, +094.00) (-000.00, -002.00) (+009.00, +000.00) 102.55242561733974
 4 (+050.00, +092.00) (-000.00, -002.00) (+012.00, +000.00) 99.53893710503444
 5 (+050.00, +090.00) (-000.00, -002.00) (+015.00, +000.00) 96.56603957913983
 6 (+050.00, +087.00) (-000.00, -003.00) (+018.00, +000.00) 92.69843580125827
 7 (+050.00, +083.00) (-000.00, -004.00) (+021.00, +000.00) 87.92041856133307
 8 (+050.00, +078.00) (-000.00, -005.00) (+024.00, +000.00) 82.21921916437786
 9 (+050.00, +072.00) (-000.00, -006.00) (+027.00, +000.00) 75.58438992278762
10 (+050.00, +065.00) (-000.00, -007.00) (+030.00, +000.00) 68.00735254367721
11 (+050.00, +057.00) (-000.00, -008.00) (+033.00, +000.00) 59.481089431852205
12 (+050.00, +048.00) (-000.00, -009.00) (+036.00, +000.00) 50.0
13 (+050.00, +038.00) (-000.00, -010.00) (+039.00, +000.00) 39.56008088970496
14 (+050.00, +027.00) (-000.00, -011.00) (+042.00, +000.00) 28.160255680657446
15 (+050.00, +015.00) (-000.00, -012.00) (+045.00, +000.00) 15.811388300841896
16 (+050.00, +003.00) (-000.00, -012.00) (+048.00, +000.00) 3.605551275463989
17 (+050.00, -009.00) (-000.00, -012.00) (+051.00, +000.00) 9.055385138137417

Your table should end when the distance reaches zero or starts to increase, except that the table should keep going if the distance is increasing before the rocket's engine is turned off. See below for an example.

Enter initial position of the target (x y):
10 0
Enter initial velocity of the target (speed dir):
10 0
Enter initial position of the rocket (x y):
0 0
Enter initial velocity of the rocket (speed dir):
5 0
Enter acceleration of the rocket:
5
Enter start and end of rocket engine burn (start end):
6 8
 0 (+000.00, +000.00) (+005.00, +000.00) (+010.00, +000.00) 10.0
 1 (+005.00, +000.00) (+005.00, +000.00) (+020.00, +000.00) 15.0
 2 (+010.00, +000.00) (+005.00, +000.00) (+030.00, +000.00) 20.0
 3 (+015.00, +000.00) (+005.00, +000.00) (+040.00, +000.00) 25.0
 4 (+020.00, +000.00) (+005.00, +000.00) (+050.00, +000.00) 30.0
 5 (+025.00, +000.00) (+005.00, +000.00) (+060.00, +000.00) 35.0
 6 (+030.00, +000.00) (+005.00, +000.00) (+070.00, +000.00) 40.0
 7 (+040.00, +000.00) (+010.00, +000.00) (+080.00, +000.00) 40.0
 8 (+055.00, +000.00) (+015.00, +000.00) (+090.00, +000.00) 35.0
 9 (+070.00, +000.00) (+015.00, +000.00) (+100.00, +000.00) 30.0
10 (+085.00, +000.00) (+015.00, +000.00) (+110.00, +000.00) 25.0
11 (+100.00, +000.00) (+015.00, +000.00) (+120.00, +000.00) 20.0
12 (+115.00, +000.00) (+015.00, +000.00) (+130.00, +000.00) 15.0
13 (+130.00, +000.00) (+015.00, +000.00) (+140.00, +000.00) 10.0
14 (+145.00, +000.00) (+015.00, +000.00) (+150.00, +000.00) 5.0
15 (+160.00, +000.00) (+015.00, +000.00) (+160.00, +000.00) 0.0

You can use the Vector and Target classes described in the documentation. A Target object can be created from the target's initial position, speed, and direction. You can then ask its position at any point in time using the getPosition or getX and getY methods. You can create Vector objects from Cartesian (x, y) coordinates or from polar coordinates (direction and length) and then add them together using the add method. Vectors can also 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 combination of engine start and stop times entered by the user, repeats the process for every combination of start time from 0 to 20 and stop time from start+1 to start+10. As it runs through all the combinations, it should keep track of the combination that produces the shortest minimum distance between the rocket and the target, breaking ties in favor of combinations that hit the target soonest. For example, using the same values as in the previous example (omitting the start and end times) should produce the following output.
Enter initial position of the target (x y):
10 0
Enter initial velocity of the target (speed dir):
10 0
Enter initial position of the rocket (x y):
0 0
Enter initial velocity of the rocket (speed dir):
5 0
Enter acceleration of the rocket:
5
Start: 0
End: 2
Note that when you feed that input back into the first program, you get the following.
Enter initial position of the target (x y):
10 0
Enter initial velocity of the target (speed dir):
10 0
Enter initial position of the rocket (x y):
0 0
Enter initial velocity of the rocket (speed dir):
5 0
Enter acceleration of the rocket:
5
Enter start and end of rocket engine burn (start end):
0 2
 0 (+000.00, +000.00) (+005.00, +000.00) (+010.00, +000.00) 10.0
 1 (+010.00, +000.00) (+010.00, +000.00) (+020.00, +000.00) 10.0
 2 (+025.00, +000.00) (+015.00, +000.00) (+030.00, +000.00) 5.0
 3 (+040.00, +000.00) (+015.00, +000.00) (+040.00, +000.00) 0.0

Part 3

Copy your code from part 2 into the optimzeBurn method in SpaceShip. You should copy the code from after reading the user's input to the end of the method. You do not need to copy the instantiation of the Target object since one is supplied for you. You may need to change some of the variable names.

Once this is done you should be able to play the game with rockets. Note that rockets do not necessarily hit the target because we don't have fine control over the engine start and stop times; we found the best integer times to control the engine.

Files

The Java archive contains

To test part 3, you should run SpaceWarWindow.

Advice

Grading

Submissions

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