2017-07-26 13 views
1

最近、LibGdxとJavaの学習を始めました。 私は衝突検出の問題に直面しています。libGdx衝突検出ポリゴン

私は2つのスプライトを持っています。スプライトは、ポリゴンとサークルの2つのシェイプで表現でき、任意の瞬間に衝突/交差します。これらの2つの形状が衝突すると、何かが引き起こされます。

これまでのところ、これは私が行ったことです。それはちょっと動作しますが、正確ではありません。これは、レンダリング()関数内で呼び出されます。

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を使って単独で実行できますか?もしそうなら、私は何を調べるべきですか?衝突検出のために使用することができ、多くの静的メソッドを持つ

おかげ

答えて

2

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

EDIT

ポリゴンの頂点が円の内部にある場合にのみ、衝突を検出するには、どのような

場合
  • 円はポリゴンの内側に完全に
  • 円の一部がポリゴンの内側にあるが、頂点が円

モデルのビューに円形、多角形として作用円ポリゴンを作成外にある

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(配列ポリゴン、ベクトル2点)をチェックする必要があります。 – Madmenyo

+0

投稿したコード(編集前) –

2

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(頂点[(i + 2)%numFloats]、頂点[(i + 3)%numFloats])を説明してください。 'ステートメント – Aryan

+0

@AbhishekAryan確かに、私はより完全なプロセスを説明するコメントを追加しました – dfour