# Object's speed control against different framerates

**URL:** <https://discourse.libsdl.org/t/objects-speed-control-against-different-framerates/14497>\
**Category:** SDL Development\
**Created:** [April 27, 2007, 2:48pm UTC](https://discourse.libsdl.org/t/objects-speed-control-against-different-framerates/14497 "2007-04-27T14:48:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![C.Alvarez](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@C.Alvarez](https://discourse.libsdl.org/u/C.Alvarez)\
**Post date:** [April 27, 2007, 2:48pm UTC](https://discourse.libsdl.org/t/objects-speed-control-against-different-framerates/14497/1 "2007-04-27T14:48:58Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Jeffrey\_Grembecki](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/cdc98d/32.png) [@Jeffrey\_Grembecki](https://discourse.libsdl.org/u/Jeffrey_Grembecki)\
**Post date:** [April 27, 2007, 6:33pm UTC](https://discourse.libsdl.org/t/objects-speed-control-against-different-framerates/14497/2 "2007-04-27T18:33:38Z")

</div>

> 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](http://dewitters.koonsolo.com/gameloop.htmlOn) Fri, 27 Apr 2007 16:48:58 +0200 C.Alvarez wrote:

---

<div class="post-metadata">

**Author:** ![Tommy\_Hinks](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/tommy_hinks/32/654_2.png) [@Tommy\_Hinks](https://discourse.libsdl.org/u/Tommy_Hinks)\
**Post date:** [April 28, 2007, 11:11am UTC](https://discourse.libsdl.org/t/objects-speed-control-against-different-framerates/14497/3 "2007-04-28T11:11:57Z")

</div>

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](http://dewitters.koonsolo.com/gameloop.html)
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![C.Alvarez](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/b487fb/32.png) [@C.Alvarez](https://discourse.libsdl.org/u/C.Alvarez)\
**Post date:** [April 29, 2007, 8:40am UTC](https://discourse.libsdl.org/t/objects-speed-control-against-different-framerates/14497/4 "2007-04-29T08:40:48Z")

</div>

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](http://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](http://dewitters.koonsolo.com/gameloop.html)
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
