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.