ComputeMass() public method

Compute the mass properties of this shape using its dimensions and density. The inertia tensor is computed about the local origin, not the centroid.
public ComputeMass ( MassData &massData ) : void
massData Box2DX.Collision.MassData Returns the mass data for this shape.
return void
Esempio n. 1
0
        // TODO_ERIN adjust linear velocity and torque to account for movement of center.
        /// <summary>
        /// Compute the mass properties from the attached shapes. You typically call this
        /// after adding all the shapes. If you add or remove shapes later, you may want
        /// to call this again. Note that this changes the center of mass position.
        /// </summary>
        public void SetMassFromShapes()
        {
            Box2DXDebug.Assert(_world._lock == false);
            if (_world._lock == true)
            {
                return;
            }

            // Compute mass data from shapes. Each shape has its own density.
            _mass    = 0.0f;
            _invMass = 0.0f;
            _I       = 0.0f;
            _invI    = 0.0f;

            Vector2 center = Vector2.zero;

            for (Fixture f = _fixtureList; f != null; f = f.Next)
            {
                MassData massData;
                f.ComputeMass(out massData);
                _mass  += massData.Mass;
                center += massData.Mass * massData.Center;
                _I     += massData.I;
            }

            // Compute center of mass, and shift the origin to the COM.
            if (_mass > 0.0f)
            {
                _invMass = 1.0f / _mass;
                center  *= _invMass;
            }

            if (_I > 0.0f && (_flags & BodyFlags.FixedRotation) == 0)
            {
                // Center the inertia about the center of mass.
                _I -= _mass * Vector2.Dot(center, center);
                Box2DXDebug.Assert(_I > 0.0f);
                _invI = 1.0f / _I;
            }
            else
            {
                _I    = 0.0f;
                _invI = 0.0f;
            }

            // Move center of mass.
            _sweep.LocalCenter = center;
            _sweep.C0          = _sweep.C = _xf.TransformPoint(_sweep.LocalCenter);

            BodyType oldType = _type;

            if (_invMass == 0.0f && _invI == 0.0f)
            {
                _type = BodyType.Static;
            }
            else
            {
                _type = BodyType.Dynamic;
            }

            // If the body type changed, we need to refilter the broad-phase proxies.
            if (oldType != _type)
            {
                for (Fixture f = _fixtureList; f != null; f = f.Next)
                {
                    f.RefilterProxy(_world._broadPhase, _xf);
                }
            }
        }