X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=lib%2Fspecial%2Fcollision.cpp;h=46ed0bf796152269a29f24133dc541d585e56831;hb=8e0bad9f82ccbc811a18edd7ce6c6f69c5bca082;hp=b0cb2295de76757030add74f855f7702ba9fcecf;hpb=22e7accd3e335951d82850ec2e54563e67e1cf97;p=supertux.git diff --git a/lib/special/collision.cpp b/lib/special/collision.cpp index b0cb2295d..46ed0bf79 100644 --- a/lib/special/collision.cpp +++ b/lib/special/collision.cpp @@ -26,37 +26,42 @@ Collision::rectangle_rectangle(CollisionHit& hit, const Rectangle& r1, if(r1.p2.y < r2.p1.y || r1.p1.y > r2.p2.y) return false; - float xr; if(movement.x > DELTA) { hit.depth = r1.p2.x - r2.p1.x; + hit.time = hit.depth / movement.x; hit.normal.x = -1; hit.normal.y = 0; - xr = hit.depth / movement.x; } else if(movement.x < -DELTA) { hit.depth = r2.p2.x - r1.p1.x; + hit.time = hit.depth / -movement.x; hit.normal.x = 1; hit.normal.y = 0; - xr = hit.depth / -movement.x; } else { - xr = FLT_MAX; if(movement.y > -DELTA && movement.y < DELTA) { - return false; + hit.time = 0; + hit.depth = 0; + hit.normal.x = 1; + hit.normal.y = 0; + return true; } + hit.time = FLT_MAX; } if(movement.y > DELTA) { float ydepth = r1.p2.y - r2.p1.y; - float yr = ydepth / movement.y; - if(yr < xr) { + float yt = ydepth / movement.y; + if(yt < hit.time) { hit.depth = ydepth; + hit.time = yt; hit.normal.x = 0; hit.normal.y = -1; } } else if(movement.y < -DELTA) { float ydepth = r2.p2.y - r1.p1.y; - float yr = ydepth / -movement.y; - if(yr < xr) { + float yt = ydepth / -movement.y; + if(yt < hit.time) { hit.depth = ydepth; + hit.time = yt; hit.normal.x = 0; hit.normal.y = 1; } @@ -82,36 +87,67 @@ Collision::rectangle_aatriangle(CollisionHit& hit, const Rectangle& rect, { if(!rectangle_rectangle(hit, rect, movement, (const Rectangle&) triangle)) return false; - + Vector normal; float c; Vector p1; - switch(triangle.dir) { + Vector tp1, tp2; + switch(triangle.dir & AATriangle::DEFORM_MASK) { + case 0: + tp1 = triangle.p1; + tp2 = triangle.p2; + break; + case AATriangle::DEFORM1: + tp1 = Vector(triangle.p1.x, triangle.p1.y + triangle.get_height()/2); + tp2 = triangle.p2; + break; + case AATriangle::DEFORM2: + tp1 = triangle.p1; + tp2 = Vector(triangle.p2.x, triangle.p1.y + triangle.get_height()/2); + break; + case AATriangle::DEFORM3: + tp1 = triangle.p1; + tp2 = Vector(triangle.p1.x + triangle.get_width()/2, triangle.p2.y); + break; + case AATriangle::DEFORM4: + tp1 = Vector(triangle.p1.x + triangle.get_width()/2, triangle.p1.y); + tp2 = triangle.p2; + break; + default: + assert(false); + } + + switch(triangle.dir & AATriangle::DIRECTION_MASK) { case AATriangle::SOUTHWEST: p1 = Vector(rect.p1.x, rect.p2.y); - makePlane(triangle.p1, triangle.p2, normal, c); + makePlane(tp1, tp2, normal, c); break; case AATriangle::NORTHEAST: p1 = Vector(rect.p2.x, rect.p1.y); - makePlane(triangle.p2, triangle.p1, normal, c); + makePlane(tp2, tp1, normal, c); break; case AATriangle::SOUTHEAST: p1 = rect.p2; - makePlane(Vector(triangle.p1.x, triangle.p2.y), - Vector(triangle.p2.x, triangle.p1.y), normal, c); + makePlane(Vector(tp1.x, tp2.y), + Vector(tp2.x, tp1.y), normal, c); break; case AATriangle::NORTHWEST: p1 = rect.p1; - makePlane(Vector(triangle.p2.x, triangle.p1.y), - Vector(triangle.p1.x, triangle.p2.y), normal, c); + makePlane(Vector(tp2.x, tp1.y), + Vector(tp1.x, tp2.y), normal, c); break; + default: + assert(false); } - float depth = -(normal * p1) - c; + float n_p1 = -(normal * p1); + float depth = n_p1 - c; if(depth < 0) return false; - if(depth < hit.depth) { + float time = depth / -(normal * movement); + if(time < hit.time) { hit.depth = depth; + hit.time = time; hit.normal = normal; }