Original Post
I have a algorithim that does collion detection between two images by comparing the color of parts of the images. The problem is this algorithem access some image sturcture I don't know. I'd like to convert it to compare two LPDIRECTDRAWSURFACE4 surfaces. The original code looks like this:
Edited by - afastrunner on 11/16/00 4:27:33 PM
// Full object-to-object pixel-level collision detector:
short int Sprite_Collide(sprite_ptr object1, sprite_ptr object2) {
int left1, left2, over_left;
int right1, right2, over_right;
int top1, top2, over_top;
int bottom1, bottom2, over_bottom;
int over_width, over_height;
int i, j;
unsigned char *pixel1, *pixel2;
left1 = object1->x;
left2 = object2->x;
right1 = object1->x + object1->width;
right2 = object2->x + object2->width;
top1 = object1->y;
top2 = object2->y;
bottom1 = object1->y + object1->height;
bottom2 = object2->y + object2->height;
// Trivial rejections:
if (bottom1 < top2) return(0);
if (top1 > bottom2) return(0);
if (right1 < left2) return(0);
if (left1 > right2) return(0);
// Ok, compute the rectangle of overlap:
if (bottom1 > bottom2) over_bottom = bottom2;
else over_bottom = bottom1;
if (top1 < top2) over_top = top2;
else over_top = top1;
if (right1 > right2) over_right = right2;
else over_right = right1;
if (left1 < left2) over_left = left2;
else over_left = left1;
// Now compute starting offsets into both object's bitmaps:
i = ((over_top - object1->y) * object1->width) + over_left;
pixel1 = object1->frames[object1->curr_frame] + i;
//HERE I'D LIKE TO ACCESS AN LPDIRECTDRAWSURFACE4 SUFACE
j = ((over_top - object2->y) * object2->width) + over_left;
pixel2 = object2->frames[object2->curr_frame] + j;
//HERE I'D LIKE TO ACCESS AN LPDIRECTDRAWSURFACE4 SUFACE
// Now start scanning the whole rectangle of overlap, checking the
// corresponding pixel of each object's bitmap to see if they're both non-zero:
for (i=0; i < over_height; i++) {
for (j=0; j < over_width; j++) {
if (*pixel1 > 0) && (*pixel2 > 0) return(1);
pixel1++;
pixel2++;
};
pixel1 += (object1->width - over_width);
pixel2 += (object2->width - over_width);
};
// Worst case! We scanned through the whole darn rectangle of overlap
// and couldn't find a single colliding pixel!
return(0);
};
|