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

Author Topic: stuck in World::adaptPlayerPosition() pls help  (Read 2053 times)

0 Members and 1 Guest are viewing this topic.

The Thinker

  • Newbie
  • *
  • Posts: 19
    • View Profile
stuck in World::adaptPlayerPosition() pls help
« on: January 10, 2015, 01:59:18 am »
Hi all,

At page 108 of SFML Game Development book I have this code:

void World::adaptPlayerPosition()
{
    // Keep player's position inside the screen bounds, at least borderDistance units from the border
    sf::FloatRect viewBounds(mWorldView.getCenter() - mWorldView.getSize() / 2.f, mWorldView.getSize());
    const float borderDistance = 40.f;
   
    sf::Vector2f position = mPlayerAircraft->getPosition();
    position.x = std::max(position.x, viewBounds.left + borderDistance);
    position.x = std::min(position.x, viewBounds.left + viewBounds.width - borderDistance);
    position.y = std::max(position.y, viewBounds.top + borderDistance);
    position.y = std::min(position.y, viewBounds.top + viewBounds.height - borderDistance);
    mPlayerAircraft->setPosition(position);
}

-Can you please explain me where we set viewBounds.width and viewBounds.height? Or how we retrieved them..

From my understanding we just set the LEFT X and TOP Y position with
sf::FloatRect viewBounds(mWorldView.getCenter() - mWorldView.getSize() / 2.f, mWorldView.getSize());

-Also can you please explain me in simple worlds how the following code works with the values, maybe with an example please.
I know how max and min work but i'm confused about the values we are using inside the brackets (i think viewBounds.left is 160f 'cause our window is 640w, 480h,....) :/
   
    position.x = std::max(position.x, viewBounds.left + borderDistance);
    position.x = std::min(position.x, viewBounds.left + viewBounds.width - borderDistance);
    position.y = std::max(position.y, viewBounds.top + borderDistance);
    position.y = std::min(position.y, viewBounds.top + viewBounds.height - borderDistance);

Thank you :)


Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: stuck in World::adaptPlayerPosition() pls help
« Reply #1 on: January 10, 2015, 02:10:56 am »
From my understanding we just set the LEFT X and TOP Y position with
sf::FloatRect viewBounds(mWorldView.getCenter() - mWorldView.getSize() / 2.f, mWorldView.getSize());
Those methods all return Vector2s, so it's using the FloatRect constructor that takes two Vector2s.

Quote
I know how max and min work but i'm confused about the values we are using inside the brackets (i think viewBounds.left is 160f 'cause our window is 640w, 480h,....) :/
I don't think the view is identical to the window size/position.  Otherwise mWorldView wouldn't serve much of a purpose.  So the actual numbers involved are going to vary depending on where the view is.

Quote
-Also can you please explain me in simple worlds how the following code works with the values, maybe with an example please.

The code is essentially:
    position.x = std::max(position.x, xCoordinateOfLeftEdge);
    position.x = std::min(position.x, xCoordinateOfRightEdge);
    position.y = std::max(position.y, yCoordinateOfTopEdge);
    position.y = std::min(position.y, yCoordinateOfBottomEdge);

which in English means "if position is not inside the current world view, move it back into the current world view".  In some contexts you would call this "clamping" the position variable.

The +/-borderDistance simply ensures that the object will not only remain inside the view, but at least borderDistance pixels inside the view, presumably so that you can't end up with half the object outside the view or something.

Since you requested an example: Say your character is 40x40 pixels, so you set borderDistance to 20 pixels.  Your world view is 100x100 at (0,0), because I like round numbers.  Somehow your player ends up at position=(-5, -5).  Those four lines of code move him back to (20, 20), so he's fully visible.


P.S. I've never read the book so all of the above is guessing from the code snippet you posted.  But it's clean, readable code doing very simple, standard things so I should be pretty close.
« Last Edit: January 10, 2015, 02:16:43 am by Ixrec »

The Thinker

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: stuck in World::adaptPlayerPosition() pls help
« Reply #2 on: January 10, 2015, 03:53:03 am »
Great explanation, now I get it!

Thank you! :)