Skip to content

Instantly share code, notes, and snippets.

@imrehg
Created July 13, 2011 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imrehg/1079927 to your computer and use it in GitHub Desktop.
Save imrehg/1079927 to your computer and use it in GitHub Desktop.
Angry birds trajectory
"""
Angry birds trajectory calculation and plotting
"""
import numpy as np
import pylab as pl
#### Input parameters
Y = 2
X = 55
g = 10
tmax = 2.5
####
def trajectory(v, a, g, tf):
"""
Return a trajectory with these parameters
v: starting velocity
a: starting angle (radian)
g: the gravitational constant
tf: final time
"""
t = np.linspace(0, tf, 100)
x = v * np.cos(a) * t
y = v * np.sin(a) * t - 0.5 * g * t**2
return x, y
## Calculate and plot things
alpha = np.arctan((Y + 0.5*g*tmax**2)/X)
v0 = X/(tmax * np.cos(alpha))
print "Starting angle: %f rad / %f deg" %(alpha, alpha/np.pi*180)
print "Starting velocity: %f m/s" %(v0)
xpos, ypos = trajectory(v0, alpha, g, tmax)
pl.plot(X, Y, 'go', markersize=10, label="Pig") # Target to hit
pl.plot(xpos, ypos, 'r-', linewidth=2, label="An angry bird") # trajectory
pl.legend(loc='best', numpoints=1)
pl.xlabel('Distance (m)')
pl.ylabel('Height (m)')
pl.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment