[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Directional collision detection.
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] Directional collision detection.
- From: B W <stabbingfinger@xxxxxxxxx>
- Date: Sun, 22 Feb 2009 13:53:07 -0800
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Sun, 22 Feb 2009 16:53:10 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=/1Qhjkoz/I4Tg/MhIR8fUWMvuTrfQhP607N476b4asM=; b=vo5wjsKqYSxN5zIigFG0qMWBvmOJNzW18p1gqmEuOSPaSS+FcHb8Tpi2+RP9eFArQO qOrBI+qdYdAx4AjhrAVGmUI02hGFX9twnWYxoHUQFCM4uTP7vS2khmp35sxj2A1Tfhst hWr2DNGlgSwHWzcSlcW5KqAX7wPZWxLC9ZKOo=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=ppoa1ooGh8zVsPb6rQg+o79L/BHeFlShsIvnP7DqcenyW13hgtdtr15xCSG92kU/DQ ZFFUCepxCVSVtiLwiRYRjIEZVsavFCx5U5b9DHTpy8gUyrcjs1b8+JM3oVNZ/rpkDRJ2 BftC7ABAddtx88AXIL/E3zz9MA9j1sgb8LBw0=
- In-reply-to: <49A1C1F7.6090506@xxxxxxxxx>
- References: <49A1C1F7.6090506@xxxxxxxxx>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
I did the following, which adequately fit my needs. Unfortunately
there is some ambiguity to overcome. For example, if one rect is
moving at speed 5,5 and the other at -5,-5 their corners will collide.
In this case two sides are in contact and you must quantify it in
program code to decide which way they should rebound.
class Struct(object):
def __init__(self, **entries):
self.__dict__.update(entries)
def collide_edges(r, other):
"""Detect the edges of rectangle r that collide with rectangle
other. Returns an ad hoc object with boolean attributes left,
right, top, and bottom whose values indicate whether that side
collided."""
Rect = pygame.Rect
side = Struct(left=False, right=False, top=False, bottom=False)
left=Rect(r.left, r.top+1, 1, r.height-2)
right=Rect(r.right, r.top+1, 1, r.height-2)
top=Rect(r.left+1, r.top, r.width-2, 1)
bottom=Rect(r.left+1, r.bottom, r.width-2, 1)
if left.colliderect(other):
side.left = True
if right.colliderect(other):
side.right = True
if bottom.colliderect(other):
side.bottom = True
if top.colliderect(other):
side.top = True
return side
if __name__ == '__main__':
pygame.init()
r1 = pygame.Rect(1,1,10,10)
r2 = pygame.Rect(9,9,10,10)
edges = collide_edges(r1, r2)
print '%s: %s' % ('left',edges.left)
print '%s: %s' % ('right',edges.right)
print '%s: %s' % ('top',edges.top)
print '%s: %s' % ('bottom',edges.bottom)
fin
On Sun, Feb 22, 2009 at 1:21 PM, Daniel Mateos <daniel@xxxxxxxxx> wrote:
> Hey,
>
> Can anyone suggest a good way to do collision detection between two
> rects in a way that i can also get the side of the rect that the
> collision was detected on.
>
> --
> Daniel Mateos
> http://daniel.mateos.cc
>
>