Skip to main content
GameDev.net gamedev.net
🔒 Locked

High Resolution Timing In Games

Started by Myopic Rhino Feb 21, 2001 at 9:08 AM 3 replies 3.6k views
Jamez
Jamez
I found an article here on gamedev about High Resolution Timing In Games. It can be found here:

http://www.gamedev.net/reference/articles/article753.asp

I downloaded the source code for VC, and got it running fine and I understand how the timer is working more or less. My problem is I don''t understand how to update (a variable for instance) a specific number of times per second using the timer.

The article mentions this:

players.location_x+=players.x_units_per_second*frametime;

Where players.x_units_per_second is supposed to be say the update rate of a variable. So you multiply the update rate of the variable with the time it takes to display a "frame", and you sum up this value...

So I did a little theoretical application of this:

players.x_units_per_second = 4
players.location_x = 0;

players.location_x += 4 * 0.025; // 0 + 4 * 0.025 = 0.1
players.location_x += 4 * 0.070; // 0.1 + 4 * 0.070 = 0.38
players.location_x += 4 * 0.011; // 0.38 + 4 * 0.011 = 0.424
players.location_x += 4 * 0.059; // 0.424 + 4 * 0.059 = 0.66

I just made up some theoretical frame times to multiply with my units per second, and I can''t figure out how I''m supposed to use this to update something a specific number of times per second.

Please help!

PlasmicSoup
PlasmicSoup
i used the code in this article for frame-rate indepentent movement, but it still runs way to fast on my friends ultra-powerful computer, and runs the same no matter how fast i get it. (by turning off vsync i got it to run around 1000fps fullscreen... still speed stayed perfect) how come?

-plasmicsoup
-plasmicsoup
brekehan
brekehan
My application before adding a timer for movement using QueryPerformanceTimer and QueryPerformanceFrequency ran smooth as silk, after adding the timer to move in units per second instead of just a constant speed makes the application look terribly chunky, I did not limit the fps or change
anything else I just replaced

float speed = 0.1f

with

float speed = static_cast<float> (1.0f * m_input_timer.Elapsed());


Here is the defintion of the timer:

#ifndef GAME_TIMER_H
#define GAME_TIMER_H
//-----------------------------------------------------------------------------------------------

// game_timer.h

//

// Timer object

//

// Purpose: To provide an interface for timers

//

// Christopher Pisz

// 04/27/04

//-----------------------------------------------------------------------------------------------

#include <windows.h>

class Game_Timer
{
public:
Game_Timer();
~Game_Timer();

void Start();
double Elapsed();

private:
double m_start;

double GetCurrentCount();
};

#endif // GAME_TIMER_H


//-----------------------------------------------------------------------------------------------

// game_timer.cpp

//

// Timer object

//

// Purpose: To provide an interface for game timers such as frame timer

//

//

// Christopher Pisz

// 04/27/04

//-----------------------------------------------------------------------------------------------

#include "game_timer.h"

Game_Timer::Game_Timer()
:
m_start( GetCurrentCount() )
{
}

//-----------------------------------------------------------------------------------------------

Game_Timer::~Game_Timer()
{
}

//-----------------------------------------------------------------------------------------------

void Game_Timer::Start()
{
m_start = GetCurrentCount();
}

//-----------------------------------------------------------------------------------------------

double Game_Timer::Elapsed()
{
return GetCurrentCount() - m_start;
}

//-----------------------------------------------------------------------------------------------

double Game_Timer::GetCurrentCount()
{
LARGE_INTEGER time,
frequency;

QueryPerformanceCounter(&time);
QueryPerformanceFrequency(&frequency);

return static_cast<double>(time.QuadPart) / frequency.QuadPart;
}




and here is where it is in my render loop

// Enter main event loop

while(TRUE)
{
// Test if there is a message in que, if so get it

if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// Test if this is a quit

if(msg.message==WM_QUIT)
break;

// Translate any accelerator keys

TranslateMessage(&msg);

// Send the message to window proc

DispatchMessage(&msg);
}

// Main processing loop

if(m_active)
{
// Process input

ProcessInput();
m_input_timer.Start();

// Perform any game logic

ProcessLogic();
//m_logic_timer.Start();


// Render

//if( m_frame_timer.Elapsed() > 0.0083333333f )

{
// m_frame_timer.Start();

RenderLoop();
}
}
}


and here is the process input function

void MyGameHost::ProcessInput()
{
float speed = static_cast<float> (1.0f * m_input_timer.Elapsed());
//float speed = 0.01f;


// Rotations /////////////////////////////////////


// If the ''a'' key is pressed look towards the left

if(m_keys[65])
m_camera.Rotate(-speed, D3DXVECTOR3(0.0f, 1.0f, 0.0f));

// If the ''d'' key is pressed look towards the right

if(m_keys[68])
m_camera.Rotate(speed, D3DXVECTOR3(0.0f, 1.0f, 0.0f));

// If the ''w'' key is pressed look up

if(m_keys[87])
m_camera.Rotate(-speed, D3DXVECTOR3(1.0f, 0.0f, 0.0f));

// If the ''x'' key is pressed look down

if(m_keys[88])
m_camera.Rotate(speed, D3DXVECTOR3(1.0f, 0.0f, 0.0f));

// If the ''e'' key is pressed roll right

if(m_keys[69])
m_camera.Rotate(-speed, D3DXVECTOR3(0.0f, 0.0f, 1.0f));

// If the ''q'' key is pressed roll left

if(m_keys[81])
m_camera.Rotate(speed, D3DXVECTOR3(0.0f, 0.0f, 1.0f));

// Translations ///////////////////////////////////


// If the ''s'' key is pressed move foward

if(m_keys[83])
m_camera.Translate(speed, D3DXVECTOR3(0.0f, 0.0f, 1.0f));

// If the ''v'' key is pressed move backward

if(m_keys[86])
m_camera.Translate(speed, D3DXVECTOR3(0.0f, 0.0f, -1.0f));

// If the ''z'' key is pressed strafe left

if(m_keys[90])
m_camera.Translate(speed, D3DXVECTOR3(-1.0f, 0.0f, 0.0f));

// If the ''c'' key is pressed strafe right

if(m_keys[67])
m_camera.Translate(speed, D3DXVECTOR3(1.0f, 0.0f, 0.0f));

// If the ''r'' key is pressed move up

if(m_keys[82])
m_camera.Translate(speed, D3DXVECTOR3(0.0f, 1.0f, 0.0f));

// If the ''f'' key is pressed move down

if(m_keys[70])
m_camera.Translate(speed, D3DXVECTOR3(0.0f, -1.0f, 0.0f));

}



Any ideas on why things became chukified?

I can also provide the full source and .exe from my ftp is anyone wants to see first hand.

Thanks,
Christopher

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.