So I've been working with SFML for a little while now and wanted to try using delta time to move an object in game. Here's my code:
AssetManager::LoadTexture("player");
VideoManager vm;
Paddle paddle;
sf::Clock clock;
sf::Time time = sf::milliseconds(0);
float lastDelta = 0;
while (vm.isWindowOpen())
{
vm.Update();
sf::Time curTime = clock.getElapsedTime();
sf::Time frameDelta = curTime - time;
std::cout << frameDelta.asMilliseconds() << std::endl;
paddle.Update(frameDelta.asMilliseconds());
time = curTime;
vm.Clear();
paddle.Render(&vm);
vm.Display();
}
AssetManager::UnloadAssets();
return 0;
The problem is though that I get some strange timings in the system that seems to be messing with the movement of the object and resulting in it sort of skipping ahead a little bit sometimes. I'm getting a pretty constant 16 milliseconds per update with vsync on, but sometimes it becomes 17 (rounding, makes sense I suppose) or in some sort of common instances, it becomes 33 or 1 or 0 and I would be okay with this if it wasn't for the fact that the skipping around of the object is somewhat noticeable. Is there any way to deal with this big time jump? The 33 milliseconds literally skips the object ahead, it's really annoying for me.
Not sure if the object movement code is relevant but here it is:
sf::Vector2f vel;
// Use user input to determine direction of paddle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
vel.x = PADDLE_SPEED;
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
vel.x = -PADDLE_SPEED;
}
pos.x += vel.x * (delta / 1000.f);
pos.y += vel.y * (delta / 1000.f);