I have this loop:
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (mWindow.isOpen())
{
processEvents();
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
render();
}
and in update function I do this:
object.speed = object.speed * TimePerFrame.asSeconds();
and then I run a method in which I do:
sprite.move(cos(sprite.getRotation()*PI / 180) * speed, sin(sprite.getRotation()*PI / 180) * speed);
But the problem is that the sprite doesn't move. When I don't multiply speed by TimePerFrame.asSeconds(), it does move. How should I fix it so everything is correct? Where and how to use TimePerFrame variable?
object.speed = object.speed * TimePerFrame.asSeconds();
This line is reducing the speed every frame until it is 0.
You need to scale the movement distance by delta time whenever you move the object. Try this:
float distance = speed * TimePerFrame.asSeconds();
sprite.move(cos(sprite.getRotation()*PI / 180) * distance, sin(sprite.getRotation()*PI / 180) * distance);