2017-05-04 14 views
2

円の円弧の衝突検出を実装するにはどうすればよいですか?ボックス2dの衝突を使用する必要がありますか、それともRectangleを使って他のやり方をすることができますか?円の円弧の衝突検出

私はそれの中のほとんどのことを理解していないので、私はbox2dが嫌いです。ボックス2dを除外するソリューションがあれば、それは非常に感謝しています。

yellow arc circle

黄色アークは黒丸にわたって回転し続けます。ここで衝突検出を実装するにはどうすればよいですか?

助けてください!ありがとう!あなたは、多角形などの形状を定義し、polygon.contains(x,y)メソッドを使用するか、または以下Intersector

を使用することができBOX2Dを使用しないように

答えて

2

は両方を使用した例です。

import com.badlogic.gdx.ApplicationAdapter; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.InputProcessor; 
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 
import com.badlogic.gdx.math.Circle; 
import com.badlogic.gdx.math.Intersector; 
import com.badlogic.gdx.math.Polygon; 

public class Test extends ApplicationAdapter implements InputProcessor{ 
    private ShapeRenderer sr; 
    private Polygon polya; 

    private boolean isColliding = false; 
    private Circle mp; 

    @Override 
    public void create() { 

     //define arc as polygon 
     // the more points used to define the shape will 
     // increase both required computation and collision precision 
     polya = new Polygon(); 

    // create vertices 
    float section = 15f; 
    float[] newVerts = new float[200]; 
    for(int i = 0; i < 50; i++){ 
     newVerts[i*2] = (float)Math.sin(i/section); //x 0 to 98 even 
     newVerts[i*2+1] = (float)Math.cos(i/section); //y 1 to 99 odd 

     newVerts[199-i*2] = (float)Math.cos(i/section); //x 100 to 108 
     newVerts[198-i*2] = (float)Math.sin(i/section) + 0.2f; //y 101 to 199 

    } 

    polya.setVertices(newVerts); 
    polya.scale(50); 
    polya.setOrigin(1, 1); 
    polya.rotate(60); 

     //define circle to act as point for checking intersections 
     mp = new Circle(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2,4); 
     // setup batchers 
     sr = new ShapeRenderer(); 
     sr.setAutoShapeType(true); 

     Gdx.input.setInputProcessor(this); 

    } 

    @Override 
    public void render() { 
     Gdx.gl.glClearColor(0f, 0f, 0f, 0f); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     // check collision with polygon 
     isColliding = polya.contains(mp.x,mp.y); 

     //check collision using Intersector 
     isColliding = Intersector.isPointInPolygon(polya.getTransformedVertices(),0,polya.getVertices().length,mp.x,mp.y); 


     sr.begin(); 
     sr.setColor(Color.WHITE); 
     if(isColliding){ 
      sr.setColor(Color.RED); 
     } 
     sr.polygon(polya.getTransformedVertices()); 
     sr.circle(mp.x,mp.y,mp.radius); 
     sr.end(); 

    } 

    @Override 
    public void dispose() { 
    } 

    @Override 
    public boolean mouseMoved(int screenX, int screenY) { 
     int newy = Gdx.graphics.getHeight() - screenY; 
     polya.setPosition(screenX, newy); 
     return false; 
    } 


    (... removed unused input processor methods for clarity ...) 
} 
+0

さてさて、私は私ができるので、あなたのコードを実行しますあなたが作ったものを参照してください、私は私が非常にクールなbtwを発見あなたのポリゴンを作ったのを見た:)しかし、私は私のポリゴンを回転させる必要がありますので、 –

+2

polya.rotate(度)を使用してポリゴンを回転できます。原点を右の点を中心に回転するように設定する必要があります。polya.setOrigin(originX、originY);私はそれらを例に加​​えました。 – dfour