This is a method i have at the moment
bool check_collision( SDL_Rect A, SDL_Rect B )
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Calculate the sides of rect A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;
//Calculate the sides of rect B
leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;
//If any of the sides from A are outside of B
if( bottomA <= topB )
{
return false;
}
if( topA >= bottomB )
{
return false;
}
if( rightA <= leftB )
{
return false;
}
if( leftA >= rightB )
{
return false;
}
//If none of the sides from A are outside B
return true;
}
now this works, but I'm trying to make a 2D side scroller, now how can i use this code to just check lets say the sides? so he cant move left or right, how can I check to see if he hits his head so it tells yVel to be set to 0, and how can i check to see if there is a collision at the bottom, so he doesn't fall through the blocks, now this collision detection at the moment is fine for a monster, but not movement, could someone help
thank you so much
Ok i have just changed my character class so it has 2 collision boxes,
height = 81;
width = 33;
headH = 83;
headW = 28;
I think i have some stuff working now, but when i move my character speeds up, so for instance 1,2,3 speed, once it hits 3 it will stop at 3, but if i get to 3 then hit a block my character will get stuck inside, any idea on how i can resolve this?
this code is in my update method
if(moveRight == true && collision.check_collision(A,B))
{
moveRight = false;
pos.x = tempX;
}
if(moveLeft == true && collision.check_collision(A,B))
{
moveLeft = false;
pos.x += tempX;
}
if(falling == true && collision.check_collision(A2,B))
{
falling = false;
pos.y = tempY;
yVel = 0;
}