def colliding_with_ladder(self): # check if the player is colliding with a ladder for ladder in self.entity.world.ladders: if PhysicsSystem.tolerance_collision(self.entity.collider, ladder.collider): return True return False
def check_if_near_crate(self): result = (False, None) # check if the player is near a box for crate in self.entity.world.crates: # this code stops crates from being pushed inside of colliders such as walls for entity in self.entity.world.entity_manager.entities: # collider exists and is not a trigger valid_collider = entity.collider is not None and not entity.collider.is_trigger # don't consider the player or yourself during this collision test if valid_collider and entity is not self.entity and entity is not crate: # collision occurs if PhysicsSystem.box2box_collision(crate.collider, entity.collider): # check of the collision occurred from the sides side = PhysicsSystem.calc_box_hit_orientation( crate.collider, entity.collider) if side == PhysicsSystem.left or side == PhysicsSystem.right: # stop the crate from moving return False, None player = self.entity if PhysicsSystem.tolerance_collision(player.collider, crate.collider): side = PhysicsSystem.calc_box_hit_orientation( player.collider, crate.collider) if side == PhysicsSystem.left or side == PhysicsSystem.right: result = (True, crate) # some glitchy behavior required to offset the crate forward if the player moved the crate # towards to right and pushed the crate from the left side. Basically, we check the distance # between centers of the player and crate, and if the sum of the half-widths of both tolerance # hit boxes (of the player and create) are greater then the distance then we must offset # the create towards to right by some value distance = crate.transform.position - player.transform.position dx = abs(distance.x) x_tolerance_distance = crate.collider.tolerance_hitbox.w / 2.0 + player.collider.tolerance_hitbox.w / 2.0 if dx < x_tolerance_distance - 2: shift = x_tolerance_distance - dx # Player hits rate from his right and is pushing right if side == PhysicsSystem.right and player.rigid_body.velocity.x > 0: crate.transform.position.x += shift - 20 elif side == PhysicsSystem.left and player.rigid_body.velocity.x < 0: crate.transform.position.x -= shift - 20 return result
def check_if_near_crate(self): result = (False, None) # check if the player is near a box for crate in self.entity.world.crates: # this code stops crates from being pushed inside of colliders such as walls for entity in self.entity.world.entity_manager.entities: # collider exists and is not a trigger valid_collider = entity.collider is not None and not entity.collider.is_trigger # don't consider the player or yourself during this collision test if valid_collider and entity is not self.entity and entity is not crate: # collision occurs if PhysicsSystem.box2box_collision(crate.collider, entity.collider): # check of the collision occurred from the sides side = PhysicsSystem.calc_box_hit_orientation(crate.collider, entity.collider) if side == PhysicsSystem.left or side == PhysicsSystem.right: # stop the crate from moving return False, None player = self.entity if PhysicsSystem.tolerance_collision(player.collider, crate.collider): side = PhysicsSystem.calc_box_hit_orientation(player.collider, crate.collider) if side == PhysicsSystem.left or side == PhysicsSystem.right: result = (True, crate) # some glitchy behavior required to offset the crate forward if the player moved the crate # towards to right and pushed the crate from the left side. Basically, we check the distance # between centers of the player and crate, and if the sum of the half-widths of both tolerance # hit boxes (of the player and create) are greater then the distance then we must offset # the create towards to right by some value distance = crate.transform.position - player.transform.position dx = abs(distance.x) x_tolerance_distance = crate.collider.tolerance_hitbox.w/2.0 + player.collider.tolerance_hitbox.w/2.0 if dx < x_tolerance_distance-2: shift = x_tolerance_distance - dx # Player hits rate from his right and is pushing right if side == PhysicsSystem.right and player.rigid_body.velocity.x > 0: crate.transform.position.x += shift-20 elif side == PhysicsSystem.left and player.rigid_body.velocity.x < 0: crate.transform.position.x -= shift-20 return result
def test_if_grounded(self): self.grounded = False # iterate through object considered as ground for other in self.entity.world.ground: player = self.entity # if the player collided with an element considered as ground, then ground the player if PhysicsSystem.tolerance_collision(player.collider, other.collider): # check orientation of the collision orientation = PhysicsSystem.calc_box_hit_orientation if orientation(player.collider, other.collider) == PhysicsSystem.bottom: self.grounded = True