[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] I want to do pixel collision detection



> I want to know how to do pixel collision detection in pygame and how to
> rotate an image. Can someone please help?

hello. there has been some talk about pixel-level collision detection.
in an earlier thread Peter Nicolai wrote up some quick code that should
be able to find pixel collisions. this has been a topic i've discussed
with users of the C SDL library, and the usual opinion is that pixel
level collision is too slow (even in C code). therefore, if you do end
up using some sort of pixel collision detection, i'd recommend using it
only where you absolutely have to, because there is a significant
performance penalty.

anyways, you will need to use the pygame surfarray module to work with
individual pixels. (btw, this depends on Numeric python) if needed you
could go back in the pygame mailing list archives to find the code, but
it went something like this...

find the overlapping area for each sprite's rectangle area. then grab
the alpha or colorkey array for your surface with the surfarray module.
from each array, slice out the area that is overlapping. then simply
use Numeric Python's elementwise-logical-and operator to see if any
pixels overlap.

that's how it would work, but i would still recommend just checking
the areas for each sprite collision. you can easily use the Rect.inflate()
method to make a smaller version of the rectangle. or even break each
sprite into 2 or 3 smaller rectangle areas to check collision against.

at one point i was planning to write a pixel-level collision detection
for pygame, but after discussing it with other C game developers i've
decided against it. to really use a lot of pixel collisions, a generic
solution will likely be too slow anyways. on the other hand, if someone
does write one that works good, i wouldn't be opposed to adding it into
the pygame library :]


as for the rotations. if you are just thinking about 90 degree rotations
this can easily be done, again using the surfarray module. Using Numeric's
"transpose" and negative slicing to flip (like in the surfarray module)
you should be able to very quickly rotate the image in any 90 degree
increment.

if you are hoping for arbitrary rotation angles, i'm afraid there's
currently nothing to do that. SDL does not provide any rotation or
scaling functions, so it's not something immediately available in pygame.
nonetheless, scaling and rotation are two features i really want to add
into pygame, so in a coming 1.1 or 1.2 release, there will be a pygame
module to do arbitrary scaling and rotation.


well, leonardo, hopefully these answers will get you going. i'm afraid
that what you are looking for is not really supported in pygame. mainly
because these are not functions available in SDL, the library which
pygame wraps on top of. eventually some of these functions will be coming.
good luck


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org