[Fix] Silly collision bug. Pixel-Perfect collision should now be *perfect*(tm).

This commit is contained in:
Allanis 2013-02-12 20:22:35 +00:00
parent e1b23f928b
commit d288a2db9f
2 changed files with 19 additions and 7 deletions

View File

@ -38,11 +38,21 @@ int CollideSprite(const gl_texture* at, const int asx, const int asy, const Vec2
int inter_y0 = MAX(ay1, by1); int inter_y0 = MAX(ay1, by1);
int inter_y1 = MIN(ay2, by2); int inter_y1 = MIN(ay2, by2);
// Real vertical sprite value (flipped).
int rasy = at->sy - asy - 1;
int rbsy = bt->sy - bsy - 1;
// Set up the base points.
int abx = asx*(int)(at->sw) - ax1;
int aby = rasy*(int)(at->sh) - ay1;
int bbx = bsx*(int)(bt->sw) - bx1;
int bby = rbsy*(int)(bt->sh) - by1;
for(y = inter_y0; y <= inter_y1; y++) for(y = inter_y0; y <= inter_y1; y++)
for(x = inter_x0; x <= inter_x1; x++) for(x = inter_x0; x <= inter_x1; x++)
// Computer offsets for surface before passing to TransparentPixel test. // Computer offsets for surface before passing to TransparentPixel test.
if((!gl_isTrans(at, asx*(int)(at->sw) + x-ax1, asy*(int)(at->sh) + y-ay1)) && if((!gl_isTrans(at, abx + x, aby + y)) &&
(!gl_isTrans(bt, bsx*(int)(bt->sw) + x-bx1, bsy*(int)(bt->sh) + y-by1))) (!gl_isTrans(bt, bbx + x, bby + y)))
return 1; return 1;
return 0; return 0;

View File

@ -148,7 +148,7 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) {
if(rw)*rw = potw; if(rw)*rw = potw;
if(rh)*rh = poth; if(rh)*rh = poth;
if(surface->w != potw || surface->h != poth) { if((surface->w != potw) || (surface->h != poth)) {
// Size isn't original. // Size isn't original.
SDL_Rect rtemp; SDL_Rect rtemp;
rtemp.x = rtemp.y = 0; rtemp.x = rtemp.y = 0;
@ -273,17 +273,19 @@ gl_texture* gl_newImage(const char* path) {
SDL_FreeSurface(tmp); // Free the temp surface. SDL_FreeSurface(tmp); // Free the temp surface.
SDL_LockSurface(surface);
trans = SDL_MapTrans(surface);
SDL_UnlockSurface(surface);
if(SDL_VFlipSurface(surface)) { if(SDL_VFlipSurface(surface)) {
WARN("Error flipping surface"); WARN("Error flipping surface");
return NULL; return NULL;
} }
// Do after flipping for collision detection.
SDL_LockSurface(surface);
trans = SDL_MapTrans(surface);
SDL_UnlockSurface(surface);
t = gl_loadImage(surface); t = gl_loadImage(surface);
t->trans = trans; t->trans = trans;
return t; return t;
} }