コード例 #1
0
    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
コード例 #2
0
    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
コード例 #3
0
 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
コード例 #4
0
    def collision_event(self, other_collider):

        # collided with a wall, floor, platform
        if self.entity.world.is_ground(other_collider.entity):

            # hit from the top which means that this collider bottom side was hit by the other collider
            if PhysicsSystem.calc_box_hit_orientation(self.entity.collider, other_collider) == PhysicsSystem.bottom:
                self.grounded = True
コード例 #5
0
    def collision_event(self, other_collider):

        # collided with a wall, floor, platform
        if self.entity.world.is_ground(other_collider.entity):

            # hit from the top which means that this collider bottom side was hit by the other collider
            if PhysicsSystem.calc_box_hit_orientation(
                    self.entity.collider,
                    other_collider) == PhysicsSystem.bottom:
                self.grounded = True
コード例 #6
0
    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
コード例 #7
0
    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
コード例 #8
0
 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