Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Get Position of shape relative to window when drawn to render texture  (Read 2983 times)

0 Members and 1 Guest are viewing this topic.

janos_

  • Newbie
  • *
  • Posts: 9
    • View Profile
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 8)

#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();
        }
}
 
« Last Edit: August 18, 2020, 04:33:50 pm by janos_ »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Get Position of shape relative to window when drawn to render texture
« Reply #1 on: August 18, 2020, 04:50:34 pm »
Remove your sprite position from your mouse position before you mapPixelToCoords it

janos_

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Get Position of shape relative to window when drawn to render texture
« Reply #2 on: August 18, 2020, 04:59:23 pm »
But the sprite position is always (0,0) or am I missunderstanding something? You would need to subtract the distance to the original position(of the renderTexture) from the mouse position, but there is no way to calculate that distance. Just by keeping track yourself on how you moved the sprite(meaning the renderTexture), but that's annoying.
« Last Edit: August 18, 2020, 05:02:07 pm by janos_ »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Get Position of shape relative to window when drawn to render texture
« Reply #3 on: August 18, 2020, 09:12:51 pm »
        sprite.move(100.f, 100.f);
It's (100, 100)

janos_

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Get Position of shape relative to window when drawn to render texture
« Reply #4 on: August 19, 2020, 12:09:44 am »
Nevermind your right. Sänks very much
« Last Edit: August 19, 2020, 11:44:35 pm by janos_ »

 

anything