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

Author Topic: Choppy Movement With DeltaTime  (Read 2316 times)

0 Members and 1 Guest are viewing this topic.

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Choppy Movement With DeltaTime
« on: April 04, 2017, 01:31:48 am »
Hi everyone,

I'm struggling to get smooth movement in a game I'm working on. I was originally using a fixed timestep loop for handling the game logic, but I noticed that the movement was choppy, so I changed it (at least for now) to use delta time instead, but the choppy movement still persists.

Here is a minimal example of the code :

#include <SFML\Graphics.hpp>

int main()
{
       
        sf::RenderWindow win;
        win.create(sf::VideoMode(640, 512), "");
        win.setVerticalSyncEnabled(true);
        sf::Sprite p;
        sf::Texture t;
        t.loadFromFile("graphics/player.png");
        p.setTexture(t);
        p.setPosition(100, 100);
        sf::Clock clock;
        sf::Time dt;
        while (win.isOpen()) {
                dt = clock.restart();
                sf::Event event;
               
                while (win.pollEvent(event)) {

                        if (event.type == sf::Event::Closed) {
                                win.close();
                        }

                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
                p.move(0, -32 * dt.asSeconds());
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
                p.move(-32 * dt.asSeconds(), 0);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
                p.move(0, 32 * dt.asSeconds());
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
                p.move(32 * dt.asSeconds(), 0);
                }

                win.clear();
                win.draw(p);
                win.display();
        }
        return 0;
}
 

As I say this results in choppy movement. By that, I mean that roughly once or twice a second the player stutters and the movement is not completely smooth or continuous. I've looked at a lot of questions regarding this same problem already, but most were caused by problems not present in my code, and otherwise I couldn't find a solution.

Any help would be appreciated thanks.

Hapax

  • Hero Member
  • *****
  • Posts: 3359
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Choppy Movement With DeltaTime
« Reply #1 on: April 04, 2017, 03:35:47 am »
The first thing to try would be to "fix your timestep". That isn't to just use a fixed timestep but rather a fixed timestep in a flexible way.

If you are still having "choppy" movement after implementing the "free the physics" section, you may need to implement the more complicated "the final touch" section.

If you are interested in using a small library that implements that solution, you can try Kairos' Timestep.
This Kairos Timestep Example
shows the usage of Kairos to implement The Final Touch section in the above artice.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything