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

Author Topic: Move the view if the entity's out view.  (Read 1444 times)

0 Members and 1 Guest are viewing this topic.

decoder

  • Newbie
  • *
  • Posts: 8
    • View Profile
Move the view if the entity's out view.
« 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).
« Last Edit: June 11, 2018, 09:51:30 am by decoder »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Move the view if the entity's out view.
« Reply #1 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.

decoder

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Move the view if the entity's out view.
« Reply #2 on: June 16, 2018, 04:48:19 am »
The horizontal movement is fine, but how could I do the same for vertical.