# Python Tutorial 8: Modeling A Projectile

## Tutorial Objectives and Overview

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:

or in code:

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.

## Simulating Surface Gravity

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

Python Tutorial 8 - Modeling A Projectile

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:

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:

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:

To define this force in code, we just multiply the ball's mass by the vector [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:

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:

Then the next step is to iteratively add `dv`

, updating the velocity each step:

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:

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.

## Analyze The Motion

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:

Once you have created the graphs, go to the **Calculations** pane and plot the points:

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

## Curved Path Motion

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:

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:

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:

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.

## Conclusion

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

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.

Define the change in momentum using the momentum principle and the fact that the duration of time is defined by

`dt`

.Give the object an initial velocity in the X and Y directions.

Update the velocity based on the change in velocity caused by the change in momentum.

Update the position using the updated velocity.

Last updated