Meaning of offset in pygame Mask.overlap methods

Posted by Alan on Game Development See other posts from Game Development or by Alan
Published on 2013-01-19T21:07:37Z Indexed on 2013/10/20 16:13 UTC
Read the original article Hit count: 310

I have a situation in which two rectangles collide, and I have to detect how much did they collide so so I can redraw the objects in a way that they are only touching each others edges.

It's a situation in which a moving ball should hit a completely unmovable wall and instantly stop moving. Since the ball sometimes moves multiple pixels per screen refresh, it it possible that it enters the wall with more that half its surface when the collision is detected, in which case i want to shift it position back to the point where it only touches the edges of the wall.

Here is the conceptual image it:

enter image description here

I decided to implement this with masks, and thought that i could supply the masks of both objects (wall and ball) and get the surface (as a square) of their intersection. However, there is also the offset parameter which i don't understand.

Here are the docs for the method:

Mask.overlap

    Returns the point of intersection if the masks 
    overlap with the given offset - or None if it does not overlap.
    Mask.overlap(othermask, offset) -> x,y

    The overlap tests uses the following offsets (which may be negative):

   +----+----------..
   |A   | yoffset
   |  +-+----------..
   +--|B
   |xoffset
   |  |
   :  :

© Game Development or respective owner

Related posts about collision-detection

Related posts about pygame