Sign in to follow this  

Time Based Movement

Recommended Posts

Hey, I'm working on a RTS with a friend and we are having some issues with time based movement; notably, units move slower at lower FPS than at higher FPS. We do have a system for calculating the speed that a unit is traveling based on elapsed time, but it generally returns the same results, no matter the FPS. Here is the implementation TimeBasedUpdater.hpp
class TimeBasedUpdater
	static Uint32 start;
	static Uint32 current;

	// returns the value adjusted for time based movement
	static float GetCorrectedValue(float value);

	// needs to be restarted each frame
	static void Restart()
		start = SDL_GetTicks();


#include "TimeBasedUpdater.hpp"
#include <iostream>

Uint32 TimeBasedUpdater::start		= 0;
Uint32 TimeBasedUpdater::current	= 0;

float TimeBasedUpdater::GetCorrectedValue(float value)
	do {
		current = SDL_GetTicks();
	} while (start>=current);
	//Uint32 frame_time = current-start;
	std::cout << (value*(current-start)) << std::endl;
	return float(value*(current-start));	

(Yes, Restart() is being called at the end of each frame) All of the source is located at Let me know if you want something specific. Thanks; any thoughts?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this