Framerate inconsistent..

This topic is 2038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi,

I'm currently programming a simple asteroids like game.

What I can't figure out is why I'm having fluctuations in the framerate/ motion of the game elements.

These are the symptons I have:

- something on the same PC I get different 'motion speeds'

- on several PC's the speed is way to high (release build), and when I set 'compatibility mode' of the executable to WinXP SP3 it's better

- my game AI/ game logics are all 'statíc' (not FPS related)

- I try to achieve FPS indedendent movement by doing the things below:

// at initial startup

start = GetTickCount();

// in the game loop

// keep the game running at a steady frame rate
if(GetTickCount() - start >= 30)
{
start = GetTickCount();

// do everything
}



Any help is really appreciated.

(maybe I have to do something with the framerate in relation to moving game elements?)

Share on other sites

Then use a high precision timer, GetTickCount() is only accurate about 15-16 ms and is not well suited for games anyway. What i generaly do is get the time elapsed since the last frame, then move stuffs relative to that value.

The Tick() function return the elapsed time, in seconds, since the last time Tick() was called.

As a bonus you can also get the frame per seconds value using GetFPS().

class CGameTimer {
public:
CGameTimer();
private:
bool    IsPerfCounterAvailable;
float   TimeScale;
__int64 PerfCounterFrequency;
private:
__int64 LastTime;
__int64 CurrentTime;
private:
float FPS;
float FPS_ElapsedTime;
public:
void  Reset();
float Tick();
float GetFPS();
};



CGameTimer::CGameTimer()
{
//Check if a High resolution timer is available
if(QueryPerformanceFrequency((LARGE_INTEGER*)&PerfCounterFrequency)){
IsPerfCounterAvailable = true;
QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
TimeScale = 1.0f / PerfCounterFrequency;
} else {
IsPerfCounterAvailable = false;
CurrentTime = timeGetTime();
TimeScale	= 0.001f;
}

Reset();
}

void CGameTimer::Reset()
{
if(IsPerfCounterAvailable){
QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
} else {
CurrentTime = timeGetTime();
}

// Initialization
LastTime = CurrentTime;

FPS = 0;
FPS_ElapsedTime = 0.0f;
}

float CGameTimer::Tick()
{
LastTime = CurrentTime;

if(IsPerfCounterAvailable){
QueryPerformanceCounter((LARGE_INTEGER*)&CurrentTime);
} else {
CurrentTime = timeGetTime();
}

// Calculate the elapsed time
float ElapsedTime = (CurrentTime - LastTime) * TimeScale;

// Calculate the elapsed time for our fps
static int Frame = 0;
Frame++;
FPS_ElapsedTime += ElapsedTime;
if(FPS_ElapsedTime >= 1.0f){
FPS = (float)Frame / FPS_ElapsedTime;
Frame = 0;
while(FPS_ElapsedTime >= 1.0f)
FPS_ElapsedTime -= 1.0f;
}

return ElapsedTime;
}

float CGameTimer::GetFPS()
{
return FPS;
}


Edited by Vortez

Share on other sites

Hi,
I'm currently programming a simple asteroids like game.
What I can't figure out is why I'm having fluctuations in the framerate/ motion of the game elements.

These are the symptons I have:
- something on the same PC I get different 'motion speeds'
- on several PC's the speed is way to high (release build), and when I set 'compatibility mode' of the executable to WinXP SP3 it's better
- my game AI/ game logics are all 'statíc' (not FPS related)
- I try to achieve FPS indedendent movement by doing the things below:

// at initial startup

start = GetTickCount();

// in the game loop

// keep the game running at a steady frame rate
if(GetTickCount() - start >= 30)
{
start = GetTickCount();

// do everything
}



Any help is really appreciated.
(maybe I have to do something with the framerate in relation to moving game elements?)

Three things I do to maintain a smooth game loop:

First off as was pointed out above, use a proper timer.  Better resolution is probably always slightly better when it comes to timing but unless you're using a timer that goes under the millisecond mark you'll probably get rather bad results.

Second, use a proper game loop. http://gafferongames.com/game-physics/fix-your-timestep/ I personally use the last timestep he mentions and I have to say there's almost no reason not to use it.

Third, use smoothing tricks like interpolation. To be honest if you use the fixed timestep method interpolation is pretty much required rather than optional, since movement will look jagged without it. But with it, if you're running at a high tickrate like 120 HZ you get butter smooth movement.

I use that setup even on games without complicated 3d physics or something, it's simple to implement and easy to change if for some reason it isn't working for you.

Share on other sites

Thanks, that helps.

The link is also very usefull to get more into.

I'm implementing a class now to handling 'timing' properly, so I can use it in any future applications/ games.

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 10
• Forum Statistics

• Total Topics
633653
• Total Posts
3013153
×