Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
   |  |
   :  :
share|improve this question
    
I think it returns the x,y co-ordinates of the overlapping image. Something like this: i.imgur.com/nRe6Nd0.jpg –  DragoonHP Jan 21 '13 at 9:44
    
+1 for the diagram! Very neat! –  Jan Jan 23 '13 at 10:05
add comment

1 Answer

The offset gets added to the coordinates of all parts of the mask that you pass into the function.

So, if you have mask A:

000
001
001

and mask B:

000
010
000

and you ask

A.overlap(B, (ofsx, ofsy))

then ofsx and ofsy are added to the coordinates of B.

Imagine both masks in the context of a large raster with the top-left corner of A at the origin. B is shifted (ofsx, ofsy) within that raster before its overlap with A is calculated.

For example if you ask

A.overlap(B, (3, 4))

Then the following raster is evaluated for overlaps:

X X X X X X X X X X X X X X X X
X X X X X X X X X X X X X X X X
X X X X X X X X X X X X X X X X
X X X X X X X X X X X X X X X X
X X X X X X X X X X X X X X X X
X X X X X X X|0 0 0|X X X X X X
X X X X X X X|0 0 1|X X X X X X
X X X X X X X|0 0 1|X X X X X X
X X X X X X X X X X X X X X X X
X X X X X X X X X X|0 0 0|X X X <-  B @ imaginary origin + (3, 4)
X X X X X X X X X X|0 1 0|X X X
X X X X X X X X X X|0 0 0|X X X
X X X X X X X X X X X X X X X X
             ^     
             |     
             A @ imaginary origin

if you put negative values into the offset parameter, it shifts B in the other direction.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.