- SFML Game Development By Example
- Raimondas Pupius
- 307字
- 2021-07-23 14:55:06
Fixed time-step
In some cases, the code for time management that we've written doesn't really apply correctly. Let's say we only want to call certain methods at a fixed rate of 60 times per second. It could be a physics system that requires updating only a certain amount of times, or it can be useful if the game is grid-based. Whatever the case is, when an update rate is really important, a fixed time-step is your friend. Unlike the variable time-step, where the next update and draw happens as soon as the previous one is done, the fixed time-step approach will ensure that certain game logic is only happening at a provided rate. It's fairly simple to implement a fixed time-step. First, we must make sure that instead of overwriting the elapsed time value of the previous iteration, we add to it like so:
void Game::RestartClock(){ m_elapsed += m_clock.restart(); }
The basic expression for calculating the amount of time for an individual update throughout a 1 second interval is illustrated here:

Let's say we want our game to update 60 times a second. To find the frame time, we would divide 1 by 60 and check if the elapsed time has exceeded that value, as shown here:
float frametime = 1.0f / 60.0f; if(m_elapsed.asSeconds() >= frametime){ // Do something 60 times a second. ... m_elapsed -= sf::seconds(frametime); // Subtracting. }
Notice the subtraction at the end. This is how we reset the cycle and keep the simulation running at a constant speed. Depending on your application, you might want to put it to sleep in between updates in order to relieve the CPU. Aside from that detail, these are the bare bones of the fixed time-step. This is the exact technique that will be used in the game that we will finish building in the next chapter.
- C#高級編程(第10版) C# 6 & .NET Core 1.0 (.NET開發(fā)經(jīng)典名著)
- 自己動手寫搜索引擎
- Python機器學習:數(shù)據(jù)分析與評分卡建模(微課版)
- TensorFlow Lite移動端深度學習
- OpenCV for Secret Agents
- 面向STEM的Scratch創(chuàng)新課程
- C語言實驗指導及習題解析
- 批調(diào)度與網(wǎng)絡問題的組合算法
- Mastering Business Intelligence with MicroStrategy
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- Python網(wǎng)絡爬蟲技術與應用
- 程序員的英語
- Java Web應用開發(fā)
- Sony Vegas Pro 11 Beginner’s Guide
- Learning Spark SQL