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

Author Topic: Help with Co-ordinate system  (Read 1164 times)

0 Members and 1 Guest are viewing this topic.

adishy

  • Newbie
  • *
  • Posts: 1
    • View Profile
Help with Co-ordinate system
« on: January 27, 2018, 02:53:13 am »
Hello,

I am writing a small program using SFML which draws a 5 x 5 rectangle at any point specified by clicking the mouse.

I am using mapPixelToCoords() to map the mouse coordinates to the pixels as recommended in the documentation, however I am still encountering a weird offset between the coordinates and the actual shape which is drawn.
The offset varies on different parts of the window (it is not a constant x,y value).

I would like the rectangle to be drawn precisely under the mouse pointer, how do I go about removing this offset?

(I have also attached screenshots to help clarify the issue I'm encountering)

This is the code I'm using to draw the rectangle on click:
int main(){

        sf::RenderWindow window(sf::VideoMode(800, 720), "SFML window");

        sf::Event event;

        //Rectangle to be drawn on the screen
        sf::RectangleShape someRect;
        someRect.setFillColor(sf::Color(0, 0, 0));
        someRect.setSize({ 5, 5 });
        someRect.setPosition(100, 120);

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

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

                        if (event.type == sf::Event::MouseButtonPressed) {
                                someRect.setPosition(window.mapPixelToCoords(sf::Mouse::getPosition(window)));
                        }
                }

                window.clear(sf::Color(255, 255, 255));

                window.draw(someRect);

                window.display();

        }

         return 0;
}
« Last Edit: January 27, 2018, 02:59:50 am by adityashylesh »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Help with Co-ordinate system
« Reply #1 on: January 27, 2018, 08:46:33 am »
i compiled your code, and it works perfectly to me  ???
i just had to remove this segment:

if (!windowStarted) {
                        event.type = sf::Event::Closed;
                }

because there is no windowStarted definition there. what makes me think: isn't something outside this code, that we cannot see, making any difference?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Help with Co-ordinate system
« Reply #2 on: January 27, 2018, 10:06:12 am »
It will probably not make a big difference, but it is more correct: you should use the coordinates given by the event rather than calling sf::Mouse::getPosition.

If what you posted works for others, you should post a complete minimal code that reproduces the problem, so that everyone can test under the same conditions.
Laurent Gomila - SFML developer

 

anything