Posted By


bhart on 10/27/13

Tagged


Statistics


Viewed 166 times
Favorited by 1 user(s)

Projectile Motion


/ Published in: Python
Save to your folder(s)

see projectiles


Copy this code and paste it in your HTML
  1. from __future__ import division
  2. from visual import *
  3. '''
  4. Brendan Hart
  5.  
  6. Projectile Motion
  7.  
  8. 9/30/13
  9. '''
  10.  
  11. #define variables
  12.  
  13. ag = -9.8
  14. # gravity
  15. t0 = 0
  16. # time
  17. dt0 = 0.01
  18. # change in time
  19. t1 = 0
  20. # second clock
  21. dt1 = 0.01
  22. # second change in time
  23.  
  24. ivelocity = float(raw_input("What is the intial velocity (m/s): "))
  25. # demand user input for initial velocity
  26. while ivelocity < 0:
  27. print'Give another value for the initial velocity'
  28. ivelocity = float(raw_input("What is the intial velocity (m/s): "))
  29. # check to see if valid velocity
  30.  
  31. angle = float(raw_input("What is the angle to launch the projectile (degrees): "))
  32. while angle > 90 or angle < 0:
  33. print'Doh! The projectile is not supposed to go off that side of the cliff'
  34. angle = int(raw_input("What is the angle to launch the projectile: "))
  35. # check to see if valid angle
  36.  
  37. angle = math.radians(angle)
  38. # convert degrees to radians
  39.  
  40. ivelocityx = ivelocity*math.cos(angle)
  41. print'The initial x velocity is: ', ivelocityx
  42. # inital x velocity in w/ angle radians
  43. ivelocityy = ivelocity*math.sin(angle)
  44. print'The initial y velocity is: ', ivelocityy
  45. # intial y velocity in y/angle radians
  46.  
  47. def ballislife():
  48. trail = raw_input('Do you want a trail (Yes/No): ')
  49. if trail.upper() == 'Y' or 'YES':
  50. trail = True
  51. return trail
  52. elif trail.upper() == 'N' or 'NO':
  53. trail = False
  54. return trail
  55. else:
  56. print'Really! You did not enter the proper input (Yes or No)'
  57. ballislife()
  58. # make trail optional
  59.  
  60. tt = ballislife()
  61. #call the function and assign return value to a variable
  62.  
  63. projectile = sphere(pos=(0,52.5,0), radius=2.5, make_trail = tt, material = materials.blazed)
  64. cliff = box(pos=(0,25,0), length=20, height=50, width=25, material = materials.shiny)
  65. earth = box(pos=(0,-2.5,0), length=30, height=5, width=25, material = materials.earth)
  66. # e.length is depdent on ivelocity and velcoity of ball
  67. # e.xposition depdent on length
  68.  
  69.  
  70. while true:
  71. rate(100)
  72. if projectile.pos.x <= 7.5:
  73. t0 = t0 + dt0
  74. l = ivelocityx*t0
  75. projectile.pos= vector(l, 52.5, 0)
  76. #Ball rolls on the cliff
  77. else:
  78. t1 = t1 + dt1
  79. # increment time
  80. h = 52.5 + ivelocityy*t1 + 0.5*ag*t1**2
  81. # height position of ball
  82. l = 10 + ivelocityx*t1
  83. # x position of the ball
  84. fvelocityy = ivelocityy + t1*ag
  85. projectile.pos = vector(l,h,0)
  86. earth.pos = vector(l,-2.5,0)
  87. fvelocity = math.sqrt(((fvelocityy - ivelocityy)**2) + (ivelocityx**2))
  88. if h <= 2.5:
  89. print'The final velocity is: ', fvelocity
  90. print'The final y velocity is: ', fvelocityy
  91. print'The final x velocity is: ', ivelocityx
  92. print'The time the projectile traveled: ', t1, 's'
  93. print'The distance the ball is from the cliff is: ', l - 7.5, 'm'
  94. break
  95. # projectile motion

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.