2016-01-30 46 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) –