Object's speed control against different framerates

For time based movements, I use a basic pos = pos + speed * lastframetime
When I move an object with constant speed during constant time, one
second for example, I have no problem to end the movement at the same
position in any framerate.

When I tried to include variable speed movement, this seems to be a
little more complicated to interpolate the speed variation.

I have made this little code to explain what I mean, it just need to
press space to start the test, runs a desired movement, decreases the
speed, and automatically ends ( with log in stdout.txt ).

( There is no function’s check because this is just a little test ) :

/*** code start ***/
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

int main( int argc, char** argv )
{
// framerate vars
int iCurrentTick = 0, iLastTick = 0, iTmpFPS = 0, iNextUpdate = 0;
float fFrameDuration;

   // object vars
float fObjPosX = 0;
float fSpeed = 500.0f;

   // other
BOOL bLoop = TRUE, bMoving = FALSE;
SDL_Event event;

  // start
SDL_Init( SDL_INIT_VIDEO|SDL_INIT_TIMER );

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

SDL_SetVideoMode( 800, 600, 32, SDL_OPENGL );

glMatrixMode ( GL_PROJECTION );
glLoadIdentity ();
glOrtho( 0, 800, 0, 600, -1.0, 1.0 );

glMatrixMode ( GL_MODELVIEW );

   // main loop
while( bLoop )
{

		// EVENT SECTION
	while( SDL_PollEvent( &event ) )
	{
		switch(  event.type )
		{
			case SDL_QUIT:
			{
				SDL_Quit();
				return 0;
			}

			case SDL_KEYDOWN:
				if( event.key.keysym.sym == SDLK_ESCAPE )
				{
					SDL_Quit();
					return 0;
				}

				if( event.key.keysym.sym == SDLK_SPACE )
					bMoving = TRUE;
		}
	}

		// UPDATE SECTION
	if( bMoving )
	{
		float deltaf = fFrameDuration / 1000.0f;
		fObjPosX += fSpeed * deltaf; // adjust the position
		fSpeed -= 200.0f * deltaf; // adjust the speed

		if( fSpeed <= 0.0f )
		{
			bMoving = FALSE;
			SDL_Quit();
			return 0;
		}
		
		printf( "FT=%.02f pos=%.02f speed=%.02f\n", fFrameDuration,

fObjPosX, fSpeed ); // logger
}

		// DRAWING SECTION
	glClearColor( 0, 0, 0, 0 );
	glClear( GL_COLOR_BUFFER_BIT );

	glLoadIdentity();
	glTranslatef( fObjPosX, 300.0f, 0.0f );

	glBegin(GL_TRIANGLES);
		glColor3f( 255, 255, 0 );
		glVertex3f( 10.5f, .0f, .0f );

		glColor3f( 255, 0, 0);
		glVertex3f( -10.5f, -10.25f, 0.0f );

		glColor3f( 255, 0, 0 );
		glVertex3f( -10.5f, 10.25f, .0f );
	glEnd();
	SDL_GL_SwapBuffers();


		// FRAMERATE UPDATE
	iCurrentTick = SDL_GetTicks();
	fFrameDuration = iCurrentTick - iLastTick;
	iLastTick = iCurrentTick;

		// FPS count
	iTmpFPS++;
	if( iCurrentTick >= iNextUpdate )
	{
		iNextUpdate += 1000;
		printf("FPS=%i\n", iTmpFPS );
		iTmpFPS = 0;
	}
}

return 0;

}

/*** code end ***/

Running at max speed ( 360/380 fps on my system ) the object position
ends at 625.
Running at 75 fps ( vsync forced from drivers ) the object position ends at 628.

I tried with different framerates ( with FPSmanager from sdl_gfx ),
and lower is the framerate, and greater is the ending position.

How to do a correct speed adjustment ?

For time based movements, I use a basic pos = pos + speed * lastframetime
When I move an object with constant speed during constant time, one
second for example, I have no problem to end the movement at the same
position in any framerate.

When I tried to include variable speed movement, this seems to be a
little more complicated to interpolate the speed variation.

I have made this little code to explain what I mean, it just need to
press space to start the test, runs a desired movement, decreases the
speed, and automatically ends ( with log in stdout.txt ).

Without looking at the code it sounds like a lack of precision. I’ve found the
best method for timing is to use a fixed timer on the game loop, say 25 fps
and within the render function do interpolation.

This method while very good does add a little complication with the addition
of prediction of events within the render function.

The advantage is your game loop will always result in the same output no
matter what frame rate you have and it is also possible to record player
input for a replay.

There’s a good article on the subject at
http://dewitters.koonsolo.com/gameloop.htmlOn Fri, 27 Apr 2007 16:48:58 +0200 C.Alvarez wrote:

The thing to watch out for is if you have more complicated equations
to solve. In your case it seems to be x = v*dt. This equation is not
extremely sensitive for large timesteps, but other equations are. The
problem is then that if the framerate drops, the physics of the
simulation crash. Just something to be aware of. You might even notice
this effect if you add acceleration to the equations you have now.

One solution to this problem is to simply set a maximum allowed
timestep, but this means that the application is no longer produces
the same results for the same amount of frames.

TommyOn 27/04/07, Jeffrey Grembecki wrote:

On Fri, 27 Apr 2007 16:48:58 +0200 C.Alvarez wrote:

For time based movements, I use a basic pos = pos + speed * lastframetime
When I move an object with constant speed during constant time, one
second for example, I have no problem to end the movement at the same
position in any framerate.

When I tried to include variable speed movement, this seems to be a
little more complicated to interpolate the speed variation.

I have made this little code to explain what I mean, it just need to
press space to start the test, runs a desired movement, decreases the
speed, and automatically ends ( with log in stdout.txt ).

Without looking at the code it sounds like a lack of precision. I’ve found the
best method for timing is to use a fixed timer on the game loop, say 25 fps
and within the render function do interpolation.

This method while very good does add a little complication with the addition
of prediction of events within the render function.

The advantage is your game loop will always result in the same output no
matter what frame rate you have and it is also possible to record player
input for a replay.

There’s a good article on the subject at
http://dewitters.koonsolo.com/gameloop.html


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

do you know some documentation that could help me to learn a best way
to implement fixed time step against free framerate?

2007/4/28, Tommy Hinks <tommy.hinks at gmail.com>:> The thing to watch out for is if you have more complicated equations

to solve. In your case it seems to be x = v*dt. This equation is not
extremely sensitive for large timesteps, but other equations are. The
problem is then that if the framerate drops, the physics of the
simulation crash. Just something to be aware of. You might even notice
this effect if you add acceleration to the equations you have now.

One solution to this problem is to simply set a maximum allowed
timestep, but this means that the application is no longer produces
the same results for the same amount of frames.

Tommy

On 27/04/07, Jeffrey Grembecki wrote:

On Fri, 27 Apr 2007 16:48:58 +0200 C.Alvarez <@C.Alvarez> wrote:

For time based movements, I use a basic pos = pos + speed * lastframetime
When I move an object with constant speed during constant time, one
second for example, I have no problem to end the movement at the same
position in any framerate.

When I tried to include variable speed movement, this seems to be a
little more complicated to interpolate the speed variation.

I have made this little code to explain what I mean, it just need to
press space to start the test, runs a desired movement, decreases the
speed, and automatically ends ( with log in stdout.txt ).

Without looking at the code it sounds like a lack of precision. I’ve found the
best method for timing is to use a fixed timer on the game loop, say 25 fps
and within the render function do interpolation.

This method while very good does add a little complication with the addition
of prediction of events within the render function.

The advantage is your game loop will always result in the same output no
matter what frame rate you have and it is also possible to record player
input for a replay.

There’s a good article on the subject at
http://dewitters.koonsolo.com/gameloop.html


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org