FPS independant movement question

Started by
15 comments, last by Panopticon 19 years, 6 months ago
Hey everyone, I'm trying to produce FPS independant movement, Can anyone tell me if this is good?
[source lang=cpp]
bool running=true;
float time2, time, deltatime;
while (running==true;)
{
    time2=SDL_GetTicks();
    deltatime=(time2-time) * 0.1;
    time=time2;
  
     //Game Logic, get events etc..
     //Eg  xposition += spritespeed * deltatime 
  
    SDL_Delay(1);
}
[\source]
Advertisement
You are using an uninitialized variable in the first iteration (time). What does the 0.1 signify? A comment is needed there me thinks.
Quote:Original post by elementary
You are using an uninitialized variable in the first iteration (time). What does the 0.1 signify? A comment is needed there me thinks.


its a snippit, uninitialized is irrelivant. This is a snippit I found out on the inter-web. I have no idea how FPS independant is supposed to work and after a few days of reading i'm at my wit's end. Most examples dont really explain how things are supposed to work and thats frustrating :S

~my~ understanding of FPS-I movement and delta time is that if the loop goes to fast, sleep, otherwise do stuff. Movement would be a speed * the delta time. But I can't find anywere that really explains it to me.

help! :s



What you have to do is generate a frame per amount of time.

Process a frame every one 60th of a second for instance, giving you a max fps of 60 frames per second.

What you need to do is, store the current time in say variable 'currenttime', and store the time at wich you want to process a frame at variable say 'frametime'. Now set 'frametime' inicially as 'currenttime' plus one 60th of a second.

Now in the game loop, you update 'currenttime' at every pass of the cicle, and when 'currenttime' is bigger or equal to 'frametime' that means one 60th of a second has passed and you should process a frame. You increase another 60th of a second to 'frametime', and keep running the cicle until 'currenttime' is equal or bigger then 'time' again.
"Follow the white rabbit."
simple....






float cTime,pTime; /* ctime Is the current time, pTime is the previous time*/
int framerate = 60; /* frames per a second*/


while(1)
{
cTime = getTickCount() * 0.001f; // Need to convert getTickCount
if( (cTime - pTime) > (1.0/framerate))
{

/* if the current time, minus the last time the pTime variable was update, is more then 1 / frame rate then update the pTime to the cTime and do your gameplay stuff */


pTime = cTime;
/* This is where you put all your game play stuff */





/* Not past here though. */
}

}





and does anyone know the bbcode to put that stuff in a lil box?
ah ha.

I found my answer. If anyone else finds this thread go here

http://www.gamedev.net/community/forums/topic.asp?topic_id=220793
Quote:Original post by melkor_98
and does anyone know the bbcode to put that stuff in a lil box?


try the tags

Thermo
Quote:Original post by melkor_98
simple....
<br>float cTime,pTime; /* ctime Is the current time, pTime is the previous time*/<br>int framerate = 60; /* frames per a second*/<br>while(1)<br>{<br>cTime = getTickCount() * 0.001f; // Need to convert getTickCount<br>if( (cTime - pTime) &gt; (1.0/framerate))<br>{<br><br> /* if the current time, minus the last time the pTime variable was update, is more then 1 / frame rate then update the pTime to the cTime and do your gameplay stuff */<br><br><br> pTime = cTime;<br> /* This is where you put all your game play stuff */<br><br><br><br><br><br> /* Not past here though. */<br>}<br><br>}<br></code><br><!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE--><br><br>hmm ok can you give an explination of whats going &#111;n? mabe its just a long day but my head is mush.<br><br>also, what is the resolution of gettickcount? can I substitute SDL_GetTicks()?
Hey folks, for the most part i believe i have figured out what i need. Its pieced together from mostly snippets and examples i could find.

Using cone3d i even show FPS using his font.h (thats lesson4 in the tuts)

If you guys feel up to it, can you run my little test app and report the fps it shows and your brief system specs? It just shows a blue rectangle floating back and forth. 800x600x16 fullscreen, double buffered, hwsurface

Thanks !!!! :D


p.s. Its amazing how much better one feels after accomplishing something!

Test App

[Edited by - Panopticon on September 27, 2004 12:24:03 AM]
worst fps: 69
avg fps: 85
best fps: 86

p4 1.8ghz 256mb ram
ati radeon 8500 64mb agp 4x

This topic is closed to new replies.

Advertisement