Skip to main content
GameDev.net gamedev.net
🔒 Locked

c++ SDL, Time step problem

Started by twety94Gmail May 6, 2012 at 5:50 PM 3 replies 4.6k views
Original Post
twety94Gmail
twety94Gmail
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
peous
peous
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;
twety94Gmail
twety94Gmail
Thanks peous for help.
i knew its something simple, chouldnt figure it out.
Storyyeller
Storyyeller
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.
I trust exceptions about as far as I can throw them.
Lightness1024
Lightness1024
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).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.