I want to check if my mouse is over a shape inside a renderTexture. The problem: shape.GetGlobalBounds is relative to the renderTexture, whereas the mouse positon is relative to the window. I don't know how to make both relative to the same origin. So how would I check a collision between mouse and shape?
Thanks for any help
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(900, 600), "Title");
sf::CircleShape circle(50.f);
circle.setPosition(0.f, 0.f);
circle.setFillColor(sf::Color::Red);
sf::RenderTexture rTexture;
rTexture.create(400.f, 400.f);
sf::Sprite sprite;
sf::Texture texture;
sprite.move(100.f, 100.f);
sf::Event myEvent;
while (window.isOpen())
{
while (window.pollEvent(myEvent))
{
if (myEvent.type == sf::Event::Closed)
window.close();
}
rTexture.clear(sf::Color::Blue);
rTexture.draw(circle);
rTexture.display();
texture = rTexture.getTexture();
sprite.setTexture(texture);
window.clear();
window.draw(sprite);
if (circle.getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) /*Mouse positon is relative to window -> returns (100,100) when getGlobalBounds returns (0,0)*/
circle.setFillColor(sf::Color::White);
else circle.setFillColor(sf::Color::Red);
std::cout << circle.getGlobalBounds().left << " " << circle.getGlobalBounds().top << std::endl;
window.display();
}
}