Skip to content

Commit 8015885

Browse files
committed
Detection Collision : Broad Phase algorithm
1 parent 9aa3851 commit 8015885

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

pygorithm/geometry/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Collection of special geometry functions
3+
"""
4+
from . import collision_detection
5+
6+
__all__ = [
7+
'collision_detection.py'
8+
]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
Author: ALEXEY SARAPULOV
3+
Created On: 23 August 2017
4+
"""
5+
6+
# To test if two rectangle intersect, we only have to find out
7+
# if their projections intersect on all of the coordinate axes
8+
9+
import inspect
10+
11+
class Coord:
12+
"""Coord
13+
Class to initialize Coordinate of one point
14+
"""
15+
16+
def __init__(self, x, y):
17+
self.x = x
18+
self.y = y
19+
20+
21+
class Body:
22+
"""Body
23+
Class to initialize Body of Object
24+
"""
25+
26+
def __init__(self, coord1, coord2):
27+
"""
28+
:type coord1: object of class Coord
29+
:type coord2: object of class Coord
30+
"""
31+
self.min_x = coord1.x
32+
self.min_y = coord1.y
33+
self.max_x = coord2.x
34+
self.max_y = coord2.y
35+
36+
37+
def broad_phase(body1, body2):
38+
"""
39+
:type body1: object
40+
:type body2: object
41+
"""
42+
d1x = body2.min_x - body1.max_x
43+
d1y = body2.min_y - body1.max_y
44+
d2x = body1.min_x - body2.max_x
45+
d2y = body1.min_y - body2.max_y
46+
47+
if d1x > 0 or d1y > 0:
48+
return False
49+
50+
if d2x > 0 or d2y > 0:
51+
return False
52+
53+
return True
54+
55+
56+
def get_code():
57+
"""
58+
returns the code for the broad phase function
59+
"""
60+
return inspect.getsource(broad_phase)
61+
62+
if __name__ == '__main__':
63+
coord1 = Coord(1, 1)
64+
coord2 = Coord(6, 8)
65+
body1 = Body(coord1, coord2)
66+
coord3 = Coord(4, 0)
67+
coord4 = Coord(7, 4)
68+
body2 = Body(coord3, coord4)
69+
print(broad_phase(body1, body2))

pygorithm/geometry/modules.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
List all modules
3+
"""
4+
import pkgutil
5+
6+
def modules():
7+
import pygorithm.geometry
8+
package = pygorithm.geometry
9+
modules = []
10+
for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
11+
modules.append(modname)
12+
modules.remove('modules')
13+
modules.sort()
14+
return modules

tests/test_geometry.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import unittest
2+
3+
from pygorithm.geometry import (
4+
collision_detection
5+
)
6+
7+
8+
class TestCollisionDetection(unittest.TestCase):
9+
def setUp(self):
10+
# first pair of objects
11+
self.coord1 = collision_detection.Coord(1, 1)
12+
self.coord2 = collision_detection.Coord(6, 8)
13+
self.body1 = collision_detection.Body(self.coord1, self.coord2)
14+
self.coord3 = collision_detection.Coord(4, 0)
15+
self.coord4 = collision_detection.Coord(7, 4)
16+
self.body2 = collision_detection.Body(self.coord3, self.coord4)
17+
# second pair
18+
self.coord1 = collision_detection.Coord(1, 1)
19+
self.coord2 = collision_detection.Coord(2, 3)
20+
self.body3 = collision_detection.Body(self.coord1, self.coord2)
21+
self.coord3 = collision_detection.Coord(4, 3)
22+
self.coord4 = collision_detection.Coord(7, 8)
23+
self.body4 = collision_detection.Body(self.coord3, self.coord4)
24+
25+
26+
class TestBroadPhase(TestCollisionDetection):
27+
def test_collision_detect(self):
28+
self.assertTrue(collision_detection.broad_phase(self.body1, self.body2))
29+
self.assertFalse(collision_detection.broad_phase(self.body3, self.body4))
30+
31+
if __name__ == '__main__':
32+
unittest.main()

0 commit comments

Comments
 (0)