I want to translate a C++ function I found on the net. Here is the C++ code:
// 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 objects' bitmaps:
i = ((over_top - object1\1->y) * object1->width) + over_left;
pixel1 = object1->frames[object1->curr_frame] + i;
j = ((over_top - object2->y) * object2->width) + over_left;
pixel2 = object2->frames[object2->curr_frame] + j;
// 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);
};
I understand most of the code. I get lost at line 45 and downwards.
Some help?
PS I am using BufferedImage, and the getRGB methods are extremely slow. I am thinking to store all the colors in an int array, that is initialized before the game starts.