Thread: good time control system

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    good time control system

    my game right now has a main loop like this:

    Code:
    PHP Code:
    do {        //check input functions//        //update player position//        //update computer logic (moveX, moveY)//        //update computer position (xPos+=moveX, etc.//                //clear screen//        //draw background//        //draw player//        //draw computer//        //flip screen// } while(!gameOver); 
    Where in all that do i put in something to control speed? How can i do this? (I've never made a large arcade type game before, so I've never really faced this problem)

  2. #2
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    To control the speed of the input or control the speed of the player movement? I guess you would want to do them both equaly, so I would put a call to delay before the closing brace in the do...while statement.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    4
    I don't know much about programming but I can tell you you can try to use the

    Sleep(miliseconds);

    function located in the <windows.h>. It will give you a delay in between your commands, and it's what I used for animation.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    i was looking for something that'd keep the game speed at a relative constant

    anybody ever done this?

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    If you are using the SDL look at SDL_AddTimer or you could
    try something like

    Code:
    void Something::update()
    {
           Uint32 time_now = SDL_GetTicks();
           double delta_time = (time_stamp - time_now) / 1000.0;
           time_stamp = time_now;
    
           x += xvel * delta_time;
           y += yvel * delta_time;
    }
    Calling SDL_Delay, I've found, usually makes the game too slugish.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    how's this?

    ok so my speeds were real low (originally pixel by pixel so they were like 10) and I got the following suggestion:

    xPos += (((SDL_GetTicks()-last_time)/1000.0)*xVel);

    but b/c of my speeds being low it was real slow so instead of multipling each speed by about 200 (which i found gave me a desired speed) i did this:

    xPos += (((SDL_GetTicks()-last_time)/1000.0)*xVel*200);

    which i later simplified to this:

    xPos += (((SDL_GetTicks()-last_time)/50.0)*xVel);

    is this a decent solution?

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    If you have xvel=10 and the game is set in 800x600 it should take the main charecter a minute to move across the screen. But what actually happens is that SDL_GetTicks()-last_time)/1000.0)*xVel
    is less than one and since the x and y coords are ints
    the charecter ends up standing still.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    not really

    i changed the x and y coords to doubles for this, but should it work then?

  9. #9
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Try floats since SDL_GetTicks only is in milliseconds and you don't really need to much precision anyways.
    Also see how fast the game goes between updates and
    print out xvel * delta_time and so on. 10 pixels is pretty slow so try like 60 pixels per second.

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    thanks for helping out

    I've got it running at good speed now, It ran the same on the P3 800MHz, the P2 233MHz, and another P3 that was a bit slower (400 i think, but i'm not sure).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting system time with better than a second resolution
    By sigfriedmcwild in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2006, 08:05 AM
  2. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  3. Cprogramming System Time
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-06-2003, 02:28 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Passing system time to a function
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 01:56 PM