Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java libGdx碰撞检测多边形_Java_Android_Libgdx - Fatal编程技术网

Java libGdx碰撞检测多边形

Java libGdx碰撞检测多边形,java,android,libgdx,Java,Android,Libgdx,我最近开始学习LibGdx和Java,到目前为止进展顺利。 我面临碰撞检测的问题 我有两个精灵,可以表示为两个形状,一个多边形和一个圆,在任何给定时刻都会碰撞/相交。一旦这两个形状发生碰撞,就会触发某些东西 到目前为止,这就是我所做的。这有点可行,但并不准确。这在Render()函数中调用: public boolean CollectPowerUp(PowerUps powerUp) { if (powerUp.position.dst(position) < Co

我最近开始学习LibGdx和Java,到目前为止进展顺利。 我面临碰撞检测的问题

我有两个精灵,可以表示为两个形状,一个多边形和一个圆,在任何给定时刻都会碰撞/相交。一旦这两个形状发生碰撞,就会触发某些东西

到目前为止,这就是我所做的。这有点可行,但并不准确。这在Render()函数中调用:

   public boolean CollectPowerUp(PowerUps powerUp) {
        if (powerUp.position.dst(position) < Constants.PLAYER_HEIGHT -3) {
            Gdx.app.log("Collected PowerUp", "TRUE");
            EnablePowerUp(powerUp);
            return true;
        }
        return false;
public boolean collector通电(通电通电){
if(通电.位置.dst(位置)
我搜索了很多网站,大多数解决方案包括其他软件,如2DCube或PhysicsEditor。是否可以仅使用LibGdx和Java来执行此交叉?如果可以,我应该研究什么


感谢Intersector类拥有许多可用于碰撞检测的静态方法

如果多边形为矩形,则可以使用:

Intersector.overlaps(Circle c, Rectangle r)
否则

Polygon Polygon=新多边形();
SetVertics(新浮点[]{0,0,…});
圆=新的圆(x,y,半径);
浮点[]=polygon.getTransformedAdverties();

对于(inti=0;i有一篇关于碰撞检测的文章,介绍了如何检测大多数形状的碰撞。这是本文中所示的Libgdx圆、多边形碰撞检测方法

public boolean contains (Polygon poly, Circle circ) {
    final float[] vertices = poly.getTransformedVertices(); // get all points for this polygon (x and y)
    final int numFloats = vertices.length; // get the amount of points(x and y)
    // loop through each  point's x and y values
    for (int i = 0; i < numFloats; i += 2) {
        // get the first and second point(x and y of first vertice)
        Vector2 start = new Vector2(vertices[i],vertices[i + 1]);
        // get 3rd and 4th point (x and y of second vertice) (uses modulo so last point can use first point as end)
        Vector2 end = new Vector2(vertices[(i + 2) % numFloats], vertices[(i + 3) % numFloats]);
        // get the center of the circle
        Vector2 center = new Vector2(circ.x, circ.y);
        // get the square radius
        float squareRadius = circ.radius * circ.radius;
        // use square radius to check if the given line segment intersects the given circle.
        return Intersector.intersectSegmentCircle (start, end, center, squareRadius);
    }
}
公共布尔包含(多边形多边形、圆圈){
最终浮点[]顶点=poly.getTransformedAdverties();//获取此多边形的所有点(x和y)
final int numFloats=vertices.length;//获取点的数量(x和y)
//循环遍历每个点的x和y值
对于(int i=0;i

类中有许多有用的方法可用于碰撞检测。

如果圆完全落在多边形内怎么办?您需要检查圆的中心与多边形的对比
isPointInPolygon(数组多边形,向量2点)
。请解释
Vector2 end=new Vector2(顶点[(i+2))好吗%numFloats],顶点[(i+3)%numFloats]);
statement@AbhishekAryan当然,我添加了注释,以便更好地解释整个过程
float radius=100;
FloatArray floatArray=new FloatArray();
int accuracy=24;       // can be use 1 for complete circle

for (int angle=0;angle<360;angle += accuracy){
    floatArray.add(radius * MathUtils.cosDeg(angle));
    floatArray.add(radius * MathUtils.sinDeg(angle));
}

Polygon circle=new Polygon(floatArray.toArray()); // This is polygon whose vertices are on circumference of circle

float[] circularPoint=circle.getTransformedVertices();
for (int i=0;i<circularPoint.length;i+=2){
    if(polygon.contains(circularPoint[i],circularPoint[i+1])){
        System.out.println("Collide With circumference");
        break;
    }  
}
public boolean contains (Polygon poly, Circle circ) {
    final float[] vertices = poly.getTransformedVertices(); // get all points for this polygon (x and y)
    final int numFloats = vertices.length; // get the amount of points(x and y)
    // loop through each  point's x and y values
    for (int i = 0; i < numFloats; i += 2) {
        // get the first and second point(x and y of first vertice)
        Vector2 start = new Vector2(vertices[i],vertices[i + 1]);
        // get 3rd and 4th point (x and y of second vertice) (uses modulo so last point can use first point as end)
        Vector2 end = new Vector2(vertices[(i + 2) % numFloats], vertices[(i + 3) % numFloats]);
        // get the center of the circle
        Vector2 center = new Vector2(circ.x, circ.y);
        // get the square radius
        float squareRadius = circ.radius * circ.radius;
        // use square radius to check if the given line segment intersects the given circle.
        return Intersector.intersectSegmentCircle (start, end, center, squareRadius);
    }
}