Jump to content
  • Advertisement
Sign in to follow this  
Panopticon

FPS independant movement question

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

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]

Share this post


Link to post
Share on other sites
Advertisement
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



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by melkor_98
simple....
[code lang=cpp]
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. */
}

}



hmm ok can you give an explination of whats going on? mabe its just a long day but my head is mush.

also, what is the resolution of gettickcount? can I substitute SDL_GetTicks()?

Share this post


Link to post
Share on other sites
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]

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!