Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Game Speed Consistency  (Read 3420 times)

0 Members and 1 Guest are viewing this topic.

idontmakesense

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Game Speed Consistency
« on: April 14, 2014, 10:53:46 am »
How do I make sure that my game runs at the same speed on all computers? The problem occurred when i transferred my game project from my old laptop to my new one. The game runs too fast and is unplayable.

Game background: Its a small top-down car game with three ai's trying to shoot you down. Game ends when a bullet collides.

If any more information is required I'll be more than happy to share.

int main()
{      
        sf::Clock clock;
        bool end_game= false;
        initialize_cars();
        initialize_bullets();
        initialize_explosion_sprite();
    while (window.isOpen())
    {  
        sf::Event event;
        if (window.pollEvent(event))
        {
            if (event.type== sf::Event::Closed || (event.type== sf::Event::KeyReleased) && (event.key.code== sf::Keyboard::Escape))
                window.close();
        }
                       
        move_cars();
        set_bullet_timer(&clock);
        move_bullets();
       
                end_game= bullet_collision(&clock, &black_car, &blue_bullet);
                if(end_game== true)
                break;
               
                end_game= bullet_collision(&clock, &black_car, &red_bullet);
                if(end_game== true)
                break;
               
                for(int i=0; i<2; i++)
        {
                end_game= bullet_collision(&clock, &black_car, &green_bullet[i]);
                if(end_game== true)
                        break;
        }
        if(end_game== true)
                break;
       
                window.clear(sf::Color::White);
                display_cars();
                display_bullets();
        window.display();
    }
    return 0;
}

Doodlemeat

  • Guest
Re: Game Speed Consistency
« Reply #1 on: April 14, 2014, 10:56:18 am »

4B

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Game Speed Consistency
« Reply #2 on: April 14, 2014, 03:08:29 pm »
You can store
clock.restart().asSeconds();
to a float and pass this to your bullet timer.
multiply your speed(per seconds) * the float(e.g. 0,04). So you can be sure that you move exactly speed/second.

Something else.. You don't need the end_game variable at all and you should make you window-close if-statement smaller.
you can write instead:

//1. There's no need of keyReleased
if(event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)
 window.close();
//2. The less variables you have to initialize above the while, the better people can read your code
if(bullet_collision(...))
 break;
 
If you're going to response to any of my comments, please avoid this smily: ";)"

I'd be grateful, thanks.

Sub

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: Game Speed Consistency
« Reply #3 on: April 15, 2014, 06:05:40 am »
Also, it's a good idea to cap the FPS at something reasonable.  You don't want your game running at 1000 FPS, or something ridiculous.

window.setFramerateLimit(60);

Veritas

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
    • Email
Re: Game Speed Consistency
« Reply #4 on: April 15, 2014, 09:58:19 am »
Also, it's a good idea to cap the FPS at something reasonable.  You don't want your game running at 1000 FPS, or something ridiculous.

window.setFramerateLimit(60);

You may want to manage time using sf::Clock instead of setting the limit using the setFramerateLimit() method. If I recall correctly it uses sf::sleep() which is not precise.
"Lasciate ogni speranza, voi ch'entrate"

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Game Speed Consistency
« Reply #5 on: April 15, 2014, 10:09:02 am »
An even better way to handle this is the vsync: http://www.sfml-dev.org/documentation/2.1/classsf_1_1Window.php#a59041c4556e0351048f8aff366034f61

Vsync is a driver option and therefore preferable over unprecise thread sleep operations.

Veritas

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
    • Email
Re: Game Speed Consistency
« Reply #6 on: April 15, 2014, 10:35:46 am »
An even better way to handle this is the vsync: http://www.sfml-dev.org/documentation/2.1/classsf_1_1Window.php#a59041c4556e0351048f8aff366034f61

Vsync is a driver option and therefore preferable over unprecise thread sleep operations.

Just noting that because Vsync is controlled by the driver, it is not guaranteed to be enabled. The user may have to enable it as a driver option in order for setVerticalSyncEnabled() to work, so maybe it should be a game setting and not the default way to limit the framerate?
« Last Edit: April 15, 2014, 10:37:53 am by Veritas »
"Lasciate ogni speranza, voi ch'entrate"

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Game Speed Consistency
« Reply #7 on: April 15, 2014, 11:07:27 am »
Yeah, that's right. Even though I never saw a driver with vsync disabled globally, your remark is valid.

But I'm not sure whether we are talking about frame drawing rate limit or game loop iteration limit?
It's always the right way to make your game loop independent of time, like the article "Fix your timestep" suggests.

Reducing your drawing rate is only sensible if you have the program running on a mobile device, to prevent cpu load. For this, you could use an easy sleep(1), but don't let it sleep for more time! The more time you let it sleep, the more lag can be induced. See for example http://gamedev.stackexchange.com/questions/18898/is-it-safe-to-use-sleep-in-game-loop-on-windows or http://gamedev.stackexchange.com/questions/62273/what-are-the-benefits-of-capping-frames-per-second-if-any

 

anything