Exemple #1
0
        private void GenerateTerrain(int gx, int gy)
        {
            FP x  = gx * this.CellSize;
            FP fP = gy * this.CellSize;
            List <Vertices> list = MarchingSquares.DetectSquares(new AABB(new TSVector2(x, fP), new TSVector2(x + this.CellSize, fP + this.CellSize)), this.SubCellSize, this.SubCellSize, this._terrainMap, this.Iterations, true);
            bool            flag = list.Count == 0;

            if (!flag)
            {
                this._bodyMap[gx, gy] = new List <Body>();
                TSVector2 tSVector = new TSVector2(1f / (float)this.PointsPerUnit, 1f / (float)(-(float)this.PointsPerUnit));
                foreach (Vertices current in list)
                {
                    current.Scale(ref tSVector);
                    current.Translate(ref this._topLeft);
                    Vertices        vertices = SimplifyTools.CollinearSimplify(current, FP.Zero);
                    List <Vertices> list2    = Triangulate.ConvexPartition(vertices, this.Decomposer, true, FP.EN3);
                    foreach (Vertices current2 in list2)
                    {
                        bool flag2 = current2.Count > 2;
                        if (flag2)
                        {
                            this._bodyMap[gx, gy].Add(BodyFactory.CreatePolygon(this.World, current2, 1, null));
                        }
                    }
                }
            }
        }
Exemple #2
0
        private static bool ValidatePolygon(Vertices polygon)
        {
            PolygonError polygonError = polygon.CheckPolygon();
            bool         flag         = polygonError == PolygonError.InvalidAmountOfVertices || polygonError == PolygonError.AreaTooSmall || polygonError == PolygonError.SideTooSmall || polygonError == PolygonError.NotSimple;
            bool         result;

            if (flag)
            {
                result = false;
            }
            else
            {
                bool flag2 = polygonError == PolygonError.NotCounterClockWise;
                if (flag2)
                {
                    polygon.Reverse();
                }
                bool flag3 = polygonError == PolygonError.NotConvex;
                if (flag3)
                {
                    polygon = GiftWrap.GetConvexHull(polygon);
                    result  = Triangulate.ValidatePolygon(polygon);
                }
                else
                {
                    result = true;
                }
            }
            return(result);
        }
Exemple #3
0
        public static BreakableBody CreateBreakableBody(World world, Vertices vertices, FP density, TSVector2 position)
        {
            List <Vertices> vertices2     = Triangulate.ConvexPartition(vertices, TriangulationAlgorithm.Earclip, true, FP.EN3);
            BreakableBody   breakableBody = new BreakableBody(vertices2, world, density);

            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);
            return(breakableBody);
        }
Exemple #4
0
        public static List <Fixture> AttachSolidArc(FP density, FP radians, int sides, FP radius, TSVector2 position, FP angle, Body body)
        {
            Vertices vertices = PolygonTools.CreateArc(radians, sides, radius);

            vertices.Rotate((MathHelper.Pi - radians) / 2 + angle);
            vertices.Translate(ref position);
            vertices.Add(vertices[0]);
            List <Vertices> list = Triangulate.ConvexPartition(vertices, TriangulationAlgorithm.Earclip, true, FP.EN3);

            return(FixtureFactory.AttachCompoundPolygon(list, density, body, null));
        }
        /// <summary>
        /// Creates a rounded rectangle.
        /// Note: Automatically decomposes the capsule if it contains too many vertices (controlled by Settings.MaxPolygonVertices)
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="xRadius">The x radius.</param>
        /// <param name="yRadius">The y radius.</param>
        /// <param name="segments">The segments.</param>
        /// <param name="density">The density.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static Body CreateRoundedRectangle(World world, FP width, FP height, FP xRadius, FP yRadius, int segments, FP density, TSVector2 position, object userData = null)
        {
            Vertices verts = PolygonTools.CreateRoundedRectangle(width, height, xRadius, yRadius, segments);

            //There are too many vertices in the capsule. We decompose it.
            if (verts.Count >= Settings.MaxPolygonVertices)
            {
                List <Vertices> vertList = Triangulate.ConvexPartition(verts, TriangulationAlgorithm.Earclip, true, FP.EN3);
                Body            body     = CreateCompoundPolygon(world, vertList, density, userData);
                body.Position = position;
                return(body);
            }

            return(CreatePolygon(world, verts, density, null));
        }
        public static Body CreateGear(World world, FP radius, int numberOfTeeth, FP tipPercentage, FP toothHeight, FP density, object userData = null)
        {
            Vertices gearPolygon = PolygonTools.CreateGear(radius, numberOfTeeth, tipPercentage, toothHeight);

            //Gears can in some cases be convex
            if (!gearPolygon.IsConvex())
            {
                //Decompose the gear:
                List <Vertices> list = Triangulate.ConvexPartition(gearPolygon, TriangulationAlgorithm.Earclip, true, FP.EN3);

                return(CreateCompoundPolygon(world, list, density, userData));
            }

            return(CreatePolygon(world, gearPolygon, density, userData));
        }
Exemple #7
0
        public static List <Fixture> AttachSolidArc(FP density, FP radians, int sides, FP radius, TSVector2 position, FP angle, Body body)
        {
            Vertices arc = PolygonTools.CreateArc(radians, sides, radius);

            arc.Rotate((FP.Pi - radians) / 2 + angle);

            arc.Translate(ref position);

            //Close the arc
            arc.Add(arc[0]);

            List <Vertices> triangles = Triangulate.ConvexPartition(arc, TriangulationAlgorithm.Earclip, true, FP.EN3);

            return(AttachCompoundPolygon(triangles, density, body));
        }
        /// <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, FP density, int subdivisions)
        {
            if (!path.Closed)
            {
                throw new Exception("The path must be closed to convert to a polygon.");
            }

            List <TSVector2> verts = path.GetVertices(subdivisions);

            List <Vertices> decomposedVerts = Triangulate.ConvexPartition(new Vertices(verts), TriangulationAlgorithm.Bayazit, true, FP.EN3);

            foreach (Vertices item in decomposedVerts)
            {
                body.CreateFixture(new PolygonShape(item, density));
            }
        }
Exemple #9
0
        public static void ConvertPathToPolygon(Path path, Body body, FP density, int subdivisions)
        {
            bool flag = !path.Closed;

            if (flag)
            {
                throw new Exception("The path must be closed to convert to a polygon.");
            }
            List <TSVector2> vertices = path.GetVertices(subdivisions);
            List <Vertices>  list     = Triangulate.ConvexPartition(new Vertices(vertices), TriangulationAlgorithm.Bayazit, true, FP.EN3);

            foreach (Vertices current in list)
            {
                body.CreateFixture(new PolygonShape(current, density), null);
            }
        }
Exemple #10
0
        public static Body CreateGear(World world, FP radius, int numberOfTeeth, FP tipPercentage, FP toothHeight, FP density, object userData = null)
        {
            Vertices vertices = PolygonTools.CreateGear(radius, numberOfTeeth, tipPercentage, toothHeight);
            bool     flag     = !vertices.IsConvex();
            Body     result;

            if (flag)
            {
                List <Vertices> list = Triangulate.ConvexPartition(vertices, TriangulationAlgorithm.Earclip, true, FP.EN3);
                result = BodyFactory.CreateCompoundPolygon(world, list, density, userData);
            }
            else
            {
                result = BodyFactory.CreatePolygon(world, vertices, density, userData);
            }
            return(result);
        }
Exemple #11
0
        public static Body CreateRoundedRectangle(World world, FP width, FP height, FP xRadius, FP yRadius, int segments, FP density, TSVector2 position, object userData = null)
        {
            Vertices vertices = PolygonTools.CreateRoundedRectangle(width, height, xRadius, yRadius, segments);
            bool     flag     = vertices.Count >= Settings.MaxPolygonVertices;
            Body     result;

            if (flag)
            {
                List <Vertices> list = Triangulate.ConvexPartition(vertices, TriangulationAlgorithm.Earclip, true, FP.EN3);
                Body            body = BodyFactory.CreateCompoundPolygon(world, list, density, userData);
                body.Position = position;
                result        = body;
            }
            else
            {
                result = BodyFactory.CreatePolygon(world, vertices, density, null);
            }
            return(result);
        }
        /// <summary>
        /// Creates a capsule.
        /// Note: Automatically decomposes the capsule if it contains too many vertices (controlled by Settings.MaxPolygonVertices)
        /// </summary>
        /// <returns></returns>
        public static Body CreateCapsule(World world, FP height, FP topRadius, int topEdges, FP bottomRadius, int bottomEdges, FP density, TSVector2 position, object userData = null)
        {
            Vertices verts = PolygonTools.CreateCapsule(height, topRadius, topEdges, bottomRadius, bottomEdges);

            Body body;

            //There are too many vertices in the capsule. We decompose it.
            if (verts.Count >= Settings.MaxPolygonVertices)
            {
                List <Vertices> vertList = Triangulate.ConvexPartition(verts, TriangulationAlgorithm.Earclip, true, FP.EN3);
                body          = CreateCompoundPolygon(world, vertList, density, userData);
                body.Position = position;

                return(body);
            }

            body          = CreatePolygon(world, verts, density, userData);
            body.Position = position;

            return(body);
        }
Exemple #13
0
        public static Body CreateCapsule(World world, FP height, FP topRadius, int topEdges, FP bottomRadius, int bottomEdges, FP density, TSVector2 position, object userData = null)
        {
            Vertices vertices = PolygonTools.CreateCapsule(height, topRadius, topEdges, bottomRadius, bottomEdges);
            bool     flag     = vertices.Count >= Settings.MaxPolygonVertices;
            Body     result;

            if (flag)
            {
                List <Vertices> list = Triangulate.ConvexPartition(vertices, TriangulationAlgorithm.Earclip, true, FP.EN3);
                Body            body = BodyFactory.CreateCompoundPolygon(world, list, density, userData);
                body.Position = position;
                result        = body;
            }
            else
            {
                Body body = BodyFactory.CreatePolygon(world, vertices, density, userData);
                body.Position = position;
                result        = body;
            }
            return(result);
        }
Exemple #14
0
        private void GenerateTerrain(int gx, int gy)
        {
            FP ax = gx * CellSize;
            FP ay = gy * CellSize;

            List <Vertices> polys = MarchingSquares.DetectSquares(new AABB(new TSVector2(ax, ay), new TSVector2(ax + CellSize, ay + CellSize)), SubCellSize, SubCellSize, _terrainMap, Iterations, true);

            if (polys.Count == 0)
            {
                return;
            }

            _bodyMap[gx, gy] = new List <Body>();

            // create the scale vector
            TSVector2 scale = new TSVector2(1f / PointsPerUnit, 1f / -PointsPerUnit);

            // create physics object for this grid cell
            foreach (Vertices item in polys)
            {
                // does this need to be negative?
                item.Scale(ref scale);
                item.Translate(ref _topLeft);
                Vertices simplified = SimplifyTools.CollinearSimplify(item, FP.Zero);

                List <Vertices> decompPolys = Triangulate.ConvexPartition(simplified, Decomposer, true, FP.EN3);

                foreach (Vertices poly in decompPolys)
                {
                    if (poly.Count > 2)
                    {
                        _bodyMap[gx, gy].Add(BodyFactory.CreatePolygon(World, poly, 1, null));
                    }
                }
            }
        }
Exemple #15
0
        public static List <Vertices> ConvexPartition(Vertices vertices, TriangulationAlgorithm algorithm, bool discardAndFixInvalid, FP tolerance)
        {
            bool            flag = vertices.Count <= 3;
            List <Vertices> result;

            if (flag)
            {
                result = new List <Vertices>
                {
                    vertices
                };
            }
            else
            {
                List <Vertices> list;
                switch (algorithm)
                {
                case TriangulationAlgorithm.Earclip:
                {
                    bool flag2 = vertices.IsCounterClockWise();
                    if (flag2)
                    {
                        Vertices vertices2 = new Vertices(vertices);
                        vertices2.Reverse();
                        list = EarclipDecomposer.ConvexPartition(vertices2, tolerance);
                    }
                    else
                    {
                        list = EarclipDecomposer.ConvexPartition(vertices, tolerance);
                    }
                    break;
                }

                case TriangulationAlgorithm.Bayazit:
                {
                    bool flag3 = !vertices.IsCounterClockWise();
                    if (flag3)
                    {
                        Vertices vertices3 = new Vertices(vertices);
                        vertices3.Reverse();
                        list = BayazitDecomposer.ConvexPartition(vertices3);
                    }
                    else
                    {
                        list = BayazitDecomposer.ConvexPartition(vertices);
                    }
                    break;
                }

                case TriangulationAlgorithm.Flipcode:
                {
                    bool flag4 = !vertices.IsCounterClockWise();
                    if (flag4)
                    {
                        Vertices vertices4 = new Vertices(vertices);
                        vertices4.Reverse();
                        list = FlipcodeDecomposer.ConvexPartition(vertices4);
                    }
                    else
                    {
                        list = FlipcodeDecomposer.ConvexPartition(vertices);
                    }
                    break;
                }

                case TriangulationAlgorithm.Seidel:
                    list = SeidelDecomposer.ConvexPartition(vertices, tolerance);
                    break;

                case TriangulationAlgorithm.SeidelTrapezoids:
                    list = SeidelDecomposer.ConvexPartitionTrapezoid(vertices, tolerance);
                    break;

                case TriangulationAlgorithm.Delauny:
                    list = CDTDecomposer.ConvexPartition(vertices);
                    break;

                default:
                    throw new ArgumentOutOfRangeException("algorithm");
                }
                if (discardAndFixInvalid)
                {
                    for (int i = list.Count - 1; i >= 0; i--)
                    {
                        Vertices polygon = list[i];
                        bool     flag5   = !Triangulate.ValidatePolygon(polygon);
                        if (flag5)
                        {
                            list.RemoveAt(i);
                        }
                    }
                }
                result = list;
            }
            return(result);
        }