noobish, probably naive question

Started by
4 comments, last by spunkybuttockszc 19 years, 2 months ago
Okay, I'm not really a noob to game programming, but I've encountered a problem that I've never had any trouble with before. Here's a code snippet:

float start_time = (float)CL_System::get_time();
float curr_time = start_time;
float delta_time = 30.0f;

while (is_running) {

	game_controller->update(1 / delta_time);
        CL_System::keep_alive();

	curr_time = CL_System::get_time();
	delta_time = curr_time - start_time;
	start_time = curr_time;
}


My problem lies in the calculation of the time step (delta_time). The whole purpose of calculating the time step is to keep animation smooth and at the same speed on all machines. Instead, motion is very jerky and changes often (sometimes as often as every frame). What am I doing wrong?
Advertisement
First you can make sure the game_controller->update() function accepts a float or double and not an int. I made that mistake once and was trying to figure out why my accuracy was to the one's position.

Second try (1.0f /delta_time ) to ensure the result is a float and not an int.

To help in debugging, you should printf out the contents of those variables so you can easily see what values they are taking, maybe that can help illustrate the problem. I do not see any logic errors in your code, but I may be mistaken. Maybe someone else can point one out.

- Drew
If your timer isn't high resolution there's a simple trick to smooth deltaT
so that it is less jerky


float filter = 0.2;   // a value between 0.0 and 1.0float deltaT = 0.0;float newTime = 0.0;float oldTime = 0.0;while( running){   newTime = (float)clock()/CLK_TCK;   // Update the average time using a smoothing filter function   deltaT = (1-filter)*(newTime - oldTime) + filter*deltaT;         // instead of deltaT = newTime - oldTime;   oldTime = newTime;   //*****************************************   // **** Do something once a frame here ****   //*****************************************}
______________________________________________________The problem with designing something completely foolproof is tounderestimate the ingenuity of a complete fool. - Douglas AdamsEmail: thefatgecko@yahoo.co.ukhttp://www.geocities.com/thefatgecko
Alright, thanks guys. I got it working. All I had to do is what Drew_Benton said, change (1 / delta_time) to (1.0f / delta_time).
float start_time = (float)CL_System::get_time();float curr_time = start_time;float delta_time = 30.0f;while (is_running) {	game_controller->update(1 / delta_time);// Up until here you appear to be using delta_time as a frequency value - // encoding the desired FPS (30), and calculating a time for update by taking// the reciprocal.        CL_System::keep_alive();	curr_time = CL_System::get_time();	delta_time = curr_time - start_time;	start_time = curr_time;// But in this logic, you use delta_time as an actual, direct time value -// encoding the amount of elapsed time between curr_time and start_time. And// then you loop back and take the reciprocal of that, and mayhem ensues.}


Since you chose to name the variable as a time, be consistent with that interpretation:

//float delta_time = 30.0f;float desired_FPS = 30.0f;float delta_time = (1 / desired_FPS);while (is_running) {	game_controller->update(/* 1 / */ delta_time);
Thanks Zahlman. That fixed a short jittering that occured when 'game_controller->update()' was first called.

This topic is closed to new replies.

Advertisement