java - 与 Libgdx 的圆形和多边形碰撞

标签 java collision-detection polygon libgdx geometry

Libgdx 中有没有办法验证多边形和圆之间的碰撞?

我看到了 Intersector 类,但只找到了 Circle 和 Rectangle 的碰撞测试。其他多边形呢?

如果我需要手动完成,使用 Libgdx 的最佳方式是什么?

最佳答案

遗憾的是,我没有足够的声誉来发表评论,所以我将其添加为另一个答案...

Cristiano 的出色答案适用于检查圆是否与多边形的线段之一重叠,但它不会检查更不寻常的情况,即圆完全包含在多边形内,如果速度较快,可能会发生这种情况移动的圆圈与一个大的多边形相撞。

我在下面重新粘贴了 Cristiano 的代码,并做了一些小改动来解决这个问题......

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

关于java - 与 Libgdx 的圆形和多边形碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15323719/

相关文章:

css - 多个多边形可能吗?

java - Cucumber Java with Selenium - 重用登录功能并将其与其他功能连接起来

java - 像接口(interface)一样使用第三方抽象类?

基于 Java 2D 方 block 的游戏碰撞(上/下/左/右)

c# - 在 XNA 中创建二维多边形

MySQL ST_Contains 结果错误

java - 如何在 Spring Boot 中为 RestTemplate 编写 JUnit 测试

java - 如何在 Java、Android 上从 ListView 中获取所选项目

java - 圆内点碰撞响应 : How do you keep the Point inside of the circle?

c# - 在 Unity 中检测重叠二维对象的输入碰撞