# Python Tutorial 8: Modeling A Projectile

In the previous lesson, we introduced the momentum principle, and how we can update an object's state of motion based on either a single force being applied, or multiple forces being applied on an object.

In this tutorial, we will build a simulation that models an object that is experiencing only a vertical gravitational force, and demonstrates how the vertical velocity will change while its horizontal velocity remains constant.

- Learn how to define a gravitational force acting on an object.
- Learn how to model a projectile, and analyze its behavior.

We have seen that we can simulate an object's behavior when it is acted upon by forces in two dimensions. When the NET force on the object points diagonally in space, then we can see that the object's momentum will change in both dimensions.

In the animation below, we see a particle that starts off from rest. Two forces are acting on it - identified by the two black arrows. The NET force acting on the particle is identified by the red arrow. Notice that the green arrow, which represents the velocity of the particle points in the same direction, but its length is increasing.

In the previous lesson we only looked at the special case when the initial momentum of the particle was zero, like in the above example. What happens when the initial momentum (and therefore the initial velocity) of the particle is not zero? How does the particle behave if the force points in a direction that is not in the same direction as the object's initial momentum?

Recall from the previous lesson that the Momentum Principle states that a force applied for a specific amount of time will change the momentum of a particle by a specific amount:

LaTeX: m\cdot\Delta v\:=\:\sum F\:\cdot\Delta t

or in code:

dp = Fnet * dt

The momentum principle, as we have stated before is a very powerful principle that helps us understand the cause and effect relationship between forces acting on an object, and how that object's state of motion is changing. You might think that a changing velocity is equivalent to either speeding up or slowing down. A change in momentum though can also mean a change in the

*direction*of the object's momentum.In this tutorial we will investigate how to simulate a particle that already has a momentum (an initial velocity that is NOT zero) and whose momentum is changing when a net force acts on the particle along a direction that is other than the direction of the object's momentum.

This is precisely the case in projectile motion. We are going to look specifically at how to simulate a projectile's motion.

To follow along in this tutorial, open the scenario in a new tab by clicking on the link below:

We are going to define a object (represented by a

`circle`

) that is being acted upon by only gravity. This is one of the defining principles of projectiles.The first thing that you are going to need to do is create a

`circle`

that we can apply our force on. For review, see if you can remember how to do this by achieving this goal:**Create a circle called "ball" with an initial position of [0, 0] a radius of 5 and a 'mass' of 5."**

The next step in the

**Initial State**pane is to give the ball a velocity that is [0, 0]. We are going to change this soon, but for now, just make it zero like this:ball.v = vec(0, 0)

To define the gravitational force, we need to use a vector, but whose value is dependent on the mass of the object. Hopefully you recall that the force of gravity for an object close to the surface of the earth is given by this simple calculation:

LaTeX: \vec{F_{gravity}}=mass\:\cdot\:\left[0,\:-\frac{9.8\:m}{s^2}\right]

The equation above states that the force of gravity is indeed a vector that points in the negative vertical direction but has no horizontal component. The gravitational force acting on a ball with a mass of 2 kg would then be:

LaTeX: \vec{F_{gravity}}=2 kg\:\cdot\:\left[0,\:-\frac{9.8\:m}{s^2}\right] =\left[0, -19.6 N\right]

To define this force in code, we just multiply the ball's mass by the vector [0, -9.8]:

# Define the force of gravity

Fg = ball.mass * vec(0, -9.8)

If the ball's mass were to change (let's say we filled it with virtual cement!), the force of gravity would change accordingly, just as it would in the real world.

In order for the object to respond to the gravitational force we just need to once again, define the momentum principle in the

**Calculations Pane**. We then define a change in momentum in code like this:# Define a change in momentum due to gravity

dp = Fg * dt

The interval time is of course the amount of time that passes each step -

`dt`

. The small change in momentum, defined by `dp`

is simply the force vector multiplied by `dt`

, the time interval. Once again, we use `dt`

here because it scales the simulation to real time no matter what the step rate of our virtual universe is set to.Again, we define the change in velocity that the object will experience based on its mass property. We divide the change in momentum by the object's mass, like this:

# Calculate the change in velocity

dv = dp / ball.mass

Then the next step is to iteratively add

`dv`

, updating the velocity each step:# Update the ball's velocity

ball.v = ball.v + dv

Once again, it is important to remember that this line of code should be read like this:

**"The new value of the ball's velocity is assigned to the old ball's velocity plus a small change in the velocity that occurred in this step."**

We just use the same iterative calculation process that we have used before:

# Update the ball's position

ball.pos = ball.pos + ball.v * dt

Once again, this is assigning a new value for the position by adding a small amount of position change to the old position.

When you play the simulation, you should see the

`circle`

accelerate in the negative Y direction.You should once again create a motion graph to see if the virtual particle's motion is similar to a real object's motion based on our observations earlier in the year. Go ahead and create four graphs in the

**Initial State**pane, two for analyzing the X and Y positions, and then two for analyzing the X and Y velocities:# Define some graphs

g_xpos = graph(title="X Position vs Time", align="right")

g_ypos = graph(title="Y Position vs Time", align="left)

g_vx = graph(title="X Velocity vs Time", align="right")

g_vy = graph(title="Y Velocity vs Time", align="left")

Once you have created the graphs, go to the

**Calculations**pane and plot the points:# Plot the positions and velocities

g_xpos.plot(x=t, y=ball.pos.x, color="blue")

g_ypos.plot(x=t, y=ball.pos.y, color="green")

g_vx.plot(x=t, y=ball.v.x, color="blue")

g_vy.plot(x=t, y=ball.v.y, color="green")

Once you plot some points, make sure that graphs match your expectations...

Now that we have an object (represented by a

`circle`

) whose state of motion is only being affected by gravity, it is time to give the particle a non-zero velocity. Let's give it an initial velocity in the X direction. Go ahead and change the velocity to this:ball.v = vec(40, 0)

Then start the simulation again. What has happened to the behavior of the object's motion? I hope you see that the

`circle`

is moving along a curved path now...but why? It will be easier to see the dynamics involved here if we draw the `circle`

's velocity vector. Add this line of code to the **Initial**pane:a_vel = arrow(pos=ball.pos, size=ball.v, color="green", stroke=3, show_components=True)

The

`stroke`

attribute with the value 3 just makes the arrow a bit thicker so that we can see it more easily. The next attribute show_components displays the arrows x and y component values. When you play the simulation again you should see that the arrows for the vertical and horizontal velocities are behaving differently. Think about these questions:- Which one is changing, and which one isn't?
- Why are they behavng this way?
- Why does this cause the ball to move along the cirved path that it is moving along?

Now, let's change the vertical velocity component so that it is initially moving in the positive vertical dimension. Once again, change the line of code for defining the initial velocity of the object:

ball.v = vec(40, 60)

When you play the simulation again, you will see that it moves upward first this time, but eventually arcs downward. Look at the graphs now. Notice the shapes of the graphs.

This was a brief but important tutorial on how to simulate a projectile. Once again, we use the momentum principle and with just a few changes to a few lines of code, we can simulate a real object like a ball being thrown into the air. Here are the steps required to simulate a projectile

- 1.Define a gravitational force acting on an object - represented by a circle - as a vector of [0, -9.8] multiplied by the mass of the particle.
- 2.Define the change in momentum using the momentum principle and the fact that the duration of time is defined by
`dt`

. - 3.Give the object an initial velocity in the X and Y directions.
- 4.Update the velocity based on the change in velocity caused by the change in momentum.
- 5.Update the position using the updated velocity.