public static BodyTemplate Scale(this BodyTemplate bodyTemplate, Vector2 scale) { List <FixtureTemplate> fixtures = new List <FixtureTemplate>(bodyTemplate.Fixtures.Count); foreach (var fixture in bodyTemplate.Fixtures) { Shape shapeCopy; switch (fixture.Shape.ShapeType) { case ShapeType.Polygon: Vertices verticesCopy = new Vertices(); verticesCopy.AddRange(((PolygonShape)fixture.Shape).Vertices); verticesCopy.Scale(scale); shapeCopy = new PolygonShape(verticesCopy, fixture.Shape.Density); break; // TODO: support other shape types default: throw new NotSupportedException(); } FixtureTemplate fixtureCopy = new FixtureTemplate(); fixtureCopy.Shape = shapeCopy; fixtures.Add(fixtureCopy); } BodyTemplate newBodyTemplate = new BodyTemplate(); newBodyTemplate.Fixtures = fixtures; return(newBodyTemplate); }
/// <summary> /// Creates a fixture and attach it to this body. /// If the density is non-zero, this function automatically updates the mass of the body. /// Contacts are not created until the next time step. /// Warning: This function is locked during callbacks. /// </summary> public Fixture CreateFixture(FixtureTemplate template) { Fixture f = new Fixture(this, template); f.FixtureId = _world._fixtureIdCounter++; return(f); }
public Fixture CreateFixture(Shape shape, object userData = null) { FixtureTemplate template = new FixtureTemplate(); template.Shape = shape; template.UserData = userData; return(CreateFixture(template)); }
public static Dictionary <string, BodyTemplate> LoadBodies(string xml) { Dictionary <string, BodyTemplate> results = new Dictionary <string, BodyTemplate>(); XDocument bodyXml = XDocument.Parse(xml); float ptm = 1f / (float)Convert.ToDouble(bodyXml.XPathSelectElement("bodydef/metadata/ptm_ratio").Value); World world = new World(); var xmlBodies = bodyXml.XPathSelectElements("bodydef/bodies/body"); foreach (var xmlBody in xmlBodies) { var bodyName = xmlBody.Attribute("name").Value; BodyTemplate body = new BodyTemplate(); results.Add(bodyName, body); var xmlFixtures = xmlBody.XPathSelectElements("fixtures/fixture"); foreach (var xmlFixture in xmlFixtures) { float density = (float)Convert.ToDouble(xmlFixture.XPathSelectElement("density").Value); var xmlPolygons = xmlFixture.XPathSelectElements("polygons/polygon"); foreach (var xmlPolygon in xmlPolygons) { Vertices vertices = new Vertices(); List <float> values = xmlPolygon.Value.Split(',') .Select(x => (float)Convert.ToDouble(x.Trim())).ToList(); for (int i = 0; i < values.Count; i += 2) { vertices.Add(new Vector2(values[i], values[i + 1]) * ptm); } PolygonShape polygon = new PolygonShape(vertices, density); FixtureTemplate fixture = new FixtureTemplate(); fixture.Shape = polygon; body.Fixtures.Add(fixture); body.Mass = 1; } } var actualBody = body.Create(world); actualBody.BodyType = BodyType.Dynamic; foreach (var fixture in body.Fixtures) { var polygon = (PolygonShape)fixture.Shape; polygon.Vertices.Translate(-actualBody.LocalCenter); } } return(results); }
internal Fixture(Body body, FixtureTemplate template) : this() { UserData = template.UserData; Friction = template.Friction; Restitution = template.Restitution; Body = body; IsSensor = template.IsSensor; Shape = template.Shape.Clone(); RegisterFixture(); }
// currently this generae only one default 1pix template.... public static List <FixtureTemplate> GenerateDefaultTemplates() { List <FixtureTemplate> templates = new List <FixtureTemplate>(); IMode mode = new ModeGridTopLeft(1, 1); List <IMode> modes = new List <IMode>(); modes.Add(mode); IPixelPatch patch = new PixelPatchSnakeRowWiseTopLeft(1, 1, 0, 3); Fixture f = new Fixture(modes, patch) { Name = "Generic 1pix" }; FixtureTemplate template = FixtureTemplateFactory.createFixtureTemplate(f); templates.Add(template); FixtureTemplateUtils.StoreTemplate(template); return(templates); }
private TemplatesTest() { BodyTemplate b1 = new BodyTemplate(); b1.Type = BodyType.Dynamic; b1.LinearVelocity = new Vector2(15.0f, 0.0f); Body body = BodyFactory.CreateFromTemplate(World, b1); //CircleShapeTemplate circle = new CircleShapeTemplate(); //circle.Density = 1f; //circle.Radius = 2f; Shape circle = new CircleShape(2f, 2f); FixtureTemplate f1 = new FixtureTemplate(); f1.Shape = circle; FixtureFactory.CreateFromTemplate(body, f1); }
public static Fixture CreateFromTemplate(Body body, FixtureTemplate f1) { return(body.CreateFixture(f1)); }
protected override BodyContainer Read(ContentReader input, BodyContainer existingInstance) { BodyContainer bodies = existingInstance ?? new BodyContainer(); int count = input.ReadInt32(); for (int i = 0; i < count; i++) { string name = input.ReadString(); BodyTemplate body = new BodyTemplate { Mass = input.ReadSingle(), BodyType = (BodyType)input.ReadInt32() }; int fixtureCount = input.ReadInt32(); for (int j = 0; j < fixtureCount; j++) { FixtureTemplate fixture = new FixtureTemplate { Name = input.ReadString(), Restitution = input.ReadSingle(), Friction = input.ReadSingle() }; ShapeType type = (ShapeType)input.ReadInt32(); switch (type) { case ShapeType.Circle: { float density = input.ReadSingle(); float radius = input.ReadSingle(); CircleShape circle = new CircleShape(radius, density); circle.Position = input.ReadVector2(); fixture.Shape = circle; } break; case ShapeType.Polygon: { Vertices verts = new Vertices(Settings.MaxPolygonVertices); float density = input.ReadSingle(); int verticeCount = input.ReadInt32(); for (int k = 0; k < verticeCount; k++) { verts.Add(input.ReadVector2()); } PolygonShape poly = new PolygonShape(verts, density); poly.MassData.Centroid = input.ReadVector2(); fixture.Shape = poly; } break; case ShapeType.Edge: { EdgeShape edge = new EdgeShape(input.ReadVector2(), input.ReadVector2()); edge.HasVertex0 = input.ReadBoolean(); if (edge.HasVertex0) { edge.Vertex0 = input.ReadVector2(); } edge.HasVertex3 = input.ReadBoolean(); if (edge.HasVertex3) { edge.Vertex3 = input.ReadVector2(); } fixture.Shape = edge; } break; case ShapeType.Chain: { Vertices verts = new Vertices(); int verticeCount = input.ReadInt32(); for (int k = 0; k < verticeCount; k++) { verts.Add(input.ReadVector2()); } fixture.Shape = new ChainShape(verts); } break; } body.Fixtures.Add(fixture); } bodies[name] = body; } return(bodies); }
public static void StoreTemplate(FixtureTemplate f) { var path = FIXTURE_TEMPLATE_PATH + f.Name + ".json"; FileIO.WriteFile(FIXTURE_TEMPLATE_PATH + FormatNameForWritting(f.Name) + ".json", true, StaticSerializer.Serialize(f)); }