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

time based movement problems

Started by andyZER0 Dec 22, 2004 at 2:13 PM 10 replies 1.7k views
Original Post
andyZER0
andyZER0
The problem I'm having in my 2D game is that the movement is choppy when there's a time duration that's longer than average. I do the basic things such as start my timer before rendering and stop it and get the duration of the frames so I can use that to move the sprites. I've logged examples of unusually long durations while moving upward. // regular Duration: 0.000331 player1 velocity x: 0.000000 palyer1 velocity y: 8.000000 Time Based Duration x: 0.000000 Time Based Duration y: 0.264615 // here's one Duration: 0.001678 player1 velocity x: 0.000000 palyer1 velocity y: 8.000000 Time Based Duration x: 0.000000 Time Based Duration y: 1.342740 // and as you can see, this y value makes my sprite movement very choppy, making it very fast for that frame //regular Duration: 0.000406 player1 velocity x: 0.000000 palyer1 velocity y: 8.000000 Time Based Duration x: 0.000000 Time Based Duration y: 0.324958 Is there a way I can get smoother movement? Thanks in advance.
grekster
grekster
your second y value is 100 times to big:S

maybe you could double check you calculation code to see if theres an error there which is given you the wrong answers.
Quote: Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
MasterWorks
MasterWorks
There is a math error finding your duration y, as grekster said.

If you fix the error, one thing you can do is keep a frame time history and then computer the current frame time based on a weighted average of the history. This smooths out hiccups (frames where input or system calls or whatever get processed).

Or, a really basic type filter:

frame_time = (frame_time * .2) + (new_frame_time * .8) but experiment with different constants that sum to 1.

I have just started playing with this so I don't know the best solution but it can definitely help.
andyZER0
andyZER0
Quote:
Original post by ZedFx
[ source ] [ / source ]


Thanks :)
andyZER0
andyZER0
How I get my timings...

// timer.hclass HiPerfTimer{private: 	double startTime, stopTime;	double freq;	double lastDuration;public:	HiPerfTimer();	void Start();	void Stop();	double Duration();};


// timer.cppHiPerfTimer::HiPerfTimer():startTime(0.0f),stopTime(0.0f),lastDuration(0.0f){	if (!QueryPerformanceFrequency((LARGE_INTEGER*)&freq))	{		Log("Could not initialize high performance timer.\n");		exit(0);	}		Log("High Performance Timer initialized successfully.\n");};void HiPerfTimer::Start(){	QueryPerformanceCounter((LARGE_INTEGER*)&startTime);};void HiPerfTimer::Stop(){	QueryPerformanceCounter((LARGE_INTEGER*)&stopTime);	lastDuration = (double)(stopTime - startTime)/(double)freq;};double HiPerfTimer::Duration(){	return lastDuration;};


So basically, I have a HiPerfTimer called "timer". In the main loop, I find the duration of the rendering.

timer.Start();Draw(Screen);timer.Stop();


With the durations, I use a function to return time based values.

inline float timeBasedValue(float value){	return static_cast<float>(value * timer.Duration() * 1000);} /* since I get durations in the ten thousandths, I multiply by 1000 to make them around the tenths */


Then, every frame, I modify positions according to a velocity.

inline void modPos(Vector nVec){	position.x += timeBasedValue(nVec.x);	position.y += timeBasedValue(nVec.y);};


Hope that helps.
mike25025
mike25025
the varibles startTime, stopTime, freq, lastDuration

should be 64 bit ints not doubles
andyZER0
andyZER0
Quote:
Original post by mike25025
the varibles startTime, stopTime, freq, lastDuration

should be 64 bit ints not doubles


how do i do that? something along the lines of unsigned long int?
mike25025
mike25025
use the type INT64
andyZER0
andyZER0
Quote:
Original post by mike25025
use the type INT64


How am I supposed to use INT64 values? Now my durations are 0. :(
mike25025
mike25025
oops

lastDuration should still be a double

sorry
grekster
grekster
I havent done much with QueryPerformanceCounter so i apologise now if this is all wrong :)

Shouldnt this line:
lastDuration = (double)(stopTime - startTime)/(double)freq;

be
lastDuration = (double)(stopTime.QuadPart - startTime.QuadPart)/(double)freq.QuadPart;

Also wont your duration be in seconds? so if your multiplying by 1000 you'll be returning milliseconds not tenths of a second.

also you say in your loop you have

timer.Start();
Draw(Screen);
timer.Stop();

Is there anything being done between 1 timer stop and another timer start? cause if there is you wont accout for that in your time. What ive been using is just having a timer.Update() which gets called once a loop and updates all the values once a frame. its basically a stop and start function all in one and works nicely.

Again if this is all wrong just ignore me:P
Quote: Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!

Topic Locked

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

Sign in to reply to this topic.