Пример #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.
            _mass              = 0.0f;
            _invMass           = 0.0f;
            _I                 = 0.0f;
            _invI              = 0.0f;
            _sweep.localCenter = Vector2.Zero;

            // Static and kinematic bodies have zero mass.
            if (_type == BodyType.Static || _type == BodyType.Kinematic)
            {
                _sweep.c0 = _sweep.c = _xf.Position;
                return;
            }

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

            // Accumulate mass over all fixtures.
            Vector2 center = Vector2.Zero;

            for (Fixture f = _fixtureList; f != null; f = f._next)
            {
                if (f._density == 0.0f)
                {
                    continue;
                }

                MassData massData;
                f.GetMassData(out massData);
                _mass  += massData.mass;
                center += massData.mass * massData.center;
                _I     += massData.I;
            }

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

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

                Debug.Assert(_I > 0.0f);
                _invI = 1.0f / _I;
            }
            else
            {
                _I    = 0.0f;
                _invI = 0.0f;
            }

            // Move center of mass.
            Vector2 oldCenter = _sweep.c;

            _sweep.localCenter = center;
            _sweep.c0          = _sweep.c = MathUtils.Multiply(ref _xf, _sweep.localCenter);

            // Update center of mass velocity.
            _linearVelocity += MathUtils.Cross(_angularVelocity, _sweep.c - oldCenter);
        }