Frame Rate Independent Movement

Published June 04, 2001 by Ben Dilts, posted by Myopic Rhino
Do you see issues with this article? Let us know.
Advertisement
I've seen countless posts on this and other message boards, and even personal email has been sent to me, all asking the same question: How do I make it so my stupid objects move at the same speed if the frame rate rises or drops?

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();
};
I leave all the members public because they work independently of each other and are all useful in some way to the outside program.

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;
}
Some explanation:
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]
Cancel Save
0 Likes 1 Comments

Comments

Priess

I worked for a company that had a very high turnover of staff, and at some point I was laid off as well. To be honest, it was somewhat unexpected, so I didn't have time to prepare and find a new job. So online earnings on such sites as in this rating https://hellagood.marketing/csgo-trading-sites literally saved me from a complete lack of money. Now it's just like a hobby.

August 14, 2023 07:53 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!

Explains how to keep game objects moving at the same rate independently of the frame rate.

Advertisement
Advertisement
Advertisement