SFML community forums

Help => Graphics => Topic started by: andrewhopps on July 19, 2013, 10:59:39 pm

Title: Camera Boundary
Post by: andrewhopps on July 19, 2013, 10:59:39 pm
Can anyone point me in the direction of some type of idea how to get a centered camera to stop moving in the bottom and right directions. Should I be figuring out the coordinates of the camera and making sure they are less than my background?
Title: Re: Camera Boundary
Post by: solver on July 19, 2013, 11:21:09 pm
I used a Camera class, that implements certain behaviour. This class can have got a field for world's size, that sets when level is loading. So, camera can make decision: to move or not.
Title: Re: Camera Boundary
Post by: eXpl0it3r on July 19, 2013, 11:53:59 pm
If I understood you right, you want to stop the camera once it would potentially leave the background texture, is that right?

For a specific answer, we'd have to know, what you're already got and at what point you're stuck.

A general answer is rather simple: Keep track of the logical boundaries and stop the movement of the camera once it would go too far. If you're using an sf::View you can track the center of the view and check with its size what the current "window" is showing. From that you can easily make a check to whether it's going outside of the desired area and thus simply blocking the movement.
Title: Re: Camera Boundary
Post by: andrewhopps on July 20, 2013, 06:41:37 am
    sf::View view;
    view.reset(sf::FloatRect(0,0, screenDimensions.x, screenDimensions.y));
    view.setViewport(sf::FloatRect(0,0, 1.0f, 1.0f));
    sf::Vector2f position(screenDimensions.x / 2, screenDimensions.y / 2);

        if(animatedSprite.getPosition().x + 16 > screenDimensions.x / 2)
            position.x = animatedSprite.getPosition().x + 16;
        else
            position.x = screenDimensions.x / 2;

        if(animatedSprite.getPosition().y + 16 > screenDimensions.y / 2)
            position.y = animatedSprite.getPosition().y + 16;
        else
            position.y = screenDimensions.y / 2;

        view.setCenter(position);

        window.setView(view);

I keep trying to keep the camera from going off the background by tracking the sprite, but that obviously was not working right!

If I understood you right, you want to stop the camera once it would potentially leave the background texture, is that right?

Yes, that is exactly what I am trying to do. Keeping everything a variable that can change with the size of the map.