Thread: Mask Collision

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0

    Mask Collision


    I am working on some sprite collision in a program. I can figure out the workings of the default block collision and have been using this program as a guide:

    http://programarcadegames.com/python_examples/f.php?file=bullets.py

    What I would like to do is mask collision with some bitmapped sprites, but I cannot figure out how to do that. I can modify that to throw in bitmaps and set a color key, but beyond that I have been hitting my head on a wall trying to figure out how to implement the mask collision. Some guidance would be appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Well assuming you are correctly creating your masks in the first place the collision code will be something like this:

    python Code:
    collisions = pg.sprite.spritecollide(self, obstacles, False)
    callback = pg.sprite.collide_mask
    collide = pg.sprite.spritecollideany(self, collisions, callback)
    if collide:
        #react to collision
    First we check if any rect collisions occur. Then we check mask collisions only against the sprites that returned positive for rect collision.

    Here is a full example here but it might be a bit much for the moment:
    https://github.com/Mekire/meks-pygam...ur_dir_mask.py

    If you could create a git repo of your current attempt and graphic resources I will be able to help further.

    -Mek
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0
    That was perfect. I got it to work right away after looking at that. Thanks! You are a rockstar
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0
    Okay, I have a followup question.

    Is there a way to buffer the collision? Either to expand where the collision zone is or contract it? If for example I want a little overlap before a wall stops a sprite from moving to create a sort of 3d feel, how would I go about doing that?

    Or if say I put a path between two walls (like a ladder or stairs or something) that is only as wide as the player sprite, how could I increase the overlap for the top of the path so the player could easily go down it without having to be pixel perfect in where he is walking? like if you are a little to the left, it would notice the collision with the ladder and shove the player to the center if they are walking down that way instead of just getting stuck on the wall.

IMN logo majestic logo threadwatch logo seochat tools logo