def in_air(self, entity): for obj, correction in get_objects(entity.position[0], entity.position[0] + entity.size[0]): if geometry.rect_intersect(entity.to_rect(), obj.to_rect()): if self.state == self.state_air and correction == True: self.correct_collision_direction(entity, obj) return False return True
def on_ground(self, entity): below_entity = (entity.position[0] + 10, entity.position[1] + entity.size[1] + 1, entity.size[0] - 20, 10) for obj, _ in get_objects(entity.position[0], entity.position[0] + entity.size[0]): if geometry.rect_intersect(below_entity, obj.to_rect()): return True return False
def collision(self, entity): for obj, correction in get_objects(entity.position[0], entity.position[0] + entity.size[0]): if obj == entity: # do not check for collision with oneself continue if geometry.rect_intersect(entity.to_rect(), obj.to_rect()): direction = self.collision_direction(entity, obj) self.on_collide.fire(obj, direction) print "collision between ", entity, "and", obj return obj, correction return None, None
def on_ground_offset(self, entity, offset_x): """ Check if the entity is still on ground if we add some offset to its position """ below_entity = (entity.position[0] + 10 + offset_x, entity.position[1] + entity.size[1] + 1, entity.size[0] - 20, 10) for obj, _ in get_objects(entity.position[0] + offset_x, entity.position[0] + entity.size[0] + offset_x): if geometry.rect_intersect(below_entity, obj.to_rect()): return True return False
def backtrack_rect(self, entity, obj, direction): print "correcting position of", entity """ Perform a backtracking algorithm to move an entity so far into a specific direction until it does not hit any object anymore entity: The entity to be moved direction: A two-dimensional vector """ # TODO: Make real condition out of this quick&dirty fix # If no speed, set to upwards (we set speed to 0, 0 if person falls directly # downwards and lands) if direction == (0, 0): direction = (0, -1) length = math.sqrt(direction[0] * direction[0] + direction[1] * direction[1]) direction = (float(direction[0]) / length, float(direction[1]) / length) print "moving to ", direction while geometry.rect_intersect(entity.to_rect(), obj.to_rect()): entity.position = (entity.position[0] + direction[0], entity.position[1] + direction[1])