游戏中的固定帧率与可变帧率:什么是最好的,什么时候最好?
在开发游戏一段时间后,我接触到了可变帧速率(您可以计算出自上次更新以来已经过去了多少时间,并相应地更新演员运动)和固定帧速率(您可以计算出多少时间)已经过去了,选择是勾选固定的时间还是休眠直到下一个窗口到来)。
哪种方法最适合特定情况? 请考虑:
- 迎合不同的系统规格;
- 易于开发/维护;
- 易于移植;
- 最后的表演。
After working for a while developing games, I've been exposed to both variable frame rates (where you work out how much time has passed since the last tick and update actor movement accordingly) and fixed frame rates (where you work out how much time has passed and choose either to tick a fixed amount of time or sleep until the next window comes).
Which method works best for specific situations? Please consider:
- Catering to different system specifications;
- Ease of development/maintenance;
- Ease of porting;
- Final performance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我倾向于使用可变帧率模型,但在内部某些系统是按固定时间步长进行勾选的。 通过使用时间累加器可以很容易地做到这一点。 物理系统最好在固定时间步长上运行,并在必要时每帧多次勾选,以避免稳定性损失并保持模拟顺利。
一些代码来演示累加器的使用:
这无论如何都不完美,但提出了基本思想 - 有很多方法可以改进这个模型。 显然,当输入帧速率非常慢时,需要解决一些问题。 然而,最大的优点是,无论增量有多快或多慢,模拟都会在“玩家时间”内以平稳的速度移动——这是用户会察觉到任何问题的地方。
一般来说,我不涉及图形和图像。 音频方面,但我认为它们不像物理、输入和网络代码那样受到影响。
I lean towards a variable framerate model, but internally some systems are ticked on a fixed timestep. This is quite easy to do by using a time accumulator. Physics is one system which is best run on a fixed timestep, and ticked multiple times per frame if necessary to avoid a loss in stability and keep the simulation smooth.
A bit of code to demonstrate the use of an accumulator:
This is not perfect by any means but presents the basic idea - there are many ways to improve on this model. Obviously there are issues to be sorted out when the input framerate is obscenely slow. However, the big advantage is that no matter how fast or slow the delta is, the simulation is moving at a smooth rate in "player time" - which is where any problems will be perceived by the user.
Generally I don't get into the graphics & audio side of things, but I don't think they are affected as much as Physics, input and network code.
似乎大多数 3D 开发人员更喜欢可变 FPS:Quake、Doom 和 Unreal 引擎都会根据系统性能进行缩放。
如果您正在编写性能很重要的网络 3D 游戏,我不得不说,咬紧牙关,实施可变帧速率。
如果这是一款 2D 益智游戏,您可能可以使用固定的帧速率,也许可以针对超慢的计算机和明年的模型进行稍微参数化。
It seems that most 3D developers prefer variable FPS: the Quake, Doom and Unreal engines both scale up and down based on system performance.
If you are writing a networked 3D game where performance matters I'd have to say, bite the bullet and implement variable frame rates.
If it's a 2D puzzle game you probably can get away with a fixed frame rate, maybe slightly parameterized for super slow computers and next years models.
作为一名用户,我希望更频繁地看到的一个选项是当帧速率变化超出特定范围时动态更改细节级别(广义上,而不仅仅是技术意义上)。 如果您以 5FPS 渲染,请关闭凹凸贴图。 如果您以 90FPS 进行渲染,请稍微增加一些附加功能,并为用户提供一些更漂亮的图像来浪费他们的 CPU 和 GPU。
如果做得正确,用户应该可以从游戏中获得最佳体验,而无需进入设置屏幕并自行调整,并且作为关卡设计师,您应该不必担心在不同场景中保持多边形数量相同。
当然,我是作为一名游戏用户这么说的,而不是一个严肃的用户——我从未尝试过编写一款不平凡的游戏。
One option that I, as a user, would like to see more often is dynamically changing the level of detail (in the broad sense, not just the technical sense) when framerates vary outside of a certian envelope. If you are rendering at 5FPS, then turn off bump-mapping. If you are rendering at 90FPS, increase the bells and whistles a bit, and give the user some prettier images to waste their CPU and GPU with.
If done right, the user should get the best experince out of the game without having to go into the settings screen and tweak themselves, and you should have to worry less, as a level designer, about keeping the polygon count the same across difference scenes.
Of course, I say this as a user of games, and not a serious one at that -- I've never attempted to write a nontrivial game.
我在使用可变长度帧时间时遇到的主要问题是浮点精度,并且可变帧时间可能会让您大吃一惊。
例如,如果您将帧时间 * 速度添加到某个位置,并且帧时间变得非常小,并且位置较大,则您的对象可能会减慢或停止移动,因为所有增量都因精度而丢失。 您可以使用单独的误差累加器来补偿这一点,但这很痛苦。
固定帧时间(或至少是帧长度的下限)可以让您控制需要考虑的 FP 错误量。
The main problem I've encountered with variable length frame times is floating point precision, and variable frame times can surprise you in how they bite you.
If, for example, you're adding the frame time * velocity to a position, and frame time gets very small, and position is largish, your objects can slow down or stop moving because all your delta was lost due to precision. You can compensate for this using a separate error accumulator, but it's a pain.
Having fixed (or at least a lower bound on frame length) frame times allows you to control how much FP error you need to take into account.
我的经验相当仅限于一些简单的游戏(使用 SDL 和 C++ 开发),但我发现实现静态帧速率非常容易。 您正在开发 2d 或 3d 游戏吗? 我认为更复杂的 3D 环境会从可变帧速率中受益更多,并且难度会更大。
My experience is fairly limited to somewhat simple games (developed with SDL and C++) but I have found that it is quite easy just to implement a static frame rate. Are you working with 2d or 3d games? I would assume that more complex 3d environments would benefit more from a variable frame rate and that the difficulty would be greater.