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

Author Topic: creating a decent camera movement  (Read 5768 times)

0 Members and 1 Guest are viewing this topic.

Alidro

  • Newbie
  • *
  • Posts: 18
    • View Profile
creating a decent camera movement
« on: September 16, 2017, 05:31:04 pm »
Hello,
so i've been trying to create a smooth and nice camera with the sf::View in sfml. Currently, i have a camera which moves when the Player leaves a specific area (the Player can move inside a box but if he gets out of it the camera will follow him). It is working as i wished, but its not smooth. The code is:
#include "Camera.h"



Camera::Camera()
{
        view.setSize(384, 216);
        view.setCenter(0, 0);
}

void Camera::Render(RenderWindow &window)
{
        window.setView(view);
}

void Camera::Update(FloatRect object, Vector2f objectVelocity)
{

        if (object.left + object.width > view.getCenter().x + radius)
        {
                if (viewX < objectVelocity.x)
                        viewX = viewX + camSpeed;
        }
        else if (object.left < view.getCenter().x - radius)
        {
                if (viewX > objectVelocity.x)
                        viewX = viewX - camSpeed;
        }
        else
        {
                if (viewX < -camSpeed * 0.9)
                {
                        viewX = viewY + camSpeed * 0.9;
                }
                else if (viewX > camSpeed * 0.9)
                {
                        viewX = viewY - camSpeed * 0.9;
                }
                else
                        viewX = 0;
        }
       
        if (object.top + object.height > view.getCenter().y + radius / 1.5)
        {
                if (viewY < objectVelocity.y)
                        viewY = viewY + camSpeed;
        }
        else if (object.top < view.getCenter().y - radius / 1.5)
        {
                if (viewY > objectVelocity.y)
                        viewY = viewY - camSpeed;
        }
        else
        {
                if (viewY < -camSpeed * 0.9)
                {
                        viewY = viewY + camSpeed * 0.9;
                }
                else  if (viewY > camSpeed * 0.9)
                {
                        viewY = viewY - camSpeed * 0.9;
                }
                else
                        viewY = 0;
        }
        view.move(viewX, viewY);
       
}
in the cpp file and the h file:
#include <SFML\Graphics.hpp>
#include <iostream>

using namespace sf;

class Camera
{
public:
        Camera();
        ~Camera();
        void Render(RenderWindow &window);
        void Update(FloatRect object, Vector2f objectVelocity);
private:
        View view;
        int radius = 60;
        float viewY = 0, viewX = 0;
        float camSpeed = 2;

};

 

The Problem seems to be the camera moving a bit faster than the player, then camera passes the player and slows down, this happening inside a fraction of a second causes the camera to look stuttering. But I dont know how to solve this Problem.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: creating a decent camera movement
« Reply #1 on: September 16, 2017, 11:03:23 pm »
What's your idea of "smooth"? Can't you just match the players position 1:1 without the need to add some speed to the view? Or do you want to slow down/speed up?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Alidro

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: creating a decent camera movement
« Reply #2 on: September 17, 2017, 11:40:07 am »
I dont want to just set the camera to the players position because that would be a pain to look at. I want the camera to move just when the player gets to far away from the Center, also i want the camera to accelerate and deccelerate and not to move suddenly.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: creating a decent camera movement
« Reply #3 on: September 17, 2017, 12:31:12 pm »
Try moving the camera by the distance between it and the player multiplied by the frame time. It'll move faster the further away it is, decelerating smoothly as it gets closer to the player.

Alidro

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: creating a decent camera movement
« Reply #4 on: September 17, 2017, 02:05:49 pm »
What exactly do you mean with that? I cant really understand your idea.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: creating a decent camera movement
« Reply #5 on: September 17, 2017, 05:07:50 pm »
Something like this:

Camera::update(float dt)
{
    sf::Vector2f movement = player.getPosition() - camera.getPosition();
    camera.move(movement * dt);
}
 

If you do that every frame the camera will move slightly closer to the player (1/60th of the distance or whatever your delta time is) which means the next frame the distance will be smaller if the player is not moving, slowing down the camera. The smaller the distance the slower the camera movement, until eventually it reaches the player position (you might have to clamp it within a few pixels here) and the camera stops moving. This will give a nice deceleration as the camera reaches its target. The target does not necessarily have to be the player position, if you want the player to be able to move within some sort of bounds without the camera moving you can set the target position to be the point at which the player intersects those bounds.

Alidro

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: creating a decent camera movement
« Reply #6 on: September 17, 2017, 07:06:08 pm »
OK, i can understand it now, but how do i get "dt" ?

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: creating a decent camera movement
« Reply #7 on: September 17, 2017, 10:12:32 pm »
dt or 'delta time' ie the time difference between frames, is derived from the assumption you're using something along these lines: https://gafferongames.com/post/fix_your_timestep/

Alidro

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: creating a decent camera movement
« Reply #8 on: September 18, 2017, 02:13:04 pm »
Thank you, this is really a great camera movement. I really appreciate your effort answering my questions.

 

anything