Jump to content
  • Advertisement
Sign in to follow this  
fishleg003

Fps problem

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

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

Share this post


Link to post
Share on other sites
Advertisement
Here's the a problem:

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


Taking an example of delta_time == 40:

first the loop runs once with delta_time == 40 - player is moved by 40, delta decreased by 16.
then the loop runs once with delta_time == 23.4 - player is moved by 23.4, delta is decreased by 16.

Total movement: 63.4 - change in delta_time: -32 (2 x -16)

If you call Player_UpdateMovement( delta_time , &P ), this does all the movement for that entire timeslice, and delta_time should be zeroed accordingly (instead of only subtracted by 16.6).

If you call Player_UpdateMovement( 16.6 , &p ), this would move the player only for that 16.6 timeslice, and the remainder of your code would work as expected.

Share this post


Link to post
Share on other sites
rrr well spotted :)

So really i want something like,

if(delta_time >= 16.6)//FRAME_TIME)
{
Player_UpdateMovement(delta_time, &P);

fps+= delta_time / 16.6;
delta_time = 0;
}

instead of that while loop ?

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!