n0tis February 2016

Pygame collision detection, transparent border

For my pygame project, I must detect collision betweeen various .png image that move on the screen.

I have put the image inside sprite, and the sprite inside group, then I use this :

pygame.sprite.spritecollide(perso, zombie_group, False)

However, sometime, my image don't touch, but pygame detect a collision... This is due to the fact that my images are png with transparent borders.

The transparent border collide, and pygame detect this :(

Any idea to stop the transparent border from colliding ?

Answers


FabioCosta February 2016

Ok the sprite will take the image he can't detect if it was on a trasnparent BG or a color bg, he basically is just seeing a rectangle right now.

If you are using irregular shapes and a rectangle approximation is not enough I would recommend using collide_mask also check masks it is probably what you want

update

Regarding performance from the tutorial :

There are other ways to do this, with ANDing sprite masks and so on, but any way you do it in pygame, it’s probably going to be too slow. For most games, it’s probably better just to do ‘sub-rect collision’ – create a rect for each sprite that’s a little smaller than the actual image, and use that for collisions instead. It will be much faster, and in most cases the player won’t notice the inprecision.

Post Status

Asked in February 2016
Viewed 3,443 times
Voted 13
Answered 1 times

Search




Leave an answer