Jump to content
  • Advertisement
Sign in to follow this  
spunkybuttockszc

noobish, probably naive question

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!