-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelastic_ball_gravity.pyw
165 lines (135 loc) · 4.13 KB
/
elastic_ball_gravity.pyw
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
import pygame
import sys
import random
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
win = pygame.display.set_mode((1280, 720))
pygame.font.init()
my_font = pygame.font.SysFont('Arial', 20)
class Vec2:
def __init__(self, x, y):
self.x = x
self.y = y
def len(self):
return (self.x*self.x+self.y*self.y)**0.5
def __add__(self, o):
return Vec2(self.x+o.x, self.y+o.y)
def __sub__(self, o):
return Vec2(self.x-o.x, self.y-o.y)
def __truediv__(self, o):
return Vec2(self.x/o, self.y/o)
def __mul__(self, o):
return Vec2(self.x*o, self.y*o)
__rmul__ = __mul__
def dot(self, o):
return self.x*o.x+self.y*o.y
# velocity verlet
# p'=p+v*dt+0.5*a*dt*dt
# v'=v+0.5*(a+a')*dt
class Ball:
def __init__(self, r, m, p0, v0, color):
self.r = r
self.m = m
self.p = p0
self.v = v0
self.a = self.olda = Vec2(0, 0)
self.color = color
def render(self):
pygame.draw.circle(win, self.color, (self.p.x, self.p.y), self.r)
def updatePos(self, dt):
self.p += self.v*dt+0.5*self.a*dt*dt
self.olda = self.a
self.a = Vec2(0, 0)
def updateVelocity(self, dt):
self.v += 0.5*(self.olda+self.a)*dt
def applyGravity():
for obj1 in objs:
for obj2 in objs:
if obj1 == obj2:
continue
r = obj2.p-obj1.p
d = r.len()
obj1.a += G*obj2.m*r/(d**3)
def solveCollision():
for i in range(len(objs)):
for j in range(i+1, len(objs)):
obj1 = objs[i]
obj2 = objs[j]
delta = obj1.p-obj2.p
r = delta.len()
mind = obj1.r+obj2.r
delta /= r
if r < mind:
obj1.p += 0.5*delta*(mind-r)
obj2.p -= 0.5*delta*(mind-r)
(v1, v2) = (obj1.v, obj2.v)
v1L = v1.dot(delta)*delta
v2L = v2.dot(delta)*delta
v1P = v1-v1L
v2P = v2-v2L
(m1, m2) = (obj1.m, obj2.m)
vcm = (m1*v1L+m2*v2L)/(m1+m2)
(obj1.v, obj2.v) = (2*vcm-v1L+v1P, 2*vcm-v2L+v2P)
def update(dt):
for obj in objs:
obj.updatePos(dt)
applyGravity()
for obj in objs:
obj.updateVelocity(dt)
solveCollision()
def render():
win.fill((0, 0, 0))
tx, ty, tm = 0, 0, 0
for obj in objs:
obj.render()
tx += obj.p.x * obj.m
ty += obj.p.y * obj.m
tm += obj.m
pygame.draw.circle(win, (255,0,0), (tx/tm, ty/tm), 3)
objs = []
nsub = 40
dt = 0.1/nsub
G = 1000
objs.append(Ball(10,100,Vec2(640,320),Vec2(25,0),(0,0,255)))
objs.append(Ball(10,100,Vec2(640,400),Vec2(-25,0),(0,0,255)))
displayE = 0
Es = []
def print_energy():
global displayE, Es
E = 0
for obj in objs:
E += 0.5 * obj.m * (obj.v.x*obj.v.x+obj.v.y*obj.v.y)
for obj1 in objs:
for obj2 in objs:
if obj1 == obj2:
continue
r = obj2.p-obj1.p
d = r.len()
E += G*obj1.m*obj2.m*(1/20-1/d)
Es.append(E)
if len(Es) > 100: del Es[0]
displayE = round(sum(Es)/100, 3)
text_surface = my_font.render('E '+str(displayE), True, (255, 255, 255))
win.blit(text_surface, (0, 0))
text_surface = my_font.render('# '+str(len(objs)), True, (255, 255, 255))
win.blit(text_surface, (0, 20))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN and event.button != 3:
pos = pygame.mouse.get_pos()
mass = random.randint(10, 100)
color = (0, round((100-mass)/90*255), round((mass-10)/90*255))
v = Vec2(50*(random.random()-0.5), 50*(random.random()-0.5))
objs.append(Ball(10, mass, Vec2(pos[0], pos[1]), v, color))
elif event.type == MOUSEBUTTONDOWN and event.button == 3:
if len(objs) > 1: del objs[0]
for i in range(nsub):
update(dt)
render()
print_energy()
pygame.display.flip()
fpsClock.tick(60)