internal bool CompareTo(Fixture fixture) { return ( CollidesWith == fixture.CollidesWith && CollisionCategories == fixture.CollisionCategories && CollisionGroup == fixture.CollisionGroup && Friction == fixture.Friction && IsSensor == fixture.IsSensor && Restitution == fixture.Restitution && Shape.CompareTo(fixture.Shape) && UserData == fixture.UserData); }
/// <summary> /// Destroy a fixture. This removes the fixture from the broad-phase and /// destroys all contacts associated with this fixture. This will /// automatically adjust the mass of the body if the body is dynamic and the /// fixture has positive density. /// All fixtures attached to a body are implicitly destroyed when the body is destroyed. /// Warning: This function is locked during callbacks. /// </summary> /// <param name="fixture">The fixture to be removed.</param> public void DestroyFixture(Fixture fixture) { Debug.Assert(fixture.Body == this); // Remove the fixture from this body's singly linked list. Debug.Assert(FixtureList.Count > 0); // You tried to remove a fixture that not present in the fixturelist. Debug.Assert(FixtureList.Contains(fixture)); // Destroy any contacts associated with the fixture. ContactEdge edge = ContactList; while (edge != null) { Contact c = edge.Contact; edge = edge.Next; Fixture fixtureA = c.FixtureA; Fixture fixtureB = c.FixtureB; if (fixture == fixtureA || fixture == fixtureB) { // This destroys the contact and removes it from // this body's contact list. World.ContactManager.Destroy(c); } } if ((Flags & BodyFlags.Enabled) == BodyFlags.Enabled) { IBroadPhase broadPhase = World.ContactManager.BroadPhase; fixture.DestroyProxies(broadPhase); } FixtureList.Remove(fixture); fixture.Destroy(); fixture.Body = null; ResetMassData(); }
/// <summary> /// Determines whether collisions are ignored between this fixture and the provided fixture. /// </summary> /// <param name="fixture">The fixture.</param> /// <returns> /// <c>true</c> if the fixture is ignored; otherwise, <c>false</c>. /// </returns> public bool IsFixtureIgnored(Fixture fixture) { if (_collisionIgnores == null) return false; if (_collisionIgnores.ContainsKey(fixture.FixtureId)) return _collisionIgnores[fixture.FixtureId]; return false; }
/// <summary> /// Restores collisions between this fixture and the provided fixture. /// </summary> /// <param name="fixture">The fixture.</param> public void RestoreCollisionWith(Fixture fixture) { if (_collisionIgnores == null) return; if (_collisionIgnores.ContainsKey(fixture.FixtureId)) { _collisionIgnores[fixture.FixtureId] = false; Refilter(); } }
public Fixture Clone(PhysicsBody body) { Fixture fixture = new Fixture(); fixture.Body = body; if (Settings.ConserveMemory) fixture.Shape = Shape; else fixture.Shape = Shape.Clone(); fixture.UserData = UserData; fixture.Restitution = Restitution; fixture.Friction = Friction; fixture.IsSensor = IsSensor; fixture._collisionGroup = CollisionGroup; fixture._collisionCategories = CollisionCategories; fixture._collidesWith = CollidesWith; if (_collisionIgnores != null) { fixture._collisionIgnores = new Dictionary<int, bool>(); foreach (KeyValuePair<int, bool> pair in _collisionIgnores) { fixture._collisionIgnores.Add(pair.Key, pair.Value); } } fixture.RegisterFixture(); return fixture; }
/// <summary> /// Ignores collisions between this fixture and the provided fixture. /// </summary> /// <param name="fixture">The fixture.</param> public void IgnoreCollisionWith(Fixture fixture) { if (_collisionIgnores == null) _collisionIgnores = new Dictionary<int, bool>(); if (_collisionIgnores.ContainsKey(fixture.FixtureId)) _collisionIgnores[fixture.FixtureId] = true; else _collisionIgnores.Add(fixture.FixtureId, true); Refilter(); }
//Cutting a shape into two is based on the work of Daid and his prototype BoxCutter: http://www.box2d.org/forum/viewtopic.php?f=3&t=1473 /// <summary> /// Split a fixture into 2 vertice collections using the given entry and exit-point. /// </summary> /// <param name="fixture">The Fixture to split</param> /// <param name="entryPoint">The entry point - The start point</param> /// <param name="exitPoint">The exit point - The end point</param> /// <param name="splitSize">The size of the split. Think of this as the laser-width</param> /// <param name="first">The first collection of vertexes</param> /// <param name="second">The second collection of vertexes</param> public static void SplitShape(Fixture fixture, Vector2 entryPoint, Vector2 exitPoint, float splitSize, out Vertices first, out Vertices second) { Vector2 localEntryPoint = fixture.Body.GetLocalPoint(ref entryPoint); Vector2 localExitPoint = fixture.Body.GetLocalPoint(ref exitPoint); PolygonShape shape = fixture.Shape as PolygonShape; if (shape == null) { first = new Vertices(); second = new Vertices(); return; } Vertices vertices = new Vertices(shape.Vertices); Vertices[] newPolygon = new Vertices[2]; for (int i = 0; i < newPolygon.Length; i++) { newPolygon[i] = new Vertices(vertices.Count); } int[] cutAdded = { -1, -1 }; int last = -1; for (int i = 0; i < vertices.Count; i++) { int n; //Find out if this vertex is on the old or new shape. if (Vector2.Dot(MathUtils.Cross(localExitPoint - localEntryPoint, 1), vertices[i] - localEntryPoint) > Settings.Epsilon) n = 0; else n = 1; if (last != n) { //If we switch from one shape to the other add the cut vertices. if (last == 0) { Debug.Assert(cutAdded[0] == -1); cutAdded[0] = newPolygon[last].Count; newPolygon[last].Add(localExitPoint); newPolygon[last].Add(localEntryPoint); } if (last == 1) { Debug.Assert(cutAdded[last] == -1); cutAdded[last] = newPolygon[last].Count; newPolygon[last].Add(localEntryPoint); newPolygon[last].Add(localExitPoint); } } newPolygon[n].Add(vertices[i]); last = n; } //Add the cut in case it has not been added yet. if (cutAdded[0] == -1) { cutAdded[0] = newPolygon[0].Count; newPolygon[0].Add(localExitPoint); newPolygon[0].Add(localEntryPoint); } if (cutAdded[1] == -1) { cutAdded[1] = newPolygon[1].Count; newPolygon[1].Add(localEntryPoint); newPolygon[1].Add(localExitPoint); } for (int n = 0; n < 2; n++) { Vector2 offset; if (cutAdded[n] > 0) { offset = (newPolygon[n][cutAdded[n] - 1] - newPolygon[n][cutAdded[n]]); } else { offset = (newPolygon[n][newPolygon[n].Count - 1] - newPolygon[n][0]); } offset.Normalize(); newPolygon[n][cutAdded[n]] += splitSize * offset; if (cutAdded[n] < newPolygon[n].Count - 2) { offset = (newPolygon[n][cutAdded[n] + 2] - newPolygon[n][cutAdded[n] + 1]); } else { offset = (newPolygon[n][0] - newPolygon[n][newPolygon[n].Count - 1]); } offset.Normalize(); newPolygon[n][cutAdded[n] + 1] += splitSize * offset; } first = newPolygon[0]; second = newPolygon[1]; }
private static bool ShouldCollide(Fixture fixtureA, Fixture fixtureB) { if (Settings.UseFPECollisionCategories) { if ((fixtureA.CollisionGroup == fixtureB.CollisionGroup) && fixtureA.CollisionGroup != 0 && fixtureB.CollisionGroup != 0) return false; if (((fixtureA.CollisionCategories & fixtureB.CollidesWith) == Category.None) & ((fixtureB.CollisionCategories & fixtureA.CollidesWith) == Category.None)) return false; if (fixtureA.IsFixtureIgnored(fixtureB) || fixtureB.IsFixtureIgnored(fixtureA)) return false; return true; } if (fixtureA.CollisionGroup == fixtureB.CollisionGroup && fixtureA.CollisionGroup != 0) { return fixtureA.CollisionGroup > 0; } bool collide = (fixtureA.CollidesWith & fixtureB.CollisionCategories) != 0 && (fixtureA.CollisionCategories & fixtureB.CollidesWith) != 0; if (collide) { if (fixtureA.IsFixtureIgnored(fixtureB) || fixtureB.IsFixtureIgnored(fixtureA)) { return false; } } return collide; }
private int FindFixtureIndex(Fixture fixture) { for (int i = 0; i < _serializedFixtures.Count; ++i) { if (_serializedFixtures[i].CompareTo(fixture)) return i; } return -1; }
public void Deserialize(PhysicsWorld world, Stream stream) { world.Clear(); XMLFragmentElement root = XMLFragmentParser.LoadFromStream(stream); if (root.Name.ToLower() != "world") throw new Exception(); foreach (XMLFragmentElement main in root.Elements) { if (main.Name.ToLower() == "gravity") { world.Gravity = ReadVector(main); } } foreach (XMLFragmentElement shapeElement in root.Elements) { if (shapeElement.Name.ToLower() == "shapes") { foreach (XMLFragmentElement n in shapeElement.Elements) { if (n.Name.ToLower() != "shape") throw new Exception(); ShapeType type = (ShapeType)Enum.Parse(typeof(ShapeType), n.Attributes[0].Value, true); switch (type) { case ShapeType.Circle: { CircleShape shape = new CircleShape(); foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "radius": shape.Radius = float.Parse(sn.Value); break; case "position": shape.Position = ReadVector(sn); break; default: throw new Exception(); } } _shapes.Add(shape); } break; case ShapeType.Polygon: { PolygonShape shape = new PolygonShape(); foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "vertices": { List<Vector2> verts = new List<Vector2>(); foreach (XMLFragmentElement vert in sn.Elements) verts.Add(ReadVector(vert)); shape.Set(new Vertices(verts.ToArray())); } break; case "centroid": shape.MassData.Centroid = ReadVector(sn); break; } } _shapes.Add(shape); } break; case ShapeType.Edge: { EdgeShape shape = new EdgeShape(); foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "hasvertex0": shape.HasVertex0 = bool.Parse(sn.Value); break; case "hasvertex3": shape.HasVertex0 = bool.Parse(sn.Value); break; case "vertex0": shape.Vertex0 = ReadVector(sn); break; case "vertex1": shape.Vertex1 = ReadVector(sn); break; case "vertex2": shape.Vertex2 = ReadVector(sn); break; case "vertex3": shape.Vertex3 = ReadVector(sn); break; default: throw new Exception(); } } _shapes.Add(shape); } break; } } } } foreach (XMLFragmentElement fixtureElement in root.Elements) { if (fixtureElement.Name.ToLower() == "fixtures") { foreach (XMLFragmentElement n in fixtureElement.Elements) { Fixture fixture = new Fixture(); if (n.Name.ToLower() != "fixture") throw new Exception(); foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "shape": fixture.Shape = _shapes[int.Parse(sn.Value)]; break; case "density": fixture.Shape.Density = float.Parse(sn.Value); break; case "filterdata": foreach (XMLFragmentElement ssn in sn.Elements) { switch (ssn.Name.ToLower()) { case "categorybits": fixture._collisionCategories = (Category)int.Parse(ssn.Value); break; case "maskbits": fixture._collidesWith = (Category)int.Parse(ssn.Value); break; case "groupindex": fixture._collisionGroup = short.Parse(ssn.Value); break; } } break; case "friction": fixture.Friction = float.Parse(sn.Value); break; case "issensor": fixture.IsSensor = bool.Parse(sn.Value); break; case "restitution": fixture.Restitution = float.Parse(sn.Value); break; case "userdata": fixture.UserData = ReadSimpleType(sn, null, false); break; } } _fixtures.Add(fixture); } } } foreach (XMLFragmentElement bodyElement in root.Elements) { if (bodyElement.Name.ToLower() == "bodies") { foreach (XMLFragmentElement n in bodyElement.Elements) { PhysicsBody body = new PhysicsBody(world); if (n.Name.ToLower() != "body") throw new Exception(); body.BodyType = (BodyType)Enum.Parse(typeof(BodyType), n.Attributes[0].Value, true); foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "active": if (bool.Parse(sn.Value)) body.Flags |= BodyFlags.Enabled; else body.Flags &= ~BodyFlags.Enabled; break; case "allowsleep": body.SleepingAllowed = bool.Parse(sn.Value); break; case "angle": { Vector2 position = body.Position; body.SetTransformIgnoreContacts(ref position, float.Parse(sn.Value)); } break; case "angulardamping": body.AngularDamping = float.Parse(sn.Value); break; case "angularvelocity": body.AngularVelocity = float.Parse(sn.Value); break; case "awake": body.Awake = bool.Parse(sn.Value); break; case "bullet": body.IsBullet = bool.Parse(sn.Value); break; case "fixedrotation": body.FixedRotation = bool.Parse(sn.Value); break; case "lineardamping": body.LinearDamping = float.Parse(sn.Value); break; case "linearvelocity": body.LinearVelocity = ReadVector(sn); break; case "position": { float rotation = body.Rotation; Vector2 position = ReadVector(sn); body.SetTransformIgnoreContacts(ref position, rotation); } break; case "userdata": body.UserData = ReadSimpleType(sn, null, false); break; case "fixtures": { foreach (XMLFragmentElement v in sn.Elements) { Fixture blueprint = _fixtures[int.Parse(v.Value)]; Fixture f = new Fixture(body, blueprint.Shape); f.Restitution = blueprint.Restitution; f.UserData = blueprint.UserData; f.Friction = blueprint.Friction; f.CollidesWith = blueprint.CollidesWith; f.CollisionCategories = blueprint.CollisionCategories; f.CollisionGroup = blueprint.CollisionGroup; } break; } } } _bodies.Add(body); } } } foreach (XMLFragmentElement jointElement in root.Elements) { if (jointElement.Name.ToLower() == "joints") { foreach (XMLFragmentElement n in jointElement.Elements) { PhysicsJoint joint; if (n.Name.ToLower() != "joint") throw new Exception(); JointType type = (JointType)Enum.Parse(typeof(JointType), n.Attributes[0].Value, true); int bodyAIndex = -1, bodyBIndex = -1; bool collideConnected = false; object userData = null; foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "bodya": bodyAIndex = int.Parse(sn.Value); break; case "bodyb": bodyBIndex = int.Parse(sn.Value); break; case "collideconnected": collideConnected = bool.Parse(sn.Value); break; case "userdata": userData = ReadSimpleType(sn, null, false); break; } } PhysicsBody bodyA = _bodies[bodyAIndex]; PhysicsBody bodyB = _bodies[bodyBIndex]; switch (type) { case JointType.Distance: joint = new DistanceJoint(); break; case JointType.Friction: joint = new FrictionJoint(); break; case JointType.Line: joint = new LineJoint(); break; case JointType.Prismatic: joint = new PrismaticJoint(); break; case JointType.Pulley: joint = new PulleyJoint(); break; case JointType.Revolute: joint = new RevoluteJoint(); break; case JointType.Weld: joint = new WeldJoint(); break; case JointType.Rope: joint = new RopeJoint(); break; case JointType.Angle: joint = new AngleJoint(); break; case JointType.Slider: joint = new SliderJoint(); break; case JointType.Gear: throw new Exception("GearJoint is not supported."); default: throw new Exception("Invalid or unsupported joint."); } joint.CollideConnected = collideConnected; joint.UserData = userData; joint.BodyA = bodyA; joint.BodyB = bodyB; _joints.Add(joint); world.AddJoint(joint); foreach (XMLFragmentElement sn in n.Elements) { // check for specific nodes switch (type) { case JointType.Distance: { switch (sn.Name.ToLower()) { case "dampingratio": ((DistanceJoint)joint).DampingRatio = float.Parse(sn.Value); break; case "frequencyhz": ((DistanceJoint)joint).Frequency = float.Parse(sn.Value); break; case "length": ((DistanceJoint)joint).Length = float.Parse(sn.Value); break; case "localanchora": ((DistanceJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((DistanceJoint)joint).LocalAnchorB = ReadVector(sn); break; } } break; case JointType.Friction: { switch (sn.Name.ToLower()) { case "localanchora": ((FrictionJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((FrictionJoint)joint).LocalAnchorB = ReadVector(sn); break; case "maxforce": ((FrictionJoint)joint).MaxForce = float.Parse(sn.Value); break; case "maxtorque": ((FrictionJoint)joint).MaxTorque = float.Parse(sn.Value); break; } } break; case JointType.Line: { switch (sn.Name.ToLower()) { case "enablemotor": ((LineJoint)joint).MotorEnabled = bool.Parse(sn.Value); break; case "localanchora": ((LineJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((LineJoint)joint).LocalAnchorB = ReadVector(sn); break; case "motorspeed": ((LineJoint)joint).MotorSpeed = float.Parse(sn.Value); break; case "dampingratio": ((LineJoint)joint).DampingRatio = float.Parse(sn.Value); break; case "maxmotortorque": ((LineJoint)joint).MaxMotorTorque = float.Parse(sn.Value); break; case "frequencyhz": ((LineJoint)joint).Frequency = float.Parse(sn.Value); break; case "localxaxis": ((LineJoint)joint).LocalXAxis = ReadVector(sn); break; } } break; case JointType.Prismatic: { switch (sn.Name.ToLower()) { case "enablelimit": ((PrismaticJoint)joint).LimitEnabled = bool.Parse(sn.Value); break; case "enablemotor": ((PrismaticJoint)joint).MotorEnabled = bool.Parse(sn.Value); break; case "localanchora": ((PrismaticJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((PrismaticJoint)joint).LocalAnchorB = ReadVector(sn); break; case "local1axis1": ((PrismaticJoint)joint).LocalXAxis1 = ReadVector(sn); break; case "maxmotorforce": ((PrismaticJoint)joint).MaxMotorForce = float.Parse(sn.Value); break; case "motorspeed": ((PrismaticJoint)joint).MotorSpeed = float.Parse(sn.Value); break; case "lowertranslation": ((PrismaticJoint)joint).LowerLimit = float.Parse(sn.Value); break; case "uppertranslation": ((PrismaticJoint)joint).UpperLimit = float.Parse(sn.Value); break; case "referenceangle": ((PrismaticJoint)joint).ReferenceAngle = float.Parse(sn.Value); break; } } break; case JointType.Pulley: { switch (sn.Name.ToLower()) { case "groundanchora": ((PulleyJoint)joint).GroundAnchorA = ReadVector(sn); break; case "groundanchorb": ((PulleyJoint)joint).GroundAnchorB = ReadVector(sn); break; case "lengtha": ((PulleyJoint)joint).LengthA = float.Parse(sn.Value); break; case "lengthb": ((PulleyJoint)joint).LengthB = float.Parse(sn.Value); break; case "localanchora": ((PulleyJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((PulleyJoint)joint).LocalAnchorB = ReadVector(sn); break; case "maxlengtha": ((PulleyJoint)joint).MaxLengthA = float.Parse(sn.Value); break; case "maxlengthb": ((PulleyJoint)joint).MaxLengthB = float.Parse(sn.Value); break; case "ratio": ((PulleyJoint)joint).Ratio = float.Parse(sn.Value); break; } } break; case JointType.Revolute: { switch (sn.Name.ToLower()) { case "enablelimit": ((RevoluteJoint)joint).LimitEnabled = bool.Parse(sn.Value); break; case "enablemotor": ((RevoluteJoint)joint).MotorEnabled = bool.Parse(sn.Value); break; case "localanchora": ((RevoluteJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((RevoluteJoint)joint).LocalAnchorB = ReadVector(sn); break; case "maxmotortorque": ((RevoluteJoint)joint).MaxMotorTorque = float.Parse(sn.Value); break; case "motorspeed": ((RevoluteJoint)joint).MotorSpeed = float.Parse(sn.Value); break; case "lowerangle": ((RevoluteJoint)joint).LowerLimit = float.Parse(sn.Value); break; case "upperangle": ((RevoluteJoint)joint).UpperLimit = float.Parse(sn.Value); break; case "referenceangle": ((RevoluteJoint)joint).ReferenceAngle = float.Parse(sn.Value); break; } } break; case JointType.Weld: { switch (sn.Name.ToLower()) { case "localanchora": ((WeldJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((WeldJoint)joint).LocalAnchorB = ReadVector(sn); break; } } break; case JointType.Rope: { switch (sn.Name.ToLower()) { case "localanchora": ((RopeJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((RopeJoint)joint).LocalAnchorB = ReadVector(sn); break; case "maxlength": ((RopeJoint)joint).MaxLength = float.Parse(sn.Value); break; } } break; case JointType.Gear: throw new Exception("Gear joint is unsupported"); case JointType.Angle: { switch (sn.Name.ToLower()) { case "biasfactor": ((AngleJoint)joint).BiasFactor = float.Parse(sn.Value); break; case "maximpulse": ((AngleJoint)joint).MaxImpulse = float.Parse(sn.Value); break; case "softness": ((AngleJoint)joint).Softness = float.Parse(sn.Value); break; case "targetangle": ((AngleJoint)joint).TargetAngle = float.Parse(sn.Value); break; } } break; case JointType.Slider: { switch (sn.Name.ToLower()) { case "dampingratio": ((SliderJoint)joint).DampingRatio = float.Parse(sn.Value); break; case "frequencyhz": ((SliderJoint)joint).Frequency = float.Parse(sn.Value); break; case "maxlength": ((SliderJoint)joint).MaxLength = float.Parse(sn.Value); break; case "minlength": ((SliderJoint)joint).MinLength = float.Parse(sn.Value); break; case "localanchora": ((SliderJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((SliderJoint)joint).LocalAnchorB = ReadVector(sn); break; } } break; } } } } } }
private void SerializeFixture(Fixture fixture) { _writer.WriteStartElement("Fixture"); _writer.WriteElementString("Shape", FindShapeIndex(fixture.Shape).ToString()); _writer.WriteElementString("Density", fixture.Shape.Density.ToString()); _writer.WriteStartElement("FilterData"); _writer.WriteElementString("CategoryBits", ((int)fixture.CollisionCategories).ToString()); _writer.WriteElementString("MaskBits", ((int)fixture.CollidesWith).ToString()); _writer.WriteElementString("GroupIndex", fixture.CollisionGroup.ToString()); _writer.WriteEndElement(); _writer.WriteElementString("Friction", fixture.Friction.ToString()); _writer.WriteElementString("IsSensor", fixture.IsSensor.ToString()); _writer.WriteElementString("Restitution", fixture.Restitution.ToString()); if (fixture.UserData != null) { _writer.WriteStartElement("UserData"); WriteDynamicType(fixture.UserData.GetType(), fixture.UserData); _writer.WriteEndElement(); } _writer.WriteEndElement(); }