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

Author Topic: getGlobalBounds().contains("mouse coordinates") not working  (Read 7185 times)

0 Members and 1 Guest are viewing this topic.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
In the example below, I'm trying to print "COLLISION" when I mouseover the box sprite.

Even though the sprite position is set elsewhere, getGlobalBounds() thinks that it's centered on coordinates 0,0.

What am I doing wrong here?

#include <SFML/Graphics.hpp>
#include <iostream>

class Box : public sf::Drawable, public sf::Transformable
{

public:

        sf::Sprite sprite;
        sf::Texture texture;

        void load()
        {
                texture.loadFromFile("box.png");
                sprite = sf::Sprite(texture);
                sprite.setTextureRect(sf::IntRect(0, 0, 420, 120));
                sprite.setOrigin(210, 60);
        }

private:

        virtual void draw(sf::RenderTarget& target, sf::RenderStates states)const
        {
                states.transform *= getTransform();
                states.texture = &texture;
                target.draw(sprite, states);
        }

};

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML");
        window.setFramerateLimit(10);

        Box box;
        box.load();
        box.setPosition(300, 300);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                sf::Vector2f Mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));
                std::cout << "Mouse.x = " << Mouse.x << ", Mouse.y = " << Mouse.y << std::endl;

                if (box.sprite.getGlobalBounds().contains(Mouse))
                {
                        std::cout << "COLLISION!" << std::endl;
                }

                window.clear();
                window.draw(box);
                window.display();
        }

        return 0;
}
« Last Edit: June 07, 2017, 11:30:39 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10845
    • View Profile
    • development blog
    • Email
Re: getGlobalBounds().contains("mouse coordinates") not working
« Reply #1 on: June 07, 2017, 11:58:16 pm »
This makes sense. In the load function you change the texture rect and the origin of the sprite, but don't change the position of the sprite.
In the main function right after the call to load() you can the position of the box which is its own transformable and doesn't have an effect on the sprite.
You collision detection then checks the global bound of the sprite which is positioned at 0,0 (default position) with the origin of 210, 60 and size of 420x120.

Btw. it's not recommended to store a texture inside an "entity" like you do, as the copying of the entity (e.g. when calling push_back on a vector) will cause the texture to be copied and the sprite referencing the texture will lose the reference as the memory location of the texture changed. Instead it's better use something like Thor's resource holder.

As for the forum, you should use the [code=cpp][/code] tag to post C++ code.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: getGlobalBounds().contains("mouse coordinates") not working
« Reply #2 on: June 08, 2017, 07:13:23 am »
This makes sense. In the load function you change the texture rect and the origin of the sprite, but don't change the position of the sprite.
In the main function right after the call to load() you can the position of the box which is its own transformable and doesn't have an effect on the sprite.
You collision detection then checks the global bound of the sprite which is positioned at 0,0 (default position) with the origin of 210, 60 and size of 420x120.

Btw. it's not recommended to store a texture inside an "entity" like you do, as the copying of the entity (e.g. when calling push_back on a vector) will cause the texture to be copied and the sprite referencing the texture will lose the reference as the memory location of the texture changed. Instead it's better use something like Thor's resource holder.

As for the forum, you should use the [code=cpp][/code] tag to post C++ code.

Thank you, I will be using the code tag from now on.

In my actual program I use the asset manager from the book SFML Essentials, so that's not a problem. This is just a minimalist example of the problem.

Also thank you for the explanation. I managed to fix it by calling

box.sprite.setPosition(300, 300);

instead of just box.setPosition

 

anything