[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Re: BOUNCE pygame-users@seul.org: Non-member submission from [MarkusBopp <markus.bopp@smail.inf.fh-bonn-rhein-sieg.de>]
- To: pygame-users@seul.org
- Subject: [pygame] Re: BOUNCE pygame-users@seul.org: Non-member submission from [MarkusBopp <markus.bopp@smail.inf.fh-bonn-rhein-sieg.de>]
- From: Pete Shinners <pete@shinners.org>
- Date: Thu, 23 Oct 2003 21:50:47 -0700
- Cc: markus.bopp@smail.inf.fh-bonn-rhein-sieg.de
- Delivered-to: archiver@seul.org
- Delivered-to: pygame-users-outgoing@seul.org
- Delivered-to: pygame-users@seul.org
- Delivery-date: Fri, 24 Oct 2003 00:57:29 -0400
- In-reply-to: <20031024011427.3A68533F4B@moria.seul.org>
- References: <20031024011427.3A68533F4B@moria.seul.org>
- Reply-to: pygame-users@seul.org
- Sender: owner-pygame-users@seul.org
- User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031020 Debian/1.5-1
I have a problem with checking rects overlapping.
I use this slow procedure to test if collision works:
for shot in shotlist:
for opp in opplist:
print opp.img.get_rect().colliderect( shot.img.get_rect() )
the problem is that colliderect always returns the index '1' although no
rects overlapped.
i assume you are not worried about optimizing this collision detection
for now, but there is a lot of simple work you can do for this 'slow
procedure'.
your problem is you are checking for collision against the image
rectangles. image rectangles always start at (0, 0) and have width and
height the same as the image. your image rectangles will always
overlap (unless perhaps one image is sized 0? but i don't believe SDL
allows that)
it's likely your objects have some sort of position attribute. if that
represents the topleft corner your code should look more like this
for shot in shotlist:
shotrect = shot.img.get_rect().move(shot.position)
for opp in opplist:
opprect = opp.img.get_rect().move(opp.position)
print opprect.colliderect(shotrect)
the bad news is that this will be even a bit slower. the good news is
it's almost easier to optimize for more speed.