0

I am currently developing a level editor, and I've gotten to the point where I have collected a list of vertices that I want to use to create a Polygon collider.

I have obtained these vertices by tagging certain tiles as "colliders" and running them through an algorithm to get a list of tiles that are connected. I then created a list of vertices from the list of connected tiles and removed any duplicates.

Below is an image that will help explain. All of the dots are verticies that are currently in my list, but I want to use the ones colored red to create a polygon.

enter image description here

4

1 回答 1

0

这就是我最终解决问题的方式。每个顶点是四个不同图块的一部分(不包括地图边缘),所以我只是遍历每个顶点并计算有多少邻居是“相同类型的”。如果结果为 4,则表示顶点位于多边形中间的某个位置。如果结果为 3,则表示顶点位于内角。2 表示它处于边缘。1 表示它是一个外角。

private List<Vector2> GetPerimeterOfVerticeList(List<Vector2> vertices, TileType type)
{
    int neighborCount = 0;
    List<Vector2> perimeter = new List<Vector2>();

    //Check the four tiles touching this vertex
    foreach (Vector2 v in vertices)
    {
        //upper left tile
        if (v.x - 1 >= 0 && v.y <= gridHeight - 1 && grid[(int)v.x - 1, (int)v.y].type == type)
        {
            neighborCount++;
        }
        //upper right
        if (v.x <= gridWidth - 1 && v.y <= gridHeight - 1 && grid[(int)v.x, (int)v.y].type == type)
        {
            neighborCount++;
        }
        //bottom right
        if (v.y - 1 >= 0 && v.x <= gridWidth - 1 && grid[(int)v.x, (int)v.y - 1].type == type)
        {
            neighborCount++;
        }
        //bottom left
        if (v.y - 1 >= 0 && v.x - 1 >= 0 && grid[(int)v.x - 1, (int)v.y - 1].type == type)
        {
            neighborCount++;
        }


        //If we have less than 4 neighbors, it means we are on the edge. 3 is an inner corner, 2 is a side piece, 1 is an outer corner
        if (neighborCount < 4)
        {
            perimeter.Add(v);
        }

        //Reset the neighbor count back to 0 for the next vertex check.
        neighborCount = 0;
    }
    return perimeter;
}
于 2016-10-30T07:06:26.973 回答