Hello! I have been recently experiment with collision between two shapes but I can't quite get it to work. It only worked with two opposite sides.
How would I do collision correctly?
Here's some minimal code that detects the collision but doesn't stop movement. Movement it controlled by holding the mouse button down and dragging.
I have removed code that isn't affecting anything.
int main()
{
//Player
sf::RectangleShape player(sf::Vector2f(80.f, 80.f));
player.setOrigin(40.f, 40.f);
player.setPosition(400.f, 40.f);
//Enemy
sf::RectangleShape enemy(sf::Vector2f(80.f, 80.f));
enemy.setOrigin(40.f, 40.f);
enemy.setPosition(600.f, 350.f);
enemy.setFillColor(sf::Color(232,74,63));
while(window.isOpen())
{
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
player.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));
//Collision
if(enemy.getGlobalBounds().intersects(player.getGlobalBounds()))
player.setFillColor(sf::Color(40,39,39));
else
player.setFillColor(sf::Color(170,227,210));
//Render
window.clear(sf::Color(240, 240, 240));
window.draw(player);
window.draw(enemy);
window.display();
}
}