Box2D Forums

It is currently Thu May 23, 2013 8:58 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Decoupled Physics
PostPosted: Sat Aug 08, 2009 11:58 pm 
Offline

Joined: Sat Aug 08, 2009 11:45 pm
Posts: 1
I realize this has been asked multiple times before, and I spent most of today reading the forums and the gaffers article trying to figure it out, so forgive me if this is redundant.

I'm attempting to just do a basic decoupling of the physics from the graphics/fps so that the physics engine runs at essentially the same speed no matter how fast the game loop. This is what I have:

Code:
    const float dt = 0.015f;
    time_t currentTime = time(NULL);
    float accumulator = 0.0f;

   while(device->run()) {

        if (receiver.IsKeyDown(irr::KEY_ESCAPE))
            break;

        if (receiver.IsKeyDown(irr::KEY_SPACE) && totalboxes == 0) {
            worldObj box("box.x", 0.0f, 12.0f, 100.0f, 1.0f, 0.2f, 0.7f);
            boxes.push_back(box);
            totalboxes++;
        }

        time_t newTime = time(NULL);
        time_t deltaTime = difftime(newTime, currentTime);
        currentTime = newTime;

        accumulator += deltaTime;

        while (accumulator>=dt) {
              world->Step(dt, 8, 1);
              accumulator -= dt;
         }
    }


It works, sort of. The simulation seems to run at the same speed independent of the frame rate, but it does so in a very "jumpy" way. In that, a falling box will fall in large steps and not a smooth falling animation.

If anyone is willing to further explain this process, and perhaps show me what is wrong, I would be most grateful.

Thanks.


Top
 Profile  
 
 Post subject: Re: Decoupled Physics
PostPosted: Sun Aug 09, 2009 2:14 am 
Offline

Joined: Mon Jan 07, 2008 10:51 am
Posts: 1911
You're physics step is at 66hz. That means if you are getting a graphical framerate between 33 adn 66, which is likely, your simulation will be either 1 or two physics steps per frame. Which can look jerky. The gaffer article recommends interpolation to fix this, but, if you can handle variable length time steps, I prefer

Code:
accumulator += deltaTime;

        while (accumulator>=dt) {
              world->Step(dt, 8, 1);
              accumulator -= dt;
         }
         world->Step(accumulator, 8, 1);
         accumulator = 0;


Which is essentially making Box2D do the interpolation step for you as an extra physics step.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group