Skip to main content
GameDev.net gamedev.net
🔒 Locked

Time Based Movement

Started by kore_gw Mar 30, 2004 at 1:23 PM 2 replies 850+ views
Original Post
kore_gw
kore_gw
I''m working on a freekick soccer game, (this is my first opengl/c++ game project), so once I discovered it was runnin at diff. speeds I found the time based movement tutorial thinkin it would solve all my problems. Now, it runs fine on my computer, nice and smooth.. but when I test it on my roommates computer, who is a bit slower Mhz but has a better video card... everything that used the g_FrameInterval * speed to translate/rotate seems to have a choppy motion, while if I disable that variable.. they will run faster on his computer. He reaches a higher FPS (4x higher), yet the frameInterval seems to be larger ?? This doesn''t make any sense to me, if anyone has experience this before or if I''m doing something wrong please let me know. I call the CalculateFrameRate() function in my main display function. Thanks in advance, Dom
eFoDay
eFoDay
I made a timer class based on that gamedev article
it works fine on two computers

this is how I use it:

CTimer m_Timer;

// Init
m_Timer.Init();

// Game Loop
// Update the frame time
m_Timer.Update();
// Increase x position at 20 units per second
m_Object.x += m_Timer.GetFrameTime() * 20.0f;


class CTimer
{

protected:

double m_Frequency;

__int64 m_StartClock;

float m_FrameTime;
float m_FrameStart;
float m_FrameEnd;

float m_FpsLimiter;
float m_Fps;

public:

float GetFrameTime() { return m_FrameTime; }

float GetFps() { return m_Fps; }

double GetTime();

void Init();
void Update();

};



#include <windows.h>
#include "timer.h"

double CTimer::GetTime()
{
__int64 EndClock;

QueryPerformanceCounter((LARGE_INTEGER*)&EndClock);

return (double)(EndClock-m_StartClock)*m_Frequency;
}

void CTimer::Init()
{
__int64 rate;

// Get the performance frequency

QueryPerformanceFrequency((LARGE_INTEGER*)&rate);

// Invert it so we can multiply instead of divide

m_Frequency = 1.0/(double)rate;

// Get the start time

QueryPerformanceCounter((LARGE_INTEGER*)&m_StartClock);

m_FrameTime = 0.0f;
m_FrameStart = (float)GetTime();
m_FrameEnd = 0.0f;
m_FpsLimiter = 0.0f;
m_Fps = 0.0f;
}

void CTimer::Update()
{
// Cap the frame rate to the timer frequency

// The timer frequency is normally always faster than the frame rate

do {
m_FrameEnd = (float)GetTime(); // Get the end frame time

} while( m_FrameEnd == m_FrameStart);

//

m_FrameTime = m_FrameEnd - m_FrameStart; // Get the elapsed time

m_FrameStart = m_FrameEnd; // Set the end time to be the next start time


// Get the frames per second

m_FpsLimiter += m_FrameTime;
if( m_FpsLimiter >= 0.25f )
{
m_FpsLimiter = 0.0f;
m_Fps = float(1.0/m_FrameTime);
}
}
kore_gw
kore_gw
Thanks alot !!

This method works smoothly on several different computers I have tested, I guess timeGetTime() and GetTickCount() aren''t as efficient as this method..

I really appreciate your help,

kore
Agony
Agony
You probably don''t need this now, but although timeGetTime() isn''t going to be as good as QueryPerformanceCounter(), you can improve it somewhat by calling timeBeginPeriod(1) before you ever use it, and timeEndPeriod(1) when you''re all done. This attempts to raise the accuracy to intervals of 1 millisecond. Typically, it is around 10 milliseconds by default. The same with GetTickCount(), although I don''t know if there is any way to change GetTickCount()''s accuracy.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.