Animation timer?

Started by
3 comments, last by sniper227 9 years, 4 months ago

Hey guys,

So I ran into a very obvious problem, I've been trying to run an animation but it runs too quick. I hate to fetch for code but I have no idea how to write a timer function.

basically all i need to do is create a timer function and after every 800 milliseconds or so it will update the frame to the next animation. I already have the animation data and know how to update it I just need the timer part.

Here's sort of what it would look like



if(timer%800 == 0)
{
Play->Animation();
}
Advertisement

#include <Windows.h>
class Timer
{
public:
Timer(){QueryPerformanceCounter(&oldTime);}
double GetElapsedTime();
void Reset();

private:

static LARGE_INTEGER ticksPerSecond;
LARGE_INTEGER oldTime;
};





double Timer::GetElapsedTime()
{
LARGE_INTEGER ticksPerSecond;
QueryPerformanceFrequency(&ticksPerSecond);


LARGE_INTEGER newTime;
QueryPerformanceCounter(&newTime);
double Diff = (newTime.QuadPart - oldTime.QuadPart);

return Diff/ticksPerSecond.QuadPart;
}

void Timer::Reset()
{
QueryPerformanceCounter(&oldTime);
}

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

#include <Windows.h>
class Timer
{
public:
Timer(){QueryPerformanceCounter(&oldTime);}
double GetElapsedTime();
void Reset();

private:

static LARGE_INTEGER ticksPerSecond;
LARGE_INTEGER oldTime;
};





double Timer::GetElapsedTime()
{
LARGE_INTEGER ticksPerSecond;
QueryPerformanceFrequency(&ticksPerSecond);


LARGE_INTEGER newTime;
QueryPerformanceCounter(&newTime);
double Diff = (newTime.QuadPart - oldTime.QuadPart);

return Diff/ticksPerSecond.QuadPart;
}

void Timer::Reset()
{
QueryPerformanceCounter(&oldTime);
}

i'm getting a memory error on this line :

double Diff = (newTime.QuadPart - oldTime.QuadPart);

The C++ standard (assuming you are using C++ as a programming language) offers functionality to query the current time: http://www.cplusplus.com/reference/ctime/ and http://www.cplusplus.com/reference/chrono/ (C++11 only). Those links contain information about functions (and classes, for the C++11 link), along with examples, that may help you.

As dpadam450 suggested with its code snippet, you can encapsulate your main loop with two query of times and calculate the difference between the two. This gives you the time that your loop took to do all that it had to do. This time is usually called the delta time.

You can then verify if 800 ms has passed by summing all those delta times and checking whether or not it is above 800 ms.

The only difference with dpadam450 is that the code snippet that he provided will work only on Windows, whether using the standard library will allow you to use the code on different platforms.

Also, another solution when using animations is to multiply the factor used for the animation with the delta time. For example, if you want to move an object 100 units per second, you would do something like: pos += 100 * dt; where pos is your current position and dt is the delta time of the last frame. The same can be applied for an animation.

Hope that helped a bit!

The C++ standard (assuming you are using C++ as a programming language) offers functionality to query the current time: http://www.cplusplus.com/reference/ctime/ and http://www.cplusplus.com/reference/chrono/ (C++11 only). Those links contain information about functions (and classes, for the C++11 link), along with examples, that may help you.

As dpadam450 suggested with its code snippet, you can encapsulate your main loop with two query of times and calculate the difference between the two. This gives you the time that your loop took to do all that it had to do. This time is usually called the delta time.

You can then verify if 800 ms has passed by summing all those delta times and checking whether or not it is above 800 ms.

The only difference with dpadam450 is that the code snippet that he provided will work only on Windows, whether using the standard library will allow you to use the code on different platforms.

Also, another solution when using animations is to multiply the factor used for the animation with the delta time. For example, if you want to move an object 100 units per second, you would do something like: pos += 100 * dt; where pos is your current position and dt is the delta time of the last frame. The same can be applied for an animation.

Hope that helped a bit!

helped a lot I had an issue with my camera changing speeds depedning on how fast your pc is but now its constant thanks to your comment!

Appreciate it!

This topic is closed to new replies.

Advertisement