HighPerformanceCounter for TimeBasedMovement
Hi there,
I have a simple Frame-Counter using GetTickCount but I need a high performance counter to get real good frame-independant values. Is there an other way then GetTickCount()?
Thx
But I don''t really understand how that works.
I checked the Tutorial (thankx for the tip) but I do not see the way he integrates the value of the timer into the calculation of the movement. (I hope that makes sense)
dark
I checked the Tutorial (thankx for the tip) but I do not see the way he integrates the value of the timer into the calculation of the movement. (I hope that makes sense)
dark
Here''s what I do for my Pong game:
In my game loop, I update the time and subtract it from the previous time, therefore getting the time for one frame only. Then I pass that value to an Update function, which I will describe below. Then the draw function is called.
For Pong, my Update function checks for keyboard input and moves the ball and paddles. If the down button was pressed, the paddle is moved downward X number of pixels. The X value is determined by mulitplying a certain speed constant by the elapsed time. So, whether Pong is running at 200 fps or 10 fps, the paddle can only move a certain distance in one second (otherwise, a fast machine would have extremely fast paddles, whereas a slow machine would have extremely slow paddles). I do the same type of calculation for the other paddle and the ball.
Hope that helps you.
In my game loop, I update the time and subtract it from the previous time, therefore getting the time for one frame only. Then I pass that value to an Update function, which I will describe below. Then the draw function is called.
For Pong, my Update function checks for keyboard input and moves the ball and paddles. If the down button was pressed, the paddle is moved downward X number of pixels. The X value is determined by mulitplying a certain speed constant by the elapsed time. So, whether Pong is running at 200 fps or 10 fps, the paddle can only move a certain distance in one second (otherwise, a fast machine would have extremely fast paddles, whereas a slow machine would have extremely slow paddles). I do the same type of calculation for the other paddle and the ball.
Hope that helps you.
he passes in the value of amount of time passed into his movement equations. then he uses the dT (delta time a.k.a change in time) in a movement equation from physics
position = position + velocity * deltaTime;
instead of saying:
position = position + velocity;
that way you move at a constant velocity no matter what the framerate is. coolio?
-me
p.s. yeah i KNOW it''s better to say position += velocity * time, it''s just more clear for instructional purposes as typed above.
position = position + velocity * deltaTime;
instead of saying:
position = position + velocity;
that way you move at a constant velocity no matter what the framerate is. coolio?
-me
p.s. yeah i KNOW it''s better to say position += velocity * time, it''s just more clear for instructional purposes as typed above.
Oh come on - can''t we learn to use the "Articles & Resources" link on every page of gamedev! Check it out; there are atleast 2 articles on this subject.
@masterg: yes, maybe we should, but why should I have to if someone has fun to explain it by him/herself ?
@Erunama and Palidine: I do exactly that but my function doesn''t work right until now. I use GetTickCout to get the framerate but my function is not exact enough when I try to get the value for e.g. 10 times in a second.
I''ll work on it later.
thankx for your help!
@Erunama and Palidine: I do exactly that but my function doesn''t work right until now. I use GetTickCout to get the framerate but my function is not exact enough when I try to get the value for e.g. 10 times in a second.
I''ll work on it later.
thankx for your help!
Perhaps a little code would help here. Anyway, here is a little snippet of my main game loop (the same as in NeHeGL).
As you can see, the tick count is updated every frame. This count is from the beginning of the program, but we only want to update based on the time since the previous frame. Therefore, we subtract the total time from the previous time (in my case, window.lastTickCount) and pass that value to our Update function. Then we update the previous time to have a reference for our next frame.
Edit: minor formatting mistake
[edited by - Erunama on June 27, 2002 2:06:40 PM]
// Process Application Loop tickCount = GetTickCount (); // Get The Tick Count Update (tickCount - window.lastTickCount); // Update The Counter window.lastTickCount = tickCount; // Set Last Count To Current Count Draw (); // Draw Our Scene SwapBuffers (window.hDC); // Swap Buffers (Double Buffering)
As you can see, the tick count is updated every frame. This count is from the beginning of the program, but we only want to update based on the time since the previous frame. Therefore, we subtract the total time from the previous time (in my case, window.lastTickCount) and pass that value to our Update function. Then we update the previous time to have a reference for our next frame.
Edit: minor formatting mistake
[edited by - Erunama on June 27, 2002 2:06:40 PM]
to compare it with mine:
This is my old standart fps function:
GLfloat GetFPS()
{
static DWORD tickold=GetTickCount();
static DWORD tick;
static long fps;
static DWORD tmptick;
static DWORD tickdiff;
static int framecount;
static bool started =false;
tick = GetTickCount();
tickdiff = (tick - tickold);
if (tickdiff>1000.0f)
{
fps = FPSFrameCount*(tickdiff/1000);
FPSFrameCount=0;
tickold = tick;
}
return (float)fps;
}
It works the same way.
The first problem with your function is that I have more then 1000 fps, sometimes. ....
well I could waste some time with a simple loop ... nice idea.
I will check it out again...
This is my old standart fps function:
GLfloat GetFPS()
{
static DWORD tickold=GetTickCount();
static DWORD tick;
static long fps;
static DWORD tmptick;
static DWORD tickdiff;
static int framecount;
static bool started =false;
tick = GetTickCount();
tickdiff = (tick - tickold);
if (tickdiff>1000.0f)
{
fps = FPSFrameCount*(tickdiff/1000);
FPSFrameCount=0;
tickold = tick;
}
return (float)fps;
}
It works the same way.
The first problem with your function is that I have more then 1000 fps, sometimes. ....
well I could waste some time with a simple loop ... nice idea.
I will check it out again...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement