-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcannonballsimulator.py
More file actions
44 lines (35 loc) · 1.33 KB
/
cannonballsimulator.py
File metadata and controls
44 lines (35 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#cannonball simulator - TopDown programming
#Neemias Moreira
from math import sin, cos, radians
class Projectile:
def __init__(self, angle, velocity, height):
self.xpos = 0.0
self.ypos = height
theta = radians(angle)
self.xvel = velocity * cos(theta)
self.yvel = velocity * sin(theta)
def update(self, time):
self.xpos = self.xpos + time * self.xvel
yvel1 = self.yvel - 9.8 * time
self.ypos = self.ypos + time * (self.yvel + yvel1) / 2.0
self.yvel = yvel1
def getY(self):
return self.ypos
def getX(self):
return self.xpos
def getInputs():
a = float(input("Enter the launch angle: "))
v = float(input("Enter the initial velocity: "))
h = float(input("Enter the initial height: "))
t = float(input("Enter the time interval between position calculations: "))
return a, v, h, t
def main():
print("This is a simulator of cannonball.")
print ("The result it's the total distance traveled by the ball in meters.")
angle, vel, hO, time = getInputs()
cball = Projectile(angle, vel, hO)
while cball.getY() >= 0:
cball.update(time)
print("\nDistance traveled: {0:.2f} meters.".format(cball.getX()))
input ("Press the <enter> key to quit.")
main()