internal Body(BodyDef bd, World world) { _flags = 0; if (bd.IsBullet) { _flags |= BodyFlags.Bullet; } if (bd.FixedRotation) { _flags |= BodyFlags.FixedRotation; } if (bd.AllowSleep) { _flags |= BodyFlags.AllowSleep; } if (bd.IsSleeping) { _flags |= BodyFlags.Sleep; } _world = world; _xf.Position = bd.Position; _xf.R.Set(bd.Angle); _sweep.LocalCenter.SetZero(); _sweep.T0 = 1.0f; _sweep.A0 = _sweep.A = bd.Angle; _sweep.C0 = _sweep.C = Math.Mul(_xf, _sweep.LocalCenter); _jointList = null; _contactList = null; _prev = null; _next = null; _linearVelocity = bd.LinearVelocity; _angularVelocity = bd.AngularVelocity; _linearDamping = bd.LinearDamping; _angularDamping = bd.AngularDamping; _force.Set(0.0f, 0.0f); _torque = 0.0f; _sleepTime = 0.0f; _mass = 0; _invMass = 0.0f; _I = 0.0f; _invI = 0.0f; _type = BodyType.Static; _userData = bd.UserData; _fixtureList = null; _fixtureCount = 0; }
// This is used to prevent connected bodies from colliding. // It may lie, depending on the collideConnected flag. internal bool IsConnected(Body other) { for (JointEdge jn = _jointList; jn != null; jn = jn.Next) { if (jn.Other == other) { return(jn.Joint._collideConnected == false); } } return(false); }
internal bool IsConnected(Body other) { bool result; for (JointEdge jointEdge = this._jointList; jointEdge != null; jointEdge = jointEdge.Next) { if (jointEdge.Other == other) { result = !jointEdge.Joint._collideConnected; return(result); } } result = false; return(result); }
private Transform _xf; // the body origin transform #endregion Fields #region Constructors internal Body(BodyDef bd, World world) { _flags = 0; if (bd.IsBullet) { _flags |= BodyFlags.Bullet; } if (bd.FixedRotation) { _flags |= BodyFlags.FixedRotation; } if (bd.AllowSleep) { _flags |= BodyFlags.AllowSleep; } if (bd.IsSleeping) { _flags |= BodyFlags.Sleep; } _world = world; _xf.Position = bd.Position; _xf.R.Set(bd.Angle); _sweep.LocalCenter.SetZero(); _sweep.T0 = 1.0f; _sweep.A0 = _sweep.A = bd.Angle; _sweep.C0 = _sweep.C = Math.Mul(_xf, _sweep.LocalCenter); _jointList = null; _contactList = null; _prev = null; _next = null; _linearVelocity = bd.LinearVelocity; _angularVelocity = bd.AngularVelocity; _linearDamping = bd.LinearDamping; _angularDamping = bd.AngularDamping; _force.Set(0.0f, 0.0f); _torque = 0.0f; _sleepTime = 0.0f; _mass = 0; _invMass = 0.0f; _I = 0.0f; _invI = 0.0f; _type = BodyType.Static; _userData = bd.UserData; _fixtureList = null; _fixtureCount = 0; }
internal Body(BodyDef bd, World world) { Box2DXDebug.Assert(world._lock == false); _flags = 0; if (bd.IsBullet) { _flags |= BodyFlags.Bullet; } if (bd.FixedRotation) { _flags |= BodyFlags.FixedRotation; } if (bd.AllowSleep) { _flags |= BodyFlags.AllowSleep; } if (bd.IsSleeping) { _flags |= BodyFlags.Sleep; } _world = world; _xf.Position = bd.Position; _xf.R.Set(bd.Angle); _sweep.LocalCenter = bd.MassData.Center; _sweep.T0 = 1.0f; _sweep.A0 = _sweep.A = bd.Angle; _sweep.C0 = _sweep.C = Common.Math.Mul(_xf, _sweep.LocalCenter); _jointList = null; _contactList = null; _prev = null; _next = null; _linearDamping = bd.LinearDamping; _angularDamping = bd.AngularDamping; _force.Set(0.0f, 0.0f); _torque = 0.0f; _linearVelocity.SetZero(); _angularVelocity = 0.0f; _sleepTime = 0.0f; _invMass = 0.0f; _I = 0.0f; _invI = 0.0f; _mass = bd.MassData.Mass; if (_mass > 0.0f) { _invMass = 1.0f / _mass; } if ((_flags & BodyFlags.FixedRotation) == 0) { _I = bd.MassData.I; } if (_I > 0.0f) { _invI = 1.0f / _I; } if (_invMass == 0.0f && _invI == 0.0f) { _type = BodyType.Static; } else { _type = BodyType.Dynamic; } _userData = bd.UserData; _shapeList = null; _shapeCount = 0; }
// Find TOI contacts and solve them. private void SolveTOI(TimeStep step) { // Reserve an island and a queue for TOI island solution. Island island = new Island(_bodyCount, Settings.MaxTOIContactsPerIsland, Settings.MaxTOIJointsPerIsland, _contactManager._contactListener); //Simple one pass queue //Relies on the fact that we're only making one pass //through and each body can only be pushed/popped once. //To push: // queue[queueStart+queueSize++] = newElement; //To pop: // poppedElement = queue[queueStart++]; // --queueSize; int queueCapacity = _bodyCount; Body[] queue = new Body[queueCapacity]; for (Body b = _bodyList; b != null; b = b._next) { b._flags &= ~Body.BodyFlags.Island; b._sweep.T0 = 0.0f; } for (Contact c = _contactManager._contactList; c != null; c = c.Next) { // Invalidate TOI c.Flags &= ~(ContactFlag.ToiFlag | ContactFlag.IslandFlag); } for (Joint j = _jointList; j != null; j = j._next) { j._islandFlag = false; } // Find TOI events and solve them. for (; ;) { // Find the first TOI. Contact minContact = null; float minTOI = 1.0f; for (Contact c = _contactManager._contactList; c != null; c = c.Next) { // Can this contact generate a solid TOI contact? if (c.IsSolid() == false || c.IsContinuous() == false) { continue; } // TODO_ERIN keep a counter on the contact, only respond to M TOIs per contact. float toi = 1.0f; if ((c.Flags & ContactFlag.ToiFlag) != 0) { // This contact has a valid cached TOI. toi = c.Toi; } else { // Compute the TOI for this contact. Fixture s1 = c.GetFixtureA(); Fixture s2 = c.GetFixtureB(); Body b1 = s1.GetBody(); Body b2 = s2.GetBody(); if ((b1.IsStatic() || b1.IsSleeping()) && (b2.IsStatic() || b2.IsSleeping())) { continue; } // Put the sweeps onto the same time interval. float t0 = b1._sweep.T0; if (b1._sweep.T0 < b2._sweep.T0) { t0 = b2._sweep.T0; b1._sweep.Advance(t0); } else if (b2._sweep.T0 < b1._sweep.T0) { t0 = b1._sweep.T0; b2._sweep.Advance(t0); } Box2DXDebug.Assert(t0 < 1.0f); // Compute the time of impact. toi = c.ComputeTOI(b1._sweep, b2._sweep); Box2DXDebug.Assert(0.0f <= toi && toi <= 1.0f); // If the TOI is in range ... if (0.0f < toi && toi < 1.0f) { // Interpolate on the actual range. toi = Math.Min((1.0f - toi) * t0 + toi, 1.0f); } c.Toi = toi; c.Flags |= ContactFlag.ToiFlag; } if (Settings.FLT_EPSILON < toi && toi < minTOI) { // This is the minimum TOI found so far. minContact = c; minTOI = toi; } } if (minContact == null || 1.0f - 100.0f * Settings.FLT_EPSILON < minTOI) { // No more TOI events. Done! break; } // Advance the bodies to the TOI. Fixture f1 = minContact.GetFixtureA(); Fixture f2 = minContact.GetFixtureB(); Body b3 = f1.GetBody(); Body b4 = f2.GetBody(); Sweep backup1 = b3._sweep; Sweep backup2 = b4._sweep; b3.Advance(minTOI); b4.Advance(minTOI); // The TOI contact likely has some new contact points. minContact.Update(_contactManager._contactListener); minContact.Flags &= ~ContactFlag.ToiFlag; // Is the contact solid? if (minContact.IsSolid() == false) { // Restore the sweeps. b3._sweep = backup1; b4._sweep = backup2; b3.SynchronizeTransform(); b4.SynchronizeTransform(); continue; } // Did numerical issues prevent a contact point from being generated? if (minContact.IsTouching() == false) { // Give up on this TOI. continue; } // Build the TOI island. We need a dynamic seed. Body seed = b3; if (seed.IsStatic()) { seed = b4; } // Reset island and queue. island.Clear(); int queueStart = 0; // starting index for queue int queueSize = 0; // elements in queue queue[queueStart + queueSize++] = seed; seed._flags |= Body.BodyFlags.Island; // Perform a breadth first search (BFS) on the contact/joint graph. while (queueSize > 0) { // Grab the next body off the stack and add it to the island. Body b = queue[queueStart++]; --queueSize; island.Add(ref b); // Make sure the body is awake. b._flags &= ~Body.BodyFlags.Sleep; // To keep islands as small as possible, we don't // propagate islands across static bodies. if (b.IsStatic()) { continue; } // Search all contacts connected to this body. for (ContactEdge cEdge = b._contactList; cEdge != null; cEdge = cEdge.Next) { // Does the TOI island still have space for contacts? if (island.ContactCount == island.ContactCapacity) { break; } // Has this contact already been added to an island? Skip slow or non-solid contacts. if ((cEdge.Contact.Flags & ContactFlag.IslandFlag) != 0) { continue; } // Is this contact touching? For performance we are not updating this contact. if (cEdge.Contact.IsSolid() == false || cEdge.Contact.IsTouching() == false) { continue; } island.Add(ref cEdge.Contact); cEdge.Contact.Flags |= ContactFlag.IslandFlag; // Update other body. Body other = cEdge.Other; // Was the other body already added to this island? if ((other._flags & Body.BodyFlags.Island) != 0) { continue; } // March forward, this can do no harm since this is the min TOI. if (other.IsStatic() == false) { other.Advance(minTOI); other.WakeUp(); } Box2DXDebug.Assert(queueStart + queueSize < queueCapacity); queue[queueStart + queueSize] = other; ++queueSize; other._flags |= Body.BodyFlags.Island; } for (JointEdge jEdge = b._jointList; jEdge != null; jEdge = jEdge.Next) { if (island.JointCount == island.JointCapacity) { continue; } if (jEdge.Joint._islandFlag == true) { continue; } island.Add(jEdge.Joint); jEdge.Joint._islandFlag = true; Body other = jEdge.Other; if ((other._flags & Body.BodyFlags.Island) != 0) { continue; } if (!other.IsStatic()) { other.Advance(minTOI); other.WakeUp(); } Box2DXDebug.Assert(queueStart + queueSize < queueCapacity); queue[queueStart + queueSize] = other; ++queueSize; other._flags |= Body.BodyFlags.Island; } } TimeStep subStep; subStep.WarmStarting = false; subStep.Dt = (1.0f - minTOI) * step.Dt; subStep.Inv_Dt = 1.0f / subStep.Dt; subStep.DtRatio = 0.0f; subStep.VelocityIterations = step.VelocityIterations; subStep.PositionIterations = step.PositionIterations; island.SolveTOI(ref subStep); // Post solve cleanup. for (int i = 0; i < island.BodyCount; ++i) { // Allow bodies to participate in future TOI islands. Body b = island.Bodies[i]; b._flags &= ~Body.BodyFlags.Island; if ((b._flags & Body.BodyFlags.Sleep) != 0) { continue; } if (b.IsStatic()) { continue; } b.SynchronizeFixtures(); // Invalidate all contact TOIs associated with this body. Some of these // may not be in the island because they were not touching. for (ContactEdge ce = b._contactList; ce != null; ce = ce.Next) { ce.Contact.Flags &= ~ContactFlag.ToiFlag; } } for (int i = 0; i < island.ContactCount; ++i) { // Allow contacts to participate in future TOI islands. Contact c = island.Contacts[i]; c.Flags &= ~(ContactFlag.ToiFlag | ContactFlag.IslandFlag); } for (int i = 0; i < island.JointCount; ++i) { // Allow joints to participate in future TOI islands. Joint j = island.Joints[i]; j._islandFlag = false; } // Commit fixture proxy movements to the broad-phase so that new contacts are created. // Also, some contacts can be destroyed. _contactManager.FindNewContacts(); } queue = null; }
// Find islands, integrate and solve constraints, solve position constraints private void Solve(TimeStep step) { // Size the island for the worst case. Island island = new Island(_bodyCount, _contactManager._contactCount, _jointCount, _contactManager._contactListener); // Clear all the island flags. for (Body b = _bodyList; b != null; b = b._next) { b._flags &= ~Body.BodyFlags.Island; } for (Contact c = _contactManager._contactList; c != null; c = c.Next) { c.Flags &= ~ContactFlag.IslandFlag; } for (Joint j = _jointList; j != null; j = j._next) { j._islandFlag = false; } // Build and simulate all awake islands. int stackSize = _bodyCount; Body[] stack = new Body[stackSize]; for (Body seed = _bodyList; seed != null; seed = seed._next) { if ((seed._flags & (Body.BodyFlags.Island | Body.BodyFlags.Sleep)) != 0) { continue; } if (seed.IsStatic()) { continue; } // Reset island and stack. island.Clear(); int stackCount = 0; stack[stackCount++] = seed; seed._flags |= Body.BodyFlags.Island; // Perform a depth first search (DFS) on the constraint graph. while (stackCount > 0) { // Grab the next body off the stack and add it to the island. Body b = stack[--stackCount]; island.Add(ref b); // Make sure the body is awake. b._flags &= ~Body.BodyFlags.Sleep; // To keep islands as small as possible, we don't // propagate islands across static bodies. if (b.IsStatic()) { continue; } // Search all contacts connected to this body. for (ContactEdge ce = b._contactList; ce != null; ce = ce.Next) { // Has this contact already been added to an island? if ((ce.Contact.Flags & ContactFlag.IslandFlag) != 0) { continue; } // Is this contact touching? if (ce.Contact.IsSolid() == false || ce.Contact.IsTouching() == false) { continue; } island.Add(ref ce.Contact); ce.Contact.Flags |= ContactFlag.IslandFlag; Body other = ce.Other; // Was the other body already added to this island? if ((other._flags & Body.BodyFlags.Island) != 0) { continue; } Box2DXDebug.Assert(stackCount < stackSize); stack[stackCount++] = other; other._flags |= Body.BodyFlags.Island; } // Search all joints connect to this body. for (JointEdge je = b._jointList; je != null; je = je.Next) { if (je.Joint._islandFlag == true) { continue; } island.Add(je.Joint); je.Joint._islandFlag = true; Body other = je.Other; if ((other._flags & Body.BodyFlags.Island) != 0) { continue; } Box2DXDebug.Assert(stackCount < stackSize); stack[stackCount++] = other; other._flags |= Body.BodyFlags.Island; } } island.Solve(step, _gravity, _allowSleep); // Post solve cleanup. for (int i = 0; i < island.BodyCount; ++i) { // Allow static bodies to participate in other islands. Body b = island.Bodies[i]; if (b.IsStatic()) { b._flags &= ~Body.BodyFlags.Island; } } } stack = null; // Synchronize shapes, check for out of range bodies. for (Body b = _bodyList; b != null; b = b.GetNext()) { if ((b._flags & Body.BodyFlags.Sleep) != 0) { continue; } if (b.IsStatic()) { continue; } // Update fixtures (for broad-phase). b.SynchronizeFixtures(); } // Look for new contacts. _contactManager.FindNewContacts(); }
public void DestroyBody(Body b) { Box2DXDebug.Assert(_bodyCount > 0); Box2DXDebug.Assert(IsLocked() == false); if (IsLocked()) { return; } // Delete the attached joints. JointEdge je = b._jointList; while (je != null) { JointEdge je0 = je; je = je.Next; if (_destructionListener != null) { _destructionListener.SayGoodbye(je0.Joint); } DestroyJoint(je0.Joint); } b._jointList = null; // Delete the attached contacts. ContactEdge ce = b._contactList; while (ce != null) { ContactEdge ce0 = ce; ce = ce.Next; _contactManager.Destroy(ce0.Contact); } b._contactList = null; // Delete the attached fixtures. This destroys broad-phase proxies. Fixture f = b._fixtureList; while (f != null) { Fixture f0 = f; f = f._next; if (_destructionListener != null) { _destructionListener.SayGoodbye(f0); } f0.Destroy(_contactManager._broadPhase); f0 = null; } b._fixtureList = null; b._fixtureCount = 0; // Remove world body list. if (b._prev != null) { b._prev._next = b._next; } if (b._next != null) { b._next._prev = b._prev; } if (b == _bodyList) { _bodyList = b._next; } --_bodyCount; b = null; }
internal Body(BodyDef bd, World world) { Box2DXDebug.Assert(world._lock == false); _flags = 0; if (bd.IsBullet) { _flags |= BodyFlags.Bullet; } if (bd.FixedRotation) { _flags |= BodyFlags.FixedRotation; } if (bd.AllowSleep) { _flags |= BodyFlags.AllowSleep; } if (bd.IsSleeping) { _flags |= BodyFlags.Sleep; } _world = world; _xf.Position = bd.Position; _xf.R.Set(bd.Angle); _sweep.LocalCenter = bd.MassData.Center; _sweep.T0 = 1.0f; _sweep.A0 = _sweep.A = bd.Angle; _sweep.C0 = _sweep.C = Common.Math.Mul(_xf, _sweep.LocalCenter); _jointList = null; _contactList = null; _prev = null; _next = null; _linearDamping = bd.LinearDamping; _angularDamping = bd.AngularDamping; _force.Set(0.0f, 0.0f); _torque = 0.0f; _linearVelocity.SetZero(); _angularVelocity = 0.0f; _sleepTime = 0.0f; _invMass = 0.0f; _I = 0.0f; _invI = 0.0f; _mass = bd.MassData.Mass; if (_mass > 0.0f) { _invMass = 1.0f / _mass; } if ((_flags & BodyFlags.FixedRotation) == 0) { _I = bd.MassData.I; } if (_I > 0.0f) { _invI = 1.0f / _I; } if (_invMass == 0.0f && _invI == 0.0f) { _type = BodyType.Static; } else { _type = BodyType.Dynamic; } _userData = bd.UserData; _shapeList = null; _shapeCount = 0; }
internal bool _useGravity; // STEVE Added internal Body(BodyDef bd, World world) { Box2DXDebug.Assert(!world._lock); this._flags = (Body.BodyFlags) 0; if (bd.IsBullet) { this._flags |= Body.BodyFlags.Bullet; } if (bd.FixedRotation) { this._flags |= Body.BodyFlags.FixedRotation; } if (bd.AllowSleep) { this._flags |= Body.BodyFlags.AllowSleep; } if (bd.IsSleeping) { this._flags |= Body.BodyFlags.Sleep; } this._world = world; this._xf.Position = bd.Position; this._xf.R.Set(bd.Angle); this._sweep.LocalCenter = bd.MassData.Center; this._sweep.T0 = 1f; this._sweep.A0 = (this._sweep.A = bd.Angle); this._sweep.C0 = (this._sweep.C = Box2DX.Common.Math.Mul(this._xf, this._sweep.LocalCenter)); this._jointList = null; this._contactList = null; this._prev = null; this._next = null; this._linearDamping = bd.LinearDamping; this._angularDamping = bd.AngularDamping; this._force.Set(0f, 0f); this._torque = 0f; this._linearVelocity.SetZero(); this._angularVelocity = 0f; this._sleepTime = 0f; this._invMass = 0f; this._I = 0f; this._invI = 0f; this._mass = bd.MassData.Mass; if (this._mass > 0f) { this._invMass = 1f / this._mass; } if ((this._flags & Body.BodyFlags.FixedRotation) == (Body.BodyFlags) 0) { this._I = bd.MassData.I; } if (this._I > 0f) { this._invI = 1f / this._I; } if (this._invMass == 0f && this._invI == 0f) { this._type = Body.BodyType.Static; } else { this._type = Body.BodyType.Dynamic; } this._userData = bd.UserData; this._shapeList = null; this._shapeCount = 0; this._gravity = _world.Gravity; // STEVE Added this._useGravity = false; // STEVE Added }
internal Body(BodyDef bd, World world) { Box2DXDebug.Assert(!world._lock); this._flags = (Body.BodyFlags)0; if (bd.IsBullet) { this._flags |= Body.BodyFlags.Bullet; } if (bd.FixedRotation) { this._flags |= Body.BodyFlags.FixedRotation; } if (bd.AllowSleep) { this._flags |= Body.BodyFlags.AllowSleep; } if (bd.IsSleeping) { this._flags |= Body.BodyFlags.Sleep; } this._world = world; this._xf.Position = bd.Position; this._xf.R.Set(bd.Angle); this._sweep.LocalCenter = bd.MassData.Center; this._sweep.T0 = 1f; this._sweep.A0 = (this._sweep.A = bd.Angle); this._sweep.C0 = (this._sweep.C = Box2DX.Common.Math.Mul(this._xf, this._sweep.LocalCenter)); this._jointList = null; this._contactList = null; this._prev = null; this._next = null; this._linearDamping = bd.LinearDamping; this._angularDamping = bd.AngularDamping; this._force.Set(0f, 0f); this._torque = 0f; this._linearVelocity.SetZero(); this._angularVelocity = 0f; this._sleepTime = 0f; this._invMass = 0f; this._I = 0f; this._invI = 0f; this._mass = bd.MassData.Mass; if (this._mass > 0f) { this._invMass = 1f / this._mass; } if ((this._flags & Body.BodyFlags.FixedRotation) == (Body.BodyFlags)0) { this._I = bd.MassData.I; } if (this._I > 0f) { this._invI = 1f / this._I; } if (this._invMass == 0f && this._invI == 0f) { this._type = Body.BodyType.Static; } else { this._type = Body.BodyType.Dynamic; } this._userData = bd.UserData; this._shapeList = null; this._shapeCount = 0; this._gravity = _world.Gravity; // STEVE Added this._useGravity = false; // STEVE Added }