【问题标题】:Dectecting whether a Circle intersects with a Rectangle on its top half or bottom half in LibGDX在 LibGDX 中检测圆形是否与其上半部分或下半部分的矩形相交
【发布时间】:2016-03-31 17:48:52
【问题描述】:

使用 LibGDX,我有一个与 Rectangle 对象碰撞的 Circle 对象。我使用 Libgdx 中包含的 Intersector 类检测到这种碰撞,如下所示:

 if(Intersector.overlaps(circle, rectangle)){
        do something
    }

这种检测工作正常,但我想知道是否有一种方法可以检测圆是否与矩形的上半部分或矩形的下半部分发生碰撞,并根据哪种方法进行相应的处理。

所以可能是这样的

if(Intersector.overlaps(circle, rectangle.getHeight() - rectangle.getHeight() / 2)){
    do something
}

但不幸的是,这会返回一个浮点数,相交类不接受。

任何关于如何实现这一点的想法将不胜感激。

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

    保留一个可重用的额外矩形实例:

    私有最终矩形 tmp = new Rectangle();

    当您的圆与您关心的矩形 A 相交时,您可以将临时矩形设置为矩形 A 的上半部分并检查是否发生碰撞。结果告诉您碰撞发生的位置是顶部还是底部。 (但如果圆与上半部和下半部相交,顶部也会发生碰撞,因此请记住这一点。)

    if(Intersector.overlaps(circle, rectangle)){
        tmp.set(rectangle.x, rectangle.y + rectangle.height/2, rectangle.width, rectangle.height/2);
        if (Intersector.overlaps(circle, tmp){
            //top half (or both top and bottom) hit
        } else {
            //bottom half hit
        }
    }
    

    【讨论】:

    • 工作就像一个魅力。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2020-11-05
    • 1970-01-01
    相关资源
    最近更新 更多