SFML community forums

Help => Graphics => Topic started by: mic4h on March 19, 2012, 07:14:33 pm

Title: speed inconsistency (different computers)
Post by: mic4h on March 19, 2012, 07:14:33 pm
Hi! I'm new btw. Anyway, I have a pong clone copied virtually line-for-line from the "sfml game from scratch" tutorial (he calls it "Pang!"). The paddles and ball move very smoothly on my computer, but are very fast and too sensitive on my friend's computer. Position is updated as follows:

get time and call update():
Code: [Select]
float timeDelta = Game::GetWindow().GetFrameTime();
Update(timeDelta);


inside of update:
Code: [Select]
Update (float elapsedTime){
  if (Game::GetInput().IsKeyDown(sf::Key::Right))
    _velocity += 3.0f;
  GetSprite().Move(_velocity * elapsedTime, 0);
}


My understanding was that this process effectively scales the speed by the framerate so that it doesn't matter how fast or slow a computer is-- the objects should always move around at the intended speed. This doesn't seem to be working though.

Thoughts?
Title: speed inconsistency (different computers)
Post by: Mjonir on March 19, 2012, 08:19:49 pm
Your problem is:

Code: [Select]
_velocity += 3.0f;

This line is executed every frame the player has the key pressed :)

Either assign a constant to "_velocity", or do something like:

Code: [Select]
_velocity += 3.0f * elapsedTime;
Title: speed inconsistency (different computers)
Post by: N1ghtly on March 19, 2012, 09:27:54 pm
This should do the trick, but I still strongly suggest using a fixed timestep!
Title: speed inconsistency (different computers)
Post by: Mjonir on March 19, 2012, 09:30:23 pm
Quote from: "N1ghtly"
This should do the trick, but I still strongly suggest using a fixed timestep!


Could you elaborate on this? Why would you ALWAYS use a fixed timestep?