Does this simple timing system work?

Started by
2 comments, last by scpedicini 20 years ago
I was wondering if I could get your opinion about the following piece of code to regulate movement. Every creature in my game(sidescroller) has velocity which is defined as pixels per second(pps).

/* The variables for each creature are: */
int x;      // position

int vx;     // velocity in pps

double dx;  // displacement(combination of elapsed_time and pps)


/* global variables are: */
LARGE_INTEGER TotalCount;
LARGE_INTEGER CurrentCount;
LARGE_INTEGER LastCount;
double time_elapsed;

QueryPerformanceFrequency(&TotalCount);

/* Game loop */

// just before creature movement


QueryPerformanceCounter(&CurrentCount);
time_elapsed = (CurrentCount - LastCount) / TotalCount;
LastCount = CurrentCount;

// creature movement


Creature.dx += Creature.vx * time_elapsed;
Creature.x += (int) Creature.dx;
Creature.dx -= (int) Creature.dx;

Is this a good way of doing framerate independent movement? Thanks for your help, Shaun
Advertisement
hmm... come to think of it, you should not use os timers. I did. It works 10 ticks in 1st second and only 5 in the 2nd. Still, I haven''t still figured out what should I use instead.
You need to use some type casts to get that code to work. Also, you can use float for the time; you don''t need double precision, and you should change the name of the variable TotalCount to TimerFreq. By the way, the performance counter is probably the most reliable timing you can use on a Win32 system. You need to change your code to look like this:
time_elapsed = ((float)CurrentCount.QuadPart - (float)LastCount.QuadPart) / (float)TotalCount.QuadPart; 
The rest of the code is fine. Notice also that you need to use the QuadPart of the LARGE_INTEGER. Look in MSDN for an explanation of this...

Windows 95 - 32 bit extensions and a graphical shell for a 16 bit patch
to an 8 bit operating system originally coded for a 4 bit microprocessor,
written by a 2 bit company that can''t stand 1 bit of competition.
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
quote:Original post by Archi
hmm... come to think of it, you should not use os timers. I did. It works 10 ticks in 1st second and only 5 in the 2nd. Still, I haven''t still figured out what should I use instead.


I use timeGetTime on Windows (Winmm.lib & windows.h) and wrote a wrapper to quickly get a time difference between this and the previous call. MSDN claims it has a default resolution of 5 ms on NT systems which should be sufficient for a game timer.
A disadvantage is that you MIGHT get weird results IF the timer wraps around (as mentioned on that MSDN page). The chances of that happening are minimal though plus you can deal with that in your wrapper (ie: negative result->difference = 0).

As for the original post, I wouldn''t use "pixels/second" as a integer speed unit. Personally I''d like a bit more freedom and use floats and units/sec where a unit is one unit defined by your graphics library. Because, have you though of what will happen when someone resizes the window, or runs your application in a different resolution?
STOP THE PLANET!! I WANT TO GET OFF!!

This topic is closed to new replies.

Advertisement