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.
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.:
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.
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: 2Note 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
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.
To test part 3, you should run SpaceWarWindow.