Exemple #1
0
        void generateTerrain(int gx, int gy)
        {
            float ax = gx * cellSize;
            float ay = gy * cellSize;

            var polys = MarchingSquares.detectSquares(new AABB(new Vector2(ax, ay), new Vector2(ax + cellSize, ay + cellSize)), subCellSize, subCellSize, _terrainMap, iterations, true);

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

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

            // create the scale vector
            var scale = new Vector2(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);
                var simplified = SimplifyTools.collinearSimplify(item);

                var decompPolys = Triangulate.convexPartition(simplified, decomposer);
                foreach (Vertices poly in decompPolys)
                {
                    if (poly.Count > 2)
                    {
                        _bodyMap[gx, gy].Add(BodyFactory.createPolygon(world, poly, 1));
                    }
                }
            }
        }
Exemple #2
0
        private void GenerateTerrain(int gx, int gy)
        {
            float ax = gx * CellSize;
            float ay = gy * CellSize;

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

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

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

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

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

                List <Vertices> decompPolys = Triangulate.ConvexPartition(p, Decomposer);

                foreach (Vertices poly in decompPolys)
                {
                    if (poly.Count > 2)
                    {
                        _bodyMap[gx, gy].Add(BodyFactory.CreatePolygon(World, poly, 1));
                    }
                }
            }
        }