如何调整游戏循环帧速率? [英] How to adjust a game loop frame rate?

查看:269
本文介绍了如何调整游戏循环帧速率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正试图编写我的第一个游戏在SFML(和我的第一个整体),但我遇到了问题,我得到一个口吃大约一秒钟。在这样的口吃时,Frametime是正常的3到4倍,真的很明显,因为我不运行真正高的FPS(300+)。

I'm at the moment trying to Programm my first Game in SFML (and my first overall) but i ran into the Problem that i get a stutter about once a second. During such a stutter the Frametime is 3 to 4 times higher than normal which is really noticeable as long i don't run really high FPS (300+).

没有问题(至少atm),因为效能不是问题,而是:

No Problem (at least atm) as performance is not an Issue, but:

运动方法真的很奇怪,并且移动方式慢,它应该做的。

When doing that my movement Method really freaks out and moves way way slower that it's supposed to do.

我的移动方法:

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是以毫秒为单位的frametime,i乘以60,被设置为像素/秒的值而不是每帧。

frametime is the frametime in Milliseconds, and i Multiply by 60, as my movementspeed is set as a value for pixel/second and not per frame.

movespeed是5,所以字符应该每秒移动5像素,无论FPS(因此Frametime)我有。

movementspeed is 5, so the character should move 5 px per second, whatever FPS( and therefore Frametime) i have.

但是:这给了我真正的跳跃运动,因为那些stutterframes导致跳跃,并且在nto口吃帧,palyer移动比它应该慢得多。

But: that gives me really jumpy movement, as those "stutterframes" result in a jump, and on nto stuttering frames the palyer moves a lot slower than it should.

我的mainloop非常简单,只是

my mainloop is really simple, just

while(mMainWindow->isOpen()){

        HandleEvents();
        Update();
        Render();
    }

使用inbuild framelimiter(尝试写我自己的,同样的结果,只要我使用sf:sleep来调节FPS,没有cpu核心在100%负载运行)到300 FPS。

while using the inbuild framelimiter (tried writing my own, but i get the very same result, as long as i use sf:sleep to regulate FPS for not having the cpu core running at 100% load) to 300 FPS.

所以,我可以设置我的标准速度为1而不是5,但
setframeratelimit不是很准确,所以我得到一些变化的movespeed,我真的不喜欢。

So yeah, i could just set my standard speed to 1 instead of 5, but setframeratelimit is not very accurate, so i get some variation in movementspeed, that i really not like.

任何人都有一个想法,我最好做什么?也许我没有看到森林所有的树(我实际上不知道,如果你说英语:P),但因为这是我第一个游戏我没有经验回头看。

anyone has an idea, what i could best do? Maybe i'm not seeing the forest for all the trees ( i actually have no idea if you say that in english :P) but as this is my first game i have no experience to look back upon.

推荐答案

frametime 真的很高或真的很低时,由于浮动精度问题,工作正常。我建议设置标准速度,也许 500 mMovementSpeedTimeFactor frametime * 60 / 10.0 f ,并检查问题是否仍然发生。

When frametime is really high or really low your calculations may not work correctly because of float precision issues. I suggest setting standard speed to, maybe, 500 and mMovementSpeedTimeFactor to frametime * 60 / 10.0f and check if the issue still happens.

这篇关于如何调整游戏循环帧速率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆