Thread: time-based movement?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    time-based movement?

    hi all

    can anyone explain the logics of it.
    i know its used to set the speed to the same on all machines.
    but what is the best way to do this?

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Oke, whe have a variable called time wich holds the time
    the program is running in milliseconds, if you want an object
    to move at a certain speed, you make a variable that is
    time+500 and you check everytime if the time is less or equal
    to time, if so you move your object, then you make the variable
    again time+500 and so on, know what i mean?

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    I'm not quite sure what you need to be clarified.

    You base your animations/physics, etc. on time rather than on how many times the game has looped.

    For instance, if you want a 2D sprite animation to advance frames at a rate of 20 per second, you check how much time has passed since the animation started to see what frame should be displayed.

    If you had based it on the gameloop rather than time, then the speed at which the animation was played would vary.

    The same goes for just about everything else in the game -- walking speed, physics, etc.

    There really is no "best" way, but basically what you want to do is poll the time once per game loop (not multiple times per loop because you want each game loop to be a "freeze-frame" in time). You then compare that time to things like the start of an animation or use it to figure out the new location of a player by adding to his location his direction times his speed times amount of time passed, etc.
    Last edited by Polymorphic OOP; 12-21-2002 at 10:31 AM.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    You use Allegro right?

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    thanks m8,s

    just 1 thing
    what time function can i use to get
    the time in seconds?

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    For simplicity you can use GetTickCount() ( #include<windows.h> ) to get time in milliseconds, or you can use the performance clock.

    That is, of course, only if you are programming for windows, though.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    In seconds i don't know, for milliseconds use time.h and then the
    variable clock() holds the time from the start of the program in
    milliseconds, you'de better use milliseconds anyhow otherwise
    you're have a real slow moving object

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    yes i'm allegro!

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Then you could use Allegro's timers to update movement
    so many times per second

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    yeah but i understand them that well
    btw it worked just fine with the clock function.
    i managed to pause the game aswell when a key is pressed

    but u will have to hold down the key to stay paused any idea how i can fix this
    i've seen a function called stimulate keypress or something i allegro
    should i use that for this pourpuse?
    anyways that would'nt look good

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    You've got a new message, anyway also this can be fix
    using timers, when the key is pressed pause the game,
    then don't check for the pause key another say 300 milliseconds

  12. #12
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Travis Dane
    You've got a new message, anyway also this can be fix
    using timers, when the key is pressed pause the game,
    then don't check for the pause key another say 300 milliseconds
    You don't want to do that, it won't solve the problem. You're best off keeping a flag that holds whether or not the key was down in the last loop and a flag saying the game is currently paused. You pause the game when the person presses the pause key and set the flag saying the key is down and the flag saying that the game is paused and then unpause when the person presses the key again -- however, don't count the press for the pause key again if it was down during the last loop because the person probably just held the button down -- same logic goes for unpausing.

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Yeah, i know that's a better thing, however this is
    undoable or VERY hard to do in allegro, of course you
    could use directx for keyboard input

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i came up with this:




    Code:
    int time1=0;
    
    int time2=time1+500;
    time2=clock();
    
     while(! key[KEY_ESC])
    
     {
    
          time2--;
     while(time2<=time1)
        {
    
            if(time2<=time1)
       {
       time2=time1+500;
       }
    
    //do the drawings and so on...
    }

    is this a good way to do it
    cause im not sure of
    time2=clock();
    gives the right time in
    m.seconds
    i mean will it be the same on any computer?

  15. #15
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You're kinda missing the point. What you did there was not time-based. All you did was poll time once and then went by loop-count instead. With each iteration you should update your time: don't increment the timer manually because a "loop" isn't always the same length.

    Edit: There's actually a lot more wrong with it than that. You might wanna go back rethink your logic on it.
    Last edited by Polymorphic OOP; 12-21-2002 at 02:40 PM.

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