Exemplo n.º 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));
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static FP Ylerp(FP y0, FP y1, FP x, FP v0, FP v1, sbyte[,] f, int c)
        {
            FP   fP   = MarchingSquares.Lerp(y0, y1, v0, v1);
            bool flag = c == 0;
            FP   result;

            if (flag)
            {
                result = fP;
            }
            else
            {
                sbyte value = f[(int)((long)x), (int)((long)fP)];
                bool  flag2 = v0 * (int)value < 0;
                if (flag2)
                {
                    result = MarchingSquares.Ylerp(y0, fP, x, v0, (int)value, f, c - 1);
                }
                else
                {
                    result = MarchingSquares.Ylerp(fP, y1, x, (int)value, v1, f, c - 1);
                }
            }
            return(result);
        }
Exemplo n.º 3
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));
                    }
                }
            }
        }
Exemplo n.º 4
0
        private static void combLeft(ref MarchingSquares.GeomPoly polya, ref MarchingSquares.GeomPoly polyb)
        {
            MarchingSquares.CxFastList <TSVector2>     points          = polya.Points;
            MarchingSquares.CxFastList <TSVector2>     points2         = polyb.Points;
            MarchingSquares.CxFastListNode <TSVector2> cxFastListNode  = points.Begin();
            MarchingSquares.CxFastListNode <TSVector2> cxFastListNode2 = points2.Begin();
            TSVector2 tSVector = cxFastListNode2.Elem();

            MarchingSquares.CxFastListNode <TSVector2> cxFastListNode3 = null;
            while (cxFastListNode != points.End())
            {
                TSVector2 tSVector2 = cxFastListNode.Elem();
                bool      flag      = MarchingSquares.VecDsq(tSVector2, tSVector) < Settings.Epsilon;
                if (flag)
                {
                    bool flag2 = cxFastListNode3 != null;
                    if (flag2)
                    {
                        TSVector2 value = cxFastListNode3.Elem();
                        tSVector = cxFastListNode2.Next().Elem();
                        TSVector2 a     = tSVector2 - value;
                        TSVector2 b     = tSVector - tSVector2;
                        FP        fP    = MarchingSquares.VecCross(a, b);
                        bool      flag3 = fP * fP < Settings.Epsilon;
                        if (flag3)
                        {
                            points.Erase(cxFastListNode3, cxFastListNode);
                            polya.Length--;
                            cxFastListNode = cxFastListNode3;
                        }
                    }
                    bool flag4 = true;
                    MarchingSquares.CxFastListNode <TSVector2> cxFastListNode4 = null;
                    while (!points2.Empty())
                    {
                        TSVector2 value2 = points2.Front();
                        points2.Pop();
                        bool flag5 = !flag4 && !points2.Empty();
                        if (flag5)
                        {
                            cxFastListNode = points.Insert(cxFastListNode, value2);
                            polya.Length++;
                            cxFastListNode4 = cxFastListNode;
                        }
                        flag4 = false;
                    }
                    cxFastListNode = cxFastListNode.Next();
                    TSVector2 tSVector3 = cxFastListNode.Elem();
                    cxFastListNode = cxFastListNode.Next();
                    bool flag6 = cxFastListNode == points.End();
                    if (flag6)
                    {
                        cxFastListNode = points.Begin();
                    }
                    TSVector2 value3 = cxFastListNode.Elem();
                    TSVector2 value4 = cxFastListNode4.Elem();
                    TSVector2 a2     = tSVector3 - value4;
                    TSVector2 b2     = value3 - tSVector3;
                    FP        fP2    = MarchingSquares.VecCross(a2, b2);
                    bool      flag7  = fP2 * fP2 < Settings.Epsilon;
                    if (flag7)
                    {
                        points.Erase(cxFastListNode4, cxFastListNode4.Next());
                        polya.Length--;
                    }
                    break;
                }
                cxFastListNode3 = cxFastListNode;
                cxFastListNode  = cxFastListNode.Next();
            }
        }
Exemplo n.º 5
0
        private static int MarchSquare(sbyte[,] f, sbyte[,] fs, ref MarchingSquares.GeomPoly poly, int ax, int ay, FP x0, FP y0, FP x1, FP y1, int bin)
        {
            int   num  = 0;
            sbyte b    = fs[ax, ay];
            bool  flag = b < 0;

            if (flag)
            {
                num |= 8;
            }
            sbyte b2    = fs[ax + 1, ay];
            bool  flag2 = b2 < 0;

            if (flag2)
            {
                num |= 4;
            }
            sbyte b3    = fs[ax + 1, ay + 1];
            bool  flag3 = b3 < 0;

            if (flag3)
            {
                num |= 2;
            }
            sbyte b4    = fs[ax, ay + 1];
            bool  flag4 = b4 < 0;

            if (flag4)
            {
                num |= 1;
            }
            int  num2  = MarchingSquares._lookMarch[num];
            bool flag5 = num2 != 0;

            if (flag5)
            {
                MarchingSquares.CxFastListNode <TSVector2> node = null;
                for (int i = 0; i < 8; i++)
                {
                    bool flag6 = (num2 & 1 << i) != 0;
                    if (flag6)
                    {
                        bool flag7 = i == 7 && (num2 & 1) == 0;
                        if (flag7)
                        {
                            MarchingSquares.CxFastList <TSVector2> arg_EA_0 = poly.Points;
                            TSVector2 value = new TSVector2(x0, MarchingSquares.Ylerp(y0, y1, x0, (int)b, (int)b4, f, bin));
                            arg_EA_0.Add(value);
                        }
                        else
                        {
                            bool      flag8 = i == 0;
                            TSVector2 value;
                            if (flag8)
                            {
                                value = new TSVector2(x0, y0);
                            }
                            else
                            {
                                bool flag9 = i == 2;
                                if (flag9)
                                {
                                    value = new TSVector2(x1, y0);
                                }
                                else
                                {
                                    bool flag10 = i == 4;
                                    if (flag10)
                                    {
                                        value = new TSVector2(x1, y1);
                                    }
                                    else
                                    {
                                        bool flag11 = i == 6;
                                        if (flag11)
                                        {
                                            value = new TSVector2(x0, y1);
                                        }
                                        else
                                        {
                                            bool flag12 = i == 1;
                                            if (flag12)
                                            {
                                                value = new TSVector2(MarchingSquares.Xlerp(x0, x1, y0, (int)b, (int)b2, f, bin), y0);
                                            }
                                            else
                                            {
                                                bool flag13 = i == 5;
                                                if (flag13)
                                                {
                                                    value = new TSVector2(MarchingSquares.Xlerp(x0, x1, y1, (int)b4, (int)b3, f, bin), y1);
                                                }
                                                else
                                                {
                                                    bool flag14 = i == 3;
                                                    if (flag14)
                                                    {
                                                        value = new TSVector2(x1, MarchingSquares.Ylerp(y0, y1, x1, (int)b2, (int)b3, f, bin));
                                                    }
                                                    else
                                                    {
                                                        value = new TSVector2(x0, MarchingSquares.Ylerp(y0, y1, x0, (int)b, (int)b4, f, bin));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            node = poly.Points.Insert(node, value);
                        }
                        poly.Length++;
                    }
                }
            }
            return(num);
        }
Exemplo n.º 6
0
        public static List <Vertices> DetectSquares(AABB domain, FP cellWidth, FP cellHeight, sbyte[,] f, int lerpCount, bool combine)
        {
            MarchingSquares.CxFastList <MarchingSquares.GeomPoly> cxFastList = new MarchingSquares.CxFastList <MarchingSquares.GeomPoly>();
            List <Vertices> list  = new List <Vertices>();
            int             num   = (int)((long)(domain.Extents.x * 2 / cellWidth));
            bool            flag  = num == domain.Extents.x * 2 / cellWidth;
            int             num2  = (int)((long)(domain.Extents.y * 2 / cellHeight));
            bool            flag2 = num2 == domain.Extents.y * 2 / cellHeight;
            bool            flag3 = !flag;

            if (flag3)
            {
                num++;
            }
            bool flag4 = !flag2;

            if (flag4)
            {
                num2++;
            }
            sbyte[,] array = new sbyte[num + 1, num2 + 1];
            MarchingSquares.GeomPolyVal[,] array2 = new MarchingSquares.GeomPolyVal[num + 1, num2 + 1];
            for (int i = 0; i < num + 1; i++)
            {
                bool flag5 = i == num;
                int  num3;
                if (flag5)
                {
                    num3 = (int)((long)domain.UpperBound.x);
                }
                else
                {
                    num3 = (int)((long)(i * cellWidth + domain.LowerBound.x));
                }
                for (int j = 0; j < num2 + 1; j++)
                {
                    bool flag6 = j == num2;
                    int  num4;
                    if (flag6)
                    {
                        num4 = (int)((long)domain.UpperBound.y);
                    }
                    else
                    {
                        num4 = (int)((long)(j * cellHeight + domain.LowerBound.y));
                    }
                    array[i, j] = f[num3, num4];
                }
            }
            for (int k = 0; k < num2; k++)
            {
                FP   fP    = k * cellHeight + domain.LowerBound.y;
                bool flag7 = k == num2 - 1;
                FP   y;
                if (flag7)
                {
                    y = domain.UpperBound.y;
                }
                else
                {
                    y = fP + cellHeight;
                }
                MarchingSquares.GeomPoly geomPoly = null;
                for (int l = 0; l < num; l++)
                {
                    FP   fP2   = l * cellWidth + domain.LowerBound.x;
                    bool flag8 = l == num - 1;
                    FP   x;
                    if (flag8)
                    {
                        x = domain.UpperBound.x;
                    }
                    else
                    {
                        x = fP2 + cellWidth;
                    }
                    MarchingSquares.GeomPoly geomPoly2 = new MarchingSquares.GeomPoly();
                    int  num5  = MarchingSquares.MarchSquare(f, array, ref geomPoly2, l, k, fP2, fP, x, y, lerpCount);
                    bool flag9 = geomPoly2.Length != 0;
                    if (flag9)
                    {
                        bool flag10 = combine && geomPoly != null && (num5 & 9) != 0;
                        if (flag10)
                        {
                            MarchingSquares.combLeft(ref geomPoly, ref geomPoly2);
                            geomPoly2 = geomPoly;
                        }
                        else
                        {
                            cxFastList.Add(geomPoly2);
                        }
                        array2[l, k] = new MarchingSquares.GeomPolyVal(geomPoly2, num5);
                    }
                    else
                    {
                        geomPoly2 = null;
                    }
                    geomPoly = geomPoly2;
                }
            }
            bool            flag11 = !combine;
            List <Vertices> result;

            if (flag11)
            {
                List <MarchingSquares.GeomPoly> listOfElements = cxFastList.GetListOfElements();
                foreach (MarchingSquares.GeomPoly current in listOfElements)
                {
                    list.Add(new Vertices(current.Points.GetListOfElements()));
                }
                result = list;
            }
            else
            {
                for (int m = 1; m < num2; m++)
                {
                    int n = 0;
                    while (n < num)
                    {
                        MarchingSquares.GeomPolyVal geomPolyVal = array2[n, m];
                        bool flag12 = geomPolyVal == null;
                        if (flag12)
                        {
                            n++;
                        }
                        else
                        {
                            bool flag13 = (geomPolyVal.Key & 12) == 0;
                            if (flag13)
                            {
                                n++;
                            }
                            else
                            {
                                MarchingSquares.GeomPolyVal geomPolyVal2 = array2[n, m - 1];
                                bool flag14 = geomPolyVal2 == null;
                                if (flag14)
                                {
                                    n++;
                                }
                                else
                                {
                                    bool flag15 = (geomPolyVal2.Key & 3) == 0;
                                    if (flag15)
                                    {
                                        n++;
                                    }
                                    else
                                    {
                                        FP fP3 = n * cellWidth + domain.LowerBound.x;
                                        FP y2  = m * cellHeight + domain.LowerBound.y;
                                        MarchingSquares.CxFastList <TSVector2> points  = geomPolyVal.GeomP.Points;
                                        MarchingSquares.CxFastList <TSVector2> points2 = geomPolyVal2.GeomP.Points;
                                        bool flag16 = geomPolyVal2.GeomP == geomPolyVal.GeomP;
                                        if (flag16)
                                        {
                                            n++;
                                        }
                                        else
                                        {
                                            MarchingSquares.CxFastListNode <TSVector2> cxFastListNode = points.Begin();
                                            while (MarchingSquares.Square(cxFastListNode.Elem().y - y2) > Settings.Epsilon || cxFastListNode.Elem().x < fP3)
                                            {
                                                cxFastListNode = cxFastListNode.Next();
                                            }
                                            TSVector2 tSVector = cxFastListNode.Next().Elem();
                                            bool      flag17   = MarchingSquares.Square(tSVector.y - y2) > Settings.Epsilon;
                                            if (flag17)
                                            {
                                                n++;
                                            }
                                            else
                                            {
                                                bool flag18 = true;
                                                MarchingSquares.CxFastListNode <TSVector2> cxFastListNode2;
                                                for (cxFastListNode2 = points2.Begin(); cxFastListNode2 != points2.End(); cxFastListNode2 = cxFastListNode2.Next())
                                                {
                                                    bool flag19 = MarchingSquares.VecDsq(cxFastListNode2.Elem(), tSVector) < Settings.Epsilon;
                                                    if (flag19)
                                                    {
                                                        flag18 = false;
                                                        break;
                                                    }
                                                }
                                                bool flag20 = flag18;
                                                if (flag20)
                                                {
                                                    n++;
                                                }
                                                else
                                                {
                                                    MarchingSquares.CxFastListNode <TSVector2> cxFastListNode3 = cxFastListNode.Next().Next();
                                                    bool flag21 = cxFastListNode3 == points.End();
                                                    if (flag21)
                                                    {
                                                        cxFastListNode3 = points.Begin();
                                                    }
                                                    while (cxFastListNode3 != cxFastListNode)
                                                    {
                                                        cxFastListNode2 = points2.Insert(cxFastListNode2, cxFastListNode3.Elem());
                                                        cxFastListNode3 = cxFastListNode3.Next();
                                                        bool flag22 = cxFastListNode3 == points.End();
                                                        if (flag22)
                                                        {
                                                            cxFastListNode3 = points.Begin();
                                                        }
                                                        geomPolyVal2.GeomP.Length++;
                                                    }
                                                    fP3 = n + 1;
                                                    while (fP3 < num)
                                                    {
                                                        MarchingSquares.GeomPolyVal geomPolyVal3 = array2[(int)((long)fP3), m];
                                                        bool flag23 = geomPolyVal3 == null || geomPolyVal3.GeomP != geomPolyVal.GeomP;
                                                        if (flag23)
                                                        {
                                                            fP3 += 1;
                                                        }
                                                        else
                                                        {
                                                            geomPolyVal3.GeomP = geomPolyVal2.GeomP;
                                                            fP3 += 1;
                                                        }
                                                    }
                                                    fP3 = n - 1;
                                                    while (fP3 >= 0)
                                                    {
                                                        MarchingSquares.GeomPolyVal geomPolyVal4 = array2[(int)((long)fP3), m];
                                                        bool flag24 = geomPolyVal4 == null || geomPolyVal4.GeomP != geomPolyVal.GeomP;
                                                        if (flag24)
                                                        {
                                                            fP3 -= 1;
                                                        }
                                                        else
                                                        {
                                                            geomPolyVal4.GeomP = geomPolyVal2.GeomP;
                                                            fP3 -= 1;
                                                        }
                                                    }
                                                    cxFastList.Remove(geomPolyVal.GeomP);
                                                    geomPolyVal.GeomP = geomPolyVal2.GeomP;
                                                    n = (int)((long)((cxFastListNode.Next().Elem().x - domain.LowerBound.x) / cellWidth)) + 1;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                List <MarchingSquares.GeomPoly> listOfElements = cxFastList.GetListOfElements();
                foreach (MarchingSquares.GeomPoly current2 in listOfElements)
                {
                    list.Add(new Vertices(current2.Points.GetListOfElements()));
                }
                result = list;
            }
            return(result);
        }