【问题标题】:Collision detection for an arc of a circle圆弧的碰撞检测
【发布时间】:2017-10-02 04:57:03
【问题描述】:

那么如何实现圆弧的碰撞检测呢?我是否必须使用 Box 2d 碰撞,或者我可以使用 Rectangle 或类似的东西以其他方式进行吗?

顺便说一句,我讨厌 box2d,因为我不了解其中的大部分内容,因此如果有排除 box2d 的解决方案,将不胜感激。

黄色弧线在黑色圆圈上不断旋转。我如何在这里实现碰撞检测?

请帮忙!谢谢!

【问题讨论】:

    标签: libgdx collision-detection collision


    【解决方案1】:

    为避免使用 Box2D,您可以将形状定义为多边形并使用 polygon.contains(x,y) 方法或使用 Intersector

    以下是同时使用两者的示例:

    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 ...)
    }
    

    【讨论】:

    • 好吧,我运行你的代码,这样我就可以看到你做了什么,所以我看到我用弧线制作了你的多边形,我觉得很酷 btw :) 但我的问题是我正在旋转我的弧线,所以我会也必须旋转我的多边形我该怎么做?
    • 您可以使用 polya.rotate(degrees); 旋转多边形您可能还需要设置原点,使其围绕正确的点旋转 polya.setOrigin(originX, originY);我将它们添加到示例中。
    猜你喜欢
    • 2016-08-08
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    相关资源
    最近更新 更多