forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyCannon.py
106 lines (84 loc) · 3.12 KB
/
PyCannon.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# PyCannon.py
# A Physics Simulation
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from numpy import *
import sys
global velhoriz
global velvert
def init():
global velhoriz
global velvert
# White background
glClearColor(1.0, 1.0, 1.0, 1.0)
# Large range for long shots
gluOrtho2D(-200.0, 12000.0, -200.0, 5000.0)
angle = 0.3
muzzvel = 10.0
# Convert the degree angle to radians
radangle = (angle*3.1415926)/180
# Solve for horizontal and vertical initial velocities
velhoriz = muzzvel*cos(radangle)
velvert = muzzvel*sin(radangle)
# Print out the initial velocities in the console
print
print ("Horizontal Velocity (m/s) = "), velhoriz
print ("Vertical Velocity (m/s) = "), velvert
def plottrajectory():
global velvert
global velhoriz
# We can now calculate and change vvel
# While preserving the original velVert
vvel = velvert
hvel = velhoriz
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0.0, 0.0, 0.0)
# Draw some horizontal and vertical axis lines
glLineWidth(2.0)
glBegin(GL_LINES)
glVertex2f(0.0, 0.0)
glVertex2f(20000.0, 0.0)
glVertex2f(0.0, 0.0)
glVertex2f(0.0, 15000.0)
glEnd()
# Set the height of the cannon barrel
# Initalize variables for later use
height = 2.0
dtime = 0.01
dist = 0.0
maxheight = 0.0
totaltime = 0.0
# Plot the trajectory as long
# as the height is above the ground
while height > 0.0:
# Equations to calculate distance and
# Height for each unit of time.
dist = dist + hvel*dtime
vvel = vvel - 9.8*dtime
height = height + vvel*dtime
totaltime += dtime
# Find the max height.
if maxheight < height:
maxheight = height
# Plot the trajectory
glBegin(GL_POINTS)
glVertex2f(dist, height)
glEnd()
glFlush()
# Print the solutions. Not indented!
print
print("Distance traveled (m) = "), dist
print("Maximum altitude (m) = "), maxheight
print("Total Time = "), totaltime
def main():
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)
glutInitWindowPosition(50, 50)
glutInitWindowSize(800, 600)
glutInit(sys.argv)
glutCreateWindow("How Far Will It Go?")
glutDisplayFunc(plottrajectory)
init()
glutMainLoop()
main()
# End Program