Skip to content

Commit b499d88

Browse files
authored
Create collisions.py
Added basic implementation of circle collision detection
1 parent e59d819 commit b499d88

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

physics/collisions.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Basic implementation of the collision of two circles.
2+
def circle_collision(fpos: tuple[float, float, float], spos: tuple[float, float, float]) -> bool:
3+
# difference by XY axes
4+
dx = fpos[0] - spos[0]
5+
dy = fpos[1] - spos[1]
6+
7+
8+
# Euclidean distance between the centers of circles
9+
distance = pow((pow(dx, 2) + pow(dy, 2)), 0.5)
10+
11+
# minimum possible distance between circles, without collision
12+
min_distance = fpos[2] + fpos[2]
13+
collide = distance < min_distance
14+
15+
# If actual distance smaller than minimal possible, cirlces collides
16+
return collide
17+
18+
if __name__ == "__main__":
19+
from doctest import testmod
20+
21+
testmod()

0 commit comments

Comments
 (0)