2016-01-30 47 views
2

我正在使用cocos2d-x 3.8。 我嘗試使用以下代碼創建兩個多邊形精靈。cocos2dx檢測與多邊形精靈的交集

我知道我們可以檢測到與BoundingBox相交,但太粗糙了。 此外,我知道我們可以使用Cocos2d-x C++物理引擎來檢測碰撞,但不會浪費移動設備的大量資源?我正在開發的遊戲不需要物理引擎。

有沒有一種方法來檢測多邊形精靈的相交? 謝謝。

auto pinfoTree = AutoPolygon::generatePolygon("Tree.png"); 
auto treeSprite= Sprite::create(pinfoTree); 
treeSprite-> setPosition(width/4 * 3 - 30 , height/2 - 200); 
this->addChild(treeSprite); 

auto pinfoBird = AutoPolygon::generatePolygon("Bird.png"); 
auto Bird= Sprite::create(pinfoTree); 
Bird->setPosition(width/4 * 3, height/2); 
this->addChild(Bird) 
+0

據我所知,現在是不可能的。我在論壇上看過一些他們想要在AutoPolygons之間添加碰撞檢測的地方。現在我想你必須使用物理。 – Makalele

回答

0

我曾經不得不編程一個碰撞檢測算法,其中一個球碰撞一個旋轉的多邊形障礙物。在我的情況下,有一定厚度的弧線的障礙。以及在原點附近移動的位置。基本上它是在一個軌道上旋轉。球也圍繞同一起點的軌道旋轉。它可以在軌道之間移動。爲了檢查碰撞,我必須檢查球的相對於原點的角度是否在弧障礙的下限和上限之間,並檢查球和障礙物是否在同一軌道上。

換句話說,我使用了碰撞中涉及的物體的各種約束和性質,使它更有效率。所以使用你的對象的屬性來引起碰撞。嘗試使用類似的方法取決於你的對象

+0

嘗試發佈更多關於鳥類動作的細節,以及它們的外觀如何,或許我可以進一步提供幫助。 –

-1

這有點複雜:AutoPolygon給你一堆三角形 - PhysicsBody :: createPolygon需要一個凸多邊形順時針纏繞...所以這些是兩個不同的東西。頂點數甚至可能是有限的。我認爲Box2d對於1多邊形的最大數量是8.

如果您想嘗試此操作,則必須合併三角形以形成多邊形。一個選項可以是從一個三角形開始,只要整個事物保持凸起就可以增加更多。如果不能添加更多的三角形,請重新開始一個新的多邊形。將所有多邊形作爲PhysicsShapes添加到物理主體以形成一個複合對象。

我建議,因爲

  1. Autopolygon被渲染優化你不走這條道路 - 不是最適合的 物理學 - 這是有區別的。自動多邊形跟蹤的多邊形將始終比原始精靈大 - 否則,您會看到渲染僞像。
  2. 你必須接近對生成多邊形
  3. 跟蹤形狀在應用程序中沒有控制會增加你的啓動時間
  4. 三角網格和物理輪廓是2件不同的事情

我會嘗試一些不同方法:離線生成碰撞形狀。這給你一堆優勢:

  1. 你可以在可視化編輯器中生成和調整多邊形,例如,通過使用PhysicsEditor
  2. 加載的準備多邊形的方式更快
  3. 您可以設置類似質量等其他參數
  4. 的解決方案是戰鬥證明和開箱

,但如果你想知道多邊形如何相交。你可以看看這段代碼。

// Calculate the projection of a polygon on an axis 
    // and returns it as a [min, max] interval 
    public void ProjectPolygon(Vector axis, Polygon polygon, ref float min, ref float max) { 
     // To project a point on an axis use the dot product 
     float dotProduct = axis.DotProduct(polygon.Points[0]); 
     min = dotProduct; 
     max = dotProduct; 
     for (int i = 0; i < polygon.Points.Count; i++) { 
      flaot d = polygon.Points[i].DotProduct(axis); 
      if (d < min) { 
       min = dotProduct; 
      } else { 
       if (dotProduct> max) { 
        max = dotProduct; 
       } 
      } 
     } 
    } 

    // Calculate the distance between [minA, maxA] and [minB, maxB] 
    // The distance will be negative if the intervals overlap 
    public float IntervalDistance(float minA, float maxA, float minB, float maxB) { 
     if (minA < minB) { 
      return minB - maxA; 
     } else { 
      return minA - maxB; 
     } 
    } 

    // Check if polygon A is going to collide with polygon B. 
    public boolean PolygonCollision(Polygon polygonA, Polygon polygonB) { 

     boolean result = true; 
     int edgeCountA = polygonA.Edges.Count; 
     int edgeCountB = polygonB.Edges.Count; 
     float minIntervalDistance = float.PositiveInfinity; 

     Vector edge; 

     // Loop through all the edges of both polygons 
     for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++) { 
      if (edgeIndex < edgeCountA) { 
       edge = polygonA.Edges[edgeIndex]; 
      } else { 
       edge = polygonB.Edges[edgeIndex - edgeCountA]; 
      } 

      // ===== Find if the polygons are currently intersecting ===== 

      // Find the axis perpendicular to the current edge 
      Vector axis = new Vector(-edge.Y, edge.X); 
      axis.Normalize(); 

      // Find the projection of the polygon on the current axis 
      float minA = 0; float minB = 0; float maxA = 0; float maxB = 0; 
      ProjectPolygon(axis, polygonA, ref minA, ref maxA); 
      ProjectPolygon(axis, polygonB, ref minB, ref maxB); 

      // Check if the polygon projections are currentlty intersecting 
      if (IntervalDistance(minA, maxA, minB, maxB) > 0) 
       result = false; 

     return result; 
     } 
    } 

該功能可以採用這種方式

boolean result = PolygonCollision(polygonA, polygonB); 
+0

歡迎您訪問解決方案的鏈接,但請確保您的答案在沒有它的情況下很有用:[添加鏈接上下文](// meta.stackexchange.com/a/8259),以便您的同行用戶瞭解它是什麼以及爲什麼它在那裏,然後引用您鏈接的頁面中最相關的部分,以防目標頁面不可用。 [只有一個鏈接的答案可能會被刪除。](// stackoverflow.com/help/deleted-answers) – Mogsdad

+0

雖然這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供該鏈接供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/18578070) –