圆弧碰撞检测 [英] Collision detection for an arc of a circle

查看:105
本文介绍了圆弧碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么我该如何实现对圆弧的碰撞检测?我需要使用Box 2d碰撞还是可以使用Rectangle或类似的东西做其他方式?

So how do i implement the collision detection for an arc of a circle? Will i have to use the Box 2d collision or can i do it some other way using Rectangle or stuff like that?

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

BTW I hate box2d because i dont understand most of the things in it, so if there is a solution that excludes the box2d, it will be very much appreciated.

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

The yellow arc keeps on rotating over the black circle. How do i implement collision detection in here?

请帮助!谢谢!

推荐答案

为避免使用Box2D,您可以将形状定义为多边形,然后使用

To avoid using Box2D you could define the shape as a polygon and use the polygon.contains(x,y) method or use the Intersector

下面是同时使用这两个示例:

Below is an example using both:

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

这篇关于圆弧碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆