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

Author Topic: speed inconsistency (different computers)  (Read 1745 times)

0 Members and 1 Guest are viewing this topic.

mic4h

  • Newbie
  • *
  • Posts: 1
    • View Profile
speed inconsistency (different computers)
« 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?
"No, the Kokiri were not lesbians; they were immortal! How is that even close?"
-kirbyvictorious

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
speed inconsistency (different computers)
« Reply #1 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;

N1ghtly

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
speed inconsistency (different computers)
« Reply #2 on: March 19, 2012, 09:27:54 pm »
This should do the trick, but I still strongly suggest using a fixed timestep!

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
speed inconsistency (different computers)
« Reply #3 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?

 

anything