您需要创建一个更抽象的地面表示,您可以从中生成图形的线条和物理的主体。
例如:
--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
如果不确切知道您想如何实现物理和绘图,很难提供比这更详细的信息。我希望您以此为例来说明如何构建此代码。我提供的代码只是一个非常近似的结构,因此它不会接近实际工作。有什么不清楚的地方请教!