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

Author Topic: Camera Boundary  (Read 2576 times)

0 Members and 1 Guest are viewing this topic.

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Camera Boundary
« 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?

solver

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Camera Boundary
« Reply #1 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Camera Boundary
« Reply #2 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

andrewhopps

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Camera Boundary
« Reply #3 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.