<< Chapter < Page Chapter >> Page >

Screen output

The text shown in Figure 3 should appear in your browser window when the html file is opened in your browser.

Figure 3 . Screen output for Listing #2.
Start Script Time for leg A = 0.30 secondsTime for leg B = 0.33 seconds Displacement magnitude = 25.00 feetDisplacement angle = 98.13 degreesTotal time = 0.63 seconds Average velocity magnitude = 39.47 feet/secondAverage velocity angle = 98.13 degrees End Script

Analysis of the code

In this exercise, the puck makes a journey across the ice consisting of two sequential legs in different directions with different magnitudes ofvelocity. Although each leg involves motion along a line, the total trip is not along a line.

The objective of the script is to

  • Compute and display the time required to traverse each leg of the trip based on the given information.
  • Compute and display the magnitude and the angle of the displacement from start to finish.
  • Compute and display the time required to complete the entire trip, which is the sum of the times from both legs.
  • Compute and display the magnitude of the average velocity as the magnitude of the displacement divided by the total time.
  • Recognize that the angle of the average velocity is the same as the angle of the displacement.

The getAngle function

The code in Listing 2 begins with the getAngle function that we developed and used an earlier module. The purpose of the function is to receive the adjacentand opposite side values for a right triangle as parameters and to return the angle in degrees in the correct quadrant.

I explained this function in an earlier module and won't repeat that explanation in this module.

Compute the time for each leg

Following the definition of the getAngle function, Listing 2 computes the time required to traverse each leg of the trip. For variable naming purposes, the two legs are identified as A and B.

Variable names that end with "mag" contain vector magnitudes. Variable names that end with "ang" contain vector angles. Variable names that endwith "vel" contain the magnitudes of straight-line, uniform velocities.

The time to complete each leg of the trip is computed by dividing the magnitude of the displacement vector for that leg by the magnitude of thestraight-line velocity for that leg. The resulting times have units of seconds, and are saved in the variables named timeA and timeB .

The overall displacement

The magnitude and angle of the overall displacement for the two-leg trip (identified by the variables named vecResultMag and vecResultAng respectively) are computed using procedures that were explained in an earlier module. Therefore, I won't repeat that explanation inthis module.

The magnitude of the overall displacement is stored in the variable named vecResultMag , and the angle for the overall displacement is stored in the variable named vecResultAng .

The total time

The total time for the trip, identified by the variable named totalTime , is computed as the sum of the times for the individual legs.

Magnitude of the average velocity vector

Get the best College algebra course in your pocket!





Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask