0
\$\begingroup\$

Been searching for quite a while now and really struggling to find an easy way, or a good tutorial on how to create a collision detection system which can indicate what side on a box a collision has occurred.

I should specify that I am using SFML2 and their sprites class, this is what I currently use and it just picks up 2 sprites intersecting

//check rectangles for collision without taking into account where the collision is
bool Game::isRectCollision(sf::Sprite &sprite1, sf::Sprite &sprite2){

    sf::FloatRect r1=sprite1.getGlobalBounds();
    sf::FloatRect r2=sprite2.getGlobalBounds();
    return r1.intersects(r2);

}
\$\endgroup\$
  • \$\begingroup\$ I have always passed the object's movement speed as parameters as well. If object1.velocity.x > 0, for example, then object1 is coming from the left and colliding its right. I wonder if there are alternative ways, tho \$\endgroup\$ – Mutoh May 10 '15 at 0:19
1
\$\begingroup\$

The Separating Axis Theorem might be what you're looking for.
Writing collision detection using SAT is easier than it seems and allows you to calculate handy things like where exactly they collided as well as the minimum translation vector. Highly recommended for shape collision detection.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.