What are good pratices about games frame rates development
-
I am developing a qml game where I used a Timer with 60 fps for the game loop.
Posts like this https://forum.qt.io/topic/60063/smooth-character-movement and Qt Blog https://www.qt.io/blog/new-in-qt-6.4-frameanimation-element, explain that Timers have flaws and can cause choppy animations with different monitors refresh rates.
I naively replaced Timer with FrameAnimation, and character movement become smooth even with different monitors refresh rates.
All good until last week, when I bought a new gaming laptop with 144Hz, and my character movement become blazing fast, which is not ok, since it should be consistent in different laptops with different frame rates.
I read the FrameAnimation blog again with more attention and found I need to multiply the movement speed with frameTime. This seems to work, the following example is a moving cube, it seems to be at the same velocity in both my old and new laptops.import QtQuick Window { id: root width: 640 height: 480 visible: true title: qsTr("Hello World") property real mm: Screen.pixelDensity Rectangle { id: rect2 property real direction: 1 //use the frameTime to make the velocity independent from different refresh rates property real speedWalk: 20 * mm * direction * frameAnimation.frameTime //velocity varies according different refresh rates //property real speedWalk: 1 * mm * direction width: 5 * mm height: width color: "orange" anchors.bottom: parent.bottom function move(){ if (x > parent.width - rect2.width) direction = -1 if (x < 0) direction = 1 x += speedWalk } } FrameAnimation { id: frameAnimation running: true property real smoothfps: smoothFrameTime > 0 ? (1.0 / smoothFrameTime) : 0 property real fps: frameTime > 0 ? (1.0 / frameTime) : 0 onTriggered: { rect2.move() } } //property real speedNormalized: rect.speed * frameAnimation.frameTime Text { id: name text: " frameTime: " + frameAnimation.frameTime + "\n smoothFrameTime: "+frameAnimation.smoothFrameTime + "\n smooth fps: " + frameAnimation.smoothfps.toFixed(0) + "\n fps: " + frameAnimation.fps.toFixed(0) + "\n rectangle velocity: " + rect2.speedWalk//.toFixed(3) } MouseArea { anchors.fill: parent onClicked: frameAnimation.running = !frameAnimation.running } }
Anything else I should know about frames rates, so I dont get any more surprises with my game? What are some other good practices that I am not aware ? How do other games platforms like Unity handle the frames rates differences ?
-
Your
speedWalk
is not just dependent of theframeTime
, but also of thepixelDensity
. So far, this seems to be correct to include both of these. However, you might have a higher resolution monitor now instead of just having a faster one. Are you using a scaling factor (its a setting of your operating system)? You should also includeScreen.devicePixelRatio
. Not sure, if this is the actual problem.