CSCI 1300
Introduction to Computer Science
Homework 6: Ship Simulation

Due Date

All work must be submitted by 7:50am on Tuesday, Mar 2. Submit the programming work online to http://culearn.colorado.edu. No late work is accepted, so submit what you have before the due date!

Working Alone

You may talk with other students and instructors about the assignments, but you may not look at or copy code written by others. The penalty for violating this code is an F for the entire semester.

Programming Style

As always, all your work must follow these ten essential style items. See (snowmen.cxx for examples of this style. Also see www.cs.colorado.edu/~main/style.html.

The Assignment

Last week, you successfully wrote a program to display the vector field of gravitational acceleration for a three-star system. Now, Captain Janeway wants to send the ship into the system and she needs you to modify your program so that it will show the path the ship will take through the field of stars. Here are the details of what you'll program:

  1. Your main program will keep track of six attributes of the ship:
    • The current x and y position of the ship in solar system coordinates. These values start at -4 and -9 (because that's where the ship is now).
    • The current speed of the ship along the x and y dimensions. These numbers are called vx and vy. The program prompts the user for these two initial values (because Captain Janeway can give the ship any initial velocity that she chooses).
    • The current acceleration due to gravity. These numbers, ax and ay, are computed by calling the system_ax and system_ay functions.

  2. After the program sets up these variables, it then goes into an animation loop with the usual items. When you draw the frame, please use a red filled ellipse (your choice of size) for the space ship. For the delay, please use DT*1000 milliseconds, where DT is a global constant that specifies how many seconds are being simulated on each time through the animation loop. (Don't make DT too big. I would suggest about 0.02.)

    When you update the values of your six variables. I would suggest doing the updates in this order: First update the positions x and y. During this update, you should assume that the velocity remains fixed (at vx and vy). Therefore, the new x position will be the old postion plus vx*DT. Next, update each of the velocities. The idea is similar here. For example the new vx will be the old vx plus ax*DT. Finally, recalculate the ax and ay values (based on the new x and y positions).

  3. The program should stop if the ship ever leaves the screen.

You write this program in a file called ship1.cxx. You and the chief astrogator run the program with various input values and find that an initial velocity of vx=0.5 and vy=0.3 gives a path that will take you by all of the stars In fact, the loop the ship takes comes near the lower star, then the upper star, then the leftmost star, then the lower star again, and then the upper star again before exiting the system. So, you take your suggested velocities to the captain, but she grumbles that software undependable! Instead of sending the ship into the system with your suggested numbers, she sends in a probe...which prompty skims the surface of the nearest star (not good for probes or space ships) and flies away from the other stars at a high velocity.

Captain Janeway glares.

The chief astrogator turns to you and says, "I hope you have learned a valuable lesson. No simulation is better than its program."

"But what went wrong with our program?" you ask.

"I don't know," replies the chief, "but we'll find out by 7:50am next Tuesday."

After a session of debugging (and some conversation about barn doors and horses), it appears that the error lies in the astrogator's original acceleration functions. She had omitted a needed term. These are the new functions:

//----------------------------------------------------------------------------
double system_ax(double x, double y)
{
    double answer = 0;
    answer += (+2 - x)/pow((x - 2)*(x - 2) + (y + 5)*(y + 5), 1.5);
    answer += (-3 - x)/pow((x + 3)*(x + 3) + (y - 2)*(y - 2), 1.5);
    answer += (+3 - x)/pow((x - 3)*(x - 3) + (y - 6)*(y - 6), 1.5);
    return 3*answer;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
double system_ay(double x, double y)
{
    double answer = 0;
    answer += (-5 - y)/pow((x - 2)*(x - 2) + (y + 5)*(y + 5), 1.5);
    answer += (+2 - y)/pow((x + 3)*(x + 3) + (y - 2)*(y - 2), 1.5);
    answer += (+6 - y)/pow((x - 3)*(x - 3) + (y - 6)*(y - 6), 1.5);
    return 3*answer;
}
//----------------------------------------------------------------------------
You put the new program in a file called ship2.cxx, and experiment with various input speeds. In the end, an initial velocity of 0.9 and 0.2 provides a good S-shaped path that comes near all three stars before exiting. You take this to the captain who fires another probe that does as expected! Note: The probe does come very close to the second star (shields up!) but the captain is happy because the path takes her pretty close to all three stars for a gander.

If anyone manages to find a cooler path through the star system, please send the cool values to Michael!

For the assignment, please submit both ship1.cxx and ship2.cxx.

Note: This method of simulating a moving item. Come talk with us if you're interested in learning more accurate and faster methods!