FrameRate dependant movement

Started by
26 comments, last by bobason456 18 years, 9 months ago
ok, i kinda editted that class u showed me in that tut. does this seem correct? cuz it's working, but when i output the "time elapsed" i'm getting huge numbers!

class cFrameRate
{
private:
LARGE_INTEGER m_TicksPerSecond;
LARGE_INTEGER m_CurrentTick;
LARGE_INTEGER m_LastTick;

float m_FPS;
double m_TimeElapsed;

public:
void Init();

void Update();
double GetTimeElapsed() {return m_TimeElapsed;};
float GetFPS() {return m_FPS;};
};

void cFrameRate::Init()
{
QueryPerformanceCounter(&m_LastTick);
QueryPerformanceFrequency(&m_TicksPerSecond);
}

void cFrameRate::Update()
{
QueryPerformanceCounter(&m_CurrentTick);

//This frame's length out of desired length
m_TimeElapsed = (double)(m_CurrentTick.QuadPart - m_LastTick.QuadPart)/(double)m_TicksPerSecond.QuadPart;
m_FPS = 1000 / (float)(m_CurrentTick.QuadPart - m_LastTick.QuadPart);
if (m_TimeElapsed < 0.001)
m_TimeElapsed = 0.001;

m_LastTick = m_CurrentTick;
}

i'm thinking that the problem has something to do with the float and double being different or something...
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Advertisement
Choppiness maybe caused by round off errors when doing math with time displacements.

Check out this thread
Link

EvilKnuckles666 can you post an EXE we can down load and try running?
here is another good link on the topic link

Just added such timers to my engine and every thing is going perfectly smooth So it just goes to show that my original thoughts on vsync were wrong :)
i looked at that link and it's the same as what i'm doing. the numerator and denominator are (double)

m_TimeElapsed = (double)(m_CurrentTick.QuadPart - m_LastTick.QuadPart)/(double)m_TicksPerSecond.QuadPart;

how would i figure out what the FPS is? at first i thought it was:
m_FPS = 1000 / (float)(m_CurrentTick.QuadPart - m_LastTick.QuadPart);

then i thought it was:
m_FPS = (float)(m_TicksPerSecond.QuadPart / m_TimeElapsed);

but i keep getting really huge numbers...

1. How do i fix the little jidderyness that i can still see?
2. How do i calculate FPS from info. that i have?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
ok, i fixed everything, i just dropped all the query stuff for now and i'm using timeGetTime().

Update()
{
m_CurrentTick = timeGetTime();

m_TimeElapsed = (float)((m_CurrentTick - m_LastTick) / 1000.0f); // in Seconds

if (m_TimeElapsed < 0.001) m_TimeElapsed = 0.001;

m_FPS = 1.0f / m_TimeElapsed;

m_LastTick = m_CurrentTick;
}

then i have an output every frame to show the FPS. but just look at it (in windowed mode by the way.)

FPS: 100.000000
FPS: 90.909088
FPS: 100.000000
FPS: 25.641026
FPS: 83.333336
FPS: 90.909088
FPS: 90.909088
FPS: 166.666672
FPS: 200.000000
FPS: 142.857132
FPS: 166.666672
FPS: 111.111115
FPS: 29.411762
FPS: 83.333336
FPS: 83.333336
FPS: 166.666672
FPS: 200.000000
FPS: 249.999985
FPS: 166.666672
FPS: 142.857132
FPS: 100.000000
FPS: 90.909088

is there a reason it jumps around like that? i've come to the conclusion that THAT'S the reason y eventhough i have the frame independant movement correct, things are still jumpy, it's b/c the fps is jumpy like that. someone correct me of i'm wrong.
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:
2. How do i calculate FPS from info. that i have?



double FPS;
if(frame_time_in_seconds_as_double) // check for non 0
{
FPS = 1.0/frame_time_in_seconds_as_double;
}

If it took 0.10 senonds, you would have 10 FPS.
If it took 0.50 seconds, you would have 2 FPS.
and so on.

Quote:
1. How do i fix the little jidderyness that i can still see?


Only thing I can think of right now that you may not have tried yet is having two timer functions. Sometimes they are way off, IIRC people would sometimes get a wild result of 1+ seconds in a tight loop. So just compare the results of the two timers, if one is way off you will have to decide how that should be handled.

Other than that, you said you read the article, and that you are already 'doing that'. What about this:
Quote:
I suggest you separate the true framerate of the program from a smoothed framerate used only with motion. The updated motion framerate is calculated by taking a weighted average of the true framerate and the motion framerate from last frame.


I have never used it, but it sounds pretty solid.

EDIT: ok, you beat me.

Per frame output (to a file) will slow you down alot. I had it doing per seconds outputs to a file, and I could see/feel when it would happen. Also, see above for same situation.
Just thought I would plunge back into this debate.

I’ve begun to use QueryPerformanceCounter because I cant see any other way of getting precise movement for my objects if the precision of GetTickCount is only 10ms at best.

As a result I’m now getting frame glitches every few seconds, that I’m sure is related to the wack QueryPerformanceCounter issue that was mentioned before.

It should be noted though that I’m not using this to limit my frames.. and that may be half the problem. I render unlimited frames per second, yet I have V-sync enabled.

I heard mention of this constant time slice thing. But those methods also used GetTickCount, so given it’s imprecision how do you know that 25 ms have elapsed?


This topic is closed to new replies.

Advertisement