/// <summary> /// Convert a closed path into a polygon. /// Convex decomposition is automatically performed. /// </summary> /// <param name="path">The path.</param> /// <param name="body">The body.</param> /// <param name="density">The density.</param> /// <param name="subdivisions">The subdivisions.</param> public static void ConvertPathToPolygon(Path path, Body body, float density, int subdivisions) { if (!path.Closed) throw new Exception("The path must be closed to convert to a polygon."); List<Vector2> verts = path.GetVertices(subdivisions); List<Vertices> decomposedVerts = EarclipDecomposer.ConvexPartition(new Vertices(verts)); //List<Vertices> decomposedVerts = BayazitDecomposer.ConvexPartition(new Vertices(verts)); foreach (Vertices item in decomposedVerts) { body.CreateFixture(new PolygonShape(item, density)); } }
/// <summary> /// Duplicates the given Body along the given path for approximatly the given copies. /// </summary> /// <param name="world">The world.</param> /// <param name="path">The path.</param> /// <param name="shapes">The shapes.</param> /// <param name="type">The type.</param> /// <param name="copies">The copies.</param> /// <returns></returns> public static List<Body> EvenlyDistributeShapesAlongPath(World world, Path path, IEnumerable<Shape> shapes, BodyType type, int copies, Object userData) { List<Vector3> centers = path.SubdivideEvenly(copies); List<Body> bodyList = new List<Body>(); for (int i = 0; i < centers.Count; i++) { Body b = new Body(world); // copy the type from original body b.BodyType = type; b.Position = new Vector2(centers[i].X, centers[i].Y); b.Rotation = centers[i].Z; foreach (Shape shape in shapes) { b.CreateFixture(shape, userData); } bodyList.Add(b); } return bodyList; }
/// <summary> /// Special overload that takes in an existing body. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="density">The density.</param> /// <param name="offset">The offset. The new shape is offset by this value</param> /// <param name="body">The body.</param> /// <returns></returns> public static Fixture CreateRectangle(float width, float height, float density, Vector2 offset, Body body, Object userData) { Vertices rectangleVertices = PolygonTools.CreateRectangle(width/2, height/2); rectangleVertices.Translate(ref offset); PolygonShape rectangleShape = new PolygonShape(rectangleVertices, density); return body.CreateFixture(rectangleShape, userData); }
//Contributed by Matthew Bettcher /// <summary> /// Convert a path into a set of edges and attaches them to the specified body. /// Note: use only for static edges. /// </summary> /// <param name="path">The path.</param> /// <param name="body">The body.</param> /// <param name="subdivisions">The subdivisions.</param> public static void ConvertPathToEdges(Path path, Body body, int subdivisions) { List<Vector2> verts = path.GetVertices(subdivisions); for (int i = 1; i < verts.Count; i++) { body.CreateFixture(new PolygonShape(PolygonTools.CreateEdge(verts[i], verts[i - 1]), 0)); } if (path.Closed) { body.CreateFixture(new PolygonShape(PolygonTools.CreateEdge(verts[verts.Count - 1], verts[0]), 0)); } }
public static Fixture CreateLoopShape(Vertices vertices, float density, Body body, Object userData) { LoopShape shape = new LoopShape(vertices, density); return body.CreateFixture(shape, userData); }
public static Fixture CreatePolygon(Vertices vertices, float density, Body body, Object userData) { if (vertices.Count <= 1) throw new ArgumentOutOfRangeException("vertices", "Too few points to be a polygon"); PolygonShape polygon = new PolygonShape(vertices, density); return body.CreateFixture(polygon, userData); }
public static Fixture CreateEllipse(float xRadius, float yRadius, int edges, float density, Body body, Object userData) { if (xRadius <= 0) throw new ArgumentOutOfRangeException("xRadius", "X-radius must be more than 0"); if (yRadius <= 0) throw new ArgumentOutOfRangeException("yRadius", "Y-radius must be more than 0"); Vertices ellipseVertices = PolygonTools.CreateEllipse(xRadius, yRadius, edges); PolygonShape polygonShape = new PolygonShape(ellipseVertices, density); return body.CreateFixture(polygonShape, userData); }
public static Fixture CreateEdge(Vector2 start, Vector2 end, Body body, Object userData) { EdgeShape edgeShape = new EdgeShape(start, end); return body.CreateFixture(edgeShape, userData); }
public static List<Fixture> CreateCompoundPolygon(List<Vertices> list, float density, Body body, Object userData) { List<Fixture> fixtures = new List<Fixture>(list.Count); //Then we create several fixtures using the body foreach (Vertices vertices in list) { PolygonShape shape = new PolygonShape(vertices, density); fixtures.Add(body.CreateFixture(shape, userData)); } return fixtures; }
public static Fixture CreateCircle(float radius, float density, Body body, Vector2 offset, Object userData) { if (radius <= 0) throw new ArgumentOutOfRangeException("radius", "Radius must be more than 0 meters"); CircleShape circleShape = new CircleShape(radius, density); circleShape.Position = offset; return body.CreateFixture(circleShape, userData); }
public BreakableBody(IEnumerable<Vertices> vertices, World world, float density, Object userData) { _world = world; _world.ContactManager.PostSolve += PostSolve; MainBody = new Body(_world); MainBody.BodyType = BodyType.Dynamic; foreach (Vertices part in vertices) { PolygonShape polygonShape = new PolygonShape(part, density); Fixture fixture = MainBody.CreateFixture(polygonShape, userData); Parts.Add(fixture); } }