CS-182 Lab 4: Integrating Functions into an Existing Program

Dr. Mark J. Sebern


CS-182 Main Page Schedule General lab information

Overview

The purpose of this lab is to design and implement a function to work with other parts of a program that has been written by someone else.

Activities

In this lab, you will:

Problem statement

Your company is working on a simple video game program, and you have been asked to design and implement one component of it. The video game involves firing a projectile from a cannon and tracking the resulting trajectory. Your job is to implement the equations that govern the motion of the projectile. The video game looks like this:

Your function will be given the following information:

You function must calculate and return:

The differential equations that govern the motion of the cannonball can be solved numerically (by a computer) and that is the job you have been given. The calculations are:

Convert the ball velocity from polar form (magnitude and direction) to rectangular form (horizontal and vertical components).
Calculate the new ball position by assuming that the velocity will remain constant in the time interval. This is not exactly correct, but will be close if the interval is short enough.

(Some people prefer a "+" sign on the g term, but then define g as negative instead of positive; take your choice.)
Given the current velocity in rectangular form, adjust the horizontal and vertical components to account for gravity. This calculation ignores any air resistance. The constant g is the acceleration due to gravity, 32 feet per second squared.
Calculate the magnitude and angle of the new ball velocity, given its horizontal and vertical components. In other words, convert the new velocity from rectangular form to polar form. Note that the arctangent function value should cover all four quadrants; take a look at the atan2 function in <math.h>.

The interface to your function (which must be named Trajectory) is as follows:

void Trajectory (double delta_time, double& ball_x, double& ball_y,
double& ball_speed, double& ball_angle);
// Calculates the updated position and speed of the projectile.
// Arguments:
//   delta_time - Time step from the current position to the next.
//   ball_x - The current horizontal position of the cannonball.
//   ball_y - The current vertical position (height) of the cannonball.
//   ball_speed - The current speed of the cannonball.
//   ball_angle - The current direction of travel of the cannonball.
//                Zero degrees is horizontal to the right.
//                Ninety degrees is upward.
// Returns (by side effect of modifying pass-by-reference arguments):
//   ball_x - The new (after delta_time) horizontal position of the cannonball.
//   ball_y - The new vertical position (height) of the cannonball.
//   ball_speed - The new speed of the cannonball.
//   ball_angle - The new direction of travel of the cannonball. 

You can find this function's interface in the file cannon2.h and its definition in the file cannon2.cpp. Your job is to fill in the body of the definition.

If you have any difficulty connecting the equations to the function arguments, please take a look at the supplementary information provided by Prof. Jeff Blessing.

Detailed instructions

If you have difficulties with any part of the lab, consult the instructor for assistance. The basic sequence is:

  1. Download the file containing the source code and other files necessary to build the application.

    If you are using an older compiler on another machine, here are some older versions you can try running. Your cannon2.cpp function body should be interchangeable.

  2. Unpack the ZIP archive using WinZIP on the warp network or a similar tool. With some tools (e.g., PKUNZIP), you may need to specify the "-d" option to create subdirectories for the Microsoft version of the files.
  3. Open the supplied project file (part of the ZIP archive) and build the application. You may need to adjust the project directory paths if you are running on your own computer. Run the application; the substitute code in cannon2.cpp won't perform the proper calculations, but you should be able to tell that the program is working.
  4. Modify the Trajectory function in cannon2.cpp so that it does the proper calculation. Do not modify any other source files.
  5. Test the program.
  6. Extra credit: If this is too easy for you, modify the Trajectory function to take air resistance into account. The acceleration (deceleration) due to air resistance can be approximated as being proportional to the velocity, with a proportionality constant in the range of 0.1 to 0.2. See how this changes the simulation results.
  7. Submit the lab report (details below).

Lab report

The lab report should consist of the following:

You may submit this report in hard-copy form, or email it to the instructor. If you choose email, it may be convenient to put the textual portion of the report directly in the message, and to attach the source file. Be sure to keep copies of all your files, in case something gets lost; it may be wise to keep a diskette backup as well.

The lab report is due by the beginning of the following lab period, though you are encouraged to submit it sooner if you can. Your grade will depend on quality of design and clarity of the code and documentation, as well as whether your program produces the correct results. If you have any questions, consult the instructor.


This page was last updated on September 02, 1997; send comments to Mark Sebern.