'Pygame mask collisions not detected
I know there are several questions about collisions but I can't understand how to do it, the mask of the two objects are created correctly (if I show the two rects, they are in the right place) I think I'm having troubles with the offset, I know that in the offset we have to put the distance between the top left corners of the images ex:
offset = (character_x - collisionrect_x), (charcater_y - collisionrect_y)
and I did it but:
- I can't get how the offset works, I searched online but I didn't find anything clear 2
- even if I put the right values, I always return no collisions.
Here is the function to detect collisions (simplified):
#inizialize collisions with room
collisionsroom1 = pygame.transform.scale(originalcollisionsroom1, (ROOTwidth/2.3, ROOTheight))
def collision_room1():
#room 1 collision
collisionsroom1_mask = pygame.mask.from_surface(collisionsroom1)
#character hitbox
characterhitbox_width = characterhitbox.get_width()
characterhitbox_height = characterhitbox.get_height()
characterhitbox_mask = pygame.mask.from_surface(characterhitbox)
offset = ((playermovement.playertopx + sprite_width/2 - characterhitbox_width/2.2) - (ROOTwidth/2 - room1_width/2), (playermovement.playertopy + sprite_height/2 - characterhitbox_height/2.2))
collision = characterhitbox_mask.overlap(collisionsroom1_mask, offset)
the values in the offset are correct, if I blit the two collisions in that position they are correct, I did (character_x - collisionrect_x), (charcater_y - collisionrect_y)
I return collision only if I go in the top - left, I don't know that to do
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|