GetMassData() public method

Get the mass data for this fixture. The mass data is based on the density and the shape. The rotational inertia is about the shape's origin.
public GetMassData ( MassData massData ) : void
massData Box2D.Collision.Shapes.MassData
return void
Example #1
0
        /// <summary>
        /// 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.
        /// </summary>
        public void ResetMassData()
        {
            // Compute mass data from shapes. Each shape has its own density.
            Mass    = 0.0f;
            InvMass = 0.0f;
            I       = 0.0f;
            InvI    = 0.0f;
            Sweep.LocalCenter.SetZero();

            // Static and kinematic bodies have zero mass.
            if (m_type == BodyType.Static || m_type == BodyType.Kinematic)
            {
                // m_sweep.c0 = m_sweep.c = m_xf.position;
                Sweep.C0.Set(Xf.P);
                Sweep.C.Set(Xf.P);
                Sweep.A0 = Sweep.A;
                return;
            }

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

            // Accumulate mass over all fixtures.
            Vec2 localCenter = World.Pool.PopVec2();

            localCenter.SetZero();
            Vec2     temp     = World.Pool.PopVec2();
            MassData massData = pmd;

            for (Fixture f = FixtureList; f != null; f = f.Next)
            {
                if (f.Density == 0.0f)
                {
                    continue;
                }
                f.GetMassData(massData);
                Mass += massData.Mass;
                // center += massData.mass * massData.center;
                temp.Set(massData.Center).MulLocal(massData.Mass);
                localCenter.AddLocal(temp);
                I += massData.I;
            }

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

            if (I > 0.0f && (Flags & TypeFlags.FixedRotation) == 0)
            {
                // Center the inertia about the center of mass.
                I -= Mass * Vec2.Dot(localCenter, localCenter);
                Debug.Assert(I > 0.0f);
                InvI = 1.0f / I;
            }
            else
            {
                I    = 0.0f;
                InvI = 0.0f;
            }

            Vec2 oldCenter = World.Pool.PopVec2();

            // Move center of mass.
            oldCenter.Set(Sweep.C);
            Sweep.LocalCenter.Set(localCenter);
            // m_sweep.c0 = m_sweep.c = Mul(m_xf, m_sweep.localCenter);
            Transform.MulToOutUnsafe(Xf, Sweep.LocalCenter, Sweep.C0);
            Sweep.C.Set(Sweep.C0);

            // Update center of mass velocity.
            // m_linearVelocity += Cross(m_angularVelocity, m_sweep.c - oldCenter);
            temp.Set(Sweep.C).SubLocal(oldCenter);

            Vec2 temp2 = oldCenter;

            Vec2.CrossToOutUnsafe(m_angularVelocity, temp, temp2);
            m_linearVelocity.AddLocal(temp2);

            World.Pool.PushVec2(3);
        }