Thread: SDL C++ Time based movements

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55

    SDL C++ Time based movements

    Hello Folks!

    I try to build a little game with SDL. But i cant figure out one thing. I need to make time based movements so the game would run on the same speed at all different types of PCs , I started looking on google at time based movements but all the examples where a little confusing for me. So my question is: you know know how to make time based movements with SDL?

    OS: Linux.
    Language: C++


    Thanks in advance.
    The Programming Dutchman

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Compute the time delta between the last frame and the current one. Use that time delta in all movement calculations.

    Note also that any constant speed values must be adjusted as well. So if you want to move 50.0f units at 16.66667 ms per frame and you are getting 32.66667 ms per frame you will need to adjust the value accordingly.
    Last edited by VirtualAce; 06-12-2010 at 09:52 AM.

  3. #3
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Wow, uhmm maybe i don't get it because my english not so good, but what is delta time?

    if you want to, could you provide a a little example code?

    sorry..... :$

    btw: thanks for you fast reply!

    Jelte.
    The Programming Dutchman

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Delta means difference:
    Code:
    delta(t) = t2 - t1
    Generally when you want your program to be CPU speed independent you need to make sure all your operations execute in a specific amount of time.
    Code:
    while()
    {
    ...
    if (GetCurrentTime() - tLastExecution >= tINTERVAL)
    {
    //do something example: move characters
    tLastExecution = GetCurrentTime();
    }
    [edit]
    //other operations can be performed with a ratio to main interval amount
    if (GetCurrentTime() - tBeeMovementLastExecution >=( tINTERVAL/4))
    {
    //do something example: move bee
    tBeeMovementLastExecution = GetCurrentTime();
    }
    }
    Last edited by siavoshkc; 06-12-2010 at 10:52 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    This is a good tutorial about the subject: Fix Your Timestep! « Gaffer on Games

  6. #6
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Thanks! that would solve my problem!

    Jelte.
    The Programming Dutchman

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. Time conversions
    By mariabair in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2004, 04:55 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM