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

Author Topic: Fixed timestep issue with box2d and sfml  (Read 2638 times)

0 Members and 1 Guest are viewing this topic.

sun

  • Newbie
  • *
  • Posts: 9
    • View Profile
Fixed timestep issue with box2d and sfml
« on: June 01, 2014, 09:04:30 pm »
I've been recently attempting to create a simple example game using both sfml and box2d. To get simple framework together, I wanted to implement a fixed timestep and I've pretty much based the fixed timestep code off this http://www.unagames.com/blog/daniele/2010/06/fixed-time-step-implementation-box2d . At the moment its simple demo with a square box that you can move with wasd keys. The problem is that when ever I attempt to move the square, I get this odd blur on the square (almost like its stretching) whilst moving. I've tried a few things from this forum and even attempting to just set the frame rate to 60, but it doesn't seem to change any thing.

Below is my fixed timestep:
void B2DWorld::update(float dt, ActionController<std::string>& actionController){
    m_fixedTimestepAccumulator += dt;
    const int steps = floor(m_fixedTimestepAccumulator / FIXED_TIMESTEP);

    if (steps > 0)
        {
                m_fixedTimestepAccumulator -= steps * FIXED_TIMESTEP;
        }

        assertAccumilation();
    m_fixedTimestepAccumulatorRatio = m_fixedTimestepAccumulator / FIXED_TIMESTEP;

    const int clampedSteps = std::min(steps, MAX_STEPS);
        for (int i = 0; i < clampedSteps; ++ i)
        {
                resetStates();
                actionController.triggerCallbacks(m_fixedTimestepAccumulatorRatio);
                step(FIXED_TIMESTEP);
        }

        interpolateStates();
        m_world.DrawDebugData();

}

void B2DWorld::step(float dt){
    m_world.Step(dt, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
}

void B2DWorld::interpolateStates(){

        const float oneMinusRatio = 1.0f - m_fixedTimestepAccumulatorRatio;

        for (b2Body * b = m_world.GetBodyList (); b != NULL; b = b->GetNext ())
        {
                if (b->GetType () == b2_staticBody)
                {
                        continue;
                }

                PhysicsComponent *c   = (PhysicsComponent*) b->GetUserData();
                c->smoothedPosition =
                m_fixedTimestepAccumulatorRatio * b->GetPosition () +
                oneMinusRatio * c->previousPosition;
                c->smoothedAngle =
                m_fixedTimestepAccumulatorRatio * b->GetAngle () +
                oneMinusRatio * c->previousAngle;
        }

}
 

Here's the full code: https://github.com/SundeepK/Clone/tree/wip/fix_time_step
Any suggestions on what be causing the issue or other example code/projects that implement fixed timestep with sfml?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Fixed timestep issue with box2d and sfml
« Reply #1 on: June 01, 2014, 10:04:35 pm »
My guess would be that the square is at non-integer coordinates.

sun

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Fixed timestep issue with box2d and sfml
« Reply #2 on: June 01, 2014, 10:21:07 pm »
My guess would be that the square is at non-integer coordinates.

If I understand correctly, you mean something like this:

        rect.setPosition(sf::Vector2f(floor(c->smoothedPosition.x*30-10),floor(c->smoothedPosition.y*30-10)));
 

Unfortuanly, that didn't fix the issue. Also I forgot to mention, I'm using 64bit ubuntu 13.04. Oh and the problem seems worse when using interpolated values to set the rect position versus using the box2d GetPosition (although you can still see the problem).
« Last Edit: June 01, 2014, 10:36:16 pm by sun »

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: Fixed timestep issue with box2d and sfml
« Reply #3 on: June 04, 2014, 07:33:47 am »
First off, I'd try it without the timestep, and see what happens.  If you still get artifacts, then maybe it's a vsync problem.  If you're running in fullscreen, try enabling vsync, or else try running in a window.

This line of code could also be a culprit.
Code: [Select]
const int clampedSteps = std::min(steps, MAX_STEPS);If MAX_STEPS is ever limiting steps, then you're going to get jittery movement.

What value for FIXED_TIMESTEP are you using?


« Last Edit: June 04, 2014, 12:44:53 pm by Jabberwocky »

sun

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Fixed timestep issue with box2d and sfml
« Reply #4 on: June 04, 2014, 09:54:48 pm »
First off, I'd try it without the timestep, and see what happens.  If you still get artifacts, then maybe it's a vsync problem.  If you're running in fullscreen, try enabling vsync, or else try running in a window.

This line of code could also be a culprit.
Code: [Select]
const int clampedSteps = std::min(steps, MAX_STEPS);If MAX_STEPS is ever limiting steps, then you're going to get jittery movement.

What value for FIXED_TIMESTEP are you using?

I've already tried everything you've said and still looks odd. It isn't jittery, more the fact that the movable square looks blurry and stretched when moving.  So it's not all that jittery. FIXED_TIMESTEP = 1f/60f and MAX_STEPS=5. changing these values doesn't change much. I've also already tried using setframerate to 60 but that hasn't done a thing.

I'm begging to think that it may be a platform thing? I may give sdl a go and see how things look, though I really much rather stick with sfml.

sun

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Fixed timestep issue with box2d and sfml
« Reply #5 on: June 06, 2014, 10:10:23 pm »
I've attached an executable (should run on linux). You'll need to have SFML, Box2d  and opengl installed. Could any one give this a go and see if they can see any issues (seems more noticeable in full screen).  Thanks.

https://www.dropbox.com/s/dzf4f8w6f6fl1o0/FixedTimeStepTest.rar

sun

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Fixed timestep issue with box2d and sfml
« Reply #6 on: June 07, 2014, 08:29:56 pm »
I've also now created a visual studio project (2010) that also shows the same problem:

https://www.dropbox.com/s/ghazj8tqyppk8v9/sfml%20test.zip

I guess it's not specific to linux, I might try and re-write it using just opengl with sfml and see what happens  :-\

 

anything