-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollisions.py
More file actions
50 lines (42 loc) · 1.86 KB
/
collisions.py
File metadata and controls
50 lines (42 loc) · 1.86 KB
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
from math import *
def checkSign(n):
if n != 0:
return abs(n)/n
else:
return n
def circleLineIntersection(p1, p2, r):
# Source: https://mathworld.wolfram.com/Circle-LineIntersection.html
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
dr2 = dx ** 2 + dy ** 2
D = p1[0] * p2[1] - p2[0] * p1[1]
delta = r ** 2 * dr2 - D ** 2
# Check if circle intersects the infinite line
if delta >= 0:
# Check if circle intersects the segment
# (By checking the signs, since point zero has to be in between a positive and a negative value)
if checkSign(p1[0]) != checkSign(p2[0]) or checkSign(p1[1]) != checkSign(p2[1]):
return True
def circleBlockIntersection(circle, block):
vals = []
p1 = [block.pos[0], block.pos2[1]]
p2 = [block.pos[0], block.pos[1]]
adjPoint1 = [p1[0] - circle.pos[0], p1[1] - circle.pos[1]]
adjPoint2 = [p2[0] - circle.pos[0], p2[1] - circle.pos[1]]
vals.append(circleLineIntersection(adjPoint1, adjPoint2, circle.radius))
p1 = [block.pos[0], block.pos[1]]
p2 = [block.pos2[0], block.pos[1]]
adjPoint1 = [p1[0] - circle.pos[0], p1[1] - circle.pos[1]]
adjPoint2 = [p2[0] - circle.pos[0], p2[1] - circle.pos[1]]
vals.append(circleLineIntersection(adjPoint1, adjPoint2, circle.radius))
p1 = [block.pos2[0], block.pos2[1]]
p2 = [block.pos2[0], block.pos[1]]
adjPoint1 = [p1[0] - circle.pos[0], p1[1] - circle.pos[1]]
adjPoint2 = [p2[0] - circle.pos[0], p2[1] - circle.pos[1]]
vals.append(circleLineIntersection(adjPoint1, adjPoint2, circle.radius))
p1 = [block.pos[0], block.pos2[1]]
p2 = [block.pos2[0], block.pos2[1]]
adjPoint1 = [p1[0] - circle.pos[0], p1[1] - circle.pos[1]]
adjPoint2 = [p2[0] - circle.pos[0], p2[1] - circle.pos[1]]
vals.append(circleLineIntersection(adjPoint1, adjPoint2, circle.radius))
return vals