SFML community forums

Help => System => Topic started by: billboard_baggins on February 18, 2012, 09:52:10 pm

Title: Finding Delta time in SFML 2.0
Post by: billboard_baggins on February 18, 2012, 09:52:10 pm
I'm still new to game programming, so please forgive me if I'm asking a stupid question. I can't seem to wrap my head around how to find the time between frames (delta time) in SFML 2.0. I understand 1.6 had "GetFrameTime()" but that it is now removed.

I'm trying to get smoother player movement by multiplying velocity by delta time, but I need to figure out delta time first. Does anyone have an easy example for this?

I tried creating a function to calculate the delta time (runs once every screen render):

Code: [Select]

    sf::Time currentTime;
    //get current elapsed time of frame
    currentTime = deltaClock.GetElapsedTime();

    deltaTime = ( currentTime - prevDeltaTime );

    prevDeltaTime = currentTime;
    deltaClock.Restart();


And then multiplying player velocity by the variable "deltaTime" (which is of type sf::Time) but it either doesn't move the player at all or at very small/random intervals (if i multiply by deltaTime.AsMilliseconds())
Title: Finding Delta time in SFML 2.0
Post by: Nexus on February 18, 2012, 10:04:03 pm
It's enough if you declare an sf::Clock outside the loop, restart it every frame and store the elapsed time. Note that Restart() returns the passed time.
Code: [Select]
sf::Clock deltaClock;
for (;;)
{
    // ...
    sf::Time dt = deltaClock.Restart();
}
Title: Finding Delta time in SFML 2.0
Post by: billboard_baggins on February 19, 2012, 12:31:11 am
Thanks, I was making it more complicated than it needed to be. So am I right in assuming I would now do something like:

mySprite.Move( xVel * dT.AsMilliseconds(), yVel * dT.AsMilliseconds() ) ?

That's what I'm doing and although it's smoother it still jerks around a bit. Should I also be utilizing delta time when updating the display?
Title: Finding Delta time in SFML 2.0
Post by: Nexus on February 19, 2012, 09:35:27 am
I prefer dt.AsSeconds() to get floating points. Of course, the velocity has to be adapted correspondingly.

Don't you want to use a velocity vector?
Code: [Select]
mySprite.Move(velocity * dt.AsSeconds());
Title: Finding Delta time in SFML 2.0
Post by: billboard_baggins on February 19, 2012, 05:06:16 pm
Thank you! Using AsSeconds did the trick. As for using a vector, I.. don't know why I'm not using them.  :)