SFML community forums

Help => Graphics => Topic started by: decoder on June 11, 2018, 09:48:45 am

Title: Move the view if the entity's out view.
Post by: decoder on June 11, 2018, 09:48:45 am
How can I make the view follow the player entity when it's nearly out of the drone's (the view) view.
I have here my code for the drone's movement.

void World::update(const sf::Time& deltaTime)
{
        mSceneGraph.update(deltaTime);
        mDrone.setCenter(mPlayer->getPosition());
}
 

What I want to achieve is to make the drone follow the player, but not really following the player's new position as it moves real-time. Instead, the drone will SMOOTHLY MOVE if the player is NEARLY OUT OF VIEW (not hitting the view's boundary).
Title: Re: Move the view if the entity's out view.
Post by: Arcade on June 11, 2018, 04:49:27 pm
Right now you are just setting the view's center to be the same position as the player every update. You just need to change that to only setting the view's center if the player is farther away from the center than you want.

For example, here is one way you could handle it
playerBoundary = 100
if (mPlayer->getPosition().x > mDrone.getCenter().x + playerBoundary)
{
   newViewCenter = {mPlayer->getPosition().x - playerBoundary, mDrone.getCenter().y}
   mDrone.setCenter(newViewCenter);
}
 

This would handle the player moving too far to the right. Just repeat something similar for the other 3 directions.
Title: Re: Move the view if the entity's out view.
Post by: decoder on June 16, 2018, 04:48:19 am
The horizontal movement is fine, but how could I do the same for vertical.