31
Window / [SOLVED] How to know if sprite is in view
« on: January 10, 2017, 11:28:58 pm »
Hi Guys, I'm having trouble trying to know if a sprite is inside the current view, this is my code:
This isn't working, I know because the sprites are lights that I only draw when they are in view. They won't draw or draw when not in view, etc.
I'm having trouble understanding the coordinates system, it's probably an issue with that.
bool Sector::lightInView(sf::FloatRect spriteAABB)
{
sf::Vector2i viewCenter(Game::GetWindow()->getView().getCenter()); // global coords, right?
sf::Vector2i viewSize(Game::GetWindow()->getView().getSize()); // in this case: 1680x1050
// create the view Rect, are the -/+ ok?
sf::FloatRect currentViewRect
(viewCenter.x - viewSize.x / 2,
viewCenter.y + viewSize.y / 2,
viewCenter.x + viewSize.x / 2,
viewCenter.y - viewSize.y / 2);
// check if the sprite is in view
return (spriteAABB.intersects(currentViewRect));
}
{
sf::Vector2i viewCenter(Game::GetWindow()->getView().getCenter()); // global coords, right?
sf::Vector2i viewSize(Game::GetWindow()->getView().getSize()); // in this case: 1680x1050
// create the view Rect, are the -/+ ok?
sf::FloatRect currentViewRect
(viewCenter.x - viewSize.x / 2,
viewCenter.y + viewSize.y / 2,
viewCenter.x + viewSize.x / 2,
viewCenter.y - viewSize.y / 2);
// check if the sprite is in view
return (spriteAABB.intersects(currentViewRect));
}
This isn't working, I know because the sprites are lights that I only draw when they are in view. They won't draw or draw when not in view, etc.
I'm having trouble understanding the coordinates system, it's probably an issue with that.