forked from MUKESHSIHAG/physics-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyParticles.py
215 lines (170 loc) · 7.8 KB
/
PyParticles.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
import math, random
def addVectors((angle1, length1), (angle2, length2)):
""" Returns the sum of two vectors """
x = math.sin(angle1) * length1 + math.sin(angle2) * length2
y = math.cos(angle1) * length1 + math.cos(angle2) * length2
angle = 0.5 * math.pi - math.atan2(y, x)
length = math.hypot(x, y)
return (angle, length)
def combine(p1, p2):
if math.hypot(p1.x - p2.x, p1.y - p2.y) < p1.size + p2.size:
total_mass = p1.mass + p2.mass
p1.x = (p1.x*p1.mass + p2.x*p2.mass)/total_mass
p1.y = (p1.y*p1.mass + p2.y*p2.mass)/total_mass
(p1.angle, p1.speed) = addVectors((p1.angle, p1.speed*p1.mass/total_mass), (p2.angle, p2.speed*p2.mass/total_mass))
p1.speed *= (p1.elasticity*p2.elasticity)
p1.mass += p2.mass
p1.collide_with = p2
def collide(p1, p2):
""" Tests whether two particles overlap
If they do, make them bounce, i.e. update their angle, speed and position """
dx = p1.x - p2.x
dy = p1.y - p2.y
dist = math.hypot(dx, dy)
if dist < p1.size + p2.size:
angle = math.atan2(dy, dx) + 0.5 * math.pi
total_mass = p1.mass + p2.mass
(p1.angle, p1.speed) = addVectors((p1.angle, p1.speed*(p1.mass-p2.mass)/total_mass), (angle, 2*p2.speed*p2.mass/total_mass))
(p2.angle, p2.speed) = addVectors((p2.angle, p2.speed*(p2.mass-p1.mass)/total_mass), (angle+math.pi, 2*p1.speed*p1.mass/total_mass))
elasticity = p1.elasticity * p2.elasticity
p1.speed *= elasticity
p2.speed *= elasticity
overlap = 0.5*(p1.size + p2.size - dist+1)
p1.x += math.sin(angle)*overlap
p1.y -= math.cos(angle)*overlap
p2.x -= math.sin(angle)*overlap
p2.y += math.cos(angle)*overlap
class Particle:
""" A circular object with a velocity, size and mass """
def __init__(self, (x, y), size, mass=1):
self.x = x
self.y = y
self.size = size
self.colour = (0, 0, 255)
self.thickness = 0
self.speed = 0
self.angle = 0
self.mass = mass
self.drag = 1
self.elasticity = 0.9
def move(self):
""" Update position based on speed, angle """
self.x += math.sin(self.angle) * self.speed
self.y -= math.cos(self.angle) * self.speed
def experienceDrag(self):
self.speed *= self.drag
def mouseMove(self, (x, y)):
""" Change angle and speed to move towards a given point """
dx = x - self.x
dy = y - self.y
self.angle = 0.5*math.pi + math.atan2(dy, dx)
self.speed = math.hypot(dx, dy) * 0.1
def accelerate(self, vector):
""" Change angle and speed by a given vector """
(self.angle, self.speed) = addVectors((self.angle, self.speed), vector)
def attract(self, other):
"""" Change velocity based on gravatational attraction between two particle"""
dx = (self.x - other.x)
dy = (self.y - other.y)
dist = math.hypot(dx, dy)
if dist < self.size + other.size:
return True
theta = math.atan2(dy, dx)
force = 0.1 * self.mass * other.mass / dist**2
self.accelerate((theta - 0.5 * math.pi, force/self.mass))
other.accelerate((theta + 0.5 * math.pi, force/other.mass))
class Spring:
def __init__(self, p1, p2, length=50, strength=0.5):
self.p1 = p1
self.p2 = p2
self.length = length
self.strength = strength
def update(self):
dx = self.p1.x - self.p2.x
dy = self.p1.y - self.p2.y
dist = math.hypot(dx, dy)
theta = math.atan2(dy, dx)
force = (self.length - dist) * self.strength
self.p1.accelerate((theta + 0.5 * math.pi, force/self.p1.mass))
self.p2.accelerate((theta - 0.5 * math.pi, force/self.p2.mass))
class Environment:
""" Defines the boundary of a simulation and its properties """
def __init__(self, (width, height)):
self.width = width
self.height = height
self.particles = []
self.springs = []
self.colour = (255,255,255)
self.mass_of_air = 0.2
self.elasticity = 0.75
self.acceleration = (0,0)
self.particle_functions1 = []
self.particle_functions2 = []
self.function_dict = {
'move': (1, lambda p: p.move()),
'drag': (1, lambda p: p.experienceDrag()),
'bounce': (1, lambda p: self.bounce(p)),
'accelerate': (1, lambda p: p.accelerate(self.acceleration)),
'collide': (2, lambda p1, p2: collide(p1, p2)),
'combine': (2, lambda p1, p2: combine(p1, p2)),
'attract': (2, lambda p1, p2: p1.attract(p2))}
def addFunctions(self, function_list):
for func in function_list:
(n, f) = self.function_dict.get(func, (-1, None))
if n == 1:
self.particle_functions1.append(f)
elif n == 2:
self.particle_functions2.append(f)
else:
print "No such function: %s" % f
def addParticles(self, n=1, **kargs):
""" Add n particles with properties given by keyword arguments """
for i in range(n):
size = kargs.get('size', random.randint(10, 20))
mass = kargs.get('mass', random.randint(100, 10000))
x = kargs.get('x', random.uniform(size, self.width - size))
y = kargs.get('y', random.uniform(size, self.height - size))
particle = Particle((x, y), size, mass)
particle.speed = kargs.get('speed', random.random())
particle.angle = kargs.get('angle', random.uniform(0, math.pi*2))
particle.colour = kargs.get('colour', (0, 0, 255))
particle.elasticity = kargs.get('elasticity', self.elasticity)
particle.drag = (particle.mass/(particle.mass + self.mass_of_air)) ** particle.size
self.particles.append(particle)
def addSpring(self, p1, p2, length=50, strength=0.5):
""" Add a spring between particles p1 and p2 """
self.springs.append(Spring(self.particles[p1], self.particles[p2], length, strength))
def update(self):
""" Moves particles and tests for collisions with the walls and each other """
for i, particle in enumerate(self.particles, 1):
for f in self.particle_functions1:
f(particle)
for particle2 in self.particles[i:]:
for f in self.particle_functions2:
f(particle, particle2)
for spring in self.springs:
spring.update()
def bounce(self, particle):
""" Tests whether a particle has hit the boundary of the environment """
if particle.x > self.width - particle.size:
particle.x = 2*(self.width - particle.size) - particle.x
particle.angle = - particle.angle
particle.speed *= self.elasticity
elif particle.x < particle.size:
particle.x = 2*particle.size - particle.x
particle.angle = - particle.angle
particle.speed *= self.elasticity
if particle.y > self.height - particle.size:
particle.y = 2*(self.height - particle.size) - particle.y
particle.angle = math.pi - particle.angle
particle.speed *= self.elasticity
elif particle.y < particle.size:
particle.y = 2*particle.size - particle.y
particle.angle = math.pi - particle.angle
particle.speed *= self.elasticity
def findParticle(self, (x, y)):
""" Returns any particle that occupies position x, y """
for particle in self.particles:
if math.hypot(particle.x - x, particle.y - y) <= particle.size:
return particle
return None