//----------------------------------------------------------------------------- int SDL_CollidePixel(SDL_Surface *as , int ax , int ay , SDL_Surface *bs , int bx , int by, int skip) { int ax1 = ax + as->w - 1; int ay1 = ay + as->h - 1; int bx1 = bx + bs->w - 1; int by1 = by + bs->h - 1; if((bx1 < ax) || (ax1 < bx)) { return false; } if((by1 < ay) || (ay1 < by)) { return false; } int xstart = SDL_COLLIDE_MAX(ax,bx); int xend = SDL_COLLIDE_MIN(ax1,bx1); int ystart = SDL_COLLIDE_MAX(ay,by); int yend = SDL_COLLIDE_MIN(ay1,by1); for(int y = ystart ; y <= yend ; y += skip) { for(int x = xstart ; x <= xend ; x += skip) { if(!SDL_CollideTransparentPixel(as , x-ax , y-ay) && !SDL_CollideTransparentPixel(bs , x-bx , y-by)) return 1; } } return 0; }
/* SDL pixel perfect collision test */ int SDL_CollidePixel(SDL_Surface *as , int ax , int ay , SDL_Surface *bs , int bx , int by, int skip) { /*a - bottom right co-ordinates in world space*/ int ax1 = ax + as->w - 1; int ay1 = ay + as->h - 1; /*b - bottom right co-ordinates in world space*/ int bx1 = bx + bs->w - 1; int by1 = by + bs->h - 1; /*check if bounding boxes intersect*/ if((bx1 < ax) || (ax1 < bx)) return 0; if((by1 < ay) || (ay1 < by)) return 0; /*Now lets make the bouding box for which we check for a pixel collision*/ /*To get the bounding box we do Ax1,Ay1______________ | | | | | | | Bx1,By1____________ | | | | | | | | |________|_______| | | Ax2,Ay2 | | | | | |___________Bx2,By2 To find that overlap we find the biggest left hand cordinate AND the smallest right hand co-ordinate To find it for y we do the biggest top y value AND the smallest bottom y value Therefore the overlap here is Bx1,By1 --> Ax2,Ay2 Remember Ax2 = Ax1 + SA->w Bx2 = Bx1 + SB->w Ay2 = Ay1 + SA->h By2 = By1 + SB->h */ /*now we loop round every pixel in area of intersection if 2 pixels alpha values on 2 surfaces at the same place != 0 then we have a collision*/ int xstart = SDL_COLLIDE_MAX(ax,bx); int xend = SDL_COLLIDE_MIN(ax1,bx1); int ystart = SDL_COLLIDE_MAX(ay,by); int yend = SDL_COLLIDE_MIN(ay1,by1); for(int y = ystart ; y <= yend ; y += skip) { for(int x = xstart ; x <= xend ; x += skip) { /*compute offsets for surface before pass to TransparentPixel test*/ if(!SDL_CollideTransparentPixel(as , x-ax , y-ay) && !SDL_CollideTransparentPixel(bs , x-bx , y-by)) return 1; } } return 0; }