Box2DX.Dynamics.Body.SetMass C# (CSharp) Method

SetMass() public method

Set the mass properties. Note that this changes the center of mass position. If you are not sure how to compute mass properties, use SetMassFromShapes. The inertia tensor is assumed to be relative to the center of mass.
public SetMass ( MassData massData ) : void
massData Box2DX.Collision.MassData The mass properties.
return void
		public void SetMass(MassData massData)
		{
			Box2DXDebug.Assert(_world._lock == false);
			if (_world._lock == true)
			{
				return;
			}

			_invMass = 0.0f;
			_I = 0.0f;
			_invI = 0.0f;

			_mass = massData.Mass;

			if (_mass > 0.0f)
			{
				_invMass = 1.0f / _mass;
			}

			_I = massData.I;

			if (_I > 0.0f && (_flags & BodyFlags.FixedRotation) == 0)
			{
				_invI = 1.0f / _I;
			}

			// Move center of mass.
			_sweep.LocalCenter = massData.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);
				}
			}
		}

Usage Example

Example #1
0
        private void InitPhysics(BattleArena arena)
        {
            // Define the dynamic body. We set its position and call the body factory.
            BodyDef bodyDef = new BodyDef();
            bodyDef.Position.Set(0.0f, 4.0f);
            body = arena.world.CreateBody(bodyDef);

            // Define another box shape for our dynamic body.
            PolygonDef shapeDef = new PolygonDef();
            shapeDef.SetAsBox(Pokemon.Width / 2, Pokemon.Height / 2);

            // Set the box density to be non-zero, so it will be dynamic.
            shapeDef.Density = 1.0f;

            // Override the default friction.
            //shapeDef.Friction = 2f;

            // Add the shape to the body.
            Fixture fix = body.CreateFixture(shapeDef);
            fix.Density = 1;
            fix.Friction = 0.3f;
            fix.Restitution = 0.1f;
            fix.UserData = this;
            fix.Filter.GroupIndex = this.ID;
            fix.Filter.CategoryBits = 0x0002;
            if (Pokemon.Types.Contains(PokemonType.Ghost)){
                fix.Filter.MaskBits = 0xFFFF - 0x0002;
            } else {
                fix.Filter.MaskBits = 0xFFFF;
            }

            // Now tell the dynamic body to compute it's mass properties base
            // on its shape.
            var mass = body.GetMassData();
            mass.Mass = Pokemon.Weight;
            body.SetMass(mass);

            body.SetFixedRotation(true);
        }