# Projectile Motion (Python)

This example calculates timed x, y points of a projectile motion that can be used for plotting or other calculations.

ddanbe commented: Great! +15
4,525 Views
About the Author

Scientist

``````''' projectile_motion.py
projectile motion equations:
height = y(t) = hs + (t * v * sin(a)) - (g * t*t)/2
distance = x(t) = v * cos(a) * t
where:
t is the time in seconds
v is the muzzle velocity of the projectile (meters/second)
a is the firing angle with repsect to ground (radians)
hs is starting height with respect to ground (meters)
g is the gravitational pull (meters/second_square)

tested with Python27/Python33  by  vegaseat  20mar2013
'''

import math

def projectile_xy(v, a, hs=0.0, g=9.8):
'''
calculate a list of (x, y) projectile motion data points
where:
x axis is distance (or range) in meters
y axis is height in meters
v is muzzle velocity of the projectile (meter/second)
a is the firing angle with repsect to ground (radians)
hs is starting height with respect to ground (meters)
g is the gravitational pull (meters/second_square)
'''
data_xy = []
t = 0.0
while True:
# now calculate the height y
y = hs + (t * v * math.sin(a)) - (g * t * t)/2
# projectile has hit ground level
if y < 0:
break
# calculate the distance x
x = v * math.cos(a) * t
# append the (x, y) tuple to the list
data_xy.append((x, y))
# use the time in increments of 0.1 seconds
t += 0.1
return data_xy

# use a firing angle of 45 degrees
d = 45
a = math.radians(d)  # radians
# muzzle velocity of the projectile (meters/second)
v = 100
data_45 = projectile_xy(v, a)

# find maximum height ...
point_height_max = max(data_45, key = lambda q: q)
xm, ym = point_height_max
print('''
Projectile Motion ...
Using a firing angle of {} degrees
and a muzzle velocity of {} meters/second
the maximum height is {:0.1f} meters
at a distance of {:0.1f} meters,'''.format(d, v, ym, xm))

# find maximum distance ...
x_max = max(data_45)
print("the maximum distance is {:0.1f} meters.".format(x_max))

''' result ...
Projectile Motion ...
Using a firing angle of 45 degrees
and a muzzle velocity of 100 meters/second
the maximum height is 255.1 meters
at a distance of 509.1 meters,
the maximum distance is 1018.2 meters.
'''``````

In the position ecuation I added a variable xs as the starting position in the x axis. However, when I run the program it can start wherever I want in the y axis, but in the x axis nothing accurs. I don't understand why. Any Ideas? Thanks!

vegaseat 1,735

You got to show your code!

ddanbe 2,724

Nice code! I did something resembling a little bit like your code, but in C#, some years ago. Click Here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.