I am working on Kick a Field Goal game from chapter 10. I know you have to use a variable when asking a question for angle and velocity, but I do not know how to take those variables and use the math code the book suggested. The code is used to move the football toward the field goal. I have added the code, but not sure if I am in the right ball park. If anyone has any suggestions please let me know.

Thanks;

Randy

This is text from the book:

When a field goal kicker kicks a football, the football moves through the air in a parabolic path because the motion of the ball is effected by gravity. As the ball travels upward, gravity slows it down until it stops (briefly) at its peak height. Then, the ball starts to come down and gravity speeds it up until it hits the ground. Two major factors influence the path of the ball: velocity (the speed of the ball) and the angle of the kick.

Ask the player to enter the angle of the kick (in degrees) and the initial velocity of the kick (in meters per second). Together, the velocity of the ball and the angle of the kick determine how long the ball will remain in the air (the “hang time”), how high the ball will go (the “peak height”), and how far the ball will go (the “range”). To compute the path of the football, first break the velocity down into two components:

horizontal (speed along the x-axis) and vertical (speed along the y-axis). The formulas for computing horizontal and vertical velocity are shown below. Note that the angle of the kick must be converted from degrees to radians.

horizontal Velocity = cos(angle of the kick, in radians) * initial velocity

vertical Velocity = sin(angle of the kick, in radians) * initial velocity

To compute the hang time, use the formula:

hang time = 2 * vertical Velocity / 9.8

Once you have the horizontal velocity, the vertical velocity, and the hang time, you can then animate the football’s motion by repeatedly computing the position of the ball (at each instant in time during the hang time ) and moving the ball to the new position at that time in the flight. For example, compute the position after 0.2 seconds has elapsed and move the ball to that position, then compute the position after 0.4 seconds has elapsed and move the ball to that position, then compute the position after 0.6 seconds

… and so forth. To compute the position of the ball (in x and y coordinates) after a specific lapse in time during the hang time, use the formulas:

x = horizontal velocity * elapsedTime

y = 0.5 *-9.8 * (vertical velocity * elapsedTime 2) + (vertical velocity * elapsedTime)

Sample test data: With an angle of 30 degrees and a velocity of 27 m/sec (about 90 ft/sec), the hang time should be approximately 2.8 seconds and the ball should travel about 66 meters forward from its original position on the ground.

Hints: We recommend using a loop that repeats while the elapsed time is less than the hang time. Each time the loop repeats, increase the lapsed time by a very small amount such as 0.2 seconds. Update the position of the ball each time through the loop, by moving the ball forward the change in x position and up the change in y position.