GlowScript Tutorial 2: Projectile Motion
Let’s start with a simple program (that I created for you).
This program won’t run – but we can fix that. The “rate(100)” in line 17 tells the program how fast to run the calculation. This says to do no more than 100 calculations every second. Since our time step is 0.01 seconds, this should make it “real time”. The basic idea of a numerical calculation is to break the problem into many tiny steps. During each of these little time steps, we want to:
- Calculate the force on the object (in this case that would just be the gravitational force)
- Update the momentum of the ball
- Update the position of the ball
- Update time
Here are some useful equations:
Notice that for the update position, I am using the momentum at time 2. If my time interval is small enough, it shouldn’t matter which momentum I use to find the new position. Also, look at line 21 (t = t+dt). This might seem like a silly algebraic equation since the t would cancel on both sides of the equation. However, this is NOT an algebraic equation. This is an assign statement. This says “Make the new time (t) the old time (t) plus dt. It is equivalent to t2 = t1 + dt.
Ok, now fill in the missing parts of the program and see if you can get it to work. This is what it should look like:
Now for something more useful. How high was the ball when the program stopped? One way to find out would be to use a print statement. Here is how you could do that:
Line 23 is dedented (instead of indented) so that it is no longer part of the while loop. This means that line 23 won’t be run until after the loop is finished. If you indent line 23, it will print out the ball position for every step (that’s annoying). Now try this:
- What is the final x-position of the ball?
- How long was the ball in the air?
- What is the final velocity of the ball?
- What is the final magnitude of the ball’s velocity?
- What happens if you change the initial velocity of the ball? Change either the magnitude of the velocity or the angle or both.
Now try something crazy. Add another constant force (other than gravity) to the ball. What happens? Play with it.