[SDL] Framerate and speed interpolation

Bob Pendleton bob at pendleton.com
Sun Nov 23 15:40:01 PST 2003


On Sun, 2003-11-23 at 17:14, Carlos Alvarez wrote:
> Hello
> 
> I am wondering what sort of equations must be used to calculate
> interpolation for a constant movement between different framerates.
> 
> I done various tests with SDL_gfx's framerates function in my algorythm.

The main problem you are facing is one that many people face. It is the
belief in a constant frame rate. There is now such thing on modern PCs.
Forget about it. Sure, the hardware is running at a constant frame rate,
but your program may or may not be running at any given time. The result
is that you might as well just assume that the time between frames is
random.

Remember that velocity is distance/time you are trying to use
frames/second as a valid time base, and it isn't working. So, use the
real time. Just grab the time using SDL_getTicks(). and use:

r.x += (distance * time) * cos(degrees_to_radiant(angle)); 
r.y += (distance * time) * sin(degrees_to_radiant(angle));

where the time is measured in milliseconds and the distance is measured
in pixels/millisecond. Doing it this way gives you a constant visual
motion independent of when you get to draw the next screen.

To say it again, the frame rate is not a usable time base on modern
computers.

		Bob Pendleton

> 
> Here is my movement loop;
> 
> /**************************************************************************/
> 
> 	/*** .. some init stuff ... **/
> 
> 
> 
> 	SDL_initFramerate(p); //from SDL_framerate.h (SDL_gfx lib)
> 	SDL_setFramerate(p, 60); //p is an FPSmanager, set to 60FPS
> 
> 	speed = 10;  //set speed for movement
> 
> 	while(loop)
> 	{
> 		while(SDL_PollEvent(&ev))
> 		{
> 			switch(ev.type)
> 			{
> 
> 				case SDL_KEYDOWN:
> 					keystate[ev.key.keysym.sym] = 1;
> 					break;
> 				
> 				case SDL_KEYUP:
> 					keystate[ev.key.keysym.sym] = 0;
> 					break;
> 
> 				case SDL_QUIT:
> 					loop=0;
> 					break;
> 			}
> 		}
> 
> 		if(keystate[SDLK_LEFT])
> 			angle-=5.0;
> 
> 		if(keystate[SDLK_RIGHT])
> 			angle+=5.0;
> 
> 		//degrees_to_radiant(double) : converts the angle to rad 		//r : a
> SDL_Rect struct
> 		r.x += speed * cos(degrees_to_radiant(angle)); 
> 		r.y += speed * sin(degrees_to_radiant(angle));
> 
> 		SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
> 		SDL_BlitSurface(obj, NULL, screen, &r);
> 
> 		SDL_Flip(screen);
> 		SDL_framerateDelay(p); //delay for framerate
> 	}
> /**********************************************************************************/
> 
> >From here all is simple, I display the animation at 60FPS
> 
> I did some tests between various FPS values to see variations. From
> 25FPS, movement seems like constant.
> 
> 40px at 4FPS
> 60px at 5FPS
> ..
> 110px at 10FPS
> 120px at 11FPS
> ...
> 220px at 22FPS
> 240px at 23FPS
> 250px at 24FPS
> 260px at 25FPS
> 
> So we can see this doesn't run same amount of pixels according with fps
> 
> My question is : How can a run the same amount of pixels in the same
> amount of time, being FPS independant ?
> 
> cya
> 
> 
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
-- 
+---------------------------------------+
+ Bob Pendleton: writer and programmer. +
+ email: Bob at Pendleton.com              +
+ web:   www.GameProgrammer.com         +
+---------------------------------------+





More information about the SDL mailing list