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

Author Topic: camera in sfml 2.0  (Read 18293 times)

0 Members and 1 Guest are viewing this topic.

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
camera in sfml 2.0
« on: June 24, 2012, 09:34:33 am »
I've read the documentation and the tutorial on the wiki but I'm still having trouble setting up a view/camera that centers on the player's sprite while it moves around. I don't want the world to move, just the camera as it follows the player moving.

I've been able to get the camera to move but then the player's sprite doesn't draw.

Could anyone help me out with some pseudo-code or something?

ex:
-set view center on player's position
-draw the background with the default view
-switch to the new view
-draw the player sprite
-switch back to old view


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: camera in sfml 2.0
« Reply #1 on: June 24, 2012, 11:50:59 am »
Are you sure you've read this tutorial?
IMHO there isn't much more you can say about the view...

Set the sprite to (0,0) set the view center as described in the tutorial. Set fixed view, draw world, set custom view, draw sprite.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: camera in sfml 2.0
« Reply #2 on: June 24, 2012, 10:28:13 pm »
Yes that is the tutorial I read, and I'm doing what you've said I think.

sf::View followPlayer; //private member in class
followPlayer.setCenter(512, 384); //in constructor
followPlayer.setSize(1024, 768); //in constructor



window.draw(backgroundSprite);

window.setView(followPlayer);
followPlayer.move(shipSprite.getPosition().x, shipSprite.getPosition().y);
window.draw(shipSprite);

//set back to default
window.setView(window.getDefaultView());
 

camera still not following the player :(
« Last Edit: June 24, 2012, 10:30:56 pm by vro »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: camera in sfml 2.0
« Reply #3 on: June 24, 2012, 11:01:48 pm »
Your code does make much sense...

If you render a sprite S to a position (x, y) on a view V, then you can't move the view V to match the sprite S, because in moving V you're moving S, that's the sense of a view. ;)

So I'm now not so sure what you're trying to achive.
Should the view's center follow the sprite rendered on a default view? Or should sprite get positioned to the center of a view?

Also keep in mind the move function of the view uses relative positions, so if you want to move from A(10, 10) to B(0, 20) you'd need to give the view the vector (-10, 20).

Furthermore I would move the view around before setting it and always set the wanted view just before you want to draw on it (instead of resetting it at the end of the draw calls).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: camera in sfml 2.0
« Reply #4 on: June 24, 2012, 11:32:02 pm »
I'm just trying to follow the player around like in asteroids or something, I want the camera to center on the player while the player moves through the world.

what you're saying helps, but I think I'm still a little confused

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: camera in sfml 2.0
« Reply #5 on: June 24, 2012, 11:39:49 pm »
So you actually won't need two views...
I mean if you want the sprite to move then just move it! :D

Now where comes the view in?
If you just move the sprite and don't updated the view position at one point your sprite will run off-screen. So what you'll have to do is move the sprite and then move the view relative to the sprite. You could even define an area in which the view doesn't get updated and only if you get close enough to the window border you can move it.

I'm just trying to follow the player around like in asteroids or something,
I'm not sure which Asteroids do you mean, because in the the ship didn't move at all...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: camera in sfml 2.0
« Reply #6 on: June 24, 2012, 11:44:39 pm »
I'm not sure which Asteroids do you mean, because in the the ship didn't move at all...
Yeah that is a bad example sorry, I just want the player to be in the center of the screen as he moves through the world, but i don't want to move the world, i want to move the player and have the view/camera follow.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: camera in sfml 2.0
« Reply #7 on: June 24, 2012, 11:46:31 pm »
Yeah that is a bad example sorry, I just want the player to be in the center of the screen as he moves through the world, but i don't want to move the world, i want to move the player and have the view/camera follow.

I mean if you want the sprite to move then just move it! :D

i.e.
sf::Sprite sprite(texture);
sprite.move(20.f, 20.f);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: camera in sfml 2.0
« Reply #8 on: June 24, 2012, 11:57:40 pm »
That doesn't keep the sprite centered.

I'm not having trouble moving the sprite :P I'm having trouble moving the camera/view WITH the sprite

here I found a good example:



thanks for being patient with me thus far :)

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: camera in sfml 2.0
« Reply #9 on: June 25, 2012, 06:59:49 pm »
I've written a little demonstration with a bunch of graphic objects, using a view always centered on the player, so the player is always centered on the screen when he's moving.
I believe this is what you're trying to achieve, and as you can see this is quite easy to do:
1. move the player
2. set the view center as the player position

#include <SFML/Graphics.hpp>
#include <list>

void populate(std::list<sf::RectangleShape>& objects, float x, float y)
{
        sf::RectangleShape s(sf::Vector2f(20, 20));
        s.setPosition(x, y);
        s.setFillColor(sf::Color::Green);
        objects.push_back(s);
}

int main()
{
        sf::RenderWindow app(sf::VideoMode(480, 320), "view demo");
       
        // create player, at the screen center
        sf::RectangleShape player(sf::Vector2f(20, 20));
        player.setFillColor(sf::Color::Red);
        player.setOrigin(10, 10);
        player.setPosition(240, 160);
        const float player_speed = 100;

        // create a bunch of objects
        std::list<sf::RectangleShape> objects;
        populate(objects, 310, 50);
        populate(objects, 100, 200);
        populate(objects, 250, 160);
        populate(objects, 10, 280);
        populate(objects, 70, 30);
        populate(objects, 120, 50);

        // we create our custom view
        sf::View player_view(sf::FloatRect(0, 0, app.getSize().x, app.getSize().y));
       
        sf::Clock clock;
        bool running = true;
        sf::Event event;
        while (running)
        {
                while (app.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                running = false;
                }

                // moving player
                float frametime = clock.getElapsedTime().asSeconds();

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        player.move(0, -player_speed * frametime);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        player.move(0, player_speed * frametime);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        player.move(-player_speed * frametime, 0);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        player.move(player_speed * frametime, 0);
       
                clock.restart();

                // we keep our view centered on the player
                player_view.setCenter(player.getPosition());
                app.setView(player_view);

                // rendering entities
                app.draw(player);
                for (std::list<sf::RectangleShape>::const_iterator it = objects.begin(); it != objects.end(); ++it)
                {
                        app.draw(*it);
                }
                app.display();
                app.clear();
        }
        app.close();
        return 0;
}
 

I hope everything is clear.

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: camera in sfml 2.0
« Reply #10 on: June 26, 2012, 12:23:41 am »
that's exactly what I wanted to do, and it turns out I had tried that exact thing Haze, but when I try it in my code it doesn't load the player sprite. but the camera does move. i don't know what its doing, thanks for making the example though.

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: camera in sfml 2.0
« Reply #11 on: June 26, 2012, 12:41:05 am »
figured it out... i was using sf::View correctly the entire time... there was an issue elsewhere in my code causing the ship not to draw. really frustrating lol, thanks for taking the time to help

 

anything