0

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

  • 1
    `mask` doesn't keep object's position so it doesn't know if two objects are close or far away, and it doesn't know if first object is on left or right of second object, etc. - and it needs distance between objects - and this is `offset`. `offset` it is `distance` between two objects. – furas May 04 '22 at 23:25
  • PyGame doc: [overla](https://www.pygame.org/docs/ref/mask.html#pygame.mask.Mask.overlap) and [mask-offset-label](https://www.pygame.org/docs/ref/mask.html#mask-offset-label) – furas May 04 '22 at 23:28
  • @furas thank you, now I understand better, I tried to put as offset the coords of my character but nothing changes, collisions are detected only in left-top – Andrea Roncella May 05 '22 at 09:04
  • maybe you should blit mask on screen (instead of its rects) to see if they are not empty. – furas May 05 '22 at 10:53
  • @furas you can't blit a mask – Andrea Roncella May 05 '22 at 13:39
  • mask has [to_surface()](https://www.pygame.org/docs/ref/mask.html#pygame.mask.Mask.to_surface) which gives image/surface and you can blit it. – furas May 05 '22 at 14:33
  • I can see my mask, but masks don't have a position, so i always see them top left, the offset is ruining everything (i think) – Andrea Roncella May 06 '22 at 09:05
  • it doesn't need positions to compare two masks - it needs only distance between objects (and this is offset). But you can display character's mask in position `(0,0)` and collisionroom's mask in position `offset` to see if they collide. – furas May 06 '22 at 12:11

0 Answers0