SFML community forums
Help => Graphics => Topic started by: Captain Lightning on September 22, 2011, 02:27:32 am
-
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();
}
}
-
Getsubrect() returns the position inside the sprite image(if you sprite is a smaller part of a larger image, like a spritesheet), not in the world coordinates. Since all images start at 0,0 and extend downwards and to the right(and all your sprites use their whole image) you will always intersect.
You cannot get the rect occupied by a sprite in the world space so easily. :(
You need to make one yourself using getposition()/getsize().
Laurent explains how to do it properly in this thread:
http://www.sfml-dev.org/forum/viewtopic.php?t=4695&highlight=getsubrect
-
D:
All of my hopes for easy collision detection are smushed.
Thanks for the link, pyro.