2017-05-04 2 views
2

Alors, comment mettre en œuvre la détection de collision pour un arc de cercle? Devrai-je utiliser la collision Box 2d ou puis-je le faire d'une autre manière en utilisant Rectangle ou des trucs comme ça?Détection de collision pour un arc de cercle

BTW Je déteste box2d parce que je ne comprends pas la plupart des choses dedans, donc s'il y a une solution qui exclut le box2d, il sera très apprécié.

yellow arc circle

L'arc jaune continue à tourner sur le cercle noir. Comment puis-je implémenter la détection de collision ici?

Aidez s'il vous plaît! Merci!

Répondre

2

Pour éviter d'utiliser Box2D vous pouvez définir la forme comme un polygone et utiliser la méthode polygon.contains(x,y) ou utiliser le Intersector

Voici un exemple utilisant à la fois:

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

Bon i exécuter votre code pour que je puisse voyez ce que vous avez fait, donc j'ai vu que je faisais votre polygone que j'ai trouvé assez cool btw :) mais ma question est que je fais tourner mon arc donc je vais devoir faire pivoter mon polygone aussi comment je fais ça? –

+2

Vous pouvez faire pivoter le polygone à l'aide de polya.rotate (degrés); vous pouvez également avoir besoin de définir l'origine de sorte qu'il tourne autour du bon point polya.setOrigin (originX, originY); Je les ai ajoutés à l'exemple. – dfour