Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Time based movement

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

I can''t seem to get time based movement to work. Delta times seem to be off, because if framerate get''s higher my characters jump for an example will be lower. Mainloop source:
void MainLoop()
{
	curr_delta_time = TimerGetTime();
	delta_time = curr_delta_time - prev_delta_time;
	prev_delta_time = curr_delta_time;
	move_multiplier = delta_time/1000;
	
	GameLogic();
	DrawGLScene();
	SwapBuffers(hDC);
}
And the move code:
void Sprite::move()
{
	//move

	x = x+(xvel * move_multiplier);
	y = y+(yvel * move_multiplier);
}

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
devide by 1000.0. Integer math is doing you in.

Share this post


Link to post
Share on other sites
I changed that but it did not work, the results are still identical. Every variable used in timings and movement are doubles, so there should not be aby integers messing it up.

Share this post


Link to post
Share on other sites
Time based movment is done by multiplying your movement velocity by the amount of time it takes to render one frame... I hope the following code will help.


float secs = g_timer.GetElapsedSeconds();
...
rot += 30.0f * secs;
...
glRotatef(rot, 0.0f, 1.0f, 0.0f);


And just in case you need the code to calculate the number of seconds to render a single frame...

float GetElapsedSeconds(unsigned long elapsedFrames = 1)
{
static LARGE_INTEGER lastTime = m_startTime;
LARGE_INTEGER currentTime;

QueryPerformanceCounter(¤tTime);

float seconds = ((float)currentTime.QuadPart - (float)lastTime.QuadPart) /
(float)m_ticksPerSecond.QuadPart;

// reset the timer

lastTime = currentTime;

return seconds;
}


Note: GetElapsedSeconds() is supposed to be called every frame... if it is not then you have to keep track of the number of frames since the last call manually.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!