-
Notifications
You must be signed in to change notification settings - Fork 11
/
OrbitalArt.py
291 lines (222 loc) · 7.51 KB
/
OrbitalArt.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import pygame
from math import *
def maps(value,istart,istop,ostart,ostop):
return ostart+(ostop-ostart)*((value-istart)/(istop-istart))
def display(value,pos,words):
text = smallfont.render(words+str(value), True, white)
screen.blit(text, pos)
def pause():
paused = True
while paused:
target = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p: paused = False
elif event.key == pygame.K_ESPACE: pygame.quit()
# screen.fill(grey)
pygame.display.update()
clock.tick(5)
def limit(x,m):
if x > m:
if x != 0 and x != 1:
x /= x
x *= m
return x
class PVector:
def __init__(self,x,y):
self.x = x
self.y = y
def fromAngle(angle):
return PVector(cos(angle), sin(angle))
def getAngle(self):
return atan2(self.y, self.x)
def mag(self):
return sqrt(self.x*self.x + self.y*self.y)
def plus(self,v):
self.x += v.x
self.y += v.y
def sub(self,v):
self.x -= v.x
self.y -= v.y
def mult(self,n):
self.x *= n
self.y *= n
def div(self,n):
self.x /= n
self.y /= n
def dist(self,v):
dx = self.x - v.x
dy = self.y - v.y
return sqrt(dx*dx + dy*dy)
def normalize(self):
m = self.mag()
if m !=0 and m != 1:
self.div(m)
def limit(self,maxi):
if self.mag() > maxi:
self.normalize()
self.mult(maxi)
def setMag(self,n):
self.normalize()
self.mult(n)
def copy(self):
return PVector(self.x, self.y)
class Block:
def __init__(self, pos, m, vel = PVector(0,0), acc = PVector(0,0),c=(200,200,200)):
self.pos = pos
self.vel = vel
self.acc = acc
self.mass = pow(m/2, 4)
self.r = m*5
self.maxspeed = 5
self.air_res = 1
self.wall_res = 0.9
self.bounce_res = 1
self.colour = c
self.immobile = False
def run(self,w,h):
self.draw()
self.update()
# self.walls(w,h)
def draw(self):
pygame.draw.circle(screen, self.colour, (int(self.pos.x), int(self.pos.y)), self.r)
pygame.draw.circle(screen, black, (int(self.pos.x), int(self.pos.y)), self.r,2)
def update(self):
self.vel.plus(self.acc)
self.pos.plus(self.vel)
self.acc.mult(0)
def applyForce(self, f):
self.acc.plus(f)
def walls(self, w,h):
if (self.pos.x - self.r <= 0 or self.pos.x + self.r >= w):
self.vel.x *= -self.wall_res
elif (self.pos.y - self.r <= 0 or self.pos.y + self.r >= h):
self.vel.y *= -self.wall_res
def caculateVel(a,b):
v1 = a.vel.mag()
θ1 = atan2(a.vel.y,a.vel.x)
m1 = a.mass
v2 = b.vel.mag()
θ2 = atan2(b.vel.y,b.vel.x)
m2 = b.mass
cpx = ((a.pos.x * b.r) + (b.pos.x * a.r))/(a.r+b.r)
cpy = ((a.pos.y * b.r) + (b.pos.y * a.r))/(a.r+b.r)
ϕ = atan2(a.pos.y - cpy, a.pos.x - cpx)
vx = (((v1 * cos(θ1 - ϕ) * (m1 - m2)) + (2 * m2 * v2 * cos(θ2 - ϕ)))/(m1 + m2)) * cos(ϕ) + v1 * sin(θ1 - ϕ) * cos(ϕ + pi/2)
vy = (((v1 * cos(θ1 - ϕ) * (m1 - m2)) + (2 * m2 * v2 * cos(θ2 - ϕ)))/(m1 + m2)) * sin(ϕ) + v1 * sin(θ1 - ϕ) * sin(ϕ + pi/2)
return PVector(vx, vy)
def bounce(a,b):
d = a.pos.dist(b.pos)
a_new_vel = a.vel
b_new_vel = b.vel
if (d > (a.r + b.r)):
force = G * ((a.mass * b.mass)/d)
y,x = b.pos.y - a.pos.y, b.pos.x - a.pos.x
a_new_vel_x = a.vel.x + cos(atan2(y,x)) * force
a_new_vel_y = a.vel.y + sin(atan2(y,x)) * force
b_new_vel_x = b.vel.x + cos(atan2(-y,-x)) * force
b_new_vel_y = b.vel.y + sin(atan2(-y,-x)) * force
if not a.immobile: a_new_vel = PVector(a_new_vel_x, a_new_vel_y)
if not b.immobile: b_new_vel = PVector(b_new_vel_x, b_new_vel_y)
elif (d <= (a.r + b.r)):
if not a.immobile: a_new_vel = Block.caculateVel(a,b)
if not b.immobile: b_new_vel = Block.caculateVel(b,a)
a.vel = a_new_vel
b.vel = b_new_vel
pygame.init()
w,h = 400,400
screen = pygame.display.set_mode((w,h))
clock = pygame.time.Clock()
smallfont = pygame.font.SysFont("comicsansms", 10)
grey = [146,149,145]
white = [255,255,255]
red = [229,0,0]
green = [21, 176, 26]
blue = [3, 67, 223]
black = [0,0,0]
G = 1 #45# is gold
r = int(w/2 - 50)
p1 = PVector.fromAngle(0)
p1.mult(r)
p2 = PVector.fromAngle((2*pi)/3)
p2.mult(r)
p3 = PVector.fromAngle(-(2*pi)/3)
p3.mult(r)
v1 = PVector.fromAngle(0 - pi/2)
v2 = PVector.fromAngle((2*pi)/3 - pi/2)
v3 = PVector.fromAngle(-(2*pi)/3 - pi/2)
p1.plus(PVector(int(w/2),int(w/2)))
p2.plus(PVector(int(w/2),int(w/2)))
p3.plus(PVector(int(w/2),int(w/2)))
b1 = Block(p1,1,v1,c=red)
b2 = Block(p2,1,v2,c=green)
b4 = Block(p3,1,v3,c=blue)
b3 = Block(PVector(int(w/2),int(w/2)),4,c=black)
b3.immobile = True
rapid = False
n = 0
FPS_base = 0
FPS = 300
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: pygame.quit()
if event.key == pygame.K_p: pause()
if event.key == pygame.K_c:
rapid = True
if event.key == pygame.K_f: FPS = 0
if event.key == pygame.K_SPACE: # reset
r = int(w/2 - 50)
p1 = PVector.fromAngle(0)
p1.mult(r)
p2 = PVector.fromAngle((2*pi)/3)
p2.mult(r)
p3 = PVector.fromAngle(-(2*pi)/3)
p3.mult(r)
v1 = PVector.fromAngle(0 - pi/2)
v2 = PVector.fromAngle((2*pi)/3 - pi/2)
v3 = PVector.fromAngle(-(2*pi)/3 - pi/2)
p1.plus(PVector(int(w/2),int(w/2)))
p2.plus(PVector(int(w/2),int(w/2)))
p3.plus(PVector(int(w/2),int(w/2)))
b1 = Block(p1,1,v1,c=red)
b2 = Block(p2,1,v2,c=green)
b4 = Block(p3,1,v3,c=blue)
b3 = Block(PVector(int(w/2),int(w/2)),4,c=black)
b3.immobile = True
screen.fill(black)
if event.key == pygame.K_o:
print(G)
if event.key == pygame.K_UP:
FPS += 50
if event.key == pygame.K_DOWN:
FPS -= 50
if event.key == pygame.K_RIGHT: G += 1
if event.key == pygame.K_LEFT: G -= 1
if event.key == pygame.K_s: FPS = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_c:
rapid = False
if event.key == pygame.K_s: FPS = 300
# screen.fill(grey)
b1.run(w,h)
b2.run(w,h)
b3.run(w,h)
b4.run(w,h)
Block.bounce(b1,b2)
Block.bounce(b1,b3)
Block.bounce(b1,b4)
Block.bounce(b2,b3)
Block.bounce(b2,b4)
Block.bounce(b4,b3)
n += 1
clock.tick(FPS)
fps = round(clock.get_fps())
if rapid:
if n%500 == 0:
G += 1
pygame.display.update()