当帧速率受到限制时,对象移动不稳定

发布于 2024-09-27 17:09:46 字数 998 浏览 9 评论 0原文

我在游戏中遇到了一些物体移动不稳定的问题。

如果帧速率不受限制,游戏运行会很流畅。

如果我将帧速率限制为 60 fps,则运动会变得不稳定。

对象的运动看起来像这样...

m_distance += m_speed * GetFrameTime()

其中速度以每秒像素为单位,GetFrameTime() 返回自上一帧以来经过的时间。请注意,对象仅沿 x 和 y 方向移动。

我正在使用 SFML,其中包含函数 SetFramerateLimit(60)UseVerticalSync(true)。在尝试解决不稳定的运动问题时,我阅读了 XNA 开发人员的一篇文章,介绍了它们如何更新一次、渲染一次,以及游戏循环中是否还有剩余时间。帧速率(即 1/60),然后他们将在该时间段的剩余时间内睡眠。我相信这就是 SFML 所做的,因为我禁用了这些功能并写入了该行为,并且我得到了相同的症状。

我并不是说上述方法是一件坏事。事实上,我喜欢应用程序没有做不必要的工作(在设置帧限制和没有设置帧限制的情况下运行应用程序时,资源使用情况存在明显差异)。不过,我实在不知道为什么动作会断断续续。我读过 gafferongames 的文章,但它们似乎不适用于这里。

如有任何帮助,我们将不胜感激,谢谢。

编辑:我已经确定了问题,现在只是不知道如何解决。

在对象“跳跃”的帧上,m_distance 明显大于其他帧中的距离。仔细观察,GetFrameTime() 也明显大于之前帧之间的时间(显着差异约为 2-3 毫秒)。

Edit2:我相信我已经想出了一个解决方案,感谢 http://gafferongames。 com/game-physicals/fix-your-timestep/ 。设置固定的增量时间而不是使用 GetFrameTime。

I've been having some trouble with choppy movement of objects in a game.

If the frame rate isn't capped, the game runs smooth.

If I cap the frame rate at say, 60 fps, the movement is choppy.

An object's movement looks like this...

m_distance += m_speed * GetFrameTime()

Where speed is in pixels per second, and GetFrameTime() returns the time elapsed since the last frame. Note that objects only move in x and y directions.

I'm using SFML, with the functions SetFramerateLimit(60) and UseVerticalSync(true). While trying to address the choppy movement issue, I read an article from an XNA developer about how they update once, render once, and then if there is time left in the game loop < the frame rate (i.e. 1/60) then they'll sleep for the rest of that period. I believe this is what SFML does, because I disabled those functions and wrote in that behavior and I got the same symptoms.

Now I'm not saying the above approach is a bad thing. In fact, I like that the application isn't doing needless work (there is a noticeable difference in resource usage when running the application with a set frame limit and without one). However, I really don't know why the movement is choppy. I've read the gafferongames articles but they don't seem to apply here.

Any assistance is appreciated, thanks.

Edit: I've identified the the issue, now just not sure how to solve it.

On the frames where the object "jumps", the m_distance is significantly greater than distances in other frames. On closer inspection, the GetFrameTime() is also significantly greater than previous times between frames (significant is like 2-3 ms difference).

Edit2: I believe I've come up with a solution thanks to http://gafferongames.com/game-physics/fix-your-timestep/ . Set a fixed delta time instead of using GetFrameTime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌入人心 2024-10-04 17:09:46

使用的唯一原因

m_distance += m_speed * GetFrameTime();

是:如果您想获得与帧无关的运动。在这种情况下,没有理由限制 FPS。

当您有 FPS 上限(例如 SFML 实现的)时,我建议保持每帧的运动恒定。也就是说,像这样:

m_distance += m_speed;

The only reason to ever use:

m_distance += m_speed * GetFrameTime();

is if you're trying to get frame-independent movement. In this case, there's no reason to cap your FPS.

When you have a FPS cap such as what SFML implements, I recommend keeping movement constant per frame. That is to say, something like this:

m_distance += m_speed;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文