PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

pixel perfect collision detection?

Hey guys I have some code I'd like to run by you. It seems to work, but it's late and I want some input on it before I go further. If you see any errors let me know.

my color mask is (255,0,255)

Code:

int mygame::CollisionDetect(Surface *ImageA,Rect &pos_A, Surface *ImageB,Rect &pos_B,Display *display)
{
int ax1 = pos_A.left,
ax2 = pos_A.left + ImageA->GetWidth(),
ay1 = pos_A.top,
ay2 = pos_A.top + ImageA->GetHeight();
int bx1 = pos_B.left,
bx2 = pos_B.left + ImageB->GetWidth(),
by1 = pos_B.top,
by2 = pos_B.top + ImageB->GetHeight();
int cx1,cx2,cy1,cy2;
if (ax2 < bx1 || bx2 < ax1 || ay2 < by1 || by2 < ay1);//no possible collision
else
{
//collision possible, now check for box boundry collision
if ( (ax1 >= bx1 && ax1 <= bx2) || (ax2 >= bx1 && ax2 <= bx2) || (ax1 <= bx2 && ax2 >= bx1) )
{
if ( (ay1 >= by1 && ay1 <= by2) || (ay2 >= by1 && ay2 <= by2) || (ay1 <= by2 && ay2 >= by1) )
{
//box boundry collision occured, now make a smaller intersection box to test
if (ax1<bx1)
{
cx1=bx1;
cx2=ax2;
}
else
{
cx1=ax1;
cx2=bx2;
}
if (ay1<by1)
{
cy1=by1;
cy2=ay2;
}
else
{
cy1=ay1;
cy2=by2;
}
Rasterizer *rastA = display->CreateRasterizer(ImageA);
Rasterizer *rastB = display->CreateRasterizer(ImageB);
//check to see if both pixels contain data (not masked)
for (int cx=cx1;cx<cx2;cx++)
{
for (int cy=cy1;cy<cy2;cy++)
{
if (rastA->GetPixel(cx-ax1,cy-ay1) != Color(255,0,255))
if (rastB->GetPixel(cx-bx1,cy-by1) != Color(255,0,255))
return 1;
}
}
}
}
}
return 0;
}


I couldn't find any built in Collision Detection so I wrote my own. Is this efficient or do you have any suggestions to make it more so?

amdzero
This thread: http://www.pocketmatrix.com/forums/view ... c&start=15 has my collision detection code. The fundamentals of the two are almost identical, except I use GetColorMask to find the transparent color, in case it's not always 255,0,255.

It's scary to think I wrote that just over a year ago.

You might want to consider creating a "Pixel cColorMask = Color(255,0,255);" and use that instead in the comparisons. Then you won't be calling the Color(,,) function so much.

At the beginning you're adding the Widths and Heights of the surfaces to get the lower-right points, which isn't right. If a 5x5 surface is at 10,5, then its range goes from 10,5 to 14,9 (not 15,10). You need to subtract 1 from those calculations.

-John
Thanks for the response I agree about the set mask I realized it this morning.

I have a question though, my math is a little off I think but if I test the small collision box with a < (less than) or > (greater than) then I don't have to subtract one right? what do you think?

btw your code looks much cleaner, I was looking for such a code for days.
Think about this:

Surface 1 is a 5x5 square at 0,0
Surface 2 is a 5x5 square at 5,3

Your function sets ax2=5 and bx1=5

Although these two surfaces can't intersect, your code thinks they might... You're checking for "ax2 < bx1", but ax2=bx1, so you'll run through the extra code even though you don't need to.

If you subtract 1 from ax2, you'll realize immediately they don't intersect.

-John
well I got my daughters colored pencils out ant mapped it on a grid and guess what...

You were right, damn the number zero! I forgot that the zero counted apparently. So, should I change my code to...

Code:

int ax1 = pos_A.left,
ax2 = pos_A.right-1,
ay1 = pos_A.top,
ay2 = pos_A.bottom-1;
int bx1 = pos_B.left,
bx2 = pos_B.right-1,
by1 = pos_B.top,
by2 = pos_B.bottom-1;


(thanks for the responses btw)