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

Author Topic: How to get the 4 corners of a view (converted into world coordinates)  (Read 4521 times)

0 Members and 1 Guest are viewing this topic.

DevilEdge

  • Newbie
  • *
  • Posts: 28
    • View Profile
Hi there,

For my project, I'm trying to restrict the player view area so they can't see the empty space of outside of the map. I am getting the top left coordinate correct, but for the other 3 I'm a bit confused on how to get them. Here's the code I'm trying to use:

auto viewCenterWorldCoords = window.mapPixelToCoords(sf::Vector2i(m_view.getCenter().x, m_view.getCenter().y));

m_topLeft.x = viewCenterWorldCoords.x - (m_view.getSize().x / 2);
        m_topLeft.y = viewCenterWorldCoords.y - (m_view.getSize().y / 2);
m_bottomLeft = sf::Vector2f(m_topLeft.x, viewCenterWorldCoords.y(viewCenterWorldCoords.y - (m_view.getSize().y / 2)));
        m_topRight = sf::Vector2f((viewCenterWorldCoords.x + (m_view.getSize().x / 2)), m_topLeft.y);
        m_bottomRight = sf::Vector2f(m_topRight.x, (viewCenterWorldCoords.y + (m_view.getSize().y / 2)));

Thanks for any help!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
You only need top-left and bottom-right (they store all four edges). If you want the other corners, you can create from those two.
Bottom-right is simply top-left + size, thus:
bottomRight = topLeft + view.getSize();

You could also get top-left a bit simpler by mapping (0, 0) to coords instead of the view's centre, thus:
topLeft = window.mapPixelToCoords(sf::Vector2i(0, 0));

The other option for bottom-right is to map it directly, rather than an offset from top-left, thus:
bottomRight = window.mapPixelToCoords(sf::Vector2i(window.getSize()));
Notice that it's the size of the window, not the size of the view.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
sf::View is a sf::FloatRect, therefore it has 4 parameters:
1. Left
2. Top
3. Width
4. Height

The first 2 parameters are used to set the starting position of the sf::FloatRect within the window.
The last 2 parameter are used to set the size of the sf::FloatRect.

So in order to get the corners of the sf::View (i.e. sf::FloatRect), the code should look something like this:

// Never mind
 

Now you have the four corners of the sf::View. ;)

Igrnore this post.  :-X
« Last Edit: July 31, 2015, 09:27:36 pm by Brax »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
sf::View is a sf::FloatRect
No it isn't; sf::View is an sf::View.
It takes an sf::FloatRect in its constructor but it doesn't store the information this way.
To get information from an sf::View, you use its getters e.g. getSize(), getCenter().
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
sf::View is a sf::FloatRect
No it isn't; sf::View is an sf::View.
It takes an sf::FloatRect in its constructor but it doesn't store the information this way.
To get information from an sf::View, you use its getters e.g. getSize(), getCenter().

You are right. I got confused because it took sf::FloatRect as a constructor. I will shut up.  :-X

DevilEdge

  • Newbie
  • *
  • Posts: 28
    • View Profile
You only need top-left and bottom-right (they store all four edges). If you want the other corners, you can create from those two.
Bottom-right is simply top-left + size, thus:
bottomRight = topLeft + view.getSize();

You could also get top-left a bit simpler by mapping (0, 0) to coords instead of the view's centre, thus:
topLeft = window.mapPixelToCoords(sf::Vector2i(0, 0));

The other option for bottom-right is to map it directly, rather than an offset from top-left, thus:
bottomRight = window.mapPixelToCoords(sf::Vector2i(window.getSize()));
Notice that it's the size of the window, not the size of the view.

Thanks a lot, I have it all solved now.

DevilEdge

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: How to get the 4 corners of a view (converted into world coordinates)
« Reply #6 on: August 01, 2015, 11:11:59 pm »
Ok, so I'm getting all of the coordinates correctly now. I'm able to keep the view within the map, but I'm having trouble allowing the camera to move one direction with the player but not the other. An example would be if the player is on the very left side of the map (so the x-direction of the camera stays within the map), but the y-axis of the viewport would move up and down. Here's some of the code I was trying to use, but it's not working as expected:

    static bool regularLeft = true;
        static bool regularUp = true;
        static bool regularRight = true;
        static bool regularDown = true;

        static auto xPos = 0.f;
        static auto yPos = 0.f;

        if (topLeft.x <= 0.f)
        {
                if (xPos == 0.f)
                        xPos = m_spriteToFollow->getPosition().x;

                regularLeft = false;

                if (m_spriteToFollow->getPosition().x > xPos)
                        regularLeft = true;

                else
                {
                        if ((regularUp && !regularDown) || (!regularUp && regularDown))
                                m_view.setCenter(xPos, m_spriteToFollow->getPosition().y);

                        else
                                m_view.setCenter(xPos, yPos);
                }
        }

        if (topLeft.y <= 0.f)
        {
                if (yPos == 0.f)
                        yPos = m_spriteToFollow->getPosition().y;

                regularUp = false;

                if (m_spriteToFollow->getPosition().y > yPos)
                        regularUp = true;

                else
                {
                        if ((regularLeft && !regularRight) || (!regularLeft && regularRight))
                                m_view.setCenter(m_spriteToFollow->getPosition().x, yPos);

                        else
                                m_view.setCenter(xPos, yPos);
                }
        }

        if (regularLeft && regularUp && regularRight && regularDown)
                m_view.setCenter(m_spriteToFollow->getPosition());

This is within my Camera::update() method.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to get the 4 corners of a view (converted into world coordinates)
« Reply #7 on: August 02, 2015, 01:12:28 am »
I don't understand why you're using auto to save yourself from typing float.

Why don't you use
sf::Vector2f pos(0.f, 0.f);
instead of
float xPos = 0.f;
float yPos = 0.f;
You can still access the x and y individually and it would make a couple of the setters 'look nicer':
m_view.setCenter(pos);

It's not perfectly clear what all of your variables do or what they're for. The only thing I noticed is that your pos variables are not getting updated.
i.e. xPos and yPos only get set once.
And this may actually be never, depending on what your "topLeft" actually is.
If xPos and yPos get altered elsewhere, this is a perfect example of why complete (and minimal) code is required  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to get the 4 corners of a view (converted into world coordinates)
« Reply #8 on: August 02, 2015, 10:01:24 am »
I totally agree with Hapax, especially regarding the use of vectors instead of separate floats.

Another question is: why static? Uses like that are almost never justified, but rather result from lazyness or lack of knowledge. If you need a variable to persist across functions, then make it a member.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: