Enjoy. :) Credit for Struct goes here (with many other savvy tricks and obscurities):
Python Infrequently Asked Questions http://norvig.com/python-iaq.html fin On Sun, Feb 22, 2009 at 1:58 PM, René Dudfield <ren...@gmail.com> wrote: > ps. I like your Struct class there! very clever, haven't seen that before. > > cu, > > On Mon, Feb 23, 2009 at 8:53 AM, B W <stabbingfin...@gmail.com> wrote: >> 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 <dan...@mateos.cc> 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 >>> >>> >> >