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