本文介紹了使用 Libgdx 的圓和多邊形碰撞的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
Libgdx 中有沒有辦法驗證多邊形和圓之間的碰撞?
Is there a way in Libgdx to verify a collision between a Polygon and a Circle?
我看到了 Intersector
類,但只發現了 Circle 和 Rectangle 的碰撞測試.其他多邊形呢?
I saw the Intersector
class but only found collision test for Circle and Rectangle. What about any other Polygon?
如果我需要手動完成,使用 Libgdx 的最佳方法是什么?
If I need to do it manually, what's the best way to do that using Libgdx?
推薦答案
所以,我設法在 Circle 和 Polygon 之間創建了一個碰撞測試方法.至少,它對我有用.
So, I managed to create a collision test method between a Circle and a Polygon. At least, it works for me.
代碼如下:
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;
}
這篇關于使用 Libgdx 的圓和多邊形碰撞的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!