Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] How to know if sprite is in view  (Read 6259 times)

0 Members and 1 Guest are viewing this topic.

Guido_Ion

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Flow Soccer development
[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:

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));
}

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.
« Last Edit: January 11, 2017, 08:28:53 pm by Guido Bisocoli »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11053
    • View Profile
    • development blog
    • Email
Re: How to know if sprite is in view
« Reply #1 on: January 11, 2017, 12:54:40 am »
By showing your uncertainty about the getCenter function, you basically already knew about the origin of the issue. If you don't understand what you're code really does, it's dangerous to assume something and it's better to dig deeper until you understand it.

The center of the view is the origin of the world coordinate system relative to the coordinate system of the view. The view's coordinate system is dependent on the viewport, but by default is centered in the middle of the window.

Quite a while ago, I've written a tutorial that tries to provide an intuition/way to think of how the view works. Currently I'm in the middle of updating it, as I notice that it has a lot of spelling mistakes.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Guido_Ion

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Flow Soccer development
Re: How to know if sprite is in view
« Reply #2 on: January 11, 2017, 08:28:32 pm »
Thanks eXpl0it3r! Solved! Here is the final code:

bool Sector::lightInView(sf::FloatRect spriteAABB)
{
        sf::Vector2i viewCenter(Game::GetWindow()->getView().getCenter()); // this returns the center of the view IN THE WORLD!
        sf::Vector2i viewSize(Game::GetWindow()->getView().getSize());

        sf::FloatRect currentViewRect
                (viewCenter.x - viewSize.x / 2, // left
                viewCenter.y - viewSize.y / 2, // top
                viewSize.x,
                viewSize.y);

        return (spriteAABB.intersects(currentViewRect));
}

Basically, as you said, I didn't understand what getCenter() returned. Also I was wrongly creating the FloatRect of the view (height and width was wrong).
The tutorial is great, thanks!

Hapax

  • Hero Member
  • *****
  • Posts: 3387
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [SOLVED] How to know if sprite is in view
« Reply #3 on: January 15, 2017, 02:11:47 pm »
FloatRects can also be constructed using a couple Vector2fs, which can make your usage even simpler with something like:
bool Sector::lightInView(sf::FloatRect spriteAABB)
{
        // these are both float vectors
        sf::Vector2f viewCenter(Game::GetWindow()->getView().getCenter());
        sf::Vector2f viewSize(Game::GetWindow()->getView().getSize());

        sf::FloatRect currentViewRect(viewCenter - viewSize / 2.f, viewSize);

        return (spriteAABB.intersects(currentViewRect));
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Guido_Ion

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Flow Soccer development
Re: [SOLVED] How to know if sprite is in view
« Reply #4 on: January 17, 2017, 07:12:59 pm »
FloatRects can also be constructed using a couple Vector2fs, which can make your usage even simpler with something like:
bool Sector::lightInView(sf::FloatRect spriteAABB)
{
        // these are both float vectors
        sf::Vector2f viewCenter(Game::GetWindow()->getView().getCenter());
        sf::Vector2f viewSize(Game::GetWindow()->getView().getSize());

        sf::FloatRect currentViewRect(viewCenter - viewSize / 2.f, viewSize);

        return (spriteAABB.intersects(currentViewRect));
}

You are right, I was converting sf::Vector2f to sf::Vector2i for no reason. Thanks. Cleaner code  :)

 

anything