After trying to use sf::Sprite.GetSubrect().Intersects() I've come to find that no matter what, it always returns true. This can be tested with the following code:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include "games.h"
struct Square
{
sf::Sprite Sprite;
sf::Image Image;
sf::String String;
Square();
};
Square::Square()
{
Image.Create(100, 100, sf::Color(255,255,255));
Sprite.SetImage(Image);
String.SetFont(sf::Font::GetDefaultFont());
}
int Collision_Test(sf::RenderWindow &App)
{
Square S1, S2, S3;
S1.Image.Create(100, 100, sf::Color(255,0,0)); // Red Square
S2.Image.Create(100, 100, sf::Color(0,255,0)); // Green Square
S3.Image.Create(100, 100, sf::Color(0,0,255)); // Blue Square
S1.Sprite.SetPosition(250, 250); // Left Bottom
S2.Sprite.SetPosition(375, 250); // Right Bottom
S3.Sprite.SetPosition(300, 300); // Center Top
S1.String.SetPosition(App.GetWidth()/2, App.GetHeight()/8);
S2.String.SetPosition(App.GetWidth()/2, App.GetHeight()/6);
bool Switched = false;
while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
{
App.Close();
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
{
App.Close();
}
}
const sf::Input& Input = App.GetInput();
bool Space_Down = Input.IsKeyDown(sf::Key::Space);
if (Space_Down)
{
if (Switched)
{
S1.Sprite.SetPosition(250, 250); // Left Bottom
S2.Sprite.SetPosition(375, 250); // Right Bottom
S3.Sprite.SetPosition(300, 300); // Center Top
Switched = false;
}
else
{
S1.Sprite.SetPosition(100, 250); // Left Bottom
S2.Sprite.SetPosition(500, 250); // Right Bottom
S3.Sprite.SetPosition(300, 500); // Center Top
Switched = true;
}
}
// Collision Logic
if (S1.Sprite.GetSubRect().Intersects(S2.Sprite.GetSubRect()))
{
S1.String.SetText("Red is currently touching Green.");
}
else
{
S1.String.SetText("Red is not touching Green.");
}
if (S2.Sprite.GetSubRect().Intersects(S1.Sprite.GetSubRect()))
{
S2.String.SetText("Blue is currently touching Red.");
}
else
{
S2.String.SetText("Blue is not touching Red.");
}
App.Clear();
App.Draw(S1.Sprite);
App.Draw(S2.Sprite);
App.Draw(S3.Sprite);
App.Draw(S1.String);
App.Draw(S2.String);
App.Display();
}
}