Archived

This topic is now archived and is closed to further replies.

Mike Dirnt

Time functions

Recommended Posts

I''m working on a 3D engine in OpenGL. Currently its really cruddy but thats beside the point. I gotta go soon so Ill make this quick. I want to make a momentum system... I have a good idea of how to do this, but I need to have a variable of some sort that actively changes with the time, more specifically, milliseconds... I know this sounds vauge but if someone could give me someplace to start, I''d appreciate it. Mike Dirnt 5: I''''m here to help! Mike Dirnt 5: ... I think...

Share this post


Link to post
Share on other sites
I''m not sure of exactly what you mean, but I think you want to have a time-based movement system. Just get the amount of time elapsed from the last render scene and pass that to your update code. Then, every velocity vector (which should now be in terms of speed per millisecond) whould be multiplied by the elpased time. Pseudo-code:

  
// earlier in WinMain

static DWORD prevTime;
static int elapsedTime;

// WinMain Loop

prevTime = GetTickCount();

updatef(elapsedTime);
renderf();

elapsedTime = GetTickCount() - prevTime;

// update function

void updatef(int elapsedtime)
{
ballPos += ballVec * elapsedtime;
...
}


Some may find it easier to convert your velocities into dist per second instead of millisecond. That''s fine just alter the updatef to:

  
voud updatef(int elapsed_millisec)
{
ballPos += ballVec * (elapsed_millisec / 1000.0f);
...
}


If you want to change your dist per frame (dpf) velocites to dist per second, just multiply your current dpf velocities by your average FPS.

Share this post


Link to post
Share on other sites
For highresolution timing use QueryPerformanceFrequency and QueryPerformanceCounter.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/timers_4z76.asp?frame=true

  
//Do this once

LARGE_INTEGER temp;
QueryPerformanceFrequency (&temp);
float ticksPerSecond = 1.0f / temp;
...

//Update every game loop

static LARGE_INTEGER oldTime;
LARGE_INTEGER currentTime;
static float elapsedSeconds;

QueryPerformanceCounter ( ¤tTime );
elapsedSeconds = ( currentTime - oldTime ) * ticksPerSecond;


Now elapsedSeconds will have the number of seconds since the last call (usually a very small fraction of a second). If you store all your velocities as vectors in units per second, you can easily update the position of your objects by position += velocity * elapsedSeconds.

  
D3DXVECTOR3 position;
D3DXVECTOR3 velocity;
...
position += velocity * elapsedSeconds;


Share this post


Link to post
Share on other sites