Sign in to follow this  

c++ SDL, Time step problem

This topic is 2039 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:
[CODE]
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);
}
[/CODE]
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
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
[CODE]
// 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;
[/CODE]

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

This topic is 2039 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this