Archived

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

HighPerformanceCounter for TimeBasedMovement

This topic is 5645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
@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!

Share this post


Link to post
Share on other sites
Perhaps a little code would help here. Anyway, here is a little snippet of my main game loop (the same as in NeHeGL).


    
// 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]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
ok, it works.

I don''t really know why it doesn''t in the past but I don''t care


GLfloat Val2Proc()
{
static DWORD tickold=(GetTickCount()-10);
static DWORD tick;
float valproc;
tick = GetTickCount();
valproc = ((tick) - (tickold));
tickold = tick;
return valproc*0.05f;
}


The only prolem is that it would be better if I get good float values from it but its enough for the moment.

Share this post


Link to post
Share on other sites
That code I posted is not from my FPS counter, so I am not sure how it would fare as one. It is only for time-based movement. In case you wanted to see my clock function (for FPS), here it is:

  
void CEruClock::update()
{
curTime = timeGetTime();
totalTime += curTime - startTime;
startTime = curTime;

if (totalTime > 1000)
{
m_fps = (float)( ((float)framesRendered / (float)totalTime) * 1000 );
totalTime = 0;
framesRendered = 0;
}

framesRendered++;
}


I call that update function once per frame, and I also call another function which returns m_fps once per frame.

NOTE: This is not my code. I forget where I got this from, but I forgot to put a note in my source code. If this class is yours, please tell me!

Share this post


Link to post
Share on other sites