Frame Rate Independent Movement
Explains how to keep game objects moving at the same rate independently of the frame rate.
After a few months' programming, I devised what I believe to be the most efficient and useful way to handle this problem. For the purposes of this article, I will encapsulate this functionality in its own class.
class framerate
{
public:
float targetfps;
float fps;
LARGE_INTEGER tickspersecond;
LARGE_INTEGER currentticks;
LARGE_INTEGER framedelay;
float speedfactor;
void Init(float tfps);
void SetSpeedFactor();
};Here is the implementation, followed by some explanation
void framerate::Init(float tfps)
{
targetfps = tfps;
QueryPerformanceCounter(&framedelay);
QueryPerformanceFrequency(&tickspersecond);
}
void framerate::SetSpeedFactor()
{
QueryPerformanceCounter(?tticks);
//This frame's length out of desired length
speedfactor = (float)(currentticks.QuadPart-framedelay.QuadPart)/((float)tickspersecond.QuadPart/targetfps);
fps = targetfps/speedfactor;
if (speedfactor <= 0)
speedfactor = 1;
framedelay = currentticks;
}targetfps is passed into the Init function, but can be set at any time. It is the target frame rate for the program. This is used in the SetSpeedFactor function to determine what the speedfactor will be. Look down at my explanation of speedfactor.
fps is the actual frame rate of the program. It is not really needed to make frame rate independent movement, but since it is so closely linked, I include it anyway.
tickspersecond is set in the Init function to be the number of ticks the high performance timer has per second.
currentticks is set in the SetSpeedFactor function to be the current high performance timer's ticks.
framedelay is the previous frame's currentticks.
speedfactor is the heart of this class. When it is set is SetSpeedFactor, it becomes a number that you multiply all your motion by. For instance, if the targetfps is 100, and the actual fps is 85, the speedfactor will be set to 100/85, or about 1.2. You then multiply all your motion is the game, at its lowest level, by this number.
For instance, rather than simply coding spaceship->MoveForward(5), I would code spaceship->MoveForward(5*framerate.speedfactor).
In conclusion:
This simple routine saves a whole heap of trouble. Just plug it into almost any game or other real-time program. Call Init at the beginning of the program, and then each frame call SetSpeedFactor. Then multiply all your movement by the speedfactor. It's that simple. Now stop flooding those message boards
Questions? Comments? Complaints? Hate mail? [email="benbeandogdilts@cs.com"]benbeandogdilts@cs.com[/email]
Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Ben Dilts
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion