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

[pygame] Some particle system insights




I've been writing weapons code and associated particle effects for my
game today, and doing a little benchmarking. I thought I'd share what
I've learned.

My game involves 16x16 characters moving around a playing field made
up of 16x16 blocks, shooting (among other things) 1x1 bullets at each
other. I was unsure how the bullets should be represented - as points,
using set_at, or as very small rects, using blit. Since I plan to have
a lot of bullets in the air simultaneously, I needed to know which was
more efficient.

I found that set_at was more efficient than blitting 1x1 rects by a
factor of about 8x, _even on non-accelerated surfaces_. This implies
that for very small sprites (2x3 or smaller), going with direct pixel
access is faster than blitting, even on non-hardware platforms like
XWindow. When I tried it under windows, I found that the factor was
around 12x.

Wow, I said, terrific! I rewrote my bullet code to use points and
set_at, and was disappointed. Performance fell! Actually, the
performance was better, right up until I did collision detection.

It took me a while to figure this out, but it turns out that:

for rect in things_a_bullet_can_hit:
    if rect.collidepoint(bullet_xy):
       do_something()

is actually about 15 times slower than doing:

if bullet_rect.collidelist(things_a_bullet_can_hit):
   do_something()

The difference was eating up the improved performance. The lesson is:
pygame's collision routines are better at detecting collisions between
rects than between points and rects.

The quick solution is clear - keep using set_at(point) to draw my
bullets, but convert them to a temporary rect just before doing
collision detection. Inelegant, but the performance is there - the
new draw/collide routines are 6x faster than the old ones.

Is there another, cleaner solution? A point class with
point.collidelist(rectstyle list) and point.collidelistall(rectstyle
list) methods? Those features would certainly help me, if they can be
made as fast as the corresponding rect functions.

So - would a point class be a good idea?

David Clark
silenus@telus.net
-- 
Mother died today. Or maybe yesterday, I don't know. I had a telegram
from the home: "Mother passed away. Funeral tomorrow. Yours sincerely."
That doesn't mean anything. It could have been yesterday. 
                            - Albert Camus, The Outsider

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