Gravity - Projectile motion

· Read in about 7 min · (1300 words)

Gravity - Projectile motion

In this article, with no professional goal, but only to share my hobby regarding the astrophysics - the study of space and laws physics - I will share my program C for simulating the launch of a projectile on a planet. I am not an expert in this field, so, if I write something wrong, please contact me, and I will change my mistake.

For simulating the motion, the program use two physics laws: the gravitation and the projectile motion, and we will see how it is works.

Executing the program

This simulator can be accessible to my Gitlab project: https://gitlab.com/gbucchino/astrophysics.

Before to execute the program, we need to analyze the gravity.yml file which contains all datas of the celestial object, in our case the Earth) and the projectile:

---
Constantes:
  grav: 6.673e-11

Projectile:
  x: 0            	 # Position in x
  y: 0            	 # Position in y
  v0: 10.0           # Initial speed in meter/s
  angle: 30          # Angle in degree
  masse: 100         # Masse of projectile
  height: 20    	   # Height
  width: 20		       # Width

Planet1:
  rayon: 6.371e6     # Rayon earth in meter -> 6371.00 km
  masse: 5.972e24    # Masse of earth, moon, etc... in kg

We will describe these datas in the next section, but, if you change these datas, you will see, the projectile will move differently.

You can execute the program with the executable file main. This program take two arguments: -p and the config file in YAML:

./main -p gravity.yml

After that, you should have this window:

Untitled

For launching the projectile, press the Enter key.

A bit of maths

Calculation of the gravity

When we throw an object, this object always go down to the ground, because a force attract it. This force is the gravity. The gravity is defined by the Newton’s Law and can be calculated with this equation:

$$ g = G\frac{m_{p}}{r^{2}} $$

So, G is the gravitational constant, \(m_{p}\) is the mass of the planet, in our case it’s the Earth, and \(r^{2}\) is the rayon of the planet.

The program calcul this force and it’s defined in the file gravity.c:

double calcul_gravity_object(const double grav, const double masse, const double r){
	// Newton's law for gravitation
	return grav * (masse / pow(r, 2));
}

These data are defined in the gravity.yml file. You have the gravitational constant:

Constantes:
  grav: 6.673e-11

And the informations regarding the planet. In our case, I use the data of the Earth:

Planet1:
  rayon: 6.371e6     # Rayon earth in meter -> 6371.00 km
  masse: 5.972e24    # Masse of earth, moon, etc... in kg

If we changed these data, you can see the motion of the projectile change. For instance, if we want to throw an object to the moon, use these datas:

Planet1:
  rayon: 1.737e6
  masse: 7.342e22

You can change differents values and to see what happens. Unfortunately, the program can’t handle when a projectile is out of the window, so, we can not see what happens and the distance. I will try to do in the next version of this application a zoom system.

Projectile motion

For determining the motion of the projectile, we need to do some equations:

  • Time of the flight
  • Total distance
  • Maximum height during the journey

Time of the flight

First, we need to determine the initial speed. For doing that, we have this equation:

$$ t = \frac{2_{v0}sin(\alpha)}{g} $$

Here, \(v_{0}\) is the initial speed of the projectile and \(\alpha\) is the initial launch angle and \(g\) is the gravity force. The program use this equation for determining the time of the flight. This equations is located in the motion.c file:

/*
 * Here, we calculate the initial speed
 * Where alpha is the initial launch angle
 * speed is the initial speed
 * and c defined if we calculate for x or y
 */
double calculate_initial_speed(double alpha, double speed, const char c) {
  // We calculated the v0 of x
  if (c == 'x')
    return speed * cos(alpha);
  // Otherwise, we calculated the v0 of y
  else
    return speed * sin(alpha);
}
/*
 * This function calcul the during of the time before the projectile have reach the ground
 * Where v is the initial speed of xor y
 * and grav is the gravitational force
 */
 double calculate_delta_t(double v, double grav){
   return (2 * v) / grav;
 }

First, we have the function calculate_initial_speed which calculate \(v_{0}sin(\alpha)\) to find the value of \(v_{0y}\).

And the function calculate_delta_t which calculate:

$$ t = \frac{2v_{0y}}{g} $$

In the file gravity.yml, you can modify these values:

Projectile:
  x: 0            	 # Position in x
  y: 0            	 # Position in y
  v0: 10.0           # Initial speed in meter/s
  angle: 30          # Angle in degree
  masse: 100         # Masse of projectile
  height: 20    	   # Height
  width: 20		       # Width

If you change the value of \(v_{0}\) and \(angle\), the projectile motion will change.

Total distance

For determining the total distance before the projectile reach the ground, we have two methods.

Method 1:

The equation of the first method is this one:

$$ d = \frac{2v_{0}^{2}sin(\alpha)cos(\alpha)}{g} $$

Where, \(v_{0}\) is the initial speed and \(\alpha\) is the initial launch angle. For instance, if we throw the projectile at 10m/s with an angle of 30°:

$$ d = \frac{2 * 10^{2}sin(30)cos(30)}{9.81} = 8.827m $$

Method 2:

The equation of the second method and use by the program is:

$$ d = v_{0x} \Delta t $$

Where \(v_{0x}\) is the initial speed and \(\Delta t\) is the total time of the flight. For instance, with a projectile launch at 10m/s with an angle at 30°:

$$ \Delta t = \frac{2 *10 sin(30)}{9.81} = 1.019 seconds $$

Now, we have \(\Delta t\), we need to find \(v_{x}\):

$$ v_{x} = 10 cos(30) = 8.66 m/s $$

$$ d = v_{x} \Delta t $$

$$ d = 8.66*1.019 = 8.827 m $$

In the program, the function below in the file motion.c do the second equation:

/*
 * This function calcul the distance total of the projectile
 * Where v is the initial speed of Vx
 * and deltat is the result total time of flight
 */
 double calculate_total_distance(double v, double deltat){
   return v * deltat;
 }

The variable v is calculated in the function calculate_initial_speed

Maximum height

For founding the maximum height of the projectile, we have two equations.

Equation 1:

The first equation is this one:

$$ h = \frac{v_{0x}^{2}sin(\alpha)^{2}}{2g} $$

Where \(v_{0x}\) is the initial speed and \(\alpha\) is the initial launch angle. For instance, if we throw a projectile at 10m/s with an angle at 30°:

$$ h = \frac{10^{2} sin(30)^{2}}{2 * 9.81} = 1.27m $$

Equation 2:

The second equation is use by the program:

$$ h = \frac{v_{x}^{2} - v_{0y}^{2}}{2 * -g} $$

Where \(v_{x}\) is the speed of the \(x\), but this value is at 0, because it the highest point in the curve, \(v_{0x}\) is the initial speed launch and \(g\) the gravitational force.

If we take the same value, the result is:

$$ h = \frac{0^{2} - 10^{2}}{2 * - 9.81} = 1.27m $$

In the file motion.c, we have this function for calculating the maximum distance of the projectile:

/*
  * This function calcul the height max of the Projectile
  * Where vx and xy are the initial speeds
  * and grav is the gravitational force
  */
double calculate_height_max(double vx, double vy, double grav){
    return (pow(vx, 2) - pow(vy, 2)) / (2 * -grav);
}

I hope you enjoy like me to understand how we calculating the projectile motion. I use basics equation, with no air resistance, and one day I will improve the program for taking some resistance.

This program will also evolve, because I would like to add new functionalities for discovering the astrophysics and the study of this field, because it is one of my hobby.