It’s not really clear to me what this scan buffer stuff is exactly, but you are on the right track it looks like, I think.
In one of my games, I do pixel level collision detection between 2 sprites on the screen. It basically comes down to checking for overlapping non-colorkey pixels. If there is any overlap, a collision has occured. Here’s the code for it, which right now only supports 16 bit color mode, but shouldn’t be too hard to change to different depths.
bool sprite_collision_check(int x1, int y1, Sprite *s1, int x2, int y2, Sprite *s2)
{
bool r = false;
int x, y, w, h, x3, y3, x4, y4;
x3 = x1 + s1->width;
x4 = x2 + s2->width;
if (x1 > x2) {
w = ((x3 > x4) ? x4 : x3) - x1;
x2 = x1 - x2;
x1 = 0;
} else {
w = ((x3 > x4) ? x4 : x3) - x2;
x1 = x2 - x1;
x2 = 0;
}
y3 = y1 + s1->height;
y4 = y2 + s2->height;
if (y1 > y2) {
h = ((y3 > y4) ? y4 : y3) - y1;
y2 = y1 - y2;
y1 = 0;
} else {
h = ((y3 > y4) ? y4 : y3) - y2;
y1 = y2 - y1;
y2 = 0;
}
// check bounding box overlap first
if ((w < 1) || (h < 1))
return false;
if (SDL_MUSTLOCK(s1->bitmap))
while (SDL_LockSurface(s1->bitmap) < 0);
if (SDL_MUSTLOCK(s2->bitmap))
while (SDL_LockSurface(s2->bitmap) < 0);
if (screen_bpp == 8) {
} else { // screen_bpp == 16
int pitch1, pitch2;
Uint16 *ptr1, *ptr2;
pitch1 = s1->bitmap->pitch >> 1;
pitch2 = s2->bitmap->pitch >> 1;
ptr1 = (Uint16 *) s1->bitmap->pixels + y1 * pitch1 + x1;
ptr2 = (Uint16 *) s2->bitmap->pixels + y2 * pitch2 + x2;
pitch1 -= w;
pitch2 -= w;
for (y=0; y<h; y++) {
for (x=0; x<w; x++) {
if (*ptr1++ && *ptr2) {
r = true;
y = h;
break;
}
ptr2++;
}
ptr1 += pitch1;
ptr2 += pitch2;
}
}
if (SDL_MUSTLOCK(s1->bitmap))
SDL_UnlockSurface(s1->bitmap);
if (SDL_MUSTLOCK(s2->bitmap))
SDL_UnlockSurface(s2->bitmap);
return r;
}
-Jason----- Original Message -----
From: Richard Perrine
To: SDLgroup
Sent: Friday, March 22, 2002 9:49 AM
Subject: [SDL] collision detection
Hi!
Is there someone out there that can give me some ideas or some code that helps me with collision detection? I’ve looked at using a color scan method and am not sure about it. Is there something that SDL allows that would make collision detection easier?
I am not too picky. I’m currently trying to move a bitmap across the screen that has a image of a path. I would like to detect collision between the bmp and the edge of the path to prevent the bmp from leaving the path. I’m currently trying to get this example code to work, but having difficulties:
int Color_Scan(int x1, int y1, int x2, int y2,
UCHAR scan_color, UCHAR *scan_buffer, int scan_lpitch)
{
// this function implements a crude collision technique
// based on scanning for a specific color within a rectangle
// clip rectangle
// x coords first
if (x1 >= SCREEN_WIDTH)
x1=SCREEN_WIDTH-1;
else
if (x1 < 0)
x1=0;
if (x2 >= SCREEN_WIDTH)
x2=SCREEN_WIDTH-1;
else
if (x2 < 0)
x2=0;
// now y-coords
if (y1 >= SCREEN_HEIGHT)
y1=SCREEN_HEIGHT-1;
else
if (y1 < 0)
y1=0;
if (y2 >= SCREEN_HEIGHT)
y2=SCREEN_HEIGHT-1;
else
if (y2 < 0)
y2=0;
// scan the region
scan_buffer +=y1*scan_lpitch;
for (int scan_y=y1; scan_y<=y2; scan_y++)
{
for (int scan_x=x1; scan_x<=x2; scan_x++)
{
if (scan_buffer[scan_x] == scan_color)
return(1);
} // end for x
// move down a line
scan_buffer+=scan_lpitch;
} // end for y
By the way, thanks! I solved my colorkey prob!
Richard
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards?