- #1
Peter P.
- 23
- 0
My problem is not so much with programming it, but rather determining which side is collided.
And for those who aren't familiar with SDL, SDL_Rect is just a structure that hold an x and y
position along with a width and height ( (x,y) is the top left corner of the rectangle).
So here is how I am planning on going about determining the collided side:
1. I check to see if there is a collision, then
2. I determine the side that collided.
Here is the function I am using for part 1.
For part 2, I was reluctantly planning on doing a pixel by pixel search on all four sides of box1 to
determine which side collided with box2. But I am hoping that i can use a more simple series of if
statements (like part 1), to check for this.
I hope I made sense to anyone reading this. Thanks for your time, and any help is much
appreciated.
-Peter
And for those who aren't familiar with SDL, SDL_Rect is just a structure that hold an x and y
position along with a width and height ( (x,y) is the top left corner of the rectangle).
So here is how I am planning on going about determining the collided side:
1. I check to see if there is a collision, then
2. I determine the side that collided.
Here is the function I am using for part 1.
Code:
bool collision_check (SDL_Rect box1, SDL_Rect box2, bool isOutside) {
// A collision is determined from the perspective of box1.
if (isOutside) {
// box1 must be located somewhere outside box2
// Checking if the bottom of box1 hits the top of box2
if (box1.y + box1.h <= box2.y)
return false;
// Checking if the top of box1 hits the bottom of box2
if (box1.y >= box2.y + box2.h)
return false;
// Checking if the right side of box1 hits the left side of box2
if (box1.x + box1.w <= box2.x)
return false;
// Checking if the left side fo box1 hits the right side of box2
if (box1.x >= box2.x + box2.w)
return false;
}
else {
// box1 must be located somewhere within box2
// Checking if the top of box1 hits the top of box2
if (box1.y >= box2.y)
return false;
// Checking if the bottom of box1 hits the bottom of box2
if (box1.y + box1.h <= box2.y + box2.h)
return false;
// Checking if the left side of box1 hits the left side of box2
if (box1.x >= box2.x)
return false;
// Checking if the right side of box1 hits the right side of box2
if (box1.x + box1.w <= box2.x + box2.w)
return false;
}
// If it failed the 4 requirements, it will return true (there was a
// collision between the boxes.
return true;
}
determine which side collided with box2. But I am hoping that i can use a more simple series of if
statements (like part 1), to check for this.
I hope I made sense to anyone reading this. Thanks for your time, and any help is much
appreciated.
-Peter