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

[pygame] Questions about rect.collidelistall



Chris McCormick writes:
 > I am trying to convert a program from tile-based collision
 > detection to pixel-based collision detection, and I see this function:
 > 
 > pygame.rect.collidelistall()
 > 
 > Where does the list of rectangles come from?  Do I have to create
 > it myself?  Does pygame keep a list entry for every rectangle I
 > create?

No, you have to create it yourself. It has to be that way, since you
may very well have rects that have nothing to do with sprites or game
actors, and you wouldn't want those to collide unnecessarily.

You may want to take a look at the pygame.sprite module; it's
basically a sophisticated series of lists of rects, with surfaces
attached.

 > 
 > And on a general level, if I have a world with 20 NPC's running
 > around, what's the easiest/fastest way to check for collisions?  Is
 >  there a way do do it in fewer than N*N steps per frame? 
 
Well, the worst it can actually be is (N/2)^2, since if sprite A
collides with sprite B, you don't need to check if B collides with A
anymore.  Another way to optimize it is to divide the screen up into
sextions, and keep separate lists of sprites - sprites on the far left
of the screen will never collide with sprites on the far right, so you
don't have to check them. Whether or not this is actually faster than
just running them through the collidelistall() function depends on
your design, and would have to be tested.

David Clark
Futility on #pygame at irc.openprojects.net


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