Bug when moving diagonally and handling collision in pygame [duplicate]

I have a collision detection setup, and it works fine. The collision is detected, and it resolves it properly IF the player is moving in one direction. Collision detection and how to resolve it isn’t the issue here.

The issue is that when the player keeps moving diagonally when colliding with the wall, i want them to slide in the respective direction. Other collision answers here stop them straight in place. So far, I have it working only on the left and right sides of the rect

If the player is moving diagonally, then it starts to break. if they move diagonally and hit the left or right side of the object it is colliding with, they will just slide up the wall like normal. If they are moving diagonally and hit the top or bottom, then it bugs out, likely due to the order of the code that resolves it. I need to know how to fix it when the player collides with the top and bottom of an object when moving the player is diagonally, the left and right sides are fine.

I had a video to show, but i cant figure out how to upload it here.

Please don’t report this question for being similar to other pygame collision questions, I have looked and cant find an answer anywhere and I’m at a loss. If you do happen to have a question with an answer, add a comment with the link and I will check if it works, but I have tried loads of answers and none have worked.

If you want a video of whats happening, leave a comment and i can send it to discord, or let me know how to share videos here 🙂

    def get_collisions(self, rect, entities):
        hit_list = []
        for entity in entities:
            if rect.colliderect(entity.rect):
                hit_list.append(entity.rect)
        return hit_list

    def update(self, player, collision_group):

        movement = [player.velocity_x, player.velocity_y]

        collision_types = {
            'top': False,
            'bottom': False,
            'left': False,
            'right': False,
        }

        player.rect.x += movement[0]
        hit_list = self.get_collisions(player.rect, collision_group)
        for entity in hit_list:
            if movement[0] > 0:
                player.rect.right = entity.left
                collision_types['right'] = True
            elif movement[0] < 0:
                player.rect.left = entity.right
                collision_types['right'] = False
        
        player.rect.y += movement[1]
        hit_list = self.get_collisions(player.rect, collision_group)
        for entity in hit_list:
            if movement[1] > 0:
                player.rect.bottom = entity.top
                collision_types['bottom'] = True
            elif movement[1] < 0:
                player.rect.top = entity.bottom
                collision_types['top'] = False
        
        player.pos = [player.rect.x, player.rect.y]

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật