Thread: time based movement

  1. #1
    Registered User
    Join Date
    May 2002

    Unhappy time based movement

    i'm trying to implement time based movement to make my game run at the same speed on all systems. however i can't seem to get it right. Could someone have a quick look through my code and tell me where i am going wrong? The time code stuff is in the "base.cpp" and "render.cpp" files.

    Thanks for any help u can give me!

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    I have never actually tried to do this, but I do remember seeing an article on it at Game Tutorials. Take a look at the last turorial on this page:

    Hopefully that will help.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    Oct 2002
    Im a novice programmer, and you probably know much more than me. I program with Borland 3.1 so that tells you what type of programs I am creating right now. Anyways, when I want my programs to run the same speed on all computers I use the monitors screen refresh. Most monitors run at the same speed, give or take a little, so may be you can do something like that. Just a thought. Im not sure if it works the same with windows programs, but it seems to work good with dos. Anyways, thats my 2 cents - and thats all it's probably worth, if that. Good luck!
    "The distinction between past, present and future is only an illussion, even if a stunning one."
    -Albert Einstein

  4. #4
    Registered User
    Join Date
    Aug 2001
    To get a program to run at the same speed one system is to calculate the time inbetween frames, then scale movement by that.

    Keeping track of the time between frames is easy, I'll assume you can figure that out.

    Then each time you want to move
    PlayerPosition += speed_val*rate;

    speed_val is the number of units to move in 1 second
    rate = time rate (seconds per frame)

  5. #5
    I have an example on another forum, is it allowed to post links in here?

  6. #6
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Time-based movement is very simple in any game. Essentially, it is this:

    You create your own clock, using the O/S's time manager. For example, lets say you want to draw a frame every 20th of a second.

    You create a callback and have your O/S interrupt (that is, call your callback) every 20th of a second. Inside the callback, you simply set one of your global variables, a "flag" to TRUE. Something to let you know it's okay to draw a frame.

    Then everytime through your program's main code, at some regular interval, you check to see the state of that flag. If it's not TRUE, you don't draw a frame. If it is TRUE, you _do_ draw the frame and then you clear the flag to FALSE.

    On slow machines it would always be TRUE, on fast ones, you'd only draw every 20th of a second.


    Note, you can still miss a frame this way if you happen to miss an interrupt (let's say the system didn't interrupt you because it had a higher-priority interrupt it had to service and yours was sacrificed), but that's not a big deal. Virtually wouldn't happen and if it did, wouldn't really affect game play.

    This is the most common method for regulating game speed in the industry today.
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  2. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  3. Time conversions
    By mariabair in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2004, 04:55 PM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM