CSCI 1300
Introduction to Computer Science
Homework 6: Ship Simulation

Due Date

All work must be submitted by 10pm on Friday, Oct 21. Submit the programming work online to http://culearn.colorado.edu. Late work is accepted until 10am on the following Monday with a ten-point late penalty.

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 the ten essential style items from 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 some details of what you'll program:

  1. In addition to any global named constants that you used in last week's assignment, you will need two more global named constants. One of them is an integer for the frames per second that your animation program will display (I would suggest about 30). The other is a double number called SIMTIME that tells how many seconds of ship time are simulated for each frame (I would suggest about 50,000).

  2. Your main program will keep track of four attributes of the ship:
    • The current x and y position of the ship in solar system coordinates. The starting positions are (-6.0e10, -1.3e11). These numbers are in meters, using the world coordinate system.
    • The current speed of the ship along the x and y dimensions. These numbers are called vx and vy. The starting values for these speeds must be zero! These numbers are in meters/second.

  3. The program must also have variables to keep track of the x and y positions of each of the three stars. (But the masses of the stars are constants.) The initial positions of the stars is the same as in last week's program.

  4. After the program sets up these variables, it then goes into an animation loop with the usual items:
    • Start by drawing the next frame. This will include the three stars, the acceleration vectors, and the space ship. Use a green filled ellipse for the ship (no more than 9 pixels for either radii).
    • Swap the buffers and delay. The delay time, in milliseconds, must be 1000 divided by the number of frames per second.
    • Change the values in the program's variables so that the next frame will be different! These calculations can affect the ship's position and velocity as described below.

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

Here are some requirements on changing the values of the program's variables:

  1. During every frame, you must change the x-position of the ship by vx*SIMTIME.

  2. During every frame, you must change vx by the current x-acceleration (from all three stars) times SIMTIME. Note that this is JUST the acceleration (do not multiply it by 5e10 as you did to display the vectors in the previous assignment).

  3. You must change y and vy in a similar way during each frame.

You must write this program in a file called ship1.cxx.


Once ship1 is working, please add these new features in a new file called ship2.cxx:

  1. The ship2 program never stops.

  2. Whenever the user clicks the left mouse button, the ship jumps to the clicked location, and the speeds are reset to zero.

  3. You must keep track of how many times the user has clicked the right mouse button in a variable called rclicks. Use this number to determine what a right click does, as follows:
    • If rclicks is 1, then the yellow star jumps to the clicked spot.
    • If rclicks is 2, then the blue star jumps to the clicked spot.
    • If rclicks is 3, then the red star jumps to the clicked spot.
    • If rclicks is 4, then the yellow star jumps to the clicked spot.
    • If rclicks is 5, then the blue star jumps to the clicked spot.
    • If rclicks is 6, then the red star jumps to the clicked spot.
    • and so on...

  4. The program must use non-blocking mouse clicks. (The animation does not stop to wait for any mouse clicks!)

After you finish ship2, you may also add optional features such as a different background (something other than the acceleration vectors) or a cooler ship, or even a rotating ship. If you do additional things, put those in a new file called ship3.cxx (but don't put these enhancements in your ship1.cxx or ship2.cxx!)


For the assignment, please submit both ship1.cxx and ship2.cxx. Submission of ship3.cxx is optional.

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