## GlowScript Tutorial 7: Useful Projectile Motion

Now you know enough stuff (from previous tutorials) to do something useful. Let’s start with a simple question: at what angle should you shoot a projectile to get the maximum range? If you just shout out “45 degrees” – you just memorized that. Let’s see if we can reproduce that with GlowScript.

Here is a starter program. http://www.glowscript.org/#/user/rhettallain/folder/phys221/program/GStutorial7changingprojectile

It looks like this:

This program first creates a function called “proj” (which is short for projectile motion). You give the function an initial velocity magnitude and angle and it models a thrown ball with that same starting velocity. The model shows the motion of the ball starting from y = 0 back to y = 0 (so on flat ground). The inside of the function looks just like your previous projectile motion program.

Notice that the function does not have a “return” statement. Instead, it just displays the motion of the ball. After the function, there are three instances that call the function. All start with the same initial speed but at different angles. When you run it, you get this:

Notice that I didn’t bother to draw the ground. Why would that matter? In fact, I don’t need to draw the ball at all. If I don’t make a sphere, GlowScript won’t create the 3d environment. Let me remove the sphere from the function and have it return the final x position. Here’s what the function now looks like.

This is essentially the same program. The big difference is the lack of objects (sphere) and the function returns the final x position and time. When you run it, you get this.

Each time the function is called, it prints out both the final x and the final time. I could just print the x position with this line.

Since the function produces two values, it gives them as a list. We can reference the first item in a list as proj(4,.2)[0] and the second item as proj(4,.2)[1] (remember that lists start with the index 0 not 1).

We are now ready to do some serious stuff. Here is the plan. Pick some starting launch velocity and some angle (a small angle). Run the projectile function and produce the final position of the projectile. Plot the starting angle and the final position. Repeat until you get to some very large angle. This should make a graph.

Here is a starter program with some blanks for you to fill in.

http://www.glowscript.org/#/user/rhettallain/folder/phys221/program/maxrangeexample

And this is the important part (the program won’t run until you fix it.

Clearly “stuff” shouldn’t be there. You will need to make some changes. Also notice that I used the variable v and theta1 instead of v0 and theta. Since these same variables are used inside the function, some would say it would be bad form to also use them outside the function.

When you fix it, you should get an output like this:

This shows a maximum range at around 45 degrees. Boom. Oh, why is the line squiggly? I will let you play with the code and see if you can figure that one out.

Next, something super useful. I mean, we already knew the answer for maximum range, right? What if I shoot the ball from a starting location that is 1 meter above the ground? At what angle will it go a maximum distance? Hint: you only need to change one thing in your program. Double hint: here is the output you should get.

**Homework**

There are many things left to explore. Here are some things you can try.

- What is the launch angle that gives the greatest time of flight?
- What if you add air resistance (see a previous tutorial)? What is the launch angle for a ping pong ball fired at a speed of 12 m/s?
- What if you start the ball on the ground a distance of 1 meter away from a vertical wall? What angle should you launch the ball (speed of 4 m/s) so that it hits the vertical wall at the highest point?
- If you shoot the ball starting 1 meter off the ground, does the angle for max range change with launch speed?
- What if you start from the ground and launch up to a table that is 1 meter off the ground? What is the angle for the maximum range? This is a little bit tougher since you will need to change the loop condition in your projectile function.