Implemented Solid Collisions

This commit is contained in:
2022-06-29 18:49:11 +02:00
parent 29854b2b5c
commit 5b0157bfc9
2 changed files with 33 additions and 10 deletions
+7 -6
View File
@@ -142,17 +142,18 @@ class ColumbusEnv(gym.Env):
def check_collisions_for(self, entity):
for other in self.entities:
if other != entity:
if self._check_collision_between(entity, other):
entity.on_collision(other)
other.on_collision(entity)
depth = self._check_collision_between(entity, other)
if depth > 0:
entity.on_collision(other, depth)
other.on_collision(entity, depth)
def _check_collision_between(self, e1, e2):
shapes = [e1.shape, e2.shape]
shapes.sort()
if shapes == ['circle', 'circle']:
sq_dist = ((e1.pos[0]-e2.pos[0])*self.width) ** 2 \
+ ((e1.pos[1]-e2.pos[1])*self.height)**2
return sq_dist < (e1.radius + e2.radius)**2
dist = math.sqrt(((e1.pos[0]-e2.pos[0])*self.width) ** 2
+ ((e1.pos[1]-e2.pos[1])*self.height)**2)
return max(0, e1.radius + e2.radius - dist)
else:
raise Exception(
'Checking for collision between unsupported shapes: '+str(shapes))