Example #1
0
    /// This resets the mass properties to the sum of the mass properties of the fixtures.
    /// This normally does not need to be called unless you called SetMassData to override
    /// the mass and you later want to reset the mass.
    public void ResetMassData()
    {
        // Compute mass data from shapes. Each shape has its own density.
        m_mass    = 0.0f;
        m_invMass = 0.0f;
        m_I       = 0.0f;
        m_invI    = 0.0f;
        m_sweep.localCenter.SetZero();

        // Static and kinematic bodies have zero mass.
        if (m_type == BodyType.b2_staticBody || m_type == BodyType.b2_kinematicBody)
        {
            m_sweep.c0 = m_xf.p;
            m_sweep.c  = m_xf.p;
            m_sweep.a0 = m_sweep.a;
            return;
        }

        Debug.Assert(m_type == BodyType.b2_dynamicBody);

        // Accumulate mass over all fixtures.
        b2Vec2 localCenter = new b2Vec2();

        for (b2Fixture f = m_fixtureList; f != null; f = f.m_next)
        {
            if (f.m_density == 0.0f)
            {
                continue;
            }

            b2MassData massData = new b2MassData();
            f.GetMassData(ref massData);
            m_mass      += massData.mass;
            localCenter += massData.mass * massData.center;
            m_I         += massData.I;
        }

        // Compute center of mass.
        if (m_mass > 0.0f)
        {
            m_invMass    = 1.0f / m_mass;
            localCenter *= m_invMass;
        }
        else
        {
            // Force all dynamic bodies to have a positive mass.
            m_mass    = 1.0f;
            m_invMass = 1.0f;
        }

        if (m_I > 0.0f && (m_flags & BodyFlags.e_fixedRotationFlag) == 0)
        {
            // Center the inertia about the center of mass.
            m_I -= m_mass * Utils.b2Dot(localCenter, localCenter);
            Debug.Assert(m_I > 0.0f);
            m_invI = 1.0f / m_I;
        }
        else
        {
            m_I    = 0.0f;
            m_invI = 0.0f;
        }

        // Move center of mass.


        b2Vec2 oldCenter = new b2Vec2(m_sweep.c);


        m_sweep.localCenter = localCenter;
        m_sweep.c0          = m_sweep.c = Utils.b2Mul(m_xf, m_sweep.localCenter);

        // Update center of mass velocity.
        m_linearVelocity += Utils.b2Cross(m_angularVelocity, m_sweep.c - oldCenter);
    }