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)
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
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