public override PolygonContainer Process(List <RawBodyTemplate> input, ContentProcessorContext context) { if (ScaleFactor < 1) { throw new Exception("Pixel to meter ratio must be greater than zero."); } if (BezierIterations < 1) { throw new Exception("Cubic bézier iterations must be greater than zero."); } Matrix matScale = Matrix.CreateScale(_scaleFactor, _scaleFactor, 1f); SVGPathParser parser = new SVGPathParser(BezierIterations); PolygonContainer polygons = new PolygonContainer(); foreach (RawBodyTemplate body in input) { foreach (RawFixtureTemplate fixture in body.Fixtures) { List <Polygon> paths = parser.ParseSVGPath(fixture.Path, fixture.Transformation * matScale); if (paths.Count == 1) { polygons.Add(fixture.Name, paths[0]); } else { for (int i = 0; i < paths.Count; i++) { polygons.Add(fixture.Name + i, paths[i]); } } } } if (DecomposePaths) { polygons.Decompose(); } return(polygons); }
public override VerticesContainer Process(List <PathDefinition> input, ContentProcessorContext context) { if (ScaleFactor < 1) { throw new Exception("Pixel to meter ratio must be greater than zero."); } if (BezierIterations < 1) { throw new Exception("Cubic bézier iterations must be greater than zero."); } Matrix matScale = Matrix.CreateScale(_scaleFactor, _scaleFactor, 1f); SVGPathParser parser = new SVGPathParser(BezierIterations); VerticesContainer container = new VerticesContainer(); foreach (PathDefinition d in input) { List <VerticesExt> vertices = parser.ParseSVGPath(d.Path, d.Transformation * matScale); List <VerticesExt> c = container.ContainsKey(d.Id) ? container[d.Id] : (container[d.Id] = new List <VerticesExt>()); if (vertices.Count == 1) { c.Add(vertices[0]); } else { for (int i = 0; i < vertices.Count; i++) { c.Add(vertices[i]); } } } return(container); }
public override BodyContainer Process(List <RawBodyTemplate> input, ContentProcessorContext context) { if (ScaleFactor < 1) { throw new Exception("Pixel to meter ratio must be greater than zero."); } if (BezierIterations < 1) { throw new Exception("Cubic bézier iterations must be greater than zero."); } Matrix matScale = Matrix.CreateScale(_scaleFactor, _scaleFactor, 1f); SVGPathParser parser = new SVGPathParser(BezierIterations); BodyContainer bodies = new BodyContainer(); foreach (RawBodyTemplate rawBody in input) { if (rawBody.Name == "importer_default_path_container") { continue; } BodyTemplate currentBody = new BodyTemplate(); currentBody.Mass = rawBody.Mass; currentBody.BodyType = rawBody.BodyType; foreach (RawFixtureTemplate rawFixture in rawBody.Fixtures) { List <Polygon> paths = parser.ParseSVGPath(rawFixture.Path, rawFixture.Transformation * matScale); for (int i = 0; i < paths.Count; i++) { if (paths[i].Closed) { List <Vertices> partition = Triangulate.ConvexPartition(paths[i].Vertices, TriangulationAlgorithm.Bayazit); foreach (Vertices v in partition) { currentBody.Fixtures.Add(new FixtureTemplate { Shape = new PolygonShape(v, rawFixture.Density), Restitution = rawFixture.Restitution, Friction = rawFixture.Friction, Name = rawFixture.Name }); } } else { Shape shape; if (paths[i].Vertices.Count > 2) { shape = new ChainShape(paths[i].Vertices); } else { shape = new EdgeShape(paths[i].Vertices[0], paths[i].Vertices[1]); } currentBody.Fixtures.Add(new FixtureTemplate { Shape = shape, Restitution = rawFixture.Restitution, Friction = rawFixture.Friction, Name = rawFixture.Name }); } } } if (currentBody.Fixtures.Count > 0) { bodies[rawBody.Name] = currentBody; currentBody = null; } } return(bodies); }