SFML community forums

Help => System => Topic started by: Jerry on June 27, 2011, 11:27:44 am

Title: DeltaTime question
Post by: Jerry on June 27, 2011, 11:27:44 am
Hi All,

I'm just getting started with SFML and C++ in general so apologies if this is a very basic question.

I'm trying to move a sprite as below using delta time to smooth the movement (simplified for clarity).

Code: [Select]

float deltaTime = 1.0f/window.GetFrameTime();

sf::Vector2f moveVector;

moveVector.x = 10.f * deltaTime;

sprite.Move(moveVector);

window.Draw(sprite);



When using delta time my sprite wont draw which I'm guessing is due to the type conversion between Uint32 and float.

What would an appropriate method of converting the correct value? I have tried a few conversion methods I found on the web but none seem to resolve the problem.

Thanks for your time.
Title: DeltaTime question
Post by: Lo-X on June 27, 2011, 11:56:23 am
1000 ms = 1 s

So you can multiply your window.GetFrameTime() by 1000 to have it in seconds :
Code: [Select]
float deltaTime = 1000.0f/window.GetFrameTime();

Then you have to try values for the speed of your sprite (here : moveVector.x = 10.f * deltaTime; )
Title: DeltaTime question
Post by: Jerry on June 27, 2011, 01:40:46 pm
Lo-X - thanks for the reply but I think my problem is not whether deltaTime is in seconds or milliseconds. Indeed I tried it anyway and my sprite still does not display.

Just to clarify the sprite displays fine if I don't mutiply by deltaTime or I if I set deltaTime some value directly.

E.g:

 
Code: [Select]

float deltaTime = 0.06;

sf::Vector2f moveVector;

moveVector.x = 10.f * deltaTime;

sprite.Move(moveVector);

window.Draw(sprite);


Thanks.
Title: DeltaTime question
Post by: Lupinius on June 27, 2011, 03:43:14 pm
What you save in deltaTime is actually the framerate. GetFrameTime already returns the value you're looking for. So this will do:
Code: [Select]
moveVector.x = 10.f * window.GetFrameTime();
Title: DeltaTime question
Post by: PeterWelzien on June 28, 2011, 08:09:48 am
GetFrameTime() returns the time since the last frame, not since program start. If you want to move the sprite you'll have to increase the position every frame:
Code: [Select]
moveVector.x += 10.f * deltaTime;
Title: DeltaTime question
Post by: OniLinkPlus on June 28, 2011, 10:19:07 am
Quote from: "PeterWelzien"
GetFrameTime() returns the time since the last frame, not since program start. If you want to move the sprite you'll have to increase the position every frame:
Code: [Select]
moveVector.x += 10.f * deltaTime;
Not since the last frame, during the last frame.
Title: DeltaTime question
Post by: Kian on June 30, 2011, 12:52:59 pm
How are frames delimited? Calls to sf::Window::Display()?
Title: DeltaTime question
Post by: OniLinkPlus on July 01, 2011, 02:17:13 am
Quote from: "Kian"
How are frames delimited? Calls to sf::Window::Display()?
Yes.