SFML community forums

Help => General => Topic started by: CytraL on July 30, 2010, 01:49:53 pm

Title: [Noob Question] FPS & TimeStep & box2D
Post by: CytraL on July 30, 2010, 01:49:53 pm
Hi.... sorry for this very noob question but... i'm starting with "Game Programming" and  for me more algoritms are confuse...

Ok... i have problems when i try sync box2D World with the "Game Loop"... SFML works fine... but box2D update positions & forces very fast in Linux and ~fast in Windows.....


TIMESTEP DECLARATION:
Code: [Select]
const float TIMESTEP = 1.0f / 60.0f;

GAMELOOP:
Code: [Select]

        //Update Box2D
        if (time_step_box2D.GetElapsedTime() >= 0.001)
        {
            world.world->Step(TIMESTEP, 10);
            time_step_box2D.Reset();
        }
        //updatePhysics();

        //Update Camera Pos
        camera.move();

        if (!world.paused && time_step.GetElapsedTime() >= TIMESTEP) {
            //world.world->Step(TIMESTEP, 10);
            //screen.Clear();

            draw_game();
            draw_player_hud();

            screen.Display();
            time_step.Reset();
        }

        //CPU by happy :)
        #ifndef __WIN32
            usleep(1);
        #else
            Sleep(1);
        #endif




Sry for my bad english :\
Title: [Noob Question] FPS & TimeStep & box2D
Post by: Nexus on July 30, 2010, 03:24:36 pm
Code: [Select]
       //CPU by happy :)
        #ifndef __WIN32
            usleep(1);
        #else
            Sleep(1);
        #endif
Why don't you use sf::Sleep(), but with a parameter adapted to the frame time?

Anyway, the easiest approach is still to call sf::RenderWindow::SetFramerateLimit() with a desired value.
Title: [Noob Question] FPS & TimeStep & box2D
Post by: CytraL on July 30, 2010, 05:16:12 pm
Ok now i have this:

LIMIT FRAME RATE:
Code: [Select]
screen.SetFramerateLimit(60);

RATIO PIXELS:
Code: [Select]
const float RATIO = 30.0f;

GAMELOOP:
Code: [Select]

        if (!world.paused)
        {
            world.world->Step(screen.GetFrameTime(), 10);

            camera.move();

            draw_game();
            screen.Display();
        }

        sf::Sleep(0.01);


And now.. works very slow (Similar to play in the Moon).... ¿How Sync box2D???? :S

Download Demo Game Win x86: Triog v0.1.1.2a (http://filebeam.com/58aaa6b80b441e511ed5606c544abb32)
Move with Keys: A-D-SPACE-MOUSE1
Title: [Noob Question] FPS & TimeStep & box2D
Post by: DroomNop on July 30, 2010, 10:21:01 pm
Can't help but I wanted to say I like it  :o The only issue is that sometimes it's not possible to jump while walking.