SFML community forums
Help => General => Topic started by: sec_goat 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.
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
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
-
Take the second if and add this:
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.
-
Take the second if and add this:
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?
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);
-
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
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);
}
}