libGdx碰撞检测多边形

问题描述:

我最近开始学习LibGdx和Java,至今一直很顺利。 我正面临碰撞检测问题。libGdx碰撞检测多边形

我有两个精灵可以表示为两个形状,一个多边形和一个圆形,它们在任何给定时刻都会碰撞/相交。一旦这两个形状相撞,就会触发一些东西。

到目前为止,这就是我所做的。它有点有用,但它不准确。这就是所谓的渲染()函数中:

public boolean CollectPowerUp(PowerUps powerUp) { 
     if (powerUp.position.dst(position) < Constants.PLAYER_HEIGHT -3) { 
      Gdx.app.log("Collected PowerUp", "TRUE"); 
      EnablePowerUp(powerUp); 
      return true; 
     } 
     return false; 

找遍了很多网站,大部分的解决方案,包括其他软件一样2DCube或PhysicsEditor。是否有可能仅通过使用LibGdx和Java来执行此交集?如果是这样,我应该看看什么?

由于

Intersector类具有可用于碰撞检测许多静态方法。

如果您的多边形是矩形,你可以使用:

Intersector.overlaps(Circle c, Rectangle r) 

其他

Polygon polygon=new Polygon(); 
polygon.setVertices(new float[]{0,0,.......}); 
Circle circle=new Circle(x, y, radius); 

float points[]=polygon.getTransformedVertices(); 

for (int i=0;i<points.length;i+=2){ 
    if(circle.contains(points[i],points[i+1])){ 
     System.out.println("Collide"); 
    } 
}  

编辑

上面的代码只检测碰撞,如果多边形顶点是内循环,如果有什么

  • 圆是完全内部多边形
  • 圆的某些部分是内部多边形但顶点是圆

创建圆充当鉴于圆形和多边形模型的多边形外

float radius=100; 
FloatArray floatArray=new FloatArray(); 
int accuracy=24;  // can be use 1 for complete circle 

for (int angle=0;angle<360;angle += accuracy){ 
    floatArray.add(radius * MathUtils.cosDeg(angle)); 
    floatArray.add(radius * MathUtils.sinDeg(angle)); 
} 

Polygon circle=new Polygon(floatArray.toArray()); // This is polygon whose vertices are on circumference of circle 

float[] circularPoint=circle.getTransformedVertices(); 
for (int i=0;i<circularPoint.length;i+=2){ 
    if(polygon.contains(circularPoint[i],circularPoint[i+1])){ 
     System.out.println("Collide With circumference"); 
     break; 
    } 
} 
+0

如果圆完全落在多边形内部会怎么样?您需要检查圆的中心与多边形“isPointInPolygon(Array polygon,Vector2 point)''。 – Madmenyo

+0

我已经使用了你发布的代码(在编辑之前) –

www.gamedevelopment.blog上有一篇关于碰撞检测的好文章,它展示了如何检测大多数形状的碰撞。这是Libgdx圈,文中所示的多边形碰撞检测方法。

public boolean contains (Polygon poly, Circle circ) { 
    final float[] vertices = poly.getTransformedVertices(); // get all points for this polygon (x and y) 
    final int numFloats = vertices.length; // get the amount of points(x and y) 
    // loop through each point's x and y values 
    for (int i = 0; i < numFloats; i += 2) { 
     // get the first and second point(x and y of first vertice) 
     Vector2 start = new Vector2(vertices[i],vertices[i + 1]); 
     // get 3rd and 4th point (x and y of second vertice) (uses modulo so last point can use first point as end) 
     Vector2 end = new Vector2(vertices[(i + 2) % numFloats], vertices[(i + 3) % numFloats]); 
     // get the center of the circle 
     Vector2 center = new Vector2(circ.x, circ.y); 
     // get the square radius 
     float squareRadius = circ.radius * circ.radius; 
     // use square radius to check if the given line segment intersects the given circle. 
     return Intersector.intersectSegmentCircle (start, end, center, squareRadius); 
    } 
} 

有在Intersector类许多有用的方法,其可用于冲突检测。

+0

你能解释一下吗?Vector2 end = new Vector2(vertices [(i + 2)%numFloats],vertices [(i + 3)%numFloats]); '声明 – Aryan

+0

@AbhishekAryan当然,我添加了评论,以更好地解释整个过程 – dfour