How to adjust the frame rate in the game loop?

I'm currently trying to program my first game in SFML (and my first general one), but I ran into a problem that I stutter about once per second. During such a stutter, Frametime is 3-4 times higher than usual, which is really noticeable, since I am not launching a really high FPS (300+).

There is no problem (atm at least), since performance is not a problem, but:

When I do this, my Method method is really worried and moves slower than it should.

my move method:

void Player::Update(float frametime){ mMovementSpeedTimefactor = frametime * 60 / 1000.0f; setMovementVector(sf::Vector2f( mMovementVector.x * mMovementSpeedTimefactor, mMovementVector.y *mMovementSpeedTimefactor)); validateMovement(); //std::cout << mTestClock->restart().asMilliseconds() << std::endl; moveObject(); this->updateAnimation(); } 

frametime is the cycle time in milliseconds, and I am multiplying by 60, since the speed of movement is defined as a value for a pixel / second, and not for a frame.

movementpeed is 5, so the character must move 5 pixels per second, regardless of FPS (and therefore Frametime).

But: this gives me a really jumping movement, since those โ€œstutterframesโ€ lead to a jump, and on nto stuttering frames the palyer moves much slower than necessary.

my mainloop is really simple, just

 while(mMainWindow->isOpen()){ HandleEvents(); Update(); Render(); } 

when using the built-in framework (I tried to write my own, but I get the same result if I use sf: sleep to regulate FPS for the lack of a processor core with 100% load) to 300 FPS.

So yes, I could just set the default speed to 1 instead of 5, but setframeratelimit is not very accurate, so I get some changes in motionpeed that I really don't like.

Does anyone have an idea what I could best do? Maybe I donโ€™t see the forest for all the trees (I really have no idea if you say this in English: P), but since this is my first game, I have no experience looking back.

+2
source share
2 answers

When frametime really high or very low, your calculations may not work correctly due to float issues. I suggest setting a standard speed, maybe 500 and mMovementSpeedTimeFactor to frametime * 60 / 10.0f , and check if a problem has occurred.

+1
source

A similar question: Movement without frame restriction. C ++ SFML .

What you really need is a fixed time step. Take a look at the SFML game development book source code . Here's an interesting snippet from Application.cpp :

  const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f); [...] sf::Clock clock; sf::Time timeSinceLastUpdate = sf::Time::Zero; while (mWindow.isOpen()) { sf::Time elapsedTime = clock.restart(); timeSinceLastUpdate += elapsedTime; while (timeSinceLastUpdate > TimePerFrame) { timeSinceLastUpdate -= TimePerFrame; processEvents(); update(TimePerFrame); } updateStatistics(elapsedTime); render(); } 

EDIT: If this is not quite what you want, see โ€œ Fix your timestep! โ€ Which Laurent Gomila himself was linked to in the SFML forum.

+2
source

All Articles