February 17th, 2014, 01:43 AM
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:
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.
February 17th, 2014, 05:52 AM
Well assuming you are correctly creating your masks in the first place the collision code will be something like this:
First we check if any rect collisions occur. Then we check mask collisions only against the sprites that returned positive for rect collision.
collisions = pg.sprite.spritecollide(self, obstacles, False)
callback = pg.sprite.collide_mask
collide = pg.sprite.spritecollideany(self, collisions, callback)
#react to collision
Here is a full example here but it might be a bit much for the moment:
If you could create a git repo of your current attempt and graphic resources I will be able to help further.
February 17th, 2014, 07:12 PM
That was perfect. I got it to work right away after looking at that. Thanks! You are a rockstar
February 18th, 2014, 09:04 PM
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.