Created
April 30, 2012 00:17
-
-
Save donalmacc/2554336 to your computer and use it in GitHub Desktop.
Simple projectiles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Donal Mac Carthy, simple projectiles | |
| import math | |
| #Calculates the distance in the specified direction at the given time | |
| def distattime(u, a, t): | |
| #A formula | |
| x = (u*t)+(0.5*a*t*t) | |
| #Ignore if the distance is negative | |
| if( x >= 0): | |
| return x | |
| else: | |
| return 0 | |
| #Gets the range of the projectile, with assumptions for simplfication | |
| def getRange(ux, uy): | |
| #Assumes no X-direction acceleration, and gravity is 10 | |
| #Find max height | |
| #Formula is sy = uy*t - 5 t*t | |
| #find t when sy = 0 | |
| #0 = uy*t-5*t*t | |
| #0 = t*(uy-5t) | |
| #t is 0(starting point) or t is uy/5 | |
| t = uy/5 | |
| #Formula in X direction is sx = ux*t + 1/2ax*t*t | |
| #Assume ax = 0, so | |
| sx = ux*t | |
| return sx | |
| #Gets the maximum height of the projectile, from assumptions above | |
| def getMaxHeight(uy): | |
| #using same formula as above, get time of flight t | |
| t = uy/5 | |
| #Note that this is when uy is 5. If everything is constant, then max height is at t/2 | |
| t/=2 | |
| #Use the formula! | |
| sy = uy*t - .5*10*t*t | |
| return sy | |
| #Initial Velocity to use | |
| u = 100 | |
| #Angle of projection | |
| alpha = 45 | |
| #Convert to radians | |
| alpha *=0.0174532925 | |
| #break up velocity into x and y components | |
| ux = u*math.sin(alpha) | |
| uy = u*math.cos(alpha) | |
| #Number of seconds to run simulation for | |
| t = 100 | |
| #Number of times to check per second | |
| res = 10.0 | |
| #Loop just prints out the time, and the height of the projectile at that time | |
| for i in range(0,t): | |
| print i/res, distattime(uy,-10,i/res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment