How do I retrieve the correct mouse position after moving view?
At the moment, I am doing it this way:
pixelPos = Mouse::getPosition(window);
coordPos = window.mapPixelToCoords(pixelPos);
I tried passing the currently used view as the second parameter to the 'mapPixelToCoords' method, but it didn't help. I am trying to snap objects to a 48x48 grid, but the farther I move the view, the more mouse input deviates from the expected.
What am I doing wrong?
http://pastebin.com/6tGSwLQz
When x is negative, the remainder of x % 48 is also negative. You then remove the negative remainder from x, -- gives + so it actually adds the remainder and your rectangle is drawn to a multiple of 48 to the right of where it should be.
For an obvious solution, remove 48 to x when x is < 0. (do the same for y) There are probably better solutions. :Pint x = static_cast<int>(std::trunc(coordPos.x));
int y = static_cast<int>(std::trunc(coordPos.y));
if (x < 0) x -=48;
if (y < 0) y -=48;
x -= x % 48;
y -= y % 48;
coordPos.x = static_cast<float>(x);
coordPos.y = static_cast<float>(y);