如何检测lua爱情引擎中的poly线与碰撞?

问题描述:

我正在使用lua爱情引擎来制作一个简单的游戏。然而,即时通讯碰撞有点麻烦。如何检测lua爱情引擎中的poly线与碰撞?

我在一组点(代表岩石地面)和一个箱子之间有一条折线,但我不能想到一个简单的实现。

love.graphics.line(0,60, 100,70, 150,300, 200,230, 250,230, 300,280, 350,220, 400,220, 420,150, 440,140, 480,340) 

如果有人可以帮助使用代码片段或建议,将不胜感激。

您需要创建一个更加抽象的地面表示形式,从中可以生成图形的线条和物理体。

因此,例如:

--ground represented as a set of points 
local ground = {0,60, 100,70, 150,300, 200,230, 250,230, 300,280, 350,220, 400,220, 420,150, 440,140, 480,340} 

这可能不是最好的表现,但我会继续我的例子。

现在,您已经有了地面的表示,现在您可以转换物理知识(进行碰撞)以及图形显示可以理解的东西(确实可以理解)。所以,你声明了两个功能:

--converts a table representing the ground into something that 
--can be understood by your physics. 
--If you are using love.physics then this would create 
--a Body with a set PolygonShapes attached to it. 
local function createGroundBody(ground) 
    --you may need to pass some additional arguments, 
    --such as the World in which you want to create the ground. 
end 

--converts a table representing the ground into something 
--that you are able to draw. 
local function createGroundGraphic(ground) 
    --ground is already represented in a way that love.graphics.line can handle 
    --so just pass it through. 
    return ground 
end 

现在,把他们放在一起:

local groundGraphic = nil 
local groundPhysics = nil 
love.load() 
    groundGraphic = createGroundGraphic(ground) 
    physics = makePhysicsWorld() 
    groundPhysics = createGroundBody(ground) 

end 

love.draw() 
    --Draws groundGraphic, could be implemented as 
    --love.graphics.line(groundGraphic) 
    --for example. 
    draw(groundGraphic) 
    --you also need do draw the box and everything else here. 
end 

love.update(dt) 
    --where `box` is the box you have to collide with the ground. 
    runPhysics(dt, groundPhysics, box) 
    --now you need to update the representation of your box graphic 
    --to match up with the new position of the box. 
    --This could be done implicitly by using the representation for physics 
    --when doing the drawing of the box. 
end 

不正是你要如何实现物理和绘图知道,这是很难给出比这个更详细。我希望你用这个例子来说明这个代码的结构。我提出的代码只是一个非常近似的结构,所以它不会接近实际工作。请询问我一直不清楚的事情!