SDL_GetTicks and frame independent movement - how?

Started by
29 comments, last by Specchum 19 years, 12 months ago
I''d like to have frame independent movement in my 2-d game being coded using SDL. I know that I should be using SDL_GetTicks to calculate the frames per second. In fact I have done that and I have the value for frames per second and everything, but erm... how do I tie this in with movement along x and y axis? "There is no dark side of the moon." - Pink Floyd
"There is no dark side of the moon." - Pink Floyd
Advertisement
Say you want to move 50 pixels per second, and have 50 FPS. So how many pixels per frame you have to move?

Usually you save the last update time, and on the next update you calculate the delta time.
Say you want to have a speed of 30 pixels per second, so you multiply those 30 with your delta-time, resulting to a frame independent movement. (Make sure you don''t use integers as you probably will end up with non integer movement rates)
-----The scheduled downtime is omitted cause of technical problems.
You mean something like this:

time = SDL_GetTicks()

some lines of code....

time_diff = SDL_GetTicks() - time

time = SDL_GetTicks()

hero.x = hero.x + (30 * time_diff)

where hero.x is the movement of the hero along x axis.

so, frames per second isn''t required for computation in this case?

"There is no dark side of the moon." - Pink Floyd
"There is no dark side of the moon." - Pink Floyd
Thats right, simple physics tells us...

Distance = Speed x Time

Therefore the amount you move your character should be the desired speed multiplied by the time since the last update.
usually you will do:

actualTime = getTime();
hero.x += (30 * (lastUpdate - actualTime));
...
lastUpdate = actualTime;
return;


so this will make your hero walk 30 pixels per second.
-----The scheduled downtime is omitted cause of technical problems.
Every time i try to do fps independant movement, when i press something, the player just dissapears....
Your probably factoring in the time wrong. Draw a square on the screen that changes colors every second, if it is blinking too fast, then you''ve found the problem.
Note that functions like SDL_GetTicks returns the time in millseconds since SDL was initialized, so if you want to calculate the velocity in terms of units moved per second, you''ll have to divide this value by 1000.

Uint32 timeMS = SDL_GetTicks();Uint32 oldTimeMS;Uint32 frameTimeMS;float  frameTime;while (gameIsRunning){    oldTimeMS = timeMS;    timeMS = SDL_GetTicks();    frameTimeMS = timeMS - oldTimeMS;    frameTime = (float)timeMS / 1000.0f;    // to use this to calculate speed is the same    position += velocity * frameTime;} 

This is weird... When i press UP LEFT or RIGHT nothing happens, but when i press DOWN it goes up. What the hell?
Thanks for the replies guys! I''m off to check it out!

Zaddras, post your code if you wish for help! Shooting in the dark isn''t going to help!

"There is no dark side of the moon." - Pink Floyd
"There is no dark side of the moon." - Pink Floyd

This topic is closed to new replies.

Advertisement