Ben Hollier
Ben Hollier

Reputation: 605

Mouse input isn't correct to world coordinates

There are probably millions of these questions but, I can't get the coordinates of the mouse so that they line up with the program coordinate system, when the window is re-sized. I've tried mapPixelToCoords() and getting the mouse coordinates using sf::Event::MouseButton or sf::Mouse, but to no avail.

This problem is most likely from my incompetence, but I can't figure it out for the life of me. Also, I need to do this to change the coordinates of a rectangle, not detecting whether a box is being hovered over, if it was the answer would be a lot easier to figure out I feel.

Edit:

Source Code:
//Standard C++:
#include <iostream>
//SFML:
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Example");

    sf::Event event;

    sf::RectangleShape mousePoint;
    mousePoint.setSize(sf::Vector2f(1, 1));
    mousePoint.setFillColor(sf::Color::Red);

    while (window.isOpen())
    {
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) //Close window
            {
                window.close();
                return 0;
            }
            if (event.type == sf::Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == sf::Mouse::Left)
                {
                    //Get the mouse position:
                    sf::Vector2i mouse = sf::Mouse::getPosition(window);
                    //Map Pixel to Coords:
                    window.mapPixelToCoords(mouse);
                    //Set position of the mouse to the rectangle:
                    mousePoint.setPosition(mouse.x, mouse.y);
                }
            }
        }

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

After some doubt, I have uploaded some simple source code, which demonstrates my point. When you click the LMB, it moves the rectangle to where the program thinks the mouse is. When the screen hasn't been scaled, it is correctly calibrated, but when it is changed, the rectangle moves to a point which is no where near where the mouse is.

Upvotes: 0

Views: 2823

Answers (1)

Lukas
Lukas

Reputation: 2613

As shown in the official documentation and the official tutorial section of SFML, you can use the mapPixelToCoords function to map pixel/screen coordinates to world coordinates.

The signature of the function is as following:

Vector2f sf::RenderTarget::mapPixelToCoords(const Vector2i& point) const

As such the usage would look something like that:

//Get the mouse position:
sf::Vector2i mouse = sf::Mouse::getPosition(window);
//Map Pixel to Coords:
sf::Vecotr2f mouse_world = window.mapPixelToCoords(mouse);
//Set position of the mouse to the rectangle:
mousePoint.setPosition(mouse_world);

In other words, the mapPixelToCoords function, takes a const sf::Vector2i& as parameter and returns a sf::Vector2f and the original vector is not being modified.

It's always recommended to take a closer look at the documentation if something doesn't work as expected.

Upvotes: 3

Related Questions