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

Author Topic: How to check if a sprite is visible within a view ?  (Read 6108 times)

0 Members and 2 Guests are viewing this topic.

Tigrou

  • Newbie
  • *
  • Posts: 15
    • View Profile
How to check if a sprite is visible within a view ?
« on: February 17, 2017, 11:30:07 am »
Hi all,
I am a beginner with SFML and have a question.

Let's say I have a game with a scrollable view.

Is there a simple way using SFML to check if a given sprite (eg : an enemy) is visible within a view ? By visible I mean the sprite can be seen with the current scroll values, not that it is occluded by another sprite.

I have same problem with mouse : how to check that a the mouse cursor is inside a view ?
« Last Edit: February 17, 2017, 02:51:33 pm by Tigrou »

Turbine

  • Full Member
  • ***
  • Posts: 100
    • View Profile
Re: How to check if a sprite is visible within a view ?
« Reply #1 on: February 17, 2017, 03:06:20 pm »
Just use some math.

Something along the lines of (this isn't code):
if (object.x > view.x && object.y > view.y && object.x < view.x + view.width && object.y < view.y + view.height)

It's just to give you an idea of how you could achieve it. Obviously you'd make functions and what not for handling such things in a more generic and reusable way.

JayhawkZombie

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: How to check if a sprite is visible within a view ?
« Reply #2 on: February 17, 2017, 03:57:22 pm »
try sf::Rect::Intersects https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Rect.php#a566740c8f58e01bb052266f47e7e1011

sf::FloatRect View;
...

auto rect = sprite.getGlobalBounds();
if (rect.intersects(View))
...

Tigrou

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: How to check if a sprite is visible within a view ?
« Reply #3 on: February 17, 2017, 04:18:32 pm »
@JayhawkZombie :
Thanks.

I have solved the problem for checking the if the mouse cursor is inside view (but not for the sprite) :

IntRect viewport = window.GetViewport(myView);
Vector2i mousePosition = ...
if (viewport.Contains(mousePosition))
{
  //...
}



« Last Edit: February 17, 2017, 09:06:48 pm by Tigrou »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to check if a sprite is visible within a view ?
« Reply #4 on: February 18, 2017, 11:38:10 pm »
I have solved the problem for checking the if the mouse cursor is inside view (but not for the sprite) :
rect.contains(point)
is for testing if a point (such as the position of the mouse cursor) is inside the rectangle:
http://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Rect.php#a24163acdb9b2987c0ea55c201e270d41

rect.intersects(rect)
is for testing if two rectangles (such as a sprite bounding box and a view rectangle) overlap:
http://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Rect.php#a566740c8f58e01bb052266f47e7e1011

Did JayhawkZombie's solution not work for you?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything