[Super-tux-commit] supertux/lib/special collision.cpp,1.1,1.2 collision.h,1.1,1.2
Brought to you by:
wkendrick
From: Matze B. <mat...@us...> - 2004-11-24 03:43:19
|
Update of /cvsroot/super-tux/supertux/lib/special In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22752/lib/special Modified Files: collision.cpp collision.h Log Message: Improved collision detection by taking movement into account. Fixed long standing bug where returning from bonuslevel to mainmenu and going back to bonuslevels makes supertux crash Index: collision.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/collision.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- collision.h 20 Nov 2004 22:14:36 -0000 1.1 +++ collision.h 24 Nov 2004 03:42:57 -0000 1.2 @@ -4,6 +4,7 @@ namespace SuperTux { +class Vector; class Rectangle; class CollisionHit; class AATriangle; @@ -15,13 +16,13 @@ * collision and fills in the hit structure then. */ static bool rectangle_rectangle(CollisionHit& hit, const Rectangle& r1, - const Rectangle& r2); + const Vector& movement, const Rectangle& r2); /** does collision detection between a rectangle and an axis aligned triangle * Returns true in case of a collision and fills in the hit structure then. */ static bool rectangle_aatriangle(CollisionHit& hit, const Rectangle& rect, - const AATriangle& triangle); + const Vector& movement, const AATriangle& triangle); }; } Index: collision.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/collision.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- collision.cpp 20 Nov 2004 22:14:36 -0000 1.1 +++ collision.cpp 24 Nov 2004 03:42:57 -0000 1.2 @@ -15,38 +15,51 @@ namespace SuperTux { +static const float DELTA = .0001; + bool Collision::rectangle_rectangle(CollisionHit& hit, const Rectangle& r1, - const Rectangle& r2) + const Vector& movement, const Rectangle& r2) { if(r1.p2.x < r2.p1.x || r1.p1.x > r2.p2.x) return false; if(r1.p2.y < r2.p1.y || r1.p1.y > r2.p2.y) return false; - - hit.depth = r1.p2.x - r2.p1.x; - hit.normal.x = -1; - hit.normal.y = 0; - float px2 = r2.p2.x - r1.p1.x; - if(px2 < hit.depth) { - hit.depth = px2; + float xr; + if(movement.x > DELTA) { + hit.depth = r1.p2.x - r2.p1.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.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; + } } - float py1 = r1.p2.y - r2.p1.y; - if(py1 < hit.depth) { - hit.depth = py1; - hit.normal.x = 0; - hit.normal.y = -1; - } - - float py2 = r2.p2.y - r1.p1.y; - if(py2 < hit.depth) { - hit.depth = py2; - hit.normal.x = 0; - hit.normal.y = 1; + if(movement.y > DELTA) { + float ydepth = r1.p2.y - r2.p1.y; + float yr = ydepth / movement.y; + if(yr < xr) { + hit.depth = ydepth; + 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) { + hit.depth = ydepth; + hit.normal.x = 0; + hit.normal.y = 1; + } } return true; @@ -65,9 +78,9 @@ bool Collision::rectangle_aatriangle(CollisionHit& hit, const Rectangle& rect, - const AATriangle& triangle) + const Vector& movement, const AATriangle& triangle) { - if(!rectangle_rectangle(hit, rect, (const Rectangle&) triangle)) + if(!rectangle_rectangle(hit, rect, movement, (const Rectangle&) triangle)) return false; Vector normal; |