您需要創建一個更加抽象的地面表示形式,從中可以生成圖形的線條和物理體。
因此,例如:
--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
不正是你要如何實現物理和繪圖知道,這是很難給出比這個更詳細。我希望你用這個例子來說明這個代碼的結構。我提出的代碼只是一個非常近似的結構,所以它不會接近實際工作。請詢問我一直不清楚的事情!