1
General / Re: How to detect collision when RenderWindow has an sf::View?
« on: August 13, 2020, 03:55:16 pm »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.