Body may stay in air after body below it is destroyed

Consider this:

Image

  1. Body A is on top of body B.
  2. The time in which both bodies fall asleep is much longer than b2_bodyToSleep. (Not sure if it's normal, but it happens in our games, and merely not sleeping shouldn't cause such devastating bugs.)
  3. After b2_bodyToSleep has passed but before the bodies fall asleep, body B is destroyed.
  4. The contact between bodies A and B is destroyed.
  5. Both bodies are waken up.
    Code:
    void b2Contact::Destroy(b2Contact* contact, b2BlockAllocator* allocator)
    {
            b2Assert(s_initialized == true);

            b2Fixture* fixtureA = contact->m_fixtureA;
            b2Fixture* fixtureB = contact->m_fixtureB;

            if (contact->m_manifold.pointCount > 0 &&
                    fixtureA->IsSensor() == false &&
                    fixtureB->IsSensor() == false)
            {
                    fixtureA->GetBody()->SetAwake(true);
                    fixtureB->GetBody()->SetAwake(true);
            }

            b2Shape::Type typeA = fixtureA->GetType();
            b2Shape::Type typeB = fixtureB->GetType();

            b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
            b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);

            b2ContactDestroyFcn* destroyFcn = s_registers[typeA][typeB].destroyFcn;
            destroyFcn(contact, allocator);
    }

    Code:
    inline void b2Body::SetAwake(bool flag)
    {
            if (flag)
            {
                    if ((m_flags & e_awakeFlag) == 0)
                    {
                            m_flags |= e_awakeFlag;
                            m_sleepTime = 0.0f;
                    }
            }
            else
            {
                    m_flags &= ~e_awakeFlag;
                    m_sleepTime = 0.0f;
                    m_linearVelocity.SetZero();
                    m_angularVelocity = 0.0f;
                    m_force.SetZero();
                    m_torque = 0.0f;
            }
    }
  6. No effect as both bodies are already awake.
  7. In the next world step, b2Island::Solve() notices that the position is solved and that body A hasn't slept for longer than b2_bodyToSleep. In just one step, gravity hasn't speeded up the body enough to reset the sleep timer. Thus, b2Island::Solve() puts body A to sleep.
    Code:
    if (allowSleep)
    {
            float32 minSleepTime = b2_maxFloat;

            const float32 linTolSqr = b2_linearSleepTolerance * b2_linearSleepTolerance;
            const float32 angTolSqr = b2_angularSleepTolerance * b2_angularSleepTolerance;

            for (int32 i = 0; i < m_bodyCount; ++i)
            {
                    b2Body* b = m_bodies[i];
                    if (b->GetType() == b2_staticBody)
                    {
                            continue;
                    }

                    if ((b->m_flags & b2Body::e_autoSleepFlag) == 0 ||
                            b->m_angularVelocity * b->m_angularVelocity > angTolSqr ||
                            b2Dot(b->m_linearVelocity, b->m_linearVelocity) > linTolSqr)
                    {
                            b->m_sleepTime = 0.0f;
                            minSleepTime = 0.0f;
                    }
                    else
                    {
                            b->m_sleepTime += h;
                            minSleepTime = b2Min(minSleepTime, b->m_sleepTime);
                    }
            }

            if (minSleepTime >= b2_timeToSleep && positionSolved)
            {
                    for (int32 i = 0; i < m_bodyCount; ++i)
                    {
                            b2Body* b = m_bodies[i];
                            b->SetAwake(false);
                    }
            }
    }
  8. As a result, body A floats in the air until something collides with it.

And yes, this happened in practice. In one specific situation it was about 50 % reproducible.

The attached patch implements ability to reset the sleep timer and resets it whenever a contact is destroyed. That keeps the body from getting asleep right when it's about to fall.

Original thread: https://box2d.org/forum/viewtopic.php?t=9688