-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionManager.py
More file actions
102 lines (84 loc) · 3.3 KB
/
CollisionManager.py
File metadata and controls
102 lines (84 loc) · 3.3 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
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
#!/usr/bin/env python
"""CollisionManager module: handles collision detection and resolution."""
from typing import List
import pygame
__author__ = "Joshua Sonnenberg and Ethan Richardson"
class CollisionManager:
"""Manages collision detection between game entities."""
def check_hit_direction(self, one: pygame.Rect, two: pygame.Rect) -> List[int]:
"""Determine the direction of collision between two rectangles.
Args:
one: The first rectangle.
two: The second rectangle.
Returns:
A list [x_direction, y_direction] indicating collision direction.
"""
result = [0,0]
dTop = abs(one.y - (two.y + two.height))
dBot = abs((one.y+one.height) - two.y)
dRight = abs((one.x + one.width) - two.x)
dLeft = abs(one.x - (two.x+two.width))
if((dTop <= dRight) and (dTop <= dLeft) and (dTop < dBot)):
# Top
if(dTop == dRight): # Right Corner
result[0] = 1
elif(dTop == dLeft): # Left Corner
result[0] = -1
result[1] = -1
elif((dBot <= dRight) and (dBot <= dLeft)):
# Bottom
if(dBot == dRight): # Right Corner
result[0] = 1
elif(dBot == dLeft): # Left Corner
result[0] = -1
result[1] = 1
elif(dRight < dLeft):
# Right
result[0] = 1
else:
# Left
result[0] = -1
return result
def handleBoxHit(self, one: pygame.Rect, two: pygame.Rect, hit: List[int]) -> None:
"""Handle collision response between two boxes.
Args:
one: The first rectangle.
two: The second rectangle.
hit: The collision direction from check_hit_direction.
Note:
Direction grid:
1,1 0,1 -1,1
1,0 -1,0
1,-1 0,-1 -1,-1
"""
if(one.facing != 'LSTOP' or one.facing != 'RSTOP'):
# Right Edge - hit the left side of terrain while moving right
if(hit[0] == -1):
one.x = two.x + two.width
one.x += 1
# Left Edge - hit the right side of terrain while moving left
elif(hit[0] == 1):
one.x = two.x - one.width
one.x -= 1
# Bottom Edge
if(hit[1] == -1):
one.y = two.y + two.height
one.y += 5
# Top Edge - landed on terrain from above
elif(hit[1] == 1):
one.y = two.y - one.height
def check_collision(self, one: pygame.Rect, two: pygame.Rect) -> bool:
"""Check if two rectangles are colliding.
Args:
one: The first rectangle.
two: The second rectangle.
Returns:
True if rectangles are colliding, False otherwise.
"""
if(one.x <= (two.x+two.width)) and (two.x <= (one.x+one.width)):
if(one.y <= (two.y+two.height)) and (two.y <= (one.y+one.height)):
return True
else:
return False
else:
return False