首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >圆弧碰撞检测

圆弧碰撞检测
EN

Stack Overflow用户
提问于 2017-05-04 06:42:16
回答 1查看 751关注 0票数 3

那么如何实现圆弧形的碰撞检测呢?我是否需要使用Box 2d碰撞,或者我可以用矩形或其他类似的方法来做呢?

顺便说一句,我讨厌box2d,因为我不理解其中的大部分内容,所以如果有一个排除box2d的解决方案,我们会非常感激的。

黄色的弧线继续在黑色的圆圈上旋转。如何在这里实现碰撞检测?

请帮帮忙!谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-04 14:36:07

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

下面是使用这两种方法的示例:

代码语言:javascript
复制
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 ...)
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43775650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档