EDN Admin
Well-known member
In globals.h I have check_collision like so
bool check_collision( std::vector<SDL_Rect> &A, std::vector<SDL_Rect> &B );
In globals.cpp I have check_collision like sobool check_collision( std::vector<SDL_Rect> &A, std::vector<SDL_Rect> &B )
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Go through the A boxes
for( int Abox = 0; Abox < A.size(); Abox++ )
{
//Calculate the sides of rect A
leftA = A[ Abox ].x;
rightA = A[ Abox ].x + A[ Abox ].w;
topA = A[ Abox ].y;
bottomA = A[ Abox ].y + A[ Abox ].h;
//Go through the B boxes
for( int Bbox = 0; Bbox < B.size(); Bbox++ )
{
//Calculate the sides of rect B
leftB = B[ Bbox ].x;
rightB = B[ Bbox ].x + B[ Bbox ].w;
topB = B[ Bbox ].y;
bottomB = B[ Bbox ].y + B[ Bbox ].h;
//If no sides from A are outside of B
if( ( ( bottomA <= topB ) || ( topA >= bottomB ) || ( rightA <= leftB ) || ( leftA >= rightB ) ) == false )
{
cout << "Touching" << endl;
//A collision is detected
return true;
}
}
}
//If neither set of collision boxes touched
return false;
}
In player.h I have player movement like so//Move the player
void move( std::vector<SDL_Rect> &rects );
In player.cpp I have player movement like sovoid player::move( std::vector<SDL_Rect> &rects )
{
//Move the player left or right
x += xVel;
//Move the collision boxes
shift_boxes();
//If the player went too far to the left or right or has collided
if( check_collision( box, rects ) )
{
//Move back
x -= xVel;
shift_boxes();
}
//Move the player up or down
y += yVel;
//Move the collision boxes
shift_boxes();
//If the player has collided vertically
if( check_collision( box, rects ) )
{
y -= yVel;
on_ground = true;
shift_boxes();
} else { //if there is nothing below you, you will fall
shift_boxes();
on_ground = false;
}
}
in case you are wondering, xVel and yVel move at constant speeds (for now), I set their speed with the variables xspeed and yspeed (both of which are 6).
I was trying to get jumping done in my engine, and I had something going, however the player stuck to the cieling and when i put in code to start falling, i couldnt jump either. Thats becausecheck_collision is the only thing used and it checks for all sides regardless of what is going on.
I tried editing check_collision (for testing) so that it would only check one side, but the player ended up not being able to move at all. You will see I have a message printed to console here//If no sides from A are outside of B
if( ( ( bottomA <= topB ) || ( topA >= bottomB ) || ( rightA <= leftB ) || ( leftA >= rightB ) ) == false )
{
cout << "Touching" << endl;
//A collision is detected
return true;
}
well below that if it returned false, it would print the message "Not Touching". "Touching" is printed repeatedly to console and I cannot move at all if I take one of the (sideA <=/>= oppositesideB) out from that if statement, even if I am not touching anything.
do you think you can help me create a new way to check for collision based on sides? Thanks
View the full article
bool check_collision( std::vector<SDL_Rect> &A, std::vector<SDL_Rect> &B );
In globals.cpp I have check_collision like sobool check_collision( std::vector<SDL_Rect> &A, std::vector<SDL_Rect> &B )
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Go through the A boxes
for( int Abox = 0; Abox < A.size(); Abox++ )
{
//Calculate the sides of rect A
leftA = A[ Abox ].x;
rightA = A[ Abox ].x + A[ Abox ].w;
topA = A[ Abox ].y;
bottomA = A[ Abox ].y + A[ Abox ].h;
//Go through the B boxes
for( int Bbox = 0; Bbox < B.size(); Bbox++ )
{
//Calculate the sides of rect B
leftB = B[ Bbox ].x;
rightB = B[ Bbox ].x + B[ Bbox ].w;
topB = B[ Bbox ].y;
bottomB = B[ Bbox ].y + B[ Bbox ].h;
//If no sides from A are outside of B
if( ( ( bottomA <= topB ) || ( topA >= bottomB ) || ( rightA <= leftB ) || ( leftA >= rightB ) ) == false )
{
cout << "Touching" << endl;
//A collision is detected
return true;
}
}
}
//If neither set of collision boxes touched
return false;
}
In player.h I have player movement like so//Move the player
void move( std::vector<SDL_Rect> &rects );
In player.cpp I have player movement like sovoid player::move( std::vector<SDL_Rect> &rects )
{
//Move the player left or right
x += xVel;
//Move the collision boxes
shift_boxes();
//If the player went too far to the left or right or has collided
if( check_collision( box, rects ) )
{
//Move back
x -= xVel;
shift_boxes();
}
//Move the player up or down
y += yVel;
//Move the collision boxes
shift_boxes();
//If the player has collided vertically
if( check_collision( box, rects ) )
{
y -= yVel;
on_ground = true;
shift_boxes();
} else { //if there is nothing below you, you will fall
shift_boxes();
on_ground = false;
}
}
in case you are wondering, xVel and yVel move at constant speeds (for now), I set their speed with the variables xspeed and yspeed (both of which are 6).
I was trying to get jumping done in my engine, and I had something going, however the player stuck to the cieling and when i put in code to start falling, i couldnt jump either. Thats becausecheck_collision is the only thing used and it checks for all sides regardless of what is going on.
I tried editing check_collision (for testing) so that it would only check one side, but the player ended up not being able to move at all. You will see I have a message printed to console here//If no sides from A are outside of B
if( ( ( bottomA <= topB ) || ( topA >= bottomB ) || ( rightA <= leftB ) || ( leftA >= rightB ) ) == false )
{
cout << "Touching" << endl;
//A collision is detected
return true;
}
well below that if it returned false, it would print the message "Not Touching". "Touching" is printed repeatedly to console and I cannot move at all if I take one of the (sideA <=/>= oppositesideB) out from that if statement, even if I am not touching anything.
do you think you can help me create a new way to check for collision based on sides? Thanks
View the full article