I'm using this implementation right now and it's working so far. Except for some reason the begin and end contact methods are being called in the same step. So, I can't use begin and end contacts for any purpose correctly. Is there a reason it might be doing that? I noticed a consumecontacts() function that is commented out, I'm wondering if it might have something to do with that.
This is my implementation right now
PhysicsSystem::PhysicsSystem (): fixedTimestepAccumulator_ (0),velocityIterations_(8), positionIterations_(1), fixedTimestepAccumulatorRatio_ (0) {
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
bool doSleep = true;
world_ = new b2World(gravity, doSleep);
world_->SetAutoClearForces (false);
}
void PhysicsSystem::update (float dt) {
Maximum number of steps, to avoid degrading to an halt.
const int MAX_STEPS = 6;
fixedTimestepAccumulator_ += dt;
const int nSteps = static_cast<int> ( std::floor (fixedTimestepAccumulator_ / FIXED_TIMESTEP));
// To avoid rounding errors, touches fixedTimestepAccumulator_ only
// if needed.
if (nSteps > 0) {
fixedTimestepAccumulator_ -= nSteps * FIXED_TIMESTEP;
}
assert ("Accumulator must have a value lesser than the fixed time step" &&
fixedTimestepAccumulator_ < FIXED_TIMESTEP + FLT_EPSILON
);
fixedTimestepAccumulatorRatio_ = fixedTimestepAccumulator_ / FIXED_TIMESTEP;
// This is similar to clamp "dt":
// dt = std::min (dt, MAX_STEPS * FIXED_TIMESTEP)
// but it allows above calculations of fixedTimestepAccumulator_ and
// fixedTimestepAccumulatorRatio_ to remain unchanged.
const int nStepsClamped = std::min (nSteps, MAX_STEPS);
for (int i = 0; i < nStepsClamped; ++ i) {
if (i == nStepsClamped - 1) {
resetSmoothStates_ ();
}
singleStep_ (FIXED_TIMESTEP);
}
world_->ClearForces ();
}
void PhysicsSystem::singleStep_ (float dt) {
//updateControllers_ (dt);
world_->Step (dt, velocityIterations_, positionIterations_);
//consumeContacts_ ();
}