private void Decompose() { //Unsubsribe from the PostSolve delegate _world.ContactManager.PostSolve -= PostSolve; for (int i = 0; i < Parts.Count; i++) { FSFixture fixture = Parts[i]; Shape shape = fixture.Shape.Clone(); object userdata = fixture.UserData; MainBody.DestroyFixture(fixture); FSBody body = BodyFactory.CreateBody(_world); body.BodyType = BodyType.Dynamic; body.Position = MainBody.Position; body.Rotation = MainBody.Rotation; body.UserData = MainBody.UserData; body.CreateFixture(shape, userdata); body.AngularVelocity = _angularVelocitiesCache[i]; body.LinearVelocity = _velocitiesCache[i]; } _world.RemoveBody(MainBody); _world.RemoveBreakableBody(this); }
public FSBreakableBody(IEnumerable<Vertices> vertices, FSWorld world, float density, object userData) { _world = world; _world.ContactManager.PostSolve += PostSolve; MainBody = new FSBody(_world); MainBody.BodyType = BodyType.Dynamic; foreach (Vertices part in vertices) { PolygonShape polygonShape = new PolygonShape(part, density); FSFixture fixture = MainBody.CreateFixture(polygonShape, userData); Parts.Add(fixture); } }
public FSBreakableBody(IEnumerable <Vertices> vertices, FSWorld world, float density, object userData) { _world = world; _world.ContactManager.PostSolve += PostSolve; MainBody = new FSBody(_world); MainBody.BodyType = BodyType.Dynamic; foreach (Vertices part in vertices) { PolygonShape polygonShape = new PolygonShape(part, density); FSFixture fixture = MainBody.CreateFixture(polygonShape, userData); Parts.Add(fixture); } }
/// <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> /// <param name="userData"></param> /// <returns></returns> public static List<FSBody> EvenlyDistributeShapesAlongPath(FSWorld world, Path path, IEnumerable<Shape> shapes, BodyType type, int copies, object userData) { List<FVector3> centers = path.SubdivideEvenly(copies); List<FSBody> bodyList = new List<FSBody>(); for (int i = 0; i < centers.Count; i++) { FSBody b = new FSBody(world); // copy the type from original body b.BodyType = type; b.Position = new FVector2(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> /// 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, FSBody body, float density, int subdivisions) { if (!path.Closed) throw new Exception("The path must be closed to convert to a polygon."); List<FVector2> 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)); } }
//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, FSBody body, int subdivisions) { Vertices verts = path.GetVertices(subdivisions); if (path.Closed) { ChainShape chain = new ChainShape(verts); body.CreateFixture(chain); } else { for (int i = 1; i < verts.Count; i++) { body.CreateFixture(new EdgeShape(verts[i], verts[i - 1])); } } }