-
Notifications
You must be signed in to change notification settings - Fork 1
/
ball.py
307 lines (237 loc) · 8.57 KB
/
ball.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""
A ball module for 2D rigid disc collision simulation.
The balls collide elastically with one another and the wall of the container.
Xin Kai Lee 10/3/2020
"""
import numpy as np
from copy import deepcopy
class Ball:
"""
Ball Class. This is used as the ball in a 2D rigid disc collision simulation.
Attributes:
mass (float): The mass of the ball.
radius (float): The radius of the ball.
pos (numpy.ndarray of float): The position of the ball.
vel (numpy.ndarray of float): The velocity of the ball.
count (int): The number of collisions the ball experienced.
"""
time = 0
def __init__(
self,
mass=1,
radius=1,
pos=np.array([0.0, 0.0]),
vel=np.array([0.0, 0.0]),
count=0,
):
self._mass = mass
self._radius = radius
self._pos = pos
self._vel = vel
self._count = count
self._dp = 0
def __repr__(self):
return f"Ball radius = {self._radius}, position = {self._pos}, velocity = {self._vel}, mass = {self._mass}"
def __str__(self):
return f"Ball, r = {self._radius}, pos = {self._pos}, vel = {self._vel}, mass = {self._mass}"
def mass(self):
"""
Returns:
(float): The mass of the ball.
"""
return self._mass
def radius(self):
"""
Returns:
(float): The radius of the ball.
"""
return self._radius
def pos(self):
"""
Returns:
(numpy.ndarray of float): The position array of the ball.
"""
return self._pos
def vel(self):
"""
Returns:
(numpy.ndarray of float): The velocity array of the ball.
"""
return self._vel
def set_mass(self, mass):
"""
Sets the mass of the ball.
Parameters:
mass (float): The mass of the ball.
"""
self._mass = mass
def set_radius(self, radius):
"""
Sets the radius of the ball.
Parameters:
radius (float): The radius of the ball.
"""
self._radius = radius
def set_pos(self, pos):
"""
Sets the position of the ball.
Parameters:
pos (numpy.ndarray of float): The position of the ball.
"""
self._pos = np.array(pos)
def set_vel(self, vel):
"""
Sets the velocity of the ball.
Parameters:
vel (numpy.ndarray of float): The velocity of the ball.
"""
self._vel = np.array(vel)
def copy(self):
"""
Performs a deepcopy on the ball.
Returns:
(Ball): The same Ball object with itself.
"""
return deepcopy(self)
def time_to_collision(self, other):
"""
Calculates the next collision time between balls or with the container. If they don't collide, np.inf is returned.
Parameters:
other (Ball): The other ball/container to be collided with.
Returns:
dt (float): The next collision time.
"""
r = self._pos - other._pos
v = self._vel - other._vel
a = magsquare_vector(v)
if a == 0: # If both balls move in same direction
return np.inf
else: # Conditions for collision with balls or container
if isinstance(other, Container):
R = self._radius - other._radius
else:
R = self._radius + other._radius
b = 2 * np.dot(v, r)
c = magsquare_vector(r) - R ** 2
if not isinstance(other, Container): # Floating point errors
if np.abs(c) <= 10e-13:
return np.inf
discriminant = b ** 2 - 4 * a * c
if discriminant < 0: # Complex dt
return np.inf
elif discriminant == 0:
dt = -b / (2 * a)
if dt <= 0:
return np.inf
else:
return dt
elif discriminant > 0:
dt1 = (-b + np.sqrt(discriminant)) / (2 * a)
dt2 = (-b - np.sqrt(discriminant)) / (2 * a)
if isinstance(other, Container):
return np.amax(np.array([dt1, dt2]))
else:
if dt1 <= 0 and dt2 <= 0:
return np.inf
elif dt1 > 0 and dt2 > 0:
return np.amin(np.array([dt1, dt2]))
elif dt1 < 0:
return dt2
elif dt2 < 0:
return dt1
def collide(self, other):
"""
Calculates the change in velocity of balls after a collision.
Arguments:
other (Ball): The other ball which is collided.
"""
# Transforming velocity to centre-of-mass frame
r = self._pos - other._pos
u1_parallel = projection(self._vel, r) # parallel to line of c.o.m
u1_perpendicular = self._vel - u1_parallel
if isinstance(other, Container): # Colliding with a container
vel_i = self._vel
v1_parallel = -u1_parallel
v2_parallel = np.zeros(2)
v1_perpendicular = u1_perpendicular
self.set_vel(v1_parallel + v1_perpendicular)
other.set_vel(np.zeros(2))
self._count += 1
other._count = -1 # Collision count for container is always -1
vel_f = self._vel
dv = vel_f - vel_i
self._dp = dv * self._mass
else: # Colliding with another ball
m1 = self._mass
m2 = other._mass
u2_parallel = projection(other._vel, r)
u2_perpendicular = other._vel - u2_parallel
u1_parallel_translated = u1_parallel - u2_parallel
v1_parallel_translated = (m1 - m2) / (m1 + m2) * u1_parallel_translated
v1_parallel = v1_parallel_translated + u2_parallel
v2_parallel = u1_parallel - u2_parallel + v1_parallel
v1_perpendicular = u1_perpendicular
v2_perpendicular = u2_perpendicular
self.set_vel(v1_parallel + v1_perpendicular)
other.set_vel(v2_parallel + v2_perpendicular)
self._count += 1
other._count += 1
def move(self, dt):
"""
Moving the ball to another time.
Arguments:
dt (float): The time from now to move the ball to.
"""
self.set_pos(self._pos + dt * self._vel)
class Container(Ball):
"""
This is a Container class. The container is used to enclose the balls in
the 2D rigid disc collision.
Arguments:
radius (float): The radius of the container.
mass (float): The mass of the container.
"""
def __init__(self, radius=10, mass=100):
super().__init__()
self._radius = radius
self._mass = mass
self._count = -1 # The collision count is always -1
def magsquare_vector(vector):
"""
Calculates the magnitude squared of a vector.
Arguments:
vector (numpy.ndarray of float): Vector.
Returns:
(float): The magnitude squared of the vector.
"""
return np.dot(vector, vector)
def mag_vector(vector):
"""
Calculates the magnitude of a vector.
Arguments:
vector (numpy.ndarray of float): Vector.
Returns:
(float): The magnitude of the vector.
"""
return np.sqrt(magsquare_vector(vector))
def projection(a, b):
"""
Calculates the projection vector of a on b.
Arguments:
a (numpy.ndarray of float): Vector to find projection of.
b (numpy.ndarray of float): Vector to be projected on.
Returns:
(numpy.ndarray of float): The projection vector of a on b.
"""
magsquare_b = magsquare_vector(b)
return np.vdot(a, b) / magsquare_b * b
def rejection(a, b): # writing rejection vector of a on b
"""
Calculates the rejection vector of a on b.
Arguments:
a (numpy.ndarray of float): Vector to find rejection of.
b (numpy.ndarray of float): Vector to be rejected on.
Returns:
(numpy.ndarray of float): The rejection vector of a on b.
"""
return a - projection(a, b)