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 (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 (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?
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).