Original Post
Ive been trying to get independent frame rate running for my side scrolling shooter. Ive finally got it updating 60 times a second but my problem is that delta_time is not always a constant value. At the moment the delta_time comes out as 20.3 18.4 16.7 i base my player movement off that and because these values are quite different the player speeds up and slows down randomly well thats what i think is happening. Its hard to explain but it doesnt seem smooth for 60fps theres a slight judder every so often so i must be calculating the frame rate wrong some how. thanks alot for any help Heres my idle function
void idle(void)
{
static float Old_Time = 0;
float delta_time = 0;
static int fps = 0;
static float starttime = 0;
static float LastSecond = 0;
if(starttime == 0)
{
starttime = glutGet(GLUT_ELAPSED_TIME);
LastSecond = starttime;
}
Act_Time=glutGet(GLUT_ELAPSED_TIME) - starttime;
delta_time = Act_Time - Old_Time;
while(delta_time >= 16.6)//FRAME_TIME)
{
printf("delta time %f \n", delta_time);
Player_UpdateMovement(delta_time, &P);
fps++;
delta_time = delta_time - 16.6;
}
if(Act_Time - LastSecond >= 1000)
{//calc fps
LastSecond = Act_Time;
print out new fps
fps = 0;
}
Old_Time = Act_Time - delta_time;
glutPostRedisplay();
}
void Player_UpdateMovement(float elapsed_t, Player *p)
{
p->Pos.x += p->dx * elapsed_t/1000;
p->Pos.y += p->dy * elapsed_t/1000;
}