Вам необходимо создать более абстрактное представление о земле, из которой вы можете сгенерировать как линию для графики, так и тело для физики.
Так, например:
--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
Не зная точно, как вы хотите реализовать физику и рисунок, трудно дать больше деталей, чем это. Я надеюсь, что вы используете это как пример того, как этот код может быть структурирован. Код, который я представил, является лишь очень приблизительной структурой, поэтому он не приблизится к реальной работе. Пожалуйста, спросите обо всем, что мне было неясно!