-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene_objs.py
More file actions
executable file
·139 lines (120 loc) · 4.83 KB
/
scene_objs.py
File metadata and controls
executable file
·139 lines (120 loc) · 4.83 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# import sys
from random import *
# import OpenGL
import OpenGL.GL as GL
from vec3 import *
import config
class geom_obj:
def __init__(self, color):
self.color = color
def update(self, frametime):
pass
class point(geom_obj):
def __init__(self, pos, velocity, acceleration, color):
geom_obj.__init__(self, color)
self.pos = pos
self.velocity = velocity
self.acceleration = acceleration
def draw(self):
GL.glColor3f(self.color.x, self.color.y, self.color.z)
GL.glBegin(GL.GL_POINTS)
GL.glVertex3f(self.pos.x, self.pos.y, self.pos.z)
GL.glEnd()
def update(self, frametime):
# need a method to update acceleration OUTSIDE this object
# print(self.acceleration)
self.velocity = self.acceleration * frametime + self.velocity
self.pos = self.pos + self.velocity * frametime
class physical_point(point):
def __init__(self, pos, velocity, acceleration, color):
point.__init__(self, pos, velocity, acceleration, color)
def update(self, frametime):
self.collide()
self.acceleration = self.acceleration + vec3(0,-3,0)
self.velocity = self.acceleration * frametime + self.velocity
self.pos = self.pos + self.velocity * frametime
def collide(self): # collisions with wall
if self.pos.x > config.width:
self.velocity.x = -1 * abs(self.velocity.x)
if self.pos.x < 0:
self.velocity.x = abs(self.velocity.x)
if self.pos.y > config.height:
self.velocity.y = -1 * abs(self.velocity.y)
if self.pos.y < 0:
self.velocity.y = abs(self.velocity.y)
class triangle(geom_obj):
def __init__(self, a, b, c, velocity, color):
geom_obj.__init__(self, color)
self.a = a
self.b = b
self.c = c
self.velocity = velocity
def draw(self):
GL.glColor3f(self.color.x, self.color.y, self.color.z)
GL.glBegin(GL.GL_TRIANGLES)
GL.glVertex3f(self.a.x, self.a.y, self.a.z)
GL.glVertex3f(self.b.x, self.b.y, self.b.z)
GL.glVertex3f(self.c.x, self.c.y, self.c.z)
GL.glEnd()
def update(self, frametime):
self.a = self.a + self.velocity * frametime
self.b = self.b + self.velocity * frametime
self.c = self.c + self.velocity * frametime
# supports wall collisions
class square(geom_obj):
def __init__(self, pos, side_length, velocity, color):
geom_obj.__init__(self, color)
self.pos = pos # lower left corner
self.side_length = side_length
# self.center = vec3(self.pos.x + self.side_length / 2, self.pos.y + self.side_length / 2, self.pos.z)
self.velocity = velocity
# self.color = color
def draw(self):
GL.glColor3f(self.color.x, self.color.y, self.color.z) # Set the color
# We have to declare the points in this sequence: bottom left, bottom right, top right, top left
GL.glBegin(GL.GL_QUADS) # Begin the sketch
GL.glVertex3f(self.pos.x, self.pos.y, self.pos.z) # Coordinates for the bottom left point
GL.glVertex3f(self.pos.x + self.side_length, self.pos.y, self.pos.z) # Coordinates for the bottom right point
GL.glVertex3f(self.pos.x + self.side_length, self.pos.y + self.side_length, self.pos.z) # Coordinates for the top right point
GL.glVertex3f(self.pos.x, self.pos.y + self.side_length, self.pos.z) # Coordinates for the top left point
GL.glEnd() # Mark the end of drawing
def update(self, frametime):
self.collide()
self.pos = self.pos + self.velocity * frametime
def collide(self): # collisions with wall
if self.pos.x + self.side_length > config.width:
self.velocity.x = -1 * abs(self.velocity.x)
if self.pos.x < 0:
self.velocity.x = abs(self.velocity.x)
if self.pos.y + self.side_length > config.height:
self.velocity.y = -1 * abs(self.velocity.y)
if self.pos.y < 0:
self.velocity.y = abs(self.velocity.y)
def make_squares(how_many, max_speed = 700):
squares = []
for i in range(0, how_many):
side_length = 100
position = vec3(random() * (config.width - side_length), random() * (config.height - side_length), 0)
velocity = vec3(((2 * random()) - 1) * max_speed, ((2 * random()) - 1) * max_speed, 0)
color = vec3(random(), random(), random())
squares.append(square(position, side_length, velocity, color))
return squares
def make_points(how_many, max_speed = 300, max_h = config.height, max_w = config.width, min_h = 0, min_w = 0):
points = []
for i in range(0, how_many):
pos = vec3(random() * (max_w - min_w) + min_w, random() * (max_h - min_h) + min_h, 0)
velocity = vec3(((2 * random()) - 1) * max_speed, ((2 * random()) - 1) * max_speed, 0)
acceleration = vec3()
color = vec3(1,1,1)
# color = vec3(random(), random(), random())
points.append(point(pos, velocity, acceleration, color))
return points
def make_physical_points(how_many, max_speed = 300):
points = []
for i in range(0, how_many):
pos = vec3(random() * config.width, random() * config.height, 0)
velocity = vec3(((2 * random()) - 1) * max_speed, ((2 * random()) - 1) * max_speed, 0)
acceleration = vec3()
color = vec3(1,1,1)
points.append(physical_point(pos, velocity, acceleration, color))
return points