Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: DeltaTime question  (Read 6330 times)

0 Members and 2 Guests are viewing this topic.

Jerry

  • Newbie
  • *
  • Posts: 2
    • View Profile
DeltaTime question
« 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.

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
DeltaTime question
« Reply #1 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; )

Jerry

  • Newbie
  • *
  • Posts: 2
    • View Profile
DeltaTime question
« Reply #2 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.

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
DeltaTime question
« Reply #3 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();

PeterWelzien

  • Newbie
  • *
  • Posts: 38
    • View Profile
DeltaTime question
« Reply #4 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;
/Peter Welzien

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
DeltaTime question
« Reply #5 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.
I use the latest build of SFML2

Kian

  • Newbie
  • *
  • Posts: 44
    • View Profile
DeltaTime question
« Reply #6 on: June 30, 2011, 12:52:59 pm »
How are frames delimited? Calls to sf::Window::Display()?

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
DeltaTime question
« Reply #7 on: July 01, 2011, 02:17:13 am »
Quote from: "Kian"
How are frames delimited? Calls to sf::Window::Display()?
Yes.
I use the latest build of SFML2