From 1ad17e0dae1f3b55dc8b56a856e487d468791c4d Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 20 Sep 2013 23:36:28 +0100 Subject: [PATCH] [Fix]? F*ck collisions!! I'll fix it tomorrow. kbai. --- src/collision.c | 55 +++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/src/collision.c b/src/collision.c index 102e124..0544505 100644 --- a/src/collision.c +++ b/src/collision.c @@ -141,13 +141,9 @@ int CollideLineSprite(const Vec2* ap, double ad, double al, const glTexture* bt, const int bsx, const int bsy, const Vec2* bp, Vec2* crash) { - (void) bt; - (void) bsx; - (void) bsy; - int x, y, rbsy, bbx, bby; double ep[2], bl[2], tr[2], v[2], mod; - int hits; + int hits, real_hits; Vec2 tmp_crash, border[2]; /* Set up end point of line. */ @@ -196,21 +192,25 @@ int CollideLineSprite(const Vec2* ap, double ad, double al, } /* No hits - missed. */ - if(hits == 0) + if(hits == 2) return 0; - /* Special case only one hit - Shouldn't happen, but just in case. */ + /* Beam must die in the rectangle. */ if(hits == 1) { - if(hits == 1) { - /* We just return the same point twise. */ - crash[0].x = crash[1].x = border[0].x; - crash[0].y = crash[1].y = border[0].y; - } - return 1; + border[1].x = ep[0]; + border[1].y = ep[1]; } + /* + crash[0].x = border[0].x; + crash[0].y = border[0].y; + crash[1].x = border[1].x; + crash[1].y = border[1].y; + return 1; + */ + /* Now we do a pixel perfect approach. */ - hits = 0; + real_hits = 0; /* directional vector (normalised). */ v[0] = border[1].x - border[0].x; v[1] = border[1].y - border[0].y; @@ -232,35 +232,40 @@ int CollideLineSprite(const Vec2* ap, double ad, double al, while((x > 0.) && (x < bt->sw) && (y > 0.) && (y < bt->sh)) { /* Is non-transparent. */ if(!gl_isTrans(bt, bbx+(int)x, bby+(int)y)) { - crash[0].x = x + bl[0]; - crash[0].y = y + bl[1]; - hits++; + crash[real_hits].x = x + bl[0]; + crash[real_hits].y = y + bl[1]; + real_hits++; break; } - x += v[0]; y += v[1]; } - if(hits != 1) return 0; /* We actually missed. */ - /* Now we check the second border. */ x = border[1].x - bl[0] - v[0]; y = border[1].y - bl[1] - v[1]; while((x > 0.) && (x < bt->sw) && (y > 0.) && (y < bt->sh)) { /* Is non-transparent. */ if(!gl_isTrans(bt, bbx+(int)x, bby+(int)y)) { - crash[1].x = x + bl[0]; - crash[1].y = y + bl[1]; - hits++; + crash[real_hits].x = x + bl[0]; + crash[real_hits].y = y + bl[1]; + real_hits++; break; } - x -= v[0]; y -= v[1]; } - if(hits != 2) return 0; /* We actually missed. */ + /* Actually missed. */ + if(real_hits == 0) + return 0; + + /* Strange situation, should never happen, but just in case we duplicate + * the hits. */ + if(real_hits == 1) { + crash[1].x = crash[0].x; + crash[1].y = crash[0].y; + } /* We hit! */ return 1;