Sign in to follow this  

noobish, probably naive question

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

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.0
float 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 ****
//*****************************************

}

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

This topic is 4696 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.

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