public void ResetMassData()
{
// Compute mass data from shapes. Each shape has its own density.
_mass = 0.0f;
InvMass = 0.0f;
_inertia = 0.0f;
InvI = 0.0f;
Sweep.LocalCenter = Vector2.Zero;
// Kinematic bodies have zero mass.
if (BodyType == BodyType.Kinematic)
{
Sweep.C0 = Sweep.C = Xf.Position;
return;
}
Debug.Assert(BodyType == BodyType.Dynamic || BodyType == BodyType.Static);
// Accumulate mass over all fixtures.
Vector2 center = Vector2.Zero;
foreach (Fixture f in FixtureList)
{
if (f.Shape._density == 0)
{
continue;
}
MassData massData = f.Shape.MassData;
_mass += massData.Mass;
center += massData.Mass*massData.Centroid;
_inertia += massData.Inertia;
}
//Static bodies only have mass, they don't have other properties. A little hacky tho...
if (BodyType == BodyType.Static)
{
Sweep.C0 = Sweep.C = Xf.Position;
return;
}
// 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 (_inertia > 0.0f && (Flags & BodyFlags.FixedRotation) == 0)
{
// Center the inertia about the center of mass.
_inertia -= _mass*Vector2.Dot(center, center);
Debug.Assert(_inertia > 0.0f);
InvI = 1.0f/_inertia;
}
else
{
_inertia = 0.0f;
InvI = 0.0f;
}
// Move center of mass.
Vector2 oldCenter = Sweep.C;
Sweep.LocalCenter = center;
Sweep.C0 = Sweep.C = MathUtils.Multiply(ref Xf, ref Sweep.LocalCenter);
// Update center of mass velocity.
Vector2 a = Sweep.C - oldCenter;
LinearVelocityInternal += new Vector2(-AngularVelocityInternal*a.Y, AngularVelocityInternal*a.X);
}
public Fixture(Body body, Shape shape, Object userData) { CollisionFilter = new CollisionFilter(this); //Fixture defaults Friction = 0.2f; Restitution = 0; IsSensor = false; Body = body; UserData = userData; if (Settings.ConserveMemory) { Shape = shape; } else { Shape = shape.Clone(); } // Reserve proxy space int childCount = Shape.ChildCount; Proxies = new FixtureProxy[childCount]; for (int i = 0; i < childCount; ++i) { Proxies[i] = new FixtureProxy(); Proxies[i].Fixture = null; Proxies[i].ProxyId = BroadPhase.NullProxy; } ProxyCount = 0; FixtureId = _fixtureIdCounter++; if ((Body.Flags & BodyFlags.Enabled) == BodyFlags.Enabled) { BroadPhase broadPhase = Body.World.ContactManager.BroadPhase; CreateProxies(broadPhase, ref Body.Xf); } Body.FixtureList.Add(this); // Adjust mass properties if needed. if (Shape._density > 0.0f) { Body.ResetMassData(); } // Let the world know we have a new fixture. This will cause new contacts // to be created at the beginning of the next time step. Body.World.Flags |= WorldFlags.NewFixture; if (Body.World.FixtureAdded != null) { Body.World.FixtureAdded(this); } }