Jump to content
  • Advertisement

Archived

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

Specchum

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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)

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!