Skip to main content
gamedev.net
🔒 Locked

time based movement

Started by lack the hack 0 replies 750+ views
Original Post
lack the hack
lack the hack
hello everyone, i'm making a simple game where theres no physics involved (haha, and i'm posting this in the math and physics section?), and i'm doing animation like so:

float movement = 0;
glTranslatef(movement,0,0);
movement += 0.05f;
myFunc();
but the problem with it is, when i send the game to someone else, the game either goes way too fast or way too slow. could someone show me some code where i could do that same animation but make it so that it runs the same on all computers? here's the timer im using, (from nehe)

#ifndef timer_h
#define timer_h


struct			 							// Create A Structure For The Timer Information
{
  __int64       frequency;							// Timer Frequency
  float         resolution;							// Timer Resolution
  unsigned long mm_timer_start;							// Multimedia Timer Start Value
  unsigned long mm_timer_elapsed;						// Multimedia Timer Elapsed Time
  bool		performance_timer;						// Using The Performance Timer?
  __int64       performance_timer_start;					// Performance Timer Start Value
  __int64       performance_timer_elapsed;					// Performance Timer Elapsed Time
} timer;		

void TimerInit(void)								// Initialize Our Timer (Get It Ready)
{
	memset(&timer, 0, sizeof(timer));					// Clear Our Timer Structure

	// Check To See If A Performance Counter Is Available
	// If One Is Available The Timer Frequency Will Be Updated
	if (!QueryPerformanceFrequency((LARGE_INTEGER *) &timer.frequency))
	{
		// No Performace Counter Available
		timer.performance_timer	= FALSE;				// Set Performance Timer To FALSE
		timer.mm_timer_start	= timeGetTime();			// Use timeGetTime() To Get Current Time
		timer.resolution	= 1.0f/1000.0f;				// Set Our Timer Resolution To .001f
		timer.frequency		= 1000;					// Set Our Timer Frequency To 1000
		timer.mm_timer_elapsed	= timer.mm_timer_start;			// Set The Elapsed Time To The Current Time
	}
	else
	{
		// Performance Counter Is Available, Use It Instead Of The Multimedia Timer
		// Get The Current Time And Store It In performance_timer_start
		QueryPerformanceCounter((LARGE_INTEGER *) &timer.performance_timer_start);
		timer.performance_timer		= TRUE;				// Set Performance Timer To TRUE
		// Calculate The Timer Resolution Using The Timer Frequency
		timer.resolution		= (float) (((double)1.0f)/((double)timer.frequency));
		// Set The Elapsed Time To The Current Time
		timer.performance_timer_elapsed	= timer.performance_timer_start;
	}
}
float TimerGetTime()								// Get Time In Milliseconds
{
	__int64 time;								// time Will Hold A 64 Bit Integer

	if (timer.performance_timer)						// Are We Using The Performance Timer?
	{
		QueryPerformanceCounter((LARGE_INTEGER *) &time);		// Grab The Current Performance Time
		// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
		return ( (float) ( time - timer.performance_timer_start) * timer.resolution)*1000.0f;
	}
	else
	{
		// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
		return( (float) ( timeGetTime() - timer.mm_timer_start) * timer.resolution)*1000.0f;
	}
}

#endif

thanks :]
CombatWombat
CombatWombat
You need to scale your movement based on the time it takes to complete a frame of your game. There are many ways to do this. In addition it is a good idea to make the logic of your game run at a fixed rate and allow the rendering to run free. It will save you headaches later on.

This is a very good link that will guide you from the point you are at now, to a fixed timestep method with interpolation for smoothing when entire timesteps cannot be taken.
http://www.gaffer.org/articles/Timestep.html

Topic Locked

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

Sign in to reply to this topic.