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.
In this lab, you will:
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.
If you have difficulties with any part of the lab, consult the instructor for assistance. The basic sequence is:
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.
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.