-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobstacle.py
191 lines (155 loc) · 8.03 KB
/
obstacle.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
from typing import Type, Tuple
from vector import Vector
from shapes import Body, Circle, Rectangle
class Obstacle:
shape: Body
anchor_point: Vector
def __init__(self, initial_anchor_point: Tuple, shape: Type[Body], shape_args: Tuple) -> None:
self.shape = shape(*shape_args)
self.anchor_point = Vector.from_rectangular(list(initial_anchor_point))
return
def __str__(self) -> str:
return f"{self.shape}, Anchor point {self.anchor_point}"
def __repr__(self) -> str:
return self.__str__()
def is_colliding(self, other_obstacle: "Obstacle") -> bool:
if isinstance(self.shape, Circle) and isinstance(other_obstacle.shape, Circle):
# Calculate distance between centers
dx = self.anchor_point.components[0] - other_obstacle.anchor_point.components[0]
dy = self.anchor_point.components[1] - other_obstacle.anchor_point.components[1]
# TODO: optimize
distance = (dx**2 + dy**2) ** 0.5
return distance <= (self.shape.radius + other_obstacle.shape.radius)
elif isinstance(self.shape, Rectangle) and isinstance(other_obstacle.shape, Rectangle):
# Check for overlap
return not (
# self is to the right of other
self.anchor_point.components[0]
>= other_obstacle.anchor_point.components[0] + other_obstacle.shape.width
# self is to the left of other
or self.anchor_point.components[0] + self.shape.width <= other_obstacle.anchor_point.components[0]
# self is below other
or self.anchor_point.components[1]
>= other_obstacle.anchor_point.components[1] + other_obstacle.shape.height
# self is above other
or self.anchor_point.components[1] + self.shape.height <= other_obstacle.anchor_point.components[1]
)
elif (isinstance(self.shape, Circle) and isinstance(other_obstacle.shape, Rectangle)) or (
isinstance(self.shape, Rectangle) and isinstance(other_obstacle.shape, Circle)
):
# Check for overlap
if isinstance(self.shape, Circle):
circle = self
rect = other_obstacle
else:
circle = other_obstacle
rect = self
## Try this method(Copilot's) later (Commented out for now)
# # Find closest point on rectangle to circle
# closest_x = max(rect.anchor_point[0], min(circle.anchor_point[0], rect.anchor_point[0] + rect.shape.width))
# closest_y = max(rect.anchor_point[1], min(circle.anchor_point[1], rect.anchor_point[1] + rect.shape.height)
# # Calculate distance between closest point and circle center
# dx = circle.anchor_point[0] - closest_x
# dy = circle.anchor_point[1] - closest_y
# distance = (dx**2 + dy**2) ** 0.5
# return distance <= circle.shape.radius
## Method inspired from https://stackoverflow.com/a/402010/13483425
# Calculate absolute distance between the circle's center and the rectangle's center
dx = abs(circle.anchor_point.components[0] - (rect.anchor_point.components[0] + rect.shape.width / 2))
dy = abs(circle.anchor_point.components[1] - (rect.anchor_point.components[1] + rect.shape.height / 2))
# Check if circle is too far from rectangle in either dimension for an intersection
if dx > (rect.shape.width / 2 + circle.shape.radius):
return False
if dy > (rect.shape.height / 2 + circle.shape.radius):
return False
if dx <= (rect.shape.width / 2):
return True
if dy <= (rect.shape.height / 2):
return True
# Check for intersection with rectangle corner
cornerDistance_sq = (dx - rect.shape.width / 2) ** 2 + (dy - rect.shape.height / 2) ** 2
return cornerDistance_sq <= (circle.shape.radius**2)
def collision_axis(self, other_obstacle: "Obstacle") -> Tuple:
if (isinstance(self.shape, Circle) and isinstance(other_obstacle.shape, Rectangle)) or (
isinstance(self.shape, Rectangle) and isinstance(other_obstacle.shape, Circle)
):
if isinstance(self.shape, Circle):
circle = self
rect = other_obstacle
else:
circle = other_obstacle
rect = self
# Find the Closest Point on the Rectangle to the Circle’s Center
closest_x = max(
rect.anchor_point.components[0],
min(circle.anchor_point.components[0], rect.anchor_point.components[0] + rect.shape.width),
)
closest_y = max(
rect.anchor_point.components[1],
min(circle.anchor_point.components[1], rect.anchor_point.components[1] + rect.shape.height),
)
# Calculate distances from the circle's center to the closest point
dx = abs(closest_x - circle.anchor_point.components[0])
dy = abs(closest_y - circle.anchor_point.components[1])
# Determine the axis of collision
if dx > dy:
return "horizontal"
else:
return "vertical"
class StaticObstacle(Obstacle):
def __str__(self) -> str:
return f"Static Obstacle → {super().__str__()}"
def __repr__(self) -> str:
return self.__str__()
class DynamicObstacle(Obstacle):
velocity: Vector
ongoing_collisions: set["Obstacle"]
def __init__(self, initial_anchor_point: Tuple, velocity: Tuple, *args, **kwargs) -> None:
if len(velocity) != len(initial_anchor_point):
raise ValueError("Mismatched dimensions of position and velocity")
super().__init__(initial_anchor_point, *args, **kwargs)
self.velocity = Vector.from_rectangular(list(velocity))
self.ongoing_collisions = set()
def __str__(self) -> str:
return f"Dynamic Obstacle → {super().__str__()}, Velocity {self.velocity}"
def __repr__(self) -> str:
return self.__str__()
def move(self, t: float):
"""
Updates the anchor_point of the obstacle by time `t`
with its inherent velocity
"""
self.anchor_point += self.velocity.scale(t)
def ricochet(self, other_obstacle: Obstacle) -> None:
"""
Updates the velocity of the obstacle pair after a collision
"""
# For circle-circle collisions, directly swap velocity vectors
if isinstance(self.shape, Circle) and isinstance(other_obstacle.shape, Circle):
self.velocity, other_obstacle.velocity = other_obstacle.velocity, self.velocity
return
# For circle-rectangle collisions, reverse the velocity of the obstacle along the axis of collision
if (isinstance(self.shape, Circle) and isinstance(other_obstacle.shape, Rectangle)) or (
isinstance(self.shape, Rectangle) and isinstance(other_obstacle.shape, Circle)
):
axis = self.collision_axis(other_obstacle)
if isinstance(self.shape, Circle):
circle = self
rect = other_obstacle
else:
circle = other_obstacle
rect = self
if axis == "horizontal":
circle.velocity.components[0] *= -1
# rect.velocity.components[0] *= -1
elif axis == "vertical":
circle.velocity.components[1] *= -1
# rect.velocity.components[1] *= -1
def is_new_collision(self, other_obstacle: "Obstacle") -> bool:
collide_status: bool = self.is_colliding(other_obstacle)
result: bool = collide_status and other_obstacle not in self.ongoing_collisions
if result:
self.ongoing_collisions.add(other_obstacle)
elif not collide_status and other_obstacle in self.ongoing_collisions:
self.ongoing_collisions.remove(other_obstacle)
return result