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

Author Topic: [SOLVED] SFML 2.0 restricitng how far sf::View can move.  (Read 3022 times)

0 Members and 1 Guest are viewing this topic.

sec_goat

  • Newbie
  • *
  • Posts: 17
    • View Profile
[SOLVED] SFML 2.0 restricitng how far sf::View can move.
« on: November 16, 2011, 10:47:43 pm »
This isn't necessarily a SFML related question, more likely I just am having a hard time wrapping my head around the logic needed to restrict the sf::View movement.

I want the view to move with the player, however I do not want it to display past a certain set of coordinates, whatever my play field will be (0,0) to (800,2000) or whatever.

Currently I use GetPosition on a player sprite and then set the center of my view to the same position.

Then I try to check the edges of the view against coords to stop it from moving, which I am doing all wrong so far.

Code: [Select]


sf::Vector2f player_pos = PlayerSprite.GetPosition();
sf:vector2f view_center = View.GetCenter();
sfVector2f view_size = View.GetSize();

if(view_center.x - (view_size.x /2) >= 0)
{
   View.SetCenter(player_pos)
}


I have also tried
Code: [Select]


if(player_pos.x - (view_size.x /2) >= 0)
{
   View.SetCenter(player_pos)
}

This appears to work if I don't go too far left, when I do the view freezes, because my equation obviously will never equal or go above 0;

I guess I am looking for some advice to stop the view from moving based on player position. I know i need some combonation of Player position, view size and position. But so far my attempts have all been thwarted.

Thanks!


 :P

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
[SOLVED] SFML 2.0 restricitng how far sf::View can move.
« Reply #1 on: November 17, 2011, 12:37:46 am »
Take the second if and add this:

Code: [Select]
else
{
    view_center.x = view_size.x / 2;
    view_center.y = player_pos.y;
    View.SetCenter(view_center);
}


You might also want to check your variable declarations... Each one's type is written differently.

sec_goat

  • Newbie
  • *
  • Posts: 17
    • View Profile
[SOLVED] SFML 2.0 restricitng how far sf::View can move.
« Reply #2 on: November 17, 2011, 09:03:39 pm »
Quote from: "Tex Killer"
Take the second if and add this:

Code: [Select]
else
{
    view_center.x = view_size.x / 2;
    view_center.y = player_pos.y;
    View.SetCenter(view_center);
}


You might also want to check your variable declarations... Each one's type is written differently.


Very nice, that seems to work in the specific instance i supplied. as for my type's being different, I forgot to double check those when I typed them in here. They are in fact correct in my real code.

Now I tried to expand on that code and I am running into some problems, once again probably my use of loops and not doing it correctly.
here is what I have implemeneted, it works on the y axis, but not the x, any ideas why?

Code: [Select]

if(player_pos.x - (view_size.x /2) < 0)
    {
        view_center.x = view_size.x / 2;
        view_center.y = player_pos.y;
    }
    else if(player_pos.x + (view_size.x /2) > 2115)
    {
        view_center.x = 2115 - (view_size.x /2);
        view_center.y = player_pos.y;
    }
    if(player_pos.y - (view_size.y /2) < 0)
    {
        view_center.y = view_size.y /2;
        view_center.x = player_pos.x;
    }
    else if(player_pos.y + (view_size.y /2)> 800)
    {
        view_center.y = 800 - (view_size.y /2);
        view_center.x = player_pos.x;
    }
    currentView.SetCenter(view_center);

sec_goat

  • Newbie
  • *
  • Posts: 17
    • View Profile
[SOLVED] SFML 2.0 restricitng how far sf::View can move.
« Reply #3 on: November 18, 2011, 07:56:32 pm »
It seems I have solved the problem, not 100% bug free but here is the final code that will limit the ViewPort movement to a set rectangular area

Code: [Select]

void SetView()
{

    //get players posiiton and set current view to  center on him

    sf::Vector2f player_pos =player->GetPosition();

    //now get view center and size and make sure it isnt goign off the map
   
    sf::Vector2f view_center = currentView.GetCenter();
    sf::Vector2f view_size = currentView.GetSize();

    if(player_pos.y - (view_size.y /2) < 0) //check to see if viewport y is less than 0 on y axis
    {
        view_center.y = view_size.y /2;
        view_center.x = player_pos.x;
        currentView.SetCenter(view_center);
    }

    if(player_pos.y + (view_size.y /2)> SCREEN_HEIGHT) //check to see if viewport y is greater than height.
    {
        view_center.y = SCREEN_HEIGHT - (view_size.y /2);
        view_center.x = player_pos.x;
        currentView.SetCenter(view_center);
    }

    if(player_pos.x - (view_size.x /2) < 0) //check to see if viewport  x will be less than 0 on x axis
    {
        view_center.x = view_size.x / 2;
        //view_center.y = player_pos.y;
        currentView.SetCenter(view_center);
    }

    if(player_pos.x + (view_size.x /2) > 2115) //check to see if viewport x is greater than map size 2115
    {
        view_center.x = 2115 - (view_size.x /2);
        //view_center.y = player_pos.y;
        currentView.SetCenter(view_center);
    }

}

 

anything