Projectile Motion
Now that we have simple motion under control, let’s take advantage of the 3D environment to do some more interesting motion.
We can model the behavior of thrown objects with two simple assumptions: (1) that the speed of the object in the vertical direction changes due to an acceleration caused by gravity; and (2) that the speed of the object in directions perpendicular to vertical doesn’t change at all. (For the second assumption, we are ignoring the effects of drag by the air, known as air resistance. We can add this back in later!)
In order to accomplish this, we need to define a velocity for the object. The velocity of an object is a vector, and so has three components – one each in the x, y, and z directions. Read through the following program carefully. If you can’t see the red ball, hit RUN to run it again.
GlowScript 3.2 VPython
# Projectile Motion
# Ikaika McFadden
# 10.October.21
scene.range = 20
redBall = sphere( color = vec(1,0,0), radius = 0.5 )
greenBall = sphere( color = vec(0,1,0), pos = vec(0,2,0) )
time = 0.0
dt = 0.01
redBall.pos = vec (0, 18, 0) # this is the starting position
velocity = vec (5, 0, 0) # this is the starting velocity
acceleration = vec (0, -9.8, 0) # this is the acceleration due to gravity
while( time < 10.0 ):
rate(1/dt)
redBall.pos = redBall.pos + velocity * dt
velocity = velocity + acceleration * dt
time = time + dt
Let’s go through and make sure we understand the change we’ve made this time around.
redBall.pos = vec (0, 18, 0)
The initial position of the ball was given in the line above as a vector with components (0, 18, 0). What would happen if this vector was (0, 0, 0)? Try it and see. What if it was (18, 18, 0)? Again, try and see.
velocity = vec (5, 0, 0)
The initial velocity of the ball is provided in the line above as a vector, this time with components (5, 0, 0). This means the ball is initially moving at 5 meters per second in the +x direction, but not moving in the y or z direction initially. What would happen if instead the ball’s initial velocity was (5, 5, 0)? Make a prediction, then try it and see!
acceleration = vec (0, -9.8, 0)
The acceleration due to gravity is provided above. On the moon, the acceleration due to gravity would be weaker, with a value of (0, -1.6, 0). Predict what you’ll see if you make that change and then try it out! Acceleration is a tough concept – it is the rate of change of velocity, which is itself a rate of change of position.
In the last section, our position line read as follows:
redBall.pos.x = redBall.pos.x + speed * dt
However, in the program above, the .x has been removed, and the word speed has been replaced by velocity.
redBall.pos = redBall.pos + velocity * dt
The ability of GlowScript to add vectors like this is very welcome. But what is a vector, really? Let’s use GlowScript to draw an arrow representing the vector velocity of our object.
Web VPython 3.2
# Projectile Motion
# Ikaika McFadden
# 10.October.21
scene.range = 20 # this zooms the camera out a bit
redBall = sphere( color = vec(1,0,0), radius = 0.5 )
greenBall = sphere( color = vec(0,1,0), pos = vec(0,0,0) )
time = 0.0
dt = 0.01
redBall.pos = vec (-20, 0, 0)
velocity = vec (18, 15, 0)
acceleration = vec (0, -9.8, 0)
velArrow = arrow( color = vec(1,1,0), shaftwidth = 0.5 )
velxArrow = arrow( color = vec(0,1,0), shaftwidth = 0.25 )
velyArrow = arrow( color = vec(0,1,0), shaftwidth = 0.25 )
while( time < 10.0 ):
rate(1/dt)
redBall.pos = redBall.pos + velocity * dt
velocity = velocity + acceleration * dt
velArrow.pos = redBall.pos # places the arrow on the ball
velArrow.axis = velocity # points the arrow parallel to velocity
velxArrow.pos = redBall.pos
velxArrow.axis = vec( velocity.x, 0, 0)
velyArrow.pos = redBall.pos
velyArrow.axis = vec( 0, velocity.y, 0)
time = time + dt
As you’ll see, an arrow appears representing the direction of motion and speed of the ball. The longer the arrow, the faster the speed. In this sense, the arrow is a representation of the velocity vector itself. The vector velocity represents both the speed and the direction of motion of the ball.
Let’s add two more smaller, green arrows to represent JUST the x and y components of the ball’s velocity. This means inserting the following two lines of code right underneath the line declaring velArrow:
velxArrow = arrow( color = vec(0,1,0), shaftwidth = 0.25 )
velyArrow = arrow( color = vec(0,1,0), shaftwidth = 0.25 )
And also inserting the following two lines of code into the loop near the end of the program:
velxArrow.pos = redBall.pos
velxArrow.axis = vec( velocity.x, 0, 0)
velyArrow.pos = redBall.pos
velyArrow.axis = vec( 0, velocity.y, 0)
Here’s what it looks like now – I also changed the starting position and velocity just for fun, so it looks like the red ball is jumping over the green ball.
Web VPython 3.2
# Projectile Motion
# Ikaika McFadden
# 10.October.21
scene.range = 20 # this zooms the camera out a bit
redBall = sphere( color = vec(1,0,0), radius = 0.5 )
greenBall = sphere( color = vec(0,1,0), pos = vec(0,0,0) )
time = 0.0
dt = 0.01
redBall.pos = vec (-20, 0, 0)
velocity = vec (18, 15, 0)
acceleration = vec (0, -9.8, 0)
velArrow = arrow( color = vec(1,1,0), shaftwidth = 0.5 )
velxArrow = arrow( color = vec(0,1,0), shaftwidth = 0.25 )
velyArrow = arrow( color = vec(0,1,0), shaftwidth = 0.25 )
while( time < 10.0 ):
rate(1/dt)
redBall.pos = redBall.pos + velocity * dt
velocity = velocity + acceleration * dt
velArrow.pos = redBall.pos # places the arrow on the ball
velArrow.axis = velocity # points the arrow parallel to velocity
velxArrow.pos = redBall.pos
velxArrow.axis = vec( velocity.x, 0, 0)
velyArrow.pos = redBall.pos
velyArrow.axis = vec( 0, velocity.y, 0)
time = time + dt
Recall at the start of this section we said that the horizontal component of the velocity wouldn’t change (in the absence of a drag force) and the vertical component would change in the downward direction. Just that constraint is enough to get a realistic looking motion.
Congrats! You can animate motion in two dimensions and understand how the motion in one dimension can be independent from motion in the other. Now we need to modify this program for our projectile motion lab.