Jump to content
  • Advertisement
Sign in to follow this  

Any good Timing functions, to eleminate jerkyness?

This topic is 5427 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

When moving my sprites they sometimes seem a little jerky, is there any good (and easy) way to stop this??? Thanks.

Share this post

Link to post
Share on other sites
increasing framerate is a good general cure for the kind of jerkiness i'm thinking of. what kind of jerkiness are you thinking of?


Share this post

Link to post
Share on other sites
There's a great article here at GameDev called 'Frame Rate Independant Movement'. When I first started out I had a bit of jerkiness. That article helped me out a lot.

Share this post

Link to post
Share on other sites
I wrote a timing class a while back ago. It's probably a bit heavier than what you need (or maybe not at all what you need), but feel free to have fun with it / tear it apart, or do whatever.

Just do something akin to...
timer a;
while (mainloop)
{ float time = a;
updateposition(a*sprite velocity in units/second)

| |
| | timer.h --> a timer class by Eric Poggel
| | www.d512.com / eric (nospam_at) d512.com
| | version 1.1
| | Speed modifier functions by John Henry

#ifndef TIMER_H_ZZZZ
#define TIMER_H_ZZZZ

#include <windows.h>
#include <math.h>

// Ultra Precision Timer Class
// Values are in seconds
class timer
__int64 frequency; // ticks per second
double resolution; // 1 / frequency
__int64 last_tick; // tick count at last querry
bool hires; // high-resolution timer is available and used
double relativeTime; // time returned
double modifier; // time scale factor (1.0 by default)
double MaxTime; // time until clock rolls over
double MinTime; // time until clock rolls under (when going backwards)
bool MinSet; // use rollunder
bool MaxSet; // use rollover
bool paused;

// Return value
operator double()
{return get();}

// Constructor
{ ZeroMemory(this, sizeof(*this));
modifier = 1.0f;
if (QueryPerformanceFrequency((LARGE_INTEGER*)&frequency))
{ QueryPerformanceCounter((LARGE_INTEGER*)&last_tick);
hires = true;
resolution = double(1.0/frequency);
{ hires = false;
last_tick = GetTickCount();
resolution = 0.001f;
frequency = 1000;
} }

// Get Time
double get()
// Get System tick count
__int64 current_tick; // tick count at current querry
if (hires) QueryPerformanceCounter((LARGE_INTEGER*)&current_tick);
else current_tick = GetTickCount();

// Find time elapsed since last querried
// relativeTime here is what the time is with no rollover / under.
if (paused)
current_tick = last_tick;
{ relativeTime +=((current_tick-last_tick) * resolution * (modifier*GlobalTimeSpeed));

// Set in bounds if out of bounds
double range = (MaxTime-MinTime);
if ((relativeTime > MaxTime) && MaxSet)
relativeTime = MinTime + fmod(relativeTime-MinTime, MaxTime-MinTime);
if ((relativeTime < MinTime) && MinSet)
relativeTime = MaxTime + fmod(relativeTime-MaxTime, MaxTime-MinTime);

return relativeTime;

// Set Time
// This will take rollover / rollunder into account
void set(double seconds)
{ relativeTime=seconds;

// Pause Timer
void pause()
{ if (paused) return;
paused = true;
__int64 current_tick;
if (hires) QueryPerformanceCounter((LARGE_INTEGER*)&current_tick);
else current_tick = GetTickCount();
relativeTime +=((current_tick-last_tick) * resolution * (modifier*GlobalTimeSpeed));

// Resume Timer
void resume()
{ if (!paused) return;
paused = false;
if (hires) QueryPerformanceCounter((LARGE_INTEGER*)&last_tick);
else last_tick = GetTickCount();

// Reset Time
inline void reset()
{ relativeTime=0;

// Set Rollover
bool setrollover(double seconds)
{ if ((seconds<MinTime) && MinSet) return false;
return true;

// Set Rollunder
bool setrollunder(double seconds)
{ if ((seconds>MaxTime) && MaxSet) return false;
return true;

// Get Rollover
double getrollover()
{ return MaxTime;

// Get Rollunder
double getrollunder()
{ return MinTime;

// Set Speed Factor of timer
// (1.0 is default speed)
void setspeed(double _modifier)
{ modifier = _modifier;

// Get Speed
double getspeed()
{ return modifier;

// Clear bounds
// makes clock never roll over or under
void clearbounds()
{ MinSet=false;


[Edited by - Xaelect on February 25, 2006 11:51:48 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!