• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

### #ActualHodgman

Posted 29 November 2012 - 04:14 AM

You'll find some answers under "framerate independent movement".

The simplest way is to measure how long the previous frame took to execute, and then use that amount of time as a scaling factor on all movement.
void move(float dt)
{
x+=0.2 * dt; // moves 0.2 units per second, instead of units per frame
}

long long previousTicks; // when dealing with *absolute* time values, you want to use 64-bit integers for precision, not floating point.

void render()
{
long long currentTicks = ReadClockTickCounter();
long long deltaTicks = currentTicks - previousTicks;
previousTicks = currentTicks;
float deltaTimeInSeconds = (float)( (double)deltaTicks / (double)ClockTicksPerSecond() );
//for *relative* time values, floating point is ok

move( deltaTimeInSeconds );
On Windows, the made up methods "ReadClockTickCounter" and "ClockTicksPerSecond" can be implemented using QueryPerformanceCounter and QueryPerformanceFrequency. Alternatively you can use timeGetTime (and the number 1000 for the ticks per second), but it's not really accurate enough for most games.
Personally, I use timer_lib, which has implemented a cross-platform high-frequency timer for me.

Once you're comfortable with this basic method, you could also look at using a fixed time step.

### #5Hodgman

Posted 29 November 2012 - 04:04 AM

You'll find some answers under "framerate independent movement".

The simplest way is to measure how long the previous frame took to execute, and then use that amount of time as a scaling factor on all movement.
void move(float dt)
{
x+=0.2 * dt; // moves 0.2 units per second, instead of units per frame
}

long long previousTicks; // when dealing with *absolute* time values, you want to use 64-bit integers for precision, not floating point.

void render()
{
long long currentTicks = ReadClockTickCounter();
long long deltaTicks = currentTicks - previousTicks;
previousTicks = currentTicks;
float deltaTimeInSeconds = (float)( (double)deltaTicks / (double)ClockTicksPerSecond() );
//for *relative* time values, floating point is ok

move( deltaTimeInSeconds );
On Windows, the made up methods "ReadClockTickCounter" and "ClockTicksPerSecond" can be implemented using QueryPerformanceCounter and QueryPerformanceFrequency. Alternatively you can use timeGetTime (and the number 1000 for the ticks per second), but it's not really accurate enough for most games.
Personally, I use timer_lib, which has implemented a cross-platform high-frequency timer for me.

Once you're comfortable with this basic method, you could also look at using a fixed time step.

### #4Hodgman

Posted 29 November 2012 - 04:03 AM

You'll find some answers under "framerate independent movement".

The simplest way is to measure how long the previous frame took to execute, and then use that amount of time as a scaling factor on all movement.
void move(float dt)
{
x+=0.2 * dt; // moves 0.2 units per second, instead of units per frame
}

long long previousTicks;

void render()
{
long long currentTicks = ReadClockTickCounter();
long long deltaTicks = currentTicks - previousTicks;
previousTicks = currentTicks;
float deltaTimeInSeconds = (float)( (double)deltaTicks / (double)ClockTicksPerSecond() );

move( deltaTimeInSeconds );
On Windows, the made up methods "ReadClockTickCounter" and "ClockTicksPerSecond" can be implemented using QueryPerformanceCounter and QueryPerformanceFrequency. Alternatively you can use timeGetTime (and the number 1000 for the ticks per second), but it's not really accurate enough for most games.
Personally, I use timer_lib, which has implemented a cross-platform high-frequency timer for me.

Once you're comfortable with this basic method, you could also look at using a fixed time step.

### #3Hodgman

Posted 29 November 2012 - 04:01 AM

You'll find some answers under "framerate independent movement".

The simplest way is to measure how long the previous frame took to execute, and then use that amount of time as a scaling factor on all movement.
void move(float dt)
{
x+=0.2 * dt;
}

long long previousTicks;

void render()
{
long long currentTicks = ReadClockTickCounter();
long long deltaTicks = currentTicks - previousTicks;
previousTicks = currentTicks;
float deltaTimeInSeconds = (float)( (double)deltaTicks / (double)ClockTicksPerSecond() );

move( deltaTimeInSeconds );
On Windows, the made up methods "ReadClockTickCounter" and "ClockTicksPerSecond" can be implemented using QueryPerformanceCounter and QueryPerformanceFrequency. Alternatively you can use timeGetTime (and the number 1000 for the ticks per second), but it's not really accurate enough for most games.
Personally, I use timer_lib, which has implemented a cross-platform high-frequency timer for me.

Once you're comfortable with this basic method, you could also look at using a fixed time step.

### #2Hodgman

Posted 29 November 2012 - 04:01 AM

You'll find some answers under "framerate independent movement".

The simplest way is to measure how long the previous frame took to execute, and then use that amount of time as a scaling factor on all movement.
void move(float dt)
{
x+=0.2 * dt;
}

long long previousTicks;

void render()
{
long long currentTicks = ReadClockTickCounter();
long long deltaTicks = currentTicks - previousTicks;
previousTicks = currentTicks;
float deltaTimeInSeconds = (float)( (double)deltaTicks / (double)ClockTicksPerSecond() );

move( deltaTimeInSeconds );
On Windows, the made up methods "ReadClockTickCounter" and "ClockTicksPerSecond" can be implemented using QueryPerformanceCounter and QueryPerformanceFrequency. Alternatively you can use timeGetTime, but it's not really accurate enough for most games.
Personally, I use timer_lib, which has implemented a cross-platform high-frequency timer for me.

Once you're comfortable with this basic method, you could also look at using a fixed time step.

### #1Hodgman

Posted 29 November 2012 - 03:58 AM

You'll find some answers under "framerate independent movement".

The simplest way is to measure how long the previous frame took to execute, and then use that amount of time as a scaling factor on all movement.
void move(float dt)
{
x+=0.2 * dt;
}

long long previousTicks;

void render()
{
long long currentTicks = ReadClockTickCounter();
long long deltaTicks = currentTicks - previousTicks;
previousTicks = currentTicks;
float deltaTimeInSeconds = deltaTicks / ClockTicksPerSecond();

move( deltaTimeInSeconds );
On Windows, the made up methods "ReadClockTickCounter" and "ClockTicksPerSecond" can be replaced with QueryPerformanceCounter and QueryPerformanceFrequency.

Once you're comfortable with this basic method, you could also look at using a fixed time step.

PARTNERS