Love2D碰撞?

时间:2016-01-18 03:49:51

标签: lua love2d

我正在建造一个脆弱的鸟类克隆我喜欢2D而且我所拥有的只是鸟儿拍打,背后有背景。当鸟儿接触地面游戏结束,或者在屏幕上显示游戏时,我想这样做。我似乎无法在不使用外部库的情况下找到一个好方法。这个问题有方法解决吗?如果这有帮助或重要的话,我有理由作为单独的图像。我使用的是最新的LOVE 2D框架版本。

谢谢!我知道有人会给我我需要的答案!

2 个答案:

答案 0 :(得分:1)

我在游戏中使用了rects。

if (bird.x <= tube.x and bird.y <= tube.y and bird.x + bird.width >= tube.x and bird.y + bird.height >= tube.y) then
   {...code...}

if (bird.x <= tube.x and bird.y >= tube.y and bird.x + bird.width >= tube.x and bird.y <= tube.y + tube.height) then
   {...code...}

 if (bird.x >= tube.x and bird.y <= tube.y and bird.x <= tube.x + tube.width and bird.y + bird.height >= tube.y) then
   {...code...}

if (bird.x >= tube.x and bird.y >= tube.y and bird.x <= tube.x + tube.width and bird.y <= tube.y + tube.height) then
   {...code...}

答案 1 :(得分:1)

我使用部分取自love2d论坛的这个简单代码。这是代码,在函数下是你如何使用它。 (可以重复用于许多对象)

-----------------------Code-------------------------
function colMulti(x1, y1, w1, h1,  x2, y2, w2, h2)
    return  x1 < x2+w2 and
            x2 < x1+w1 and
            y1 < y2+h2 and
            y2 < y1+h1
end

-----------------------Usage-------------------------
x1, y1, w1, h1 = bird.x, bird.y, bird.img:getWidth(), bird.img:getHeight()
x2, y2, w2, h2 = ground.x, ground.y, ground.img:getWidth(), ground.img:getHeight()

if colMulti(x1, y1, w1, h1,  x2, y2, w2, h2) then
    —Execute bird death here
end

有一种更长,更复杂,更有效的碰撞检测方法,但你必须学习关于love2d的内置物理引擎。 (看起来你不需要它。)

其他方式的更多信息:https://love2d.org/wiki/love.physics

相关问题