簡體   English   中英

與Libgdx的圓和多邊形碰撞

[英]Circle and Polygon Collision with Libgdx

在Libgdx中有一種方法可以驗證Polygon和Circle之間的碰撞嗎?

我看到了Intersector類,但只發現了Circle和Rectangle的碰撞測試。 那么其他任何多邊形呢?

如果我需要手動完成,使用Libgdx最好的方法是什么?

所以,我設法在Circle和Polygon之間創建了一個碰撞測試方法。 至少,它對我有用。

這是代碼:

public boolean overlaps(Polygon polygon, Circle circle) {
    float []vertices=polygon.getTransformedVertices();
    Vector2 center=new Vector2(circle.x, circle.y);
    float squareRadius=circle.radius*circle.radius;
    for (int i=0;i<vertices.length;i+=2){
        if (i==0){
            if (Intersector.intersectSegmentCircle(new Vector2(vertices[vertices.length-2], vertices[vertices.length-1]), new Vector2(vertices[i], vertices[i+1]), center, squareRadius))
                return true;
        } else {
            if (Intersector.intersectSegmentCircle(new Vector2(vertices[i-2], vertices[i-1]), new Vector2(vertices[i], vertices[i+1]), center, squareRadius))
                return true;
        }
    }
    return false;
}

可悲的是,我沒有足夠的聲譽來發表評論,所以我將其作為另一個答案添加......

克里斯蒂亞諾的優秀答案適用於檢查圓與多邊形的一個線段重疊,但是它不會檢查圓形完全包含在多邊形內部的更不尋常的情況,如果一個小的快速移動圓相撞可能會發生這種情況有一個大的多邊形。

我在下面通過一些小改動來解釋克里斯蒂亞諾的代碼來解決這個問題......

public static boolean overlaps(Polygon polygon, Circle circle) {
    float []vertices=polygon.getTransformedVertices();
    Vector2 center=new Vector2(circle.x, circle.y);
    float squareRadius=circle.radius*circle.radius;
    for (int i=0;i<vertices.length;i+=2){
        if (i==0){
            if (Intersector.intersectSegmentCircle(new Vector2(vertices[vertices.length - 2], vertices[vertices.length - 1]), new Vector2(vertices[i], vertices[i + 1]), center, squareRadius))
                return true;
        } else {
            if (Intersector.intersectSegmentCircle(new Vector2(vertices[i-2], vertices[i-1]), new Vector2(vertices[i], vertices[i+1]), center, squareRadius))
                return true;
        }
    }
    return polygon.contains(circle.x, circle.y);
}

...並跟進Phil Anderson的優秀答案,這是我的版本,它只是避免每次檢查都創建新的Vector2,而是重新使用Vector2的靜態實例。

public class PolygonUtil {

static final Vector2 center = new Vector2();
static final Vector2 vec1 = new Vector2();
static final Vector2 vec2 = new Vector2();

public static boolean overlaps(Polygon polygon, Circle circle) {
    float []vertices=polygon.getTransformedVertices();
    center.set(circle.x, circle.y);
    float squareRadius=circle.radius*circle.radius;
    for (int i=0;i<vertices.length;i+=2){
        if (i==0){
            if (Intersector.intersectSegmentCircle(vec1.set(vertices[vertices.length - 2], vertices[vertices.length - 1]),
                    vec2.set(vertices[i], vertices[i + 1]), center, squareRadius))
                return true;
        } else {
            if (Intersector.intersectSegmentCircle(vec1.set(vertices[i-2], vertices[i-1]), vec2.set(vertices[i], vertices[i+1]), center, squareRadius))
                return true;
        }
    }
    return polygon.contains(circle.x, circle.y);
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM