Jump to content
  • Advertisement
Sign in to follow this  
twety94Gmail

c++ SDL, Time step problem

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

Hello.
Im doing a 2d tiled based rpg.
i was fine with per loop movment, but then i had to add a animation to all that, and it dosent look fluent.
So i went exploring and found that i need to find delta time and som other stuff.
So basicly this is my game:

int main(int argc, char* argv[])
{
while(On)
{
FPSTimer = SDL_GetTicks();
LastTime = CurentTime;
CurrentTime = SDL_GetTicks();
SpeedFactor = (CurrentTime - LastTIme) * 1000.0f;
//Events
//MainLogic
//Render
if(1000.0f / Screen_FPS > (SDL_GetTicks() - FpsTimer))
SDL_Delay(1000.0f / Screen_FPS - (SDL_GetTicks()- FpsTimer));
}
}
void Events()
{
x += (MovmentSpeed * speedfactor);
}

Movment speed = 50; and i want to move my "X" hero position by 50 pixels each loop but i never had ani complicated math at school cant figure it out.
Screen_FPS = 60 Edited by twety94Gmail

Share this post


Link to post
Share on other sites
Advertisement
1) A general remark, beware of using Int means you will get rounding errors. You could use float instead on some areas.
2) Call SDL_GetTicks() once at the beginning of you function and use it. In your case it's CurrentTime. Don't call it multiple times (it could return a different result each time)
3) You want your hero to move 50 pixels by second, then

// Considering LastTime and CurrentTime are **seconds**
// It seems that SDL_GetTicks() returns milliseconds, then CurrentTime = ((float)SDL_GetTicks()) / 1000.0f;
float MoveSpeed = 50.0f;
float deltaTime = (LastTime - CurrentTime)
x += MoveSpeed * deltaTime;

Share this post


Link to post
Share on other sites
One other thing is that you shouldn't be using delta time like that anyway. At the very least, physics should ALWAYS be done in fixed time steps.

Share this post


Link to post
Share on other sites
indeed (c.f. http://gafferongames.com/game-physics/fix-your-timestep/)
also, getticks are functions with low precision, you need a hi res timer.
this is always platform specific.
or at least, microsecond precision with gettimeofday (posix).

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!