How can I get the X and Y offset of sf::View, I’m using sf::View as my 2d camera so when I click the mouse I get the mouse X and Y coords of the tile I’m clicking on
void LMButtonDown(int mX, int mY)
{
printf("[%d][%d]\n", mX / TILE_SIZE, mY / TILE_SIZE);
}
This is great, but once I move the camera sf::View the coords, as expected dont take into account the sf::View offset. I dont see any function to get X or Y in the Documentation
so I can take into account for the offset.
Any help with this would be appreciated.
Thanks.
Take a look at sf::RenderTarget::mapPixelToCoords and mapCoordsToPixel. You can use this method to convert coordinates from “view” to “world” space and back. The documentation specifically mentions your needs as an example:
There are also overloaded versions of the methods that take a
Vector2iand convert it based on the RenderTarget’sviewwithout having to supply the view manually.If for some reason you can’t use (or don’t have access to) the RenderTarget, then you can perform the translation manually. It should be quite simple as long as your
sf::Viewdoes not perform scaling or rotation (that is, it only performs translation).To get the top left corner of the view, you simply take center and then subtract half of the width and height. Then, you translate your mouse coordinates using the top left corner of the view.
Something like this: