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