You most likely need this function: https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1RenderTarget.php#a2d3e9d7c4a1f5ea7e52b06f53e3011f9
Thank you, but it's still not working. Here's what I have to detect whether the mouse is within a rectangle that's affected by an sf::View:
bool pointRect(const sf::Vector2i& point, sf::RectangleShape& rect, const sf::RenderWindow& window) {
auto pos = window.mapPixelToCoords(point);
auto topleft = window.mapPixelToCoords((sf::Vector2i)rect.getPosition());
auto bottomright = window.mapPixelToCoords(sf::Vector2i(rect.getPosition() + rect.getSize()));
return pos.x > topleft.x && pos.x < bottomright.x && pos.y > topleft.y && pos.y < bottomright.y;
}
Helping me figure out what I've missed would be great!
That doesn't make much sense: you're trying to convert a rectangle in scene coordinates to scene coordinates (the fact that you have to cast your vector types is a good hint that something's wrong -- Vector2i defines pixels) ;)
I assume that "point" is mouse coordinates, as given by sf::Mouse::getPosition. I also assume that rect is the absolute bounding rect of an entity, as given by getGlobalBounds(). I also assume that the view that you mentioned is active in "window" when you call this function. (I wish I hadn't to assume so much stuff ;))
Then it's simple: you just have to convert mouse coordinates to "scene" coordinates, that is, coordinates in the current window's view.
bool pointRect(const sf::Vector2i& point, sf::RectangleShape& rect, const sf::RenderWindow& window) {
return rect.contains(window.mapPixelToCoords(point));
}
That doesn't make much sense: you're trying to convert a rectangle in scene coordinates to scene coordinates (the fact that you have to cast your vector types is a good hint that something's wrong -- Vector2i defines pixels) ;)
I assume that "point" is mouse coordinates, as given by sf::Mouse::getPosition. I also assume that rect is the absolute bounding rect of an entity, as given by getGlobalBounds(). I also assume that the view that you mentioned is active in "window" when you call this function. (I wish I hadn't to assume so much stuff ;))
Then it's simple: you just have to convert mouse coordinates to "scene" coordinates, that is, coordinates in the current window's view.
bool pointRect(const sf::Vector2i& point, sf::RectangleShape& rect, const sf::RenderWindow& window) {
return rect.getGlobalBounds().contains(window.mapPixelToCoords(point));
}
All your assumptions are correct, and I see the problem in converting the rectangle. Thanks again! This is very helpful.