2016-10-30 65 views
0

我目前正在開發一個關卡編輯器,並且我已經收集了我想要用來創建多邊形對撞機的頂點列表。獲取頂點列表的邊界

我已經通過將某些圖塊標記爲「colliders」並通過算法運行它們來獲取這些頂點,以獲取已連接的圖塊列表。然後,我從連接的瓷磚列表中創建了一個頂點列表,並刪除了任何重複項。

下面是一張可以幫助解釋的圖片。所有的點都是當前在我列表中的verticies,但我想使用紅色的創建一個多邊形。

enter image description here

+0

「凸包」將是很好的搜索詞......但你正在尋找的東西少定義 - 如果你提供你如何定義更好的解釋,如果對點屬於邊境你可能會回答這裏或也許在[Cs.se] –

+0

這就是我試圖找到的。我目前還沒有辦法找到頂點是在側面還是在角落,或者兩者都不是。 – user1801067

+0

你必須盯着圖片並找出繪製時使用的規則。還可以嘗試更有趣的圖片(即用點繪製字母併爲它們找到邊框)。在那一點上,你希望能更好地瞭解你想要什麼,並能夠澄清這個問題。 –

回答

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; 
}