-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygras.py
executable file
·120 lines (96 loc) · 3.11 KB
/
pygras.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
import sys
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from random import random
from math import sqrt
_running = 1
_fullscreen = 0
class Star:
def __init__(self):
# position
self.pos = (0.0, 0.0, 0.0)
# velocity
self.v = (0.0, 0.0, 0.0)
# mass
self.m = 0.02 # * pow(10, 11)
def handleKeypress(key, x, y):
global _running, _fullscreen
if ord(key) == 27:
sys.exit()
elif ord(key) == 32:
_running ^= 1
update(0)
elif key == 'f':
if _fullscreen:
glutReshapeWindow(700, 700)
else:
glutFullScreen()
_fullscreen ^= 1
def handleResize(w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, round(float(w)/float(h)), 1.0, 200.0)
def initRendering():
glEnable(GL_DEPTH_TEST)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_NORMALIZE)
def drawScene():
global stars
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0.0, 0.0, -50.0)
for star in stars:
glPushMatrix()
glTranslatef(star.pos[0], star.pos[1], star.pos[2])
glutSolidSphere(0.1, 100, 100)
glPopMatrix()
glutSwapBuffers()
def update(value):
global stars, g
for bstar in stars:
for star in stars:
if bstar != star:
#if abs(star.pos[0] - bstar.pos[0]) < 0.5 and abs(star.pos[1] - bstar.pos[1]) < 0.5 and abs(star.pos[2] - bstar.pos[2]) < 0.5:
# stars.remove(star)
# continue
d = sqrt(pow(star.pos[0] - bstar.pos[0], 2) + pow(star.pos[1] - bstar.pos[1], 2) + pow(star.pos[2] - bstar.pos[2], 2))
i = ( (star.pos[0] - bstar.pos[0]) / d, (star.pos[1] - bstar.pos[1]) / d, (star.pos[2] - bstar.pos[2]) / d )
f = g * ((bstar.m * star.m) / (d*d))
f = (f * i[0], f * i[1], f * i[2])
a = (f[0] / bstar.m / 4, f[1] / bstar.m / 4, f[2] / bstar.m / 4)
bstar.v = (bstar.v[0] + a[0], bstar.v[1] + a[1], bstar.v[2] + a[2])
glutPostRedisplay()
for star in stars:
star.pos = (star.pos[0] + star.v[0], star.pos[1] + star.v[1], star.pos[2] + star.v[2])
if _running:
glutTimerFunc(25, update, 0)
if __name__ == '__main__':
g = 6.67428 # * pow(10, -11)
stars = []
star = Star()
star.pos = (5.0, 0.0, 0.0)
star.v = (0.0, 0.1, 0.0)
stars.append(star)
star = Star()
star.pos = (-5.0, 0.0, 0.0)
star.v = (0.0, 0.0, 0.0)
stars.append(star)
star = Star()
star.pos = (0.0, 0.0, 0.0)
star.v = (0.0, -0.1, 0.0)
stars.append(star)
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(700, 700)
glutCreateWindow("PyGraS - v0.00001 =)")
glutSetCursor(GLUT_CURSOR_NONE)
initRendering()
glutDisplayFunc(drawScene)
glutKeyboardFunc(handleKeypress)
glutReshapeFunc(handleResize)
glutTimerFunc(25, update, 0)
glutMainLoop()