2017-05-04 1 views
2

원호 호 충돌 탐지를 구현하려면 어떻게해야합니까? Box 2d 충돌을 사용해야합니까, 아니면 Rectangle을 사용하거나 다른 방법으로 할 수 있습니까?원호 호 충돌 탐지

BTW 나는 상자 안에 들어있는 것들을 대부분 이해하지 못하기 때문에 box2d가 싫어서 box2d를 제외하는 해결책이 있다면 매우 감사 할 것입니다.

yellow arc circle

노란색 호는 검은 색 원 이상 회전에 유지합니다. 여기서 충돌 감지를 어떻게 구현합니까?

도와주세요! 감사!

답변

2

예를 들어 모두 사용하는 다각형 등의 모양을 정의하고 polygon.contains(x,y) 방법을 사용하거나 아래 Intersector

를 사용할 수 Box2D의 사용을 피하려면 :

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 (degrees)를 사용하여 다각형을 회전 할 수 있습니다. 원점을 설정하여 오른쪽 점을 중심으로 회전해야 할 수도 있습니다. polya.setOrigin (originX, originY); 나는 그들을 예제에 추가했다. – dfour