<< Chapter < Page Chapter >> Page >

Discussion and sample code

I will present and explain several different scenarios based on the above assumptions in this section.

Variable velocity exercise #1

An archer that is six feet tall shoots an arrow directly upward with a velocity of 100 feet per second. Assume the arrow is at a height of 6 feet whenit leaves the bow. Also ignore the effects of air resistance.

Plot height versus time

Use your graph paper and plot the height of the arrow on the vertical axis versus time on the horizontal axis. Plot one point each 0.25 seconds. Allow for amaximum time value of about 7 seconds and a maximum height value of about 165 feet.

Your plot should look something like the curve in Figure 1 .

Figure 1 - Graph for variable velocity exercise #1.

Missing image.

Create a script

Let's analyze this situation using a script. Copy the code shown in Listing 1 into an html file and open the file in your browser.

Listing 1 . Variable velocity exercise #1.
<!---------------- File JavaScript01.html ---------------------><html><body><script language="JavaScript1.3">document.write("Start Script<br/>"); //Declare and initialize constantsvar g = -32.2;//acceleration of gravity in ft/(sec*sec) var v0 = 100;//initial velocity in ft/secvar h0 = 6;//initial height in feet var tInc = 0.25;//calculation interval in seconds//Declare and initialize working variables var t = .0001;//current time in secondsvar h = h0;//current height in feet //The following variable is used to insert spaces in// the output display. var sp = "&nbsp;&nbsp;&nbsp;&nbsp;" //Compute and display various values for as long as the// height of the projectile is above the ground. while(h>0){ //This is the general equation for the height of a// projectile under the influence of gravity in a // vacuum.h = h0 + v0*t +0.5*g*t*t; //Display the information for this iterationdocument.write( "t = " + t.toFixed(2) + " seconds" + sp +" h = " + h.toFixed(1) + " feet" + sp + "<br/>"); //Increment the time for the next iteration.t = t + tInc; }//end while loopdocument.write("End Script");</script></body></html>

Screen output

The text shown in Figure 2 should appear in your browser window when you open the html file in your browser.

Figure 2 . Screen output for Listing #1.
Start Script t = 0.00 seconds h = 6.0 feett = 0.25 seconds h = 30.0 feet t = 0.50 seconds h = 52.0 feett = 0.75 seconds h = 72.0 feet t = 1.00 seconds h = 89.9 feett = 1.25 seconds h = 105.8 feet t = 1.50 seconds h = 119.8 feett = 1.75 seconds h = 131.7 feet t = 2.00 seconds h = 141.6 feett = 2.25 seconds h = 149.5 feet t = 2.50 seconds h = 155.4 feett = 2.75 seconds h = 159.2 feet t = 3.00 seconds h = 161.1 feett = 3.25 seconds h = 160.9 feet t = 3.50 seconds h = 158.8 feett = 3.75 seconds h = 154.6 feet t = 4.00 seconds h = 148.4 feett = 4.25 seconds h = 140.2 feet t = 4.50 seconds h = 130.0 feett = 4.75 seconds h = 117.7 feet t = 5.00 seconds h = 103.5 feett = 5.25 seconds h = 87.2 feet t = 5.50 seconds h = 69.0 feett = 5.75 seconds h = 48.7 feet t = 6.00 seconds h = 26.4 feett = 6.25 seconds h = 2.1 feet t = 6.50 seconds h = -24.2 feetEnd Script

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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