Jump to content

  • Log In with Google      Sign In   
  • Create Account


c++ SDL, Time step problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 twety94Gmail   Members   -  Reputation: 115

Like
0Likes
Like

Posted 06 May 2012 - 11:50 AM

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, 06 May 2012 - 11:51 AM.


Sponsor:

#2 peous   Members   -  Reputation: 182

Like
1Likes
Like

Posted 08 May 2012 - 09:58 AM

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;


#3 twety94Gmail   Members   -  Reputation: 115

Like
0Likes
Like

Posted 09 May 2012 - 01:47 PM

Thanks peous for help.
i knew its something simple, chouldnt figure it out.

#4 Storyyeller   Members   -  Reputation: 212

Like
0Likes
Like

Posted 13 May 2012 - 07:37 AM

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.

#5 Lightness1024   Members   -  Reputation: 736

Like
0Likes
Like

Posted 15 May 2012 - 06:16 AM

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).




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS