• Yet another SFML question.
    12 replies, posted
I'm feeling bad already for asking these much questions, sorry about that. How do I get a camera to follow a sprite? I tried pretty much everything that I can think of. Thanks again kind gentlemen. [cpp] #include <SFML/Graphics.hpp> int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Zombie Game Alpha"); sf::Image PlayerImage; sf::Image Background; Background.LoadFromFile("background.png"); PlayerImage.LoadFromFile("player.png"); sf::Sprite Player(PlayerImage); sf::Sprite BackgroundS(Background); sf::Sprite* follow = &Player; Player.SetPosition(400,300); sf::View Camera(sf::Vector2f(400.f, 300.f), sf::Vector2f(400.f, 300.f)); sf::Vector2f PPos = Player.GetPosition(); while(Window.IsOpened()) { sf::Event Event; while (Window.GetEvent(Event)) { if (Event.Type == sf::Event::Closed) Window.Close(); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) Window.Close(); } float ElapsedTime = Window.GetFrameTime(); if (Window.GetInput().IsKeyDown(sf::Key::W)) Player.Move(0, -425 * ElapsedTime); if (Window.GetInput().IsKeyDown(sf::Key::S)) Player.Move(0, 425 * ElapsedTime); if (Window.GetInput().IsKeyDown(sf::Key::A)) Player.Move(-425 * ElapsedTime, 0); if (Window.GetInput().IsKeyDown(sf::Key::D)) Player.Move(425 * ElapsedTime, 0); Player.SetCenter(Player.GetSize() / 2.f); if (Window.GetInput().GetMouseX() <= Player.GetPosition().x) { Player.SetRotation(((-1* 360 / 3.1415926535 * (atan2(static_cast<double>(Player.GetPosition().y - Window.GetInput().GetMouseY()), static_cast<double>(Player.GetPosition().x - Window.GetInput().GetMouseX()))))/2)+90); } else { Player.SetRotation(((-1* 360 / 3.1415926535 *(atan2(static_cast<double>(Player.GetPosition().y - Window.GetInput().GetMouseY()), static_cast<double>(Player.GetPosition().x - Window.GetInput().GetMouseX()))))/2)+90); } Camera.SetCenter(follow->GetCenter()); Window.Draw(BackgroundS); Window.Draw(Player); Window.SetView(Camera); Window.Display(); } } [/cpp]
[url]http://www.sfml-dev.org/tutorials/1.5/graphics-views.php[/url] Views
I know about views, I just don't know how to get it to follow a sprite in a top-down view.
You need to have a pointer to the sprite and use the sprites GetCenter() functions and the views SetCenter(). Something like this:[cpp] sf::Sprite* follow = &some_sprite view.SetCenter(follow->GetCenter());[/cpp]
im dumb
[cpp]sf::Sprite* follow = &Player;[/cpp] try that
Yeah, my mistake didn't put in the &. Still it doesn't work. I put "view.SetCenter(follow->GetCenter());" in the game loop and when I compile and run the background is flickering and stuff, the camera doesn't move. And I don't understand HalfSize, the window is 800x600, what should I put in the HalfSize of the view?
[QUOTE=DarkSpirit05er;17142350]Yeah, my mistake didn't put in the &. Still it doesn't work. I put "view.SetCenter(follow->GetCenter());" in the game loop and when I compile and run the background is flickering and stuff, the camera doesn't move. And I don't understand HalfSize, the window is 800x600, what should I put in the HalfSize of the view?[/QUOTE] Uh, you put half the size of the view. For 800x600 you would create the view like this: [cpp]sf::View view(sf::Vector2f(400.f, 300.f), sf::Vector2f(400.f, 300.f));[/cpp](you can obviously change the center to whatever you need)
Updated OP with non-working script. [editline]09:44PM[/editline] And to whomever is rating me funny deserves a punch in the face. I'm learning to program, I don't have full knowledge of everything so I'm terribly sorry if I make mistakes (not).
Set the view before drawing.
Did, this happened: [img]http://img32.imageshack.us/img32/6497/38941320.png[/img] But the weird flickering stopped.
GetCenter might return a local vector (dunno tbh). Try View.SetCenter(follow->GetPosition()).
Worked, thanks a lot! Although it seems like I need a really big background. Thanks again.
Sorry, you need to Log In to post a reply to this thread.