圆形矩形碰撞检测在 Corona SDK 中不起作用

发布于 2024-11-30 23:44:03 字数 1930 浏览 1 评论 0原文

我需要摆脱游戏中的物理因素,因此我需要实现自定义碰撞检测函数(每一帧都会调用碰撞检测函数)。我想检查一个矩形是否与一个圆相交。我从这里获取了代码(e.James的答案):

bool intersects(CircleType circle, RectType rect)
{
    circleDistance.x = abs(circle.x - rect.x - rect.width/2);
    circleDistance.y = abs(circle.y - rect.y - rect.height/2);

    if (circleDistance.x > (rect.width/2 + circle.r)) { return false; }
    if (circleDistance.y > (rect.height/2 + circle.r)) { return false; }

    if (circleDistance.x <= (rect.width/2)) { return true; } 
    if (circleDistance.y <= (rect.height/2)) { return true; }

    cornerDistance_sq = (circleDistance.x - rect.width/2)^2 +
                         (circleDistance.y - rect.height/2)^2;

    return (cornerDistance_sq <= (circle.r^2));
}

并更改了它对此:

--Collision detection between circle and rect
local function intersects(circle, rect)
    if circle ~= nil then
        local circleDistance_x = math.abs(circle.x - rect.x - rect.width/2);
        local circleDistance_y = math.abs(circle.y - rect.y - rect.height/2);

        if (circleDistance_x > (rect.width/2 + circle.r)) then 
            return false
        end
        if (circleDistance_y > (rect.height/2 + circle.r)) then  
            return false
        end

        if (circleDistance_x <= (rect.width/2)) then
            return true
        end

        if (circleDistance_y <= (rect.height/2)) then
            return true
        end

        cornerDistance_sq = (circleDistance_x - rect.width/2)^2 +
                             (circleDistance_y - rect.height/2)^2;

        return (cornerDistance_sq <= (circle.r^2));
        else
            return false
    end
end

我确保给圆一个 r 属性并将其设置为 20。我没有收到任何错误。当矩形靠近圆时,它们就会被删除(看起来当矩形到达圆的中心时,函数返回 true,但我可能是错的)。我在转换过程中做错了什么吗?

I needed to get rid of physics in my game so I needed to implement a custom collision detection function (the collision detection function is being called every frame). I want to check if a rectangle has intersected a circle. I took the code from here (the answer by e.James):

bool intersects(CircleType circle, RectType rect)
{
    circleDistance.x = abs(circle.x - rect.x - rect.width/2);
    circleDistance.y = abs(circle.y - rect.y - rect.height/2);

    if (circleDistance.x > (rect.width/2 + circle.r)) { return false; }
    if (circleDistance.y > (rect.height/2 + circle.r)) { return false; }

    if (circleDistance.x <= (rect.width/2)) { return true; } 
    if (circleDistance.y <= (rect.height/2)) { return true; }

    cornerDistance_sq = (circleDistance.x - rect.width/2)^2 +
                         (circleDistance.y - rect.height/2)^2;

    return (cornerDistance_sq <= (circle.r^2));
}

And changed it to this:

--Collision detection between circle and rect
local function intersects(circle, rect)
    if circle ~= nil then
        local circleDistance_x = math.abs(circle.x - rect.x - rect.width/2);
        local circleDistance_y = math.abs(circle.y - rect.y - rect.height/2);

        if (circleDistance_x > (rect.width/2 + circle.r)) then 
            return false
        end
        if (circleDistance_y > (rect.height/2 + circle.r)) then  
            return false
        end

        if (circleDistance_x <= (rect.width/2)) then
            return true
        end

        if (circleDistance_y <= (rect.height/2)) then
            return true
        end

        cornerDistance_sq = (circleDistance_x - rect.width/2)^2 +
                             (circleDistance_y - rect.height/2)^2;

        return (cornerDistance_sq <= (circle.r^2));
        else
            return false
    end
end

I made sure to give the circle a r property and made it 20. I am not getting any errors. And the rectangles are removed when they get near the circle (it seems like the function returns true when the rectangle hits the center of the circle but I may be wrong). Am I doing something wrong in my conversion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2024-12-07 23:44:03

更改这两行:

    local circleDistance_x = math.abs(circle.x - rect.x - rect.width/2);
    local circleDistance_y = math.abs(circle.y - rect.y - rect.height/2);

To:

    local circleDistance_x = math.abs(circle.x+circle.r - rect.x - rect.width/2)
    local circleDistance_y = math.abs(circle.y+circle.r - rect.y - rect.height/2)

似乎可以解决问题。随机猜测万岁!

Changing these two lines:

    local circleDistance_x = math.abs(circle.x - rect.x - rect.width/2);
    local circleDistance_y = math.abs(circle.y - rect.y - rect.height/2);

To:

    local circleDistance_x = math.abs(circle.x+circle.r - rect.x - rect.width/2)
    local circleDistance_y = math.abs(circle.y+circle.r - rect.y - rect.height/2)

Seems to do the trick. Hurray for random guessing!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文