Public Group

#### Archived

This topic is now archived and is closed to further replies.

# SDL_GetTicks and frame independent movement - how?

This topic is 5265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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)

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
Every time i try to do fps independant movement, when i press something, the player just dissapears....

##### Share on other sites
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.

##### Share on other sites
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;}

##### Share on other sites
This is weird... When i press UP LEFT or RIGHT nothing happens, but when i press DOWN it goes up. What the hell?

##### Share on other sites
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

1. 1
2. 2
3. 3
4. 4
Rutin
11
5. 5

• 12
• 19
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632665
• Total Posts
3007711
• ### Who's Online (See full list)

There are no registered users currently online

×