Re: How would you store/model this map?
Posted: Mon Apr 10, 2023 5:15 pm
The next step: take every second equidistant point and make here the cell; other second must be a pair of cells, but with normal shift as x + k*dy, y - k*dx (dx and dy is a tangent in the point).
Code: Select all
-- ladder-roads
-- license cc0, darkfrei 2023
TrackLine =
-- bezier-roads, 11 pieces
{
{0, 400, 10, 400}, -- line
{10, 400, 123, 400, 203, 282}, -- bezier
{203, 282, 279, 169, 452, 172}, -- bezier
{452, 172, 518, 173, 569, 234}, -- bezier
{569, 234, 625, 302}, -- line
{625, 302, 664, 348, 639, 422}, -- bezier
{639, 422, 610, 508, 472, 495}, -- bezier
{472, 495, 407, 489, 359, 429}, -- bezier
{359, 429, 306, 363, 189, 422}, -- bezier
{189, 422, 136, 449, 73, 448}, -- bezier
{73, 448, 4, 447}, -- line
}
-- restore bezier:
Line = {} -- the line as list of position pairs
for iRoad = 1, #TrackLine do
local road = TrackLine[iRoad]
if #road > 4 then -- bezier
local bezierObj = love.math.newBezierCurve (road)
local amount = 32
for t = 0, amount-1 do -- don't add last point
local x, y = bezierObj:evaluate (t/amount)
table.insert (Line, x)
table.insert (Line, y)
end
else
for i = 1, #road-3, 2 do -- don't add last point
table.insert (Line, road[i])
table.insert (Line, road[i+1])
end
end
end
local function get_points_along_line (line, gap)
-- from https://github.com/darkfrei/love2d-lua-tests/blob/main/railway-track/railways.lua#L88
local points = {}
local tangents = {}
local rest = gap/2 -- rest is gap to start point on this section
local x1, y1, x2, y2, dx, dy = line[1],line[2]
for i=3, #line-1, 2 do
x2, y2 = line[i],line[i+1]
dx, dy = x2-x1, y2-y1
local sector_length = (dx*dx+dy*dy)^0.5
if sector_length > rest then
-- rest is always shorter than gap; sector is shorter than rest (or gap)
dx, dy = dx/sector_length, dy/sector_length
while sector_length > rest do
local x, y = x1+rest*dx, y1+rest*dy
table.insert (points, x)
table.insert (points, y)
table.insert (tangents, dx)
table.insert (tangents, dy)
rest = rest + gap
end
else -- no point in this distance
end
-- the tail for the next
rest = rest-sector_length
x1, y1 = x2, y2
end
return points, tangents
end
--add equidistant points
EquidistantPoints, Tangents = get_points_along_line (Line, 15)
-- cells as round
Cells = {}
for i = 1, #EquidistantPoints-1, 2 do
-- position
local x, y = EquidistantPoints[i], EquidistantPoints[i+1]
-- tangent
local dx, dy = Tangents[i], Tangents[i+1]
if ((i-1)/2)%2 == 0 then
-- side pair of cell
local leftCell = {x=x+10*dy, y=y-10*dx, dx=dx, dy=dy,
left = #Cells+2, right = #Cells+3, next = #Cells+4}
table.insert (Cells, leftCell)
local rightCell = {x=x-10*dy, y=y+10*dx, dx=dx, dy=dy,
left = #Cells+2, right = #Cells+3, next = #Cells+4}
table.insert (Cells, rightCell)
else
-- middle cell
local cell = {x=x, y=y, dx=dx, dy=dy,
left = #Cells+2, right = #Cells+3, next = #Cells+4}
table.insert (Cells, cell)
end
end
function love.draw ()
love.graphics.setColor (0.25,0.25, 0.25)
love.graphics.line (Line)
-- for i = 1, #EquidistantPoints-1, 2 do
-- love.graphics.circle ('line', EquidistantPoints[i], EquidistantPoints[i+1], 3)
-- end
love.graphics.setColor (1,1,1)
for iCell, cell in ipairs (Cells) do
love.graphics.circle ('line', cell.x, cell.y, 4)
end
end