2D 碰撞问题

发布于 2024-10-24 21:06:42 字数 1394 浏览 2 评论 0原文

我有以下代码:

function collisionDetect(left1,right1,top1,bottom1,left2,right2,top2,bottom2,dir)
    local left,right,top,bottom=false,false,false,false
    if left1<right2 then left=true end
    if right1>left2 then right=true end
    if top1>bottom2 then top=true end
    if bottom1<top2 then bottom=true end
    if dir.x>0 and (top or bottom) then
        if right then return 1 end
    elseif dir.x<0 and (top or bottom) then
        if left then return 2 end
    elseif dir.y>0 and (left or right) then
        if bottom then return 3 end
    elseif dir.y<0 and (left or right) then
        if top then return 4 end
    end
    return 0
end

left1、right1 等是包含相应边界框(框 1 或 2)位置的参数。

dir 是一个“Vector2”(包含 x 和 y 属性)。

由于某种原因,我的代码返回附近不存在的对象的碰撞。有什么想法吗?

编辑:

我已经解决了我的问题,这是任何人谷歌搜索该主题的代码。

(它是用 Lua 语言编写的,这是一种非常直接且易于解释的语言)

function collisionDetect(left1,right1,top1,bottom1,left2,right2,top2,bottom2,dir)
    local insideHorizontal=false
    local insideVertical=false
    if left1<right2 then insideHorizontal=true end
    if right1>left2 then insideHorizontal=true end
    if top1>bottom2 then insideVertical=true end
    if bottom1<top2 then insideVertical=true end
    if insideHorizontal and insideVertical then
        return true
    end
    return false
end

I have the following code:

function collisionDetect(left1,right1,top1,bottom1,left2,right2,top2,bottom2,dir)
    local left,right,top,bottom=false,false,false,false
    if left1<right2 then left=true end
    if right1>left2 then right=true end
    if top1>bottom2 then top=true end
    if bottom1<top2 then bottom=true end
    if dir.x>0 and (top or bottom) then
        if right then return 1 end
    elseif dir.x<0 and (top or bottom) then
        if left then return 2 end
    elseif dir.y>0 and (left or right) then
        if bottom then return 3 end
    elseif dir.y<0 and (left or right) then
        if top then return 4 end
    end
    return 0
end

left1, right1, etc. are arguments containing the position of the respective bounding box (box 1 or 2).

dir is a "Vector2" (contains an x and y property).

For some reason, my code is returning collisions for objects no where near by. Any ideas?

EDIT:

I have solved my problem, here is the code for anybody googling the subject.

(It's in Lua, a pretty straight forward and simple to interpret language)

function collisionDetect(left1,right1,top1,bottom1,left2,right2,top2,bottom2,dir)
    local insideHorizontal=false
    local insideVertical=false
    if left1<right2 then insideHorizontal=true end
    if right1>left2 then insideHorizontal=true end
    if top1>bottom2 then insideVertical=true end
    if bottom1<top2 then insideVertical=true end
    if insideHorizontal and insideVertical then
        return true
    end
    return false
end

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

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

发布评论

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

评论(1

冬天旳寂寞 2024-10-31 21:06:42

看看这是一个很好的碰撞检测教程/算法

本质上:

bool check_collision( SDL_Rect A, SDL_Rect B )
{
   //...
   //Work out sides
   //...

   //If any of the sides from A are outside of B
    if( bottomA <= topB )
    {
        return false;
    }

    if( topA >= bottomB )
    {
        return false;
    }

    if( rightA <= leftB )
    {
        return false;
    }

    if( leftA >= rightB )
    {
        return false;
    }

    //If none of the sides from A are outside B
    return true;
}

或者,就您的代码而言:(

function collisionDetect(left1,right1,top1,bottom1,left2,right2,top2,bottom2,dir)
    if bottom1<=top2 then return true end
    if top1>=bottom2 then return true end
    if right1<=left2 then return true end
    if left1<=right2 then return true end
    return false
end

这不是我知道的语言,所以我猜测了一下)

Have a look at this for a nice collision detection tutorial/algorithm.

Essentially:

bool check_collision( SDL_Rect A, SDL_Rect B )
{
   //...
   //Work out sides
   //...

   //If any of the sides from A are outside of B
    if( bottomA <= topB )
    {
        return false;
    }

    if( topA >= bottomB )
    {
        return false;
    }

    if( rightA <= leftB )
    {
        return false;
    }

    if( leftA >= rightB )
    {
        return false;
    }

    //If none of the sides from A are outside B
    return true;
}

Or, in terms of your code:

function collisionDetect(left1,right1,top1,bottom1,left2,right2,top2,bottom2,dir)
    if bottom1<=top2 then return true end
    if top1>=bottom2 then return true end
    if right1<=left2 then return true end
    if left1<=right2 then return true end
    return false
end

(this isn't a language I know, so I'm guessing a bit)

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