c++ - Movement of sprite using fixed time step -
i have 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 this:
object.speed = object.speed * timeperframe.asseconds();
and run method in do:
sprite.move(cos(sprite.getrotation()*pi / 180) * speed, sin(sprite.getrotation()*pi / 180) * speed);
but problem sprite doesn't move. when don't multiply speed timeperframe.asseconds(), move. how should fix correct? , how use timeperframe variable?
object.speed = object.speed * timeperframe.asseconds();
this line reducing speed every frame until 0.
you need scale movement distance delta time whenever move object. try this:
float distance = speed * timeperframe.asseconds(); sprite.move(cos(sprite.getrotation()*pi / 180) * distance, sin(sprite.getrotation()*pi / 180) * distance);
Comments
Post a Comment