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

Author Topic: [Noob Question] FPS & TimeStep & box2D  (Read 3095 times)

0 Members and 1 Guest are viewing this topic.

CytraL

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • GitHub
[Noob Question] FPS & TimeStep & box2D
« 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 :\
dev@redneboa.es | WordPress | GitHub | YouTube

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
[Noob Question] FPS & TimeStep & box2D
« Reply #1 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

CytraL

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • GitHub
[Noob Question] FPS & TimeStep & box2D
« Reply #2 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
Move with Keys: A-D-SPACE-MOUSE1
dev@redneboa.es | WordPress | GitHub | YouTube

DroomNop

  • Newbie
  • *
  • Posts: 5
    • View Profile
[Noob Question] FPS & TimeStep & box2D
« Reply #3 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.