OK, I'll try to go the function through in detail now (had a busy weekend
).
static bool checkCollision(sf::Sprite sprite1, sf::Sprite sprite2) {
if(sprite1.GetPosition().x + sprite1.GetSize().x > sprite2.GetPosition().x && sprite1.GetPosition().x < sprite2.GetPosition().x + sprite2.GetSize().x && sprite1.GetPosition().y + sprite1.GetSize().y > sprite2.GetPosition().y && sprite1.GetPosition().y < sprite2.GetPosition().y + sprite2.GetSize().y)
return true;
return false;
}
The idea is very simple: compare the coordinates to determine whether the rectangle around the sprite is on top of the other sprite's rectangle. To do that I actually rule out all the possibilities how the rectangles could be positioned so that they aren't overlapping. If they can be ruled out, the rectangles are on top of each other.
The rectangle is formed using the sprite's coordinates and the sprite's size. So the upper left corner of sprite1's rectangle is at (sprite1.GetPosition().x, sprite1.GetPosition().y), right upper corner is at (sprite1.GetPosition().x + sprite1.GetSize().x, sprite1.GetPosition().y), left lower corner is at (sprite1.GetPosition().x + sprite1.GetSize().x, sprite1.GetPosition().y) and the lower right corner is at (sprite1.GetPosition().x + sprite1.GetSize().x, sprite1.GetPosition().y + sprite1.GetSize().x). The same is done for sprite2.
Now that we have formed the rectangles we only need to figure out whether they are on top of each other or not. Let's go through the x-axis first. The rectangles can't possibly be on top of each other if the sprite1 is too far to the left from sprite2 (ie. "the right side" of the sprite1's rectangle is on the left of "the left side" of the sprite2's rectangle). So we want to compare
sprite1.GetPosition().x + sprite1.GetSize().x
with
sprite2.GetPosition().x
to rule out the possibility that the sprite1 would be too far to the left for the rectangles to collide. So in code it looks like this:
if(sprite1.GetPosition().x + sprite1.GetSize().x > sprite2.GetPosition().x)
Now if the sprite1 is too far to the left, that expression is false (a collision isn't possible). Now the same is done for the other side: we rule out the possibility that the sprite1 is too far to the right from the sprite2.
if(sprite1.GetPosition().x < sprite2.GetPosition().x + sprite2.GetSize().x)
Combining the two we get:
if(sprite1.GetPosition().x + sprite1.GetSize().x > sprite2.GetPosition().x && sprite1.GetPosition().x < sprite2.GetPosition().x + sprite2.GetSize().x)
Which already works for the x-axis. The only thing left is to do the same with the y-axis and the function is complete. It's exactly the same thing: you can actually copy and paste the first part and just change the x's to y's and that's it.
I hope I got everything about right. Ask if you didn't understand something. Oh, and if you want some help with what to do with the information that the objects are indeed colliding, tell me what would you would want to happen when the collision occurs and I can help you implement it.