-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollision.py
581 lines (399 loc) · 18.6 KB
/
collision.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
from vector import Vector2D
from body import Body, Polygon, Rectangle, Circle
def collide(body_1: Body, body_2: Body, include_rotation = True):
if body_1.shape_type == "Polygon" and body_2.shape_type == "Polygon":
normal, depth = polygons_collision(body_1, body_2) #if include_rotation else aabbs_collision(body_1, body_2)
elif body_1.shape_type == "Circle" and body_2.shape_type == "Circle":
normal, depth = circles_collision(body_1, body_2)
elif body_1.shape_type == "Polygon" and body_2.shape_type == "Circle":
normal, depth = polygon_circle_collision(body_1, body_2)
elif body_1.shape_type == "Circle" and body_2.shape_type == "Polygon":
normal, depth = polygon_circle_collision(body_2, body_1)
if normal is None or depth is None:
return
if body_1.shape_type == "Polygon" and body_2.shape_type == "Polygon":
contact_points = polygons_contact_points(body_1, body_2)
elif body_1.shape_type == "Circle" and body_2.shape_type == "Circle":
contact_points = circles_contact_points(body_1, body_2)
elif body_1.shape_type == "Polygon" and body_2.shape_type == "Circle":
contact_points = polygon_circle_contact_points(body_1, body_2)
elif body_1.shape_type == "Circle" and body_2.shape_type == "Polygon":
contact_points = polygon_circle_contact_points(body_2, body_1)
normal = -normal # Temporal solution
if include_rotation:
resolution_with_rotation(body_1, body_2, normal, depth, contact_points)
else:
resolution(body_1, body_2, normal, depth)
return contact_points
def aabbs_collision(body_1: Rectangle, body_2: Rectangle):
"""DEPRECATED: Use polygons_collision instead"""
assert body_1.shape_type == "Rectangle" and body_2.shape_type == "Rectangle", \
"Both body_1 and body_2 must be of shape_type 'Rectangle' for polygon collision."
# If position is the bottom left edge of the body
# return (
# body_1.pos[0] < body_2.pos[0] + body_2.width
# and body_1.pos[0] + body_1.width > body_2.pos[0]
# and body_1.pos[1] < body_2.pos[1] + body_2.height
# and body_1.pos[1] + body_1.height > body_2.pos[1]
# )
# Another solution:
d: Vector2D = body_1.pos - body_2.pos
ad = abs(d)
sum_half_sizes = Vector2D(body_1.width / 2 + body_2.width /2, body_1.height / 2 + body_2.height / 2)
if ad.x >= sum_half_sizes.x or ad.y >= sum_half_sizes.y:
# If not collision
return None, None
# If collision
separation_vector = sum_half_sizes - ad
# Ignore longer axis
if separation_vector.x < separation_vector.y:
if separation_vector.x > 0:
separation_vector.y = 0
else:
if separation_vector.y > 0:
separation_vector.x = 0
# Correct sign
if d.x < 0:
separation_vector.x = -separation_vector.x
if d.y < 0:
separation_vector.y = -separation_vector.y
normal_vector = separation_vector.normalize()
penetration_depth = separation_vector.magnitude()
# Note: separation vector = normal_vector * penetration_depth
# contact_point = polygons_contact_points(body_1, body_2)
# resolution(body_1, body_2, normal_vector, penetration_depth)
return normal_vector, penetration_depth
def circles_collision(body_1: Circle, body_2: Circle):
assert body_1.shape_type == "Circle" and body_2.shape_type == "Circle", \
"Both body_1 and body_2 must be of shape_type 'Circle' for Circle collision."
distance = Vector2D.distance(body_1.pos, body_2.pos)
if distance >= body_1.radius + body_2.radius:
# If not collision
return None, None
# If collision
normal_vector = (body_1.pos - body_2.pos).normalize()
penetration_depth = body_1.radius + body_2.radius - distance
# resolution(body_1, body_2, normal_vector, penetration_depth)
# contact_point = circles_contact_points(body_1, body_2)
# resolution_with_rotation(body_1, body_2, normal_vector, penetration_depth, contact_point)
return normal_vector, penetration_depth
def project_circle(center, radius: float, axis: Vector2D):
direction = axis.normalize()
# direction_and_radius = Vector2D(*[direction[i] * radius for i in range(len(direction))])
direction_and_radius = direction * radius
# p1 = [center[i] + direction_and_radius[i] for i in range(len(center))]
# p2 = [center[i] - direction_and_radius[i] for i in range(len(center))]
p1 = center + direction_and_radius
p2 = center - direction_and_radius
min_proj = p1.dot(axis)
max_proj = p2.dot(axis)
if min_proj > max_proj:
min_proj, max_proj = max_proj, min_proj
return min_proj, max_proj
def project_vertices(vertices: list[Vector2D], axis: Vector2D):
min_proj = float('inf')
max_proj = float('-inf')
for v in vertices:
proj = v.dot(axis)
if proj < min_proj:
min_proj = proj
if proj > max_proj:
max_proj = proj
return min_proj, max_proj
def find_closest_point_on_polygon(circle_center: Vector2D, vertices: list[Vector2D]):
result = -1
min_distance = float('inf')
for i, v in enumerate(vertices):
dist = Vector2D.distance(v, circle_center)
if dist < min_distance:
min_distance = dist
result = i
return result
def polygon_circle_collision(polygon: Polygon, circle: Circle):
assert polygon.shape_type == "Polygon" and circle.shape_type == "Circle", \
"Shape types of polygon and circle must be 'Polygon' and 'Circle' respectively."
normal = Vector2D(0, 0)
penetration_depth = float('inf')
for i in range(len(polygon.vertices)):
va = polygon.vertices[i]
vb = polygon.vertices[(i + 1) % len(polygon.vertices)]
edge = vb - va
axis = Vector2D(-edge.y, edge.x).normalize()
# project circle onto axis
min_a, max_a = project_vertices(polygon.vertices, axis)
min_b, max_b = project_circle(circle.pos, circle.radius, axis)
if max_a <= min_b or max_b <= min_a:
return None, None
axis_depth = min(max_b - min_a, max_a - min_b)
if axis_depth < penetration_depth:
penetration_depth = axis_depth
normal = axis
cp_index = find_closest_point_on_polygon(circle.pos, polygon.vertices)
cp = polygon.vertices[cp_index]
axis = (cp - circle.pos).normalize()
min_a, max_a = project_circle(circle.pos, circle.radius, axis)
min_b, max_b = project_vertices(polygon.vertices, axis)
if max_a <= min_b or max_b <= min_a:
return None, None
axis_depth = min(max_b - min_a, max_a - min_b)
if axis_depth < penetration_depth:
penetration_depth = axis_depth
normal = axis
direction = (polygon.pos - circle.pos).normalize()
if direction.dot(normal) < 0:
normal *= -1
# resolution(polygon, circle, normal, penetration_depth)
# contact_point = polygon_circle_contact_points(polygon, circle)
# resolution_with_rotation(polygon, circle, normal, penetration_depth, contact_point)
return normal, penetration_depth
def polygons_collision(polygon_1: Polygon, polygon_2: Polygon):
normal = Vector2D(0, 0)
depth = float('inf')
for i in range(len(polygon_1.vertices)):
va = polygon_1.vertices[i]
vb = polygon_1.vertices[(i + 1) % len(polygon_1.vertices)]
edge = vb - va
axis = Vector2D(-edge.y, edge.x).normalize()
min_a, max_a = project_vertices(polygon_1.vertices, axis)
min_b, max_b = project_vertices(polygon_2.vertices, axis)
if min_a >= max_b or min_b >= max_a:
return None, None
axis_depth = min(max_b - min_a, max_a - min_b)
if axis_depth < depth:
depth = axis_depth
normal = axis
for i in range(len(polygon_2.vertices)):
va = polygon_2.vertices[i]
vb = polygon_2.vertices[(i + 1) % len(polygon_2.vertices)]
edge = vb - va
axis = Vector2D(-edge.y, edge.x).normalize()
min_a, max_a = project_vertices(polygon_1.vertices, axis)
min_b, max_b = project_vertices(polygon_2.vertices, axis)
if min_a >= max_b or min_b >= max_a:
return None, None
axis_depth = min(max_b - min_a, max_a - min_b)
if axis_depth < depth:
depth = axis_depth
normal = axis
direction = (polygon_1.pos - polygon_2.pos).normalize()
if direction.dot(normal) < 0:
normal *= -1
# resolution(polygon_1, polygon_2, normal, depth)
# contact_points = polygons_contact_points(polygon_1, polygon_2)
# resolution_with_rotation(polygon_1, polygon_2, normal, depth, contact_points)
return normal, depth
def separate_bodies(body_1: Body, body_2: Body, normal, penetration_depth):
separation_vector = normal * penetration_depth
# Another Solution
# percent = 0.2; slope = 0.01
#separation_vector = max(penetration_depth - slope, 0) / (1 / body_1.mass + 1 / body_2.mass) * percent * normal
if body_1.is_static:
body_2.pos += separation_vector
elif body_2.is_static:
body_1.pos -= separation_vector
else:
body_1.pos -= separation_vector / 2
body_2.pos += separation_vector / 2
# Another Solution
# if body_1.is_static == False:
# body_1.pos -= separation_vector * 1 / body_1.mass
# if body_2.is_static == False:
# body_2.pos += separation_vector * 1 / body_2.mass
def resolution(body_1: Body, body_2: Body, normal_vector: Vector2D, penetration_depth: float):
normal_vector *= -1
separate_bodies(body_1, body_2, normal_vector, penetration_depth)
relative_velocity = body_2.velocity - body_1.velocity
penetration_velocity = relative_velocity.dot(normal_vector)
if penetration_velocity > 0:
return
r = max(body_1.bounce, body_2.bounce) #MIN?
j = -(1 + r) * penetration_velocity
j /= 1 / body_1.mass + 1 / body_2.mass
impulse = normal_vector * j
if body_1.is_static == False:
body_1.velocity -= impulse / body_1.mass
if body_2.is_static == False:
body_2.velocity += impulse / body_2.mass
# Coulomb friction model
tangent = relative_velocity - normal_vector * relative_velocity.dot(normal_vector)
if tangent == Vector2D(0, 0):
return
else:
tangent = tangent.normalize()
jt = -1 * relative_velocity.dot(tangent)
jt /= 1 / body_1.mass + 1 / body_2.mass
static_friction = (body_1.static_friction + body_2.static_friction) / 2
if abs(jt) <= j * static_friction:
# friction_impulse = jt * tangent
friction_impulse = tangent * jt
else:
dynamic_friction = (body_1.dynamic_friction + body_2.dynamic_friction) / 2
# friction_impulse = -j * tangent * dynamic_friction
friction_impulse = tangent * (-1) * j * dynamic_friction
if body_1.is_static == False:
body_1.velocity -= friction_impulse / body_1.mass
if body_2.is_static == False:
body_2.velocity += friction_impulse / body_2.mass
def resolution_with_rotation(body_1: Body, body_2: Body, normal_vector: Vector2D, penetration_depth: float, contact_point: list[Vector2D]):
normal_vector *= -1
separate_bodies(body_1, body_2, normal_vector, penetration_depth)
relative_velocity = body_2.velocity - body_1.velocity
# Calculate restitution (bounciness)
r = max(body_1.bounce, body_2.bounce)
if len(contact_point) == 2:
contact_point = (contact_point[0] + contact_point[1]) / 2
else:
contact_point = contact_point[0]
# Calculate impulses for each contact point
r_1 = contact_point - body_1.pos
r_2 = contact_point - body_2.pos
r_1_perp = Vector2D(-r_1.y, r_1.x)
r_2_perp = Vector2D(-r_2.y, r_2.x)
relative_velocity = (body_2.velocity + r_2_perp * body_2.angular_velocity) - (body_1.velocity + r_1_perp * body_1.angular_velocity)
penetration_velocity = relative_velocity.dot(normal_vector)
if penetration_velocity > 0:
return
j = -(1 + r) * penetration_velocity
j /= 1 / body_1.mass + 1 / body_2.mass + (r_1_perp.dot(normal_vector) ** 2) / body_1.inertia + (r_2_perp.dot(normal_vector) ** 2) / body_2.inertia
impulse = normal_vector * j
body_1.velocity -= impulse / body_1.mass
body_1.angular_velocity -= r_1.cross(impulse) / body_1.inertia
body_2.velocity += impulse / body_2.mass
body_2.angular_velocity += r_2.cross(impulse) / body_2.inertia
relative_velocity = (body_2.velocity + r_2_perp * body_2.angular_velocity) - (body_1.velocity + r_1_perp * body_1.angular_velocity)
tangent = relative_velocity - normal_vector * relative_velocity.dot(normal_vector)
if tangent == Vector2D(0, 0):
return
else:
tangent = tangent.normalize()
jt = -1 * relative_velocity.dot(tangent)
jt /= 1 / body_1.mass + 1 / body_2.mass + (r_1_perp.dot(tangent) ** 2) / body_1.inertia + (r_2_perp.dot(tangent) ** 2) / body_2.inertia
static_friction = (body_1.static_friction + body_2.static_friction) / 2
if abs(jt) <= j * static_friction:
friction_impulse = jt * tangent
else:
dynamic_friction = (body_1.dynamic_friction + body_2.dynamic_friction) / 2
friction_impulse = -j * tangent * dynamic_friction
body_1.velocity -= friction_impulse / body_1.mass
body_1.angular_velocity -= r_1.cross(friction_impulse) / body_1.inertia
body_2.velocity += friction_impulse / body_2.mass
body_2.angular_velocity += r_2.cross(friction_impulse) / body_2.inertia
def circles_contact_points(body_1: Circle, body_2: Circle):
normal = (body_2.pos - body_1.pos).normalize()
contact_point = body_1.pos + normal * body_1.radius
return [contact_point]
def point_to_line_segment_projection(point: Vector2D, a: Vector2D, b: Vector2D):
ab = b - a # line segment vector
ap = point - a #point vector
proj = ap.dot(ab)
d = proj / ab.dot(ab) # ad.dot(ab) = len(ab) ** 2, but dot more efficient
if d <= 0:
contact_point = a
elif d >= 1:
contact_point = b
else:
contact_point = a + ab * d
distance = Vector2D.distance(contact_point, point)
return contact_point, distance
def polygon_circle_contact_points(polygon: Polygon, circle: Circle):
min_distance = float('inf')
for i in range(len(polygon.vertices)):
va = polygon.vertices[i]
vb = polygon.vertices[(i + 1) % len(polygon.vertices)]
cp, distance = point_to_line_segment_projection(circle.pos, va, vb)
if distance < min_distance:
min_distance = distance
contact_point = cp
return [contact_point]
def polygons_contact_points(polygon_1: Polygon, polygon_2: Polygon):
epsilon = 0.0005
min_distance = float('inf')
contact_point_1 = None
contact_point_2 = None
for i in range(len(polygon_1.vertices)):
vp = polygon_1.vertices[i]
for j in range(len(polygon_2.vertices)):
va = polygon_2.vertices[j]
vb = polygon_2.vertices[(j + 1) % len(polygon_2.vertices)]
cp, distance = point_to_line_segment_projection(vp, va, vb)
if contact_point_1 is not None and abs(distance - min_distance) < epsilon and not cp.distance_to(contact_point_1) < epsilon:
contact_point_2 = cp
elif distance < min_distance:
min_distance = distance
contact_point_2 = None
contact_point_1 = cp
for i in range(len(polygon_2.vertices)):
vp = polygon_2.vertices[i]
for j in range(len(polygon_1.vertices)):
va = polygon_1.vertices[j]
vb = polygon_1.vertices[(j + 1) % len(polygon_1.vertices)]
cp, distance = point_to_line_segment_projection(vp, va, vb)
if contact_point_1 is not None and abs(distance - min_distance) < epsilon and not cp.distance_to(contact_point_1) < epsilon:
contact_point_2 = cp
elif distance < min_distance:
min_distance = distance
contact_point_2 = None
contact_point_1 = cp
return [cp for cp in [contact_point_1, contact_point_2] if cp is not None]
# Alternative polygon vs polygon contact points detection method BUG
def clip(v1, v2, n, o):
cp = []
d1 = n.dot(v1) - o
d2 = n.dot(v2) - o
if d1 >= 0.0:
cp.append(v1)
if d2 >= 0.0:
cp.append(v2)
if d1 * d2 < 0.0:
e = v2 - v1
u = d1 / (d1 - d2)
e = e * u + v1
cp.append(e)
return cp
def find_farthest_vertex(vertices, n):
max_projection = -float('inf')
index = -1
for i, v in enumerate(vertices):
projection = n.dot(v)
if projection > max_projection:
max_projection = projection
index = i
return index
def best_edge(vertices, n):
index = find_farthest_vertex(vertices, n)
v = vertices[index]
v1 = vertices[(index + 1) % len(vertices)]
v0 = vertices[(index - 1) % len(vertices)]
l = v - v1
r = v - v0
l = l.normalize()
r = r.normalize()
if r.dot(n) <= l.dot(n):
return (v, v0)
else:
return (v, v1)
def polygons_clipping_contact_points(polygon_1, polygon_2, n):
# Contact Points Using Clipping
# https://dyn4j.org/2011/11/contact-points-using-clipping/
n *= -1
e1 = best_edge(polygon_1.vertices, n)
e2 = best_edge(polygon_2.vertices, n * -1)
ref = e1 if abs(e1[0].dot(n)) <= abs(e2[0].dot(n)) else e2
inc = e2 if ref == e1 else e1
flip = ref != e1
refv = ref[1] - ref[0]
refv = refv.normalize()
o1 = refv.dot(ref[0])
cp = clip(inc[0], inc[1], refv, o1)
if len(cp) < 2:
return []
o2 = refv.dot(ref[1])
cp = clip(cp[0], cp[1], refv * -1, o2 * -1)
if len(cp) < 2:
return []
refNorm = Vector2D(-refv.y, refv.x)
if flip:
refNorm = refNorm * -1
max_projection = max(refNorm.dot(v) for v in [ref[0], ref[1]])
cp = [point for point in cp if refNorm.dot(point) - max_projection < 0.05]
return cp