Jump to content
  • Advertisement
Sign in to follow this  
cozzie

Framerate inconsistent..

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!