Wednesday, 15 January 2014

c++ - 2D rectangle sliding collision doesn't work -



c++ - 2D rectangle sliding collision doesn't work -

so i'm developing little escape game, , want create sure can't walk through walls. want able slide across walls. mean when have wall on left, , press left , up, slide don't go through wall. first fabricated buggy code didn't work well. after searching on net found this:

http://xboxforums.create.msdn.com/forums/t/59723.aspx

it sounded logical , easy implemented in program. problem is still doesn't work, irregulary. allows slide (altough slower/faster expected), can slide faster , can't slide @ all. code looks this:

bool clevel::slidecol(sdl_rect* pos, sdl_rect* oldpos) { bool col = false; (int = 0; < walls.size(); ++i) { if (rectcol(*pos, walls[i])) { pos->x = oldpos->x; col = true; } if (rectcol(*pos, walls[i])) { pos->y = oldpos->y; col = true; } } homecoming col; }

it checks walls against current position. first adjusts x position, , if there still collision adjusts y position. can demo here if want see problem in action: https://dl.dropbox.com/u/65227065/operation%20get%20out.zip . i'm pretty sure rectcol function works, i've tested thoroughly. sure, here anyway:

bool rectcol(sdl_rect f, sdl_rect s) { bool ret = false; int t1, r1, b1, l1; int t2, r2, b2, l2; t1 = f.y; r1 = f.x + f.w; b1 = f.y + f.h; l1 = f.x; t2 = s.y; r2 = s.x + s.w; b2 = s.y + s.h; l2 = s.x; // er zijn vier situaties mogelijk: - rechtsonder1 overlapt linksboven2 // - linksonder1 overlapt rechtsboven2 // - linksboven1 overlapt rechtsonder2 // - rechtsboven1 overlapt linksonder2 // die worden hier elk individueel gechecked if (r1 > l2 && r1 < r2) { // rechts if (b1 > t2 && b1 < b2) { // onder1 overlapt linksboven2 ret = true; } else if (t1 > t2 && t1 < b2) { // boven overlapt overlapt linksonder2 ret = true; } } else if (l1 > l2 && l1 < r2) { // links if (b1 > t2 && b1 <= b2) { // onder1 overlapt rechtsboven2 ret = true; } else if (t1 > t2 && t1 < b2) { // boven1 overlapt met rechtsonder2 ret = true; } } homecoming ret; }

i'm @ end of wits here. maybe i'm missing simple (i am, always) can't seem figure out. guys can help me or "submit" problem allow me see light.

thanks in advance!

[edit] ok tried original approach 1 time again because had hunch. turned out original scheme didn't work because in 1 function hopping , forth between floating point coordinates , integer coordinates. if thread found in future similar problem, sure stick 1 type: either integers or floats. save lot of headache.

your rectcol function indicates whether there has been collision, not distinguish between collision in x , collision in y.

if there has been collision, slidecol function adjusts x position if collision exclusively in y. cannot slide along floor or ceiling.

i don't know why sliding slower or faster "than expected". see no reason in code (and not experiment dll's), suspect expectation may unrealistic.

c++ 2d sdl collision rectangles

No comments:

Post a Comment