### Author Topic: Finding Delta time in SFML 2.0  (Read 13612 times)

0 Members and 1 Guest are viewing this topic.

#### billboard_baggins

• Newbie
• Posts: 9
##### Finding Delta time in SFML 2.0
« on: February 18, 2012, 08: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())

#### Nexus

• SFML Team
• Hero Member
• Posts: 6118
• Thor Developer
##### Finding Delta time in SFML 2.0
« Reply #1 on: February 18, 2012, 09: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();}`
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: first SFML book

#### billboard_baggins

• Newbie
• Posts: 9
##### Finding Delta time in SFML 2.0
« Reply #2 on: February 18, 2012, 11:31:11 pm »
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?

#### Nexus

• SFML Team
• Hero Member
• Posts: 6118
• Thor Developer
##### Finding Delta time in SFML 2.0
« Reply #3 on: February 19, 2012, 08: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());`
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: first SFML book

#### billboard_baggins

• Newbie
• Posts: 9
##### Finding Delta time in SFML 2.0
« Reply #4 on: February 19, 2012, 04: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.

anything