【问题标题】:How do I detect a collision with a poly line in the lua love engine?如何在 lua love 引擎中检测与折线的碰撞?
【发布时间】:2013-10-04 07:34:48
【问题描述】:

我正在使用 lua love 引擎制作一个简单的游戏。但是,我在碰撞时遇到了一点麻烦。

我在一组点(代表岩石地面)和一个我需要与之碰撞的盒子之间有一条折线,但我想不出一个简单的实现方式。

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 )

如果有人可以帮助提供代码 sn-ps 或建议,将不胜感激。

【问题讨论】:

    标签: lua collision-detection


    【解决方案1】:

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

    例如:

    --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
    

    如果不确切知道您想如何实现物理和绘图,很难提供比这更详细的信息。我希望您以此为例来说明如何构建此代码。我提供的代码只是一个非常近似的结构,因此它不会接近实际工作。有什么不清楚的地方请教!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多