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

Author Topic: Delta Time  (Read 1397 times)

0 Members and 1 Guest are viewing this topic.

kidchameleon

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Delta Time
« on: January 03, 2012, 11:26:12 am »
Im confused trying to implement delta time for my character movement. here is some code (Ive only shown important parts for simplicity):

Code: [Select]

sf::RenderWindow Game1(sf::VideoMode(800, 600), "Platform Engine", sf::Style::Close);

Game1.EnableVerticalSync(true);
//Game1.SetFramerateLimit(25);

//time in seconds
float deltaTime = Game1.GetFrameTime() / 1000.0f;

if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Left))
{
    playerX -= 40.0f * deltaTime;
}

if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Right))
{
    playerX += 40.0f * deltaTime;
}

playerSprite.SetPosition(playerX, playerY);


Problem is, when I run the game with frame limit set to 25, the character takes 20 seconds to cross the screen, as it should be. But if I have no frame limit, it takes longer for the character to cross the screen. Id like to release with no frame limit, but upon testing on other machines, both slower and faster ones, I'm still getting varying speeds of character movement.

Any ideas on what I'm doing wrong?

Cheers.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Delta Time
« Reply #1 on: January 03, 2012, 11:30:40 am »
Is vertical sync actually working? If it's disabled (in your graphics driver) and you get a big number of FPS, since GetFrameTime() is rounded to millisecond, you can loose a non-negligible part of the elapsed time.
Laurent Gomila - SFML developer

kidchameleon

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Delta Time
« Reply #2 on: January 04, 2012, 03:06:28 pm »
Thanks Laurent, I'm going to look into that now.