Modeling Projectile Motion
The following snippet of code can be used as a starting point to model projectile motion. Copy the code below and paste it into a new GlowScript program window.
- Study the beginning lines of code and notice how parameters such as the object’s mass
m
, initial positionr0
, initial velocityv0
, and momentump
are defined at the start of the program before the loop. - Notice that the time
t
is initialized to zero and the size of the time stepdt
is defined outside the loop. - Inside the loop the
rate(100)
command tells the program to execute the contents of the loop 100 times each second. Since we have setdt = 0.01
seconds the program should run in near real time. When we add graphs or other features later the program may actually run slower. - Notice the loop is defined to run as long as the ball is above ground (
y > 0
). This condition could be changed if you want to model a different situation. - Notice that the time is incremented on each iteration of the loop with an equation of the form
t = t + dt
inside the loop. This might seem like a silly algebraic equation since thet
would cancel on each side of the equation. However, this is not an algebraic equation. It is an assignment which means that the value of the right hand side is evaluated and then assigned to the variable on the left hand side. It would be equivalent to the equation \(t_{new} = t_{old} + dt\). - Finally, notice that inside the loop there are three lines that are incomplete. These lines are for updating the force, the momentum, and the position of the object. Complete these lines using the methods outlined in our textbook
# Display an object undergoing basic projectile motion # Draw an object to represent the floor floor=box(pos=vector(0,-.02,0), size=vector(2,.02,.4)) # Define Constants m = 0.1 # Mass of object (kg) g = 9.8 # Gravity Constant (N/kg) # Define Initial Conditions r0 = vector(-1,0,0) # Initial Position (m) v0_mag = 4 # Magnitude of Initial Velocity (m/s) theta0 = 60 # Launch Angle in Degrees # Compute an Inital Velocity and Momentum # remember to convert angle from degrees to radians v0 = vector(v0_mag*cos(theta0*pi/180), v0_mag*sin(theta0*pi/180),0) p = m*v0 # Create the object at the initial position ball=sphere(pos=r0, radius=0.02, color=color.red, make_trail=True) # Initialize the Time Variables t=0 # Actual Time (s) dt=0.01 # Time Step (s) while ball.pos.y>=0: rate(100) # Complete the following 3 lines ... Fnet = p = ball.pos = t=t+dt
Graphing
Creating a graph in vPython is easy. First, one needs to define the graph and the functions that you want to plot on a graph. As an example, the following lines of code will create a Position versus Time graph and prepare it for plotting the two components of position. We will use the function f1
to hold the x-component of position, and the function f2
to hold the y-component of position. Copy and paste these lines of code somewhere before your loop in your program.
# Create a Position vs Time Graph g1 = graph(title='Position vs Time', xtitle='Time (s)', ytitle='Position (m)', fast=True, width=800) f1 = gcurve(color=color.blue, width=2, markers=True, marker_color=color.blue, label='Horizontal, x') f2 = gcurve(color=color.red , width=2, markers=True, marker_color=color.red, label='Vertical, y')
After the graph and the functions you wish to plot have been defined, you can add points to the plot using the plot
attribute of the functions. For example, to plot the position components on this graph you would place the following lines inside your loop after the new position has been computed. Notice that these lines must be indented at the same level as the rest of the loop contents. Be careful when copying and pasting to make sure that the indent levels match.
f1.plot(t, ball.pos.x) f2.plot(t, ball.pos.y)
Test the program after you have made these changes to make sure your position components plot as you expect.
See if you can create another graph that shows the trajectory (\(y\) vs \(x\)), or another graph that shows the velocity components, \(v_x\) and \(v_y\).
Exercises
This program defines a function named projectile(m, r0, v0, c)
that computes the trajectory of an object of mass m
, initial position r0
, and initial velocity v0
. The trajectory is drawn until the object returns to ground level at y = 0
and it is shown using the color c
. This function can be called multiple times as shown in the example main program below the function definition.
Use this program to answer the following questions.
- For a given initial velocity magnitude, what angle will produce the greatest range?
- Is it possible to launch two projectiles from the same point at the same initial speed but at two different angles and hit the same target? If so, what is the relationship between the two angles?
- In Figure 1, which ball spends the most time in the air?
- In Figure 1, which ball has the greater launch speed?
- In Figure 2, which ball spends the most time in the air?
- In Figure 2, which ball has the greater launch speed?