### SDL trouble with side collision detection

Hi, i'm working on a sidescroller game and for now Igot trouble with the collision form both right and left side. Top and bottom collision seems to work properly.

I got this function to determine if a collision occur in a Base class:

 ``123456789101112131415161718192021`` ``````bool Base::collision(SDL_Rect *rect1 , SDL_Rect *rect2) { if(rect1->y +rect1->h <= rect2->y) { return false; } if(rect1->y >= rect2->y + rect2->h) { return false; } if(rect1->x +rect1->w <= rect2->x) { return false; } if(rect1->x >= rect2->x +rect2->w) { return false; } return true; }``````

And now on my move function I check if there is a collision with a tile of the map that is not 0(nothing) and i need to know wich kind of collision it is.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` `````` bool falling= true; int start = (Base::coord.x - (Base::coord.x%Base::tileSize))/Base::tileSize; int end = (Base::coord.x + Base::coord.w +(Base::tileSize -(Base::coord.x + Base::coord.w)%Base::tileSize))/Base::tileSize; if (start<0) { start = 0; } if (end>map[0].size()) { end = map[0].size(); } for(int i=0;i= destination.y ) { ground = true; yVel = 0; } //bottom collision else if(box.y <= destination.y + destination.h) { yVel = 5; } // left collision if( box.x + box.w >= destination.x && box.y + box.h <= destination.y) { xVel = -1; } //right collision else if(box.x <= destination.x + destination.w && box.y + box.h <= destination.y) { xVel = 1; } } } } } if(falling && !jump) { yVel = 4; } if( jump && yVel < 5) { yVel += 1; } else { jump = false; } box.x += xVel; box.y += yVel; }``````

Like I said right and left collision seem to be the problem. Some help would be greatly appreciated :)
Last edited on
Topic archived. No new replies allowed.