-
Notifications
You must be signed in to change notification settings - Fork 0
/
drops.py
187 lines (164 loc) · 6.79 KB
/
drops.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import pymunk
import pygame
import time
import random
from random import randint
import sys
import hsluv
import contouring
import cv2
import numpy as np
class ShadowSpace(object):
"""Wrapper for the shadow simulation in pymunk and pygame"""
def __init__(self, windowed=False):
#start the pygame window
pygame.init()
pygame.display.init()
if windowed:
self.width = 1500
self.height = 900
self.surface = pygame.display.set_mode((self.width, self.height))
else:
self.surface = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
self.width = self.surface.get_width()
self.height = self.surface.get_height()
self.surface.fill((255, 255, 255))
#make the pymunk space
self.space = pymunk.Space()
self.space.gravity = 0, -800
#add balls to the space
self.balls = self.init_balls(40)
#option to add ground to the space
#self.ground = Ground(self.width/2, 450,10, self.width)
#self.ground.add(self.space)
#create the contour-processing object
self.contours = contouring.Contour(self.space, int(sys.argv[1]), self.height)
#option to use openCV to debug
#cv2.namedWindow('contours_img')
def init_balls(self, n):
"""create the rain of balls at the beginning of the simulation"""
ball_list = []
self.color_list = pretty_colors(n)
for i in range(n):
ball = Ball(10, i*self.width/float(n), -30, self.color_list[i], 10)
ball.add(self.space)
ball_list.append(ball)
#hugeball = Ball(50, 100, -400, color_list[0], 10)
#hugeball.add(self.space)
#ball_list.append(hugeball)
return ball_list
def update(self):
"""step forward the physics simulation and the pygame visualization"""
#remove all previous pygame drawings
self.surface.fill((255, 255, 255))
#update what shadows are being seen
tuple_contours = self.contours.update_contours()
self.create_shadows(tuple_contours)
#step the pymunk simulation forward
self.space.step(.02)
self.space.reindex_static()
if randint(0,10) == 3:
self.random_ball()
#draw all the new ball positions, but remove them from the list when they fall offscreen
balls_exist = len(self.balls)
i = 0
while balls_exist:
if abs(self.balls[i].y) > self.height or self.balls[i].x < 0 or self.balls[i].x > self.width:
del self.balls[i]
else:
self.balls[i].draw(self.surface)
i += 1
if i >= len(self.balls):
balls_exist = False
#print self.contours.img
#cv2.imshow('img', self.contours.debug_img)
#cv2.imshow('contours_img', self.contours.contours_img)
#cv2.waitKey(5000)
pygame.display.update()
#print self.space.shapes
def random_ball(self):
"""drop a randomly placed ball from the top of the simulation"""
x = randint(0,30)*self.width/float(randint(1,30))
ball = Ball(10, x, 0, random.choice(self.color_list), 10)
ball.add(self.space)
self.balls.append(ball)
def create_shadows(self, contours):
"""use the countours to add an updated shadow shape to the simulation"""
self.remove_shadows()
#print len(contours)
for cont in contours:
body = pymunk.Body(0,0,pymunk.Body.STATIC)
contour_shape = pymunk.Poly(body, cont)
#print('vertices')
#print(contour_shape.get_vertices())
self.space.add(body, contour_shape)
self.space.reindex_shape(contour_shape)
self.space.reindex_shapes_for_body(body)
def remove_shadows(self):
"""remove all shadows left from the previous step in simulation"""
for body in self.space.bodies:
if body.body_type == pymunk.Body.STATIC:
self.space.remove(body)
for shape in body.shapes: #pymunk can assign multiple shapes to a body. there shouldn't be multiple, but just in case
self.space.remove(shape)
class Ball(object):
"""it's a ball. it's got image and physics attributes"""
def __init__(self, radius=10, x=0, y=0, color=(0,0,0), mass=1):
self.radius = radius
self.x = x
self.y = y
self.color = color
self.mass = mass
self.moment = pymunk.moment_for_circle(mass, 0, radius)
self.body = pymunk.Body(self.mass, self.moment)
self.shape = pymunk.Circle(self.body, self.radius)
self.shape.elasticity = 0.999
self.body.position = self.x, self.y
def add(self, space):
"""put the ball in the simulation"""
space.add(self.body, self.shape)
def draw(self, surface):
"""make a pygame drawing of the ball"""
self.x, self.y = self.body.position
pygame.draw.circle(surface, self.color, (int(self.x), abs(int(self.y))), self.radius)
class Ground(object):
"""optional class for putting a ground in the simulation, not used in normal operation"""
def __init__(self, x=0, y=0, mass=0, width=0):
self.x = 0
self.y = y-10
self.mass = mass
self.width = width
self.color = (150,200,50)
self.body = pymunk.Body(body_type = pymunk.Body.STATIC)
self.body.elasticity = 0.999
self.body.position = (self.x,-self.y)
self.shape = pymunk.Segment(self.body, (self.x, -self.y), (self.width, -self.y), 10)
def add(self, space):
space.add(self.body, self.shape)
def draw(self, surface):
pygame.draw.rect(surface, self.color, (int(self.x), abs(int(self.y)), self.width, 10),0)
def pretty_colors(n, lightness=65, saturation=65):
"""returns a list of random colors (rbg tuples) with length n that are all similar hues with the same lightness and saturation according to hsluv standard"""
color_list = []
starting_hue = random.randint(0, 360)
for j in range(n):
hue = (starting_hue+j*100/float(n))%360
red, green, blue = hsluv.hsluv_to_rgb(([hue, saturation, lightness]))
color_list.append((int(red*255), int(green*255), int(blue*255)))
random.shuffle(color_list)
return color_list
if __name__ == '__main__':
testSpace = ShadowSpace(False)
running = True
while running:
testSpace.update()
time.sleep(.03)
cv2.waitKey(30)
current_event = pygame.event.poll()
if current_event.type == pygame.QUIT:
running = False
testSpace.contours.kill_video()
elif current_event.type == pygame.KEYDOWN and current_event.key == pygame.K_ESCAPE:
running = False
testSpace.contours.kill_video()
print 'QUIT!'