Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        //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]));
                }
            }
        }
Exemplo n.º 3
0
        public static FSFixture AttachRectangle(float width, float height, float density, FVector2 offset, FSBody 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));
        }
Exemplo n.º 4
0
        public static FSFixture AttachPolygon(Vertices vertices, float density, FSBody 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));
        }
Exemplo n.º 5
0
        public static FSFixture AttachCircle(float radius, float density, FSBody body, object userData)
        {
            if (radius <= 0)
            {
                throw new ArgumentOutOfRangeException("radius", "Radius must be more than 0 meters");
            }

            CircleShape circleShape = new CircleShape(radius, density);

            return(body.CreateFixture(circleShape, userData));
        }
Exemplo n.º 6
0
        public static List <FSFixture> AttachCompoundPolygon(List <Vertices> list, float density, FSBody body, object userData)
        {
            List <FSFixture> res = new List <FSFixture>(list.Count);

            //Then we create several fixtures using the body
            foreach (Vertices vertices in list)
            {
                if (vertices.Count == 2)
                {
                    EdgeShape shape = new EdgeShape(vertices[0], vertices[1]);
                    res.Add(body.CreateFixture(shape, userData));
                }
                else
                {
                    PolygonShape shape = new PolygonShape(vertices, density);
                    res.Add(body.CreateFixture(shape, userData));
                }
            }

            return(res);
        }
Exemplo n.º 7
0
        public static FSBody CreateCapsule(FSWorld world, float height, float endRadius, float density,
                                           object userData)
        {
            //Create the middle rectangle
            Vertices rectangle = PolygonTools.CreateRectangle(endRadius, height / 2);

            List <Vertices> list = new List <Vertices>();

            list.Add(rectangle);

            FSBody body = CreateCompoundPolygon(world, list, density, userData);

            //Create the two circles
            CircleShape topCircle = new CircleShape(endRadius, density);

            topCircle.Position = new FVector2(0, height / 2);
            body.CreateFixture(topCircle, userData);

            CircleShape bottomCircle = new CircleShape(endRadius, density);

            bottomCircle.Position = new FVector2(0, -(height / 2));
            body.CreateFixture(bottomCircle, userData);
            return(body);
        }
Exemplo n.º 8
0
        /// <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));
            }
        }
Exemplo n.º 9
0
        public static FSFixture AttachEllipse(float xRadius, float yRadius, int edges, float density, FSBody 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));
        }
Exemplo n.º 10
0
        public static FSBody CreateRectangle(FSWorld world, float width, float height, float density, FVector2 position,
                                             object userData)
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException("width", "Width must be more than 0 meters");
            }

            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height", "Height must be more than 0 meters");
            }

            FSBody       newBody           = CreateBody(world, position);
            Vertices     rectangleVertices = PolygonTools.CreateRectangle(width / 2, height / 2);
            PolygonShape rectangleShape    = new PolygonShape(rectangleVertices, density);

            newBody.CreateFixture(rectangleShape, userData);

            return(newBody);
        }
    public virtual void Start()
    {
        if (initialized)
        {
            return;
        }
        initialized = true;
        //Body = BodyFactory.CreateRectangle(FSWorldComponent.PhysicsWorld, 1f, 1f, Density);
        body = new FSBody(FSWorldComponent.PhysicsWorld);
        FSShapeComponent[] shapecs = GetComponentsInChildren <FSShapeComponent>();
        //print("shapes " + name + ": " + shapecs.Length);
        foreach (FSShapeComponent shp in shapecs)
        {
            FSFixture fixture = body.CreateFixture(shp.GetShape());
            fixture.Friction    = shp.Friction;
            fixture.Restitution = shp.Restitution;
            if (shp.tag.Length > 0)
            {
                fixture.UserTag = shp.tag;
            }

            if (shp.CollisionFilter == CollisionGroupDef.Manually)
            {
                fixture.CollisionCategories = shp.BelongsTo;
                fixture.CollidesWith        = shp.CollidesWith;
            }
            else if (shp.CollisionFilter == CollisionGroupDef.PresetFile)
            {
                if (shp.CollisionGroup != null)
                {
                    fixture.CollisionCategories = shp.CollisionGroup.BelongsTo;
                    fixture.CollidesWith        = shp.CollisionGroup.CollidesWith;
                }
            }
        }
        // try to get a single shape at the same level
        // if theres no children
        if (shapecs.Length < 1)
        {
            var shape = GetComponent <FSShapeComponent>();
            if (shape != null)
            {
                var fixture = body.CreateFixture(shape.GetShape());
                fixture.Friction    = shape.Friction;
                fixture.Restitution = shape.Restitution;
                if (shape.tag.Length > 0)
                {
                    fixture.UserTag = shape.tag;
                }
                if (shape.CollisionFilter == CollisionGroupDef.Manually)
                {
                    fixture.CollisionCategories = shape.BelongsTo;
                    fixture.CollidesWith        = shape.CollidesWith;
                }
                else if (shape.CollisionFilter == CollisionGroupDef.PresetFile)
                {
                    if (shape.CollisionGroup != null)
                    {
                        fixture.CollisionCategories = shape.CollisionGroup.BelongsTo;
                        fixture.CollidesWith        = shape.CollisionGroup.CollidesWith;
                    }
                }
            }
        }

        body.BodyType = Type;
        body.Position = new FVector2(transform.position.x, transform.position.y);
        body.Rotation = transform.rotation.eulerAngles.z * Mathf.Deg2Rad;
        if (this.tag.Length > 0)
        {
            body.UserTag = this.tag;
        }
        body.UserFSBodyComponent = this;
    }
Exemplo n.º 12
0
        public static FSFixture AttachChainShape(Vertices vertices, FSBody body, object userData)
        {
            ChainShape shape = new ChainShape(vertices);

            return(body.CreateFixture(shape, userData));
        }
Exemplo n.º 13
0
        public static FSFixture AttachEdge(FVector2 start, FVector2 end, FSBody body, object userData)
        {
            EdgeShape edgeShape = new EdgeShape(start, end);

            return(body.CreateFixture(edgeShape, userData));
        }