update to convert poly to mesh

This commit is contained in:
2021-12-23 15:05:00 +01:00
parent 7c62363827
commit 963b3ac174
4 changed files with 273 additions and 212 deletions

View File

@@ -28,11 +28,6 @@
]]
-- require('mobdebug').start()
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
require("lldebugger").start()
end
require 'src/Dependencies'
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then

View File

@@ -1,6 +1,4 @@
-- require('mobdebug').start()
Level = Class {}
function Level:init()
@@ -8,10 +6,18 @@ function Level:init()
self.segments = self:createLevel()
self.polygon = self:createPolygon()
self.player = {}
self.mesh = {}
end
function Level:update(dt)
self:createPolygon()
if #self.polygonPoints < 3 then
self.mesh = {}
else
print_r(self.polygonPoints)
self.mesh = poly2mesh(self.polygonPoints)
end
end
function Level:render()
@@ -20,21 +26,32 @@ function Level:render()
end
function Level:renderOuterSegments()
for k,segment in pairs(self.segments) do
segment:render()
end
for k, segment in pairs(self.segments) do segment:render() end
end
function Level:renderBackground()
love.graphics.draw(gImages["img1"], LEVEL_RENDER_OFFSET,LEVEL_RENDER_OFFSET_TOP)
love.graphics.draw(self.mesh, 0, 0)
end
function Level:createLevel()
local level = {
[1] = Segment({LEVEL_RENDER_OFFSET,LEVEL_RENDER_OFFSET_TOP},{VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP},.5,'down'),
[2] = Segment({VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP},{VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET},.5,'left'),
[3] = Segment({VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET},{LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET},.5,'up'),
[4] = Segment({LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET},{LEVEL_RENDER_OFFSET,LEVEL_RENDER_OFFSET_TOP},.5,'right')
[1] = Segment({LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP}, {
VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP
}, .5, 'down'),
[2] = Segment({
VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP
}, {
VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET,
VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET
}, .5, 'left'),
[3] = Segment({
VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET,
VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET
}, {LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET}, .5, 'up'),
[4] = Segment({
LEVEL_RENDER_OFFSET, VIRTUAL_HEIGHT - LEVEL_RENDER_OFFSET
}, {LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP}, .5, 'right')
}
-- local level = {
-- [1] = Segment({LEVEL_RENDER_OFFSET,LEVEL_RENDER_OFFSET_TOP},{VIRTUAL_WIDTH - LEVEL_RENDER_OFFSET, LEVEL_RENDER_OFFSET_TOP},.5),
@@ -53,9 +70,7 @@ function Level:insideBounds2(x,y)
end
function Level:insideBounds(x, y, segments)
if not segments then
segments = self.segments
end
if not segments then segments = self.segments end
local up, down, left, right = false
local upSeg, downSeg, leftSeg, rightSeg = nil
@@ -64,36 +79,40 @@ function Level:insideBounds(x,y, segments)
for i, segment in pairs(segments) do
-- check raycast on y axis
if segment.vertical then
if y <= math.max(segment.firstPointY, segment.secondPointY) and
y >= math.min(segment.firstPointY, segment.secondPointY) then
if y <= math.max(segment.firstPointY, segment.secondPointY) and y >=
math.min(segment.firstPointY, segment.secondPointY) then
if x <= segment.firstPointX then
if not rightSeg then
rightSeg = segment
elseif math.abs(segment.firstPointX - x) < math.abs(rightSeg.firstPointX - x) then
elseif math.abs(segment.firstPointX - x) <
math.abs(rightSeg.firstPointX - x) then
rightSeg = segment
end
else
if not leftSeg then
leftSeg = segment
elseif math.abs(segment.firstPointX - x) < math.abs(leftSeg.firstPointX - x) then
elseif math.abs(segment.firstPointX - x) <
math.abs(leftSeg.firstPointX - x) then
leftSeg = segment
end
end
end
end
if segment.horizontal then
if x <= math.max(segment.firstPointX, segment.secondPointX) and
x >= math.min(segment.firstPointX, segment.secondPointX) then
if x <= math.max(segment.firstPointX, segment.secondPointX) and x >=
math.min(segment.firstPointX, segment.secondPointX) then
if y <= segment.firstPointY then
if not downSeg then
downSeg = segment
elseif math.abs(segment.firstPointY - y) < math.abs(downSeg.firstPointY - y) then
elseif math.abs(segment.firstPointY - y) <
math.abs(downSeg.firstPointY - y) then
downSeg = segment
end
else
if not upSeg then
upSeg = segment
elseif math.abs(segment.firstPointY - y) < math.abs(upSeg.firstPointY - y) then
elseif math.abs(segment.firstPointY - y) <
math.abs(upSeg.firstPointY - y) then
upSeg = segment
end
end
@@ -104,9 +123,8 @@ function Level:insideBounds(x,y, segments)
return false
end
if rightSeg.face ~= 'left' or leftSeg.face ~= 'right' or upSeg.face ~= 'down' or downSeg.face ~= 'up' then
return false
end
if rightSeg.face ~= 'left' or leftSeg.face ~= 'right' or upSeg.face ~=
'down' or downSeg.face ~= 'up' then return false end
return true
end
@@ -117,10 +135,11 @@ function Level:insideBounds3(x,y)
local j = #self.polygon
local polygon = self.polygon
for i = 1, #polygon do
if (polygon[i].y < y and polygon[j].y >= y or polygon[j].y < y and polygon[i].y >= y) then
if (polygon[i].x + ( y - polygon[i].y ) / (polygon[j].y - polygon[i].y) * (polygon[j].x - polygon[i].x) < x) then
oddNodes = not oddNodes
end
if (polygon[i].y < y and polygon[j].y >= y or polygon[j].y < y and
polygon[i].y >= y) then
if (polygon[i].x + (y - polygon[i].y) /
(polygon[j].y - polygon[i].y) * (polygon[j].x - polygon[i].x) <
x) then oddNodes = not oddNodes end
end
j = i
end
@@ -134,7 +153,8 @@ function Level:createPolygon()
for i, segment in ipairs(self.segments) do
polygon[i] = {}
polygon[i + 1] = {}
polygon[i].x, polygon[i].y, polygon[i+1].x, polygon[i+1].y = segment:segmentToPoints()
polygon[i].x, polygon[i].y, polygon[i + 1].x, polygon[i + 1].y =
segment:segmentToPoints()
polygonPoints[j] = polygon[i].x
polygonPoints[j + 1] = polygon[i].y
polygonPoints[j + 2] = polygon[i + 1].x
@@ -147,23 +167,21 @@ function Level:createPolygon()
end
function Level:pointOnEdge(x, y, segments)
if not segments then
segments = self.segments
end
if not segments then segments = self.segments end
local margin = 2
for i, segment in pairs(segments) do
if segment.vertical then -- vertical line
if x >= segment.firstPointX - margin and x <= segment.firstPointX + margin and
y <= math.max(segment.firstPointY, segment.secondPointY) and
y >= math.min(segment.firstPointY, segment.secondPointY)
then
if x >= segment.firstPointX - margin and x <= segment.firstPointX +
margin and y <=
math.max(segment.firstPointY, segment.secondPointY) and y >=
math.min(segment.firstPointY, segment.secondPointY) then
return true
end
elseif segment.horizontal then -- horizontal line
if y >= segment.firstPointY - margin and y <= segment.firstPointY + margin and
x <= math.max(segment.firstPointX, segment.secondPointX) and
x >= math.min(segment.firstPointX, segment.secondPointX)
then
if y >= segment.firstPointY - margin and y <= segment.firstPointY +
margin and x <=
math.max(segment.firstPointX, segment.secondPointX) and x >=
math.min(segment.firstPointX, segment.secondPointX) then
return true
end
end
@@ -175,17 +193,17 @@ function Level:getTouchingSegment(x,y)
local margin = 2
for i, segment in pairs(self.segments) do
if segment.vertical then -- vertical line
if x >= segment.firstPointX - margin and x <= segment.firstPointX + margin and
y <= math.max(segment.firstPointY, segment.secondPointY) and
y >= math.min(segment.firstPointY, segment.secondPointY)
then
if x >= segment.firstPointX - margin and x <= segment.firstPointX +
margin and y <=
math.max(segment.firstPointY, segment.secondPointY) and y >=
math.min(segment.firstPointY, segment.secondPointY) then
return i, segment
end
elseif segment.horizontal then -- horizontal line
if y >= segment.firstPointY - margin and y <= segment.firstPointY + margin and
x <= math.max(segment.firstPointX, segment.secondPointX) and
x >= math.min(segment.firstPointX, segment.secondPointX)
then
if y >= segment.firstPointY - margin and y <= segment.firstPointY +
margin and x <=
math.max(segment.firstPointX, segment.secondPointX) and x >=
math.min(segment.firstPointX, segment.secondPointX) then
return i, segment
end
end
@@ -196,8 +214,10 @@ end
function Level:cutLevel()
local newSegs = self.player.trailSegments
local startSegi, startSeg = self.player.trailStartSegment[1],self.player.trailStartSegment[2]
local endSegi, endSeg = self.player.trailFinishSegment[1],self.player.trailFinishSegment[2]
local startSegi, startSeg = self.player.trailStartSegment[1],
self.player.trailStartSegment[2]
local endSegi, endSeg = self.player.trailFinishSegment[1],
self.player.trailFinishSegment[2]
local first = 0
local last = 0
local firstFace = ''
@@ -231,7 +251,8 @@ function Level:cutLevel()
-- print("Reached the segment being cut")
-- print_r(self.segments[k])
-- print("split the segment")
part1, temp, part2 = self.segments[k]:splitInThreeWithSegments(newSegs[1],newSegs[#newSegs])
part1, temp, part2 = self.segments[k]:splitInThreeWithSegments(
newSegs[1], newSegs[#newSegs])
-- print("part1")
part1:debug()
-- print("part2")
@@ -292,7 +313,8 @@ function Level:cutLevel()
newSegs[1]:joinPerpendicular(self.segments[k])
newSegs[1]:debug()
-- print("newSegs[1] is joined -- finished")
local startPart1, startPart2 = self.segments[k]:splitInTwoWithSegment(newSegs[1])
local startPart1, startPart2 =
self.segments[k]:splitInTwoWithSegment(newSegs[1])
-- print("-- Segment k split into 2 segments by newsegs[1]: ")
startPart1:debug()
startPart2:debug()
@@ -327,7 +349,8 @@ function Level:cutLevel()
end
-- print_r(board1)
local endPart1, endPart2 = self.segments[k]:splitInTwoWithSegment(newSegs[#newSegs])
local endPart1, endPart2 =
self.segments[k]:splitInTwoWithSegment(newSegs[#newSegs])
-- print("-- proceed to insert the second split segment")
if self.segments[last]:endEqualsEndOf(self.segments[k]) or
self.segments[last]:startEqualsEndOf(self.segments[k]) then -- next one is linked to the start of this one
@@ -353,7 +376,8 @@ function Level:cutLevel()
newSegs[#newSegs]:joinPerpendicular(self.segments[k])
newSegs[#newSegs]:debug()
-- print("newSegs[#newSegs] is joined -- finished")
local endPart1, endPart2 = self.segments[k]:splitInTwoWithSegment(newSegs[#newSegs])
local endPart1, endPart2 =
self.segments[k]:splitInTwoWithSegment(newSegs[#newSegs])
-- print("-- Segment k split into 2 segments by newsegs[#newSegs]: ")
endPart1:debug()
endPart2:debug()
@@ -387,7 +411,8 @@ function Level:cutLevel()
end
-- print_r(board1)
local startPart1, startPart2 = self.segments[k]:splitInTwoWithSegment(newSegs[1])
local startPart1, startPart2 =
self.segments[k]:splitInTwoWithSegment(newSegs[1])
-- print("-- Segment k split into 2 segments by newsegs[1]: ")
startPart1:debug()
startPart2:debug()
@@ -494,7 +519,8 @@ function Level:cutLevel()
elseif self:containsBalls(board2) == 0 then
self.segments = board1
else
self.segments = self:getPerimeter(board1) > self:getPerimeter(board2) and board1 or board2
self.segments = self:getPerimeter(board1) >
self:getPerimeter(board2) and board1 or board2
end
end
@@ -524,7 +550,8 @@ function Level:orderSegments(segs)
-- -- print("new[i] end equals start of ")
-- s:debug()
if new[i].vertical == s.vertical and new[i].horizontal == s.horizontal then
if new[i].vertical == s.vertical and new[i].horizontal ==
s.horizontal then
-- -- print("join contiguous "..tostring(i).. 'with'..tostring(j))
new[i]:joinContiguous(s)
table.remove(segs, j)
@@ -544,7 +571,8 @@ function Level:orderSegments(segs)
-- -- print("new[i] end equals end of ")
-- s:debug()
s:switchDirection()
if new[i].vertical == s.vertical and new[i].horizontal == s.horizontal then
if new[i].vertical == s.vertical and new[i].horizontal ==
s.horizontal then
-- -- print("join contiguous "..tostring(i).. 'with'..tostring(j))
new[i]:joinContiguous(s)
table.remove(segs, j)
@@ -567,10 +595,14 @@ function Level:orderSegments(segs)
-- -- print("search didnt yield any segment")
local margin = 2
while not found and margin < 5 do
fi, fs = self:findNearestSegment(new[i].secondPointX,new[i].secondPointY,segs,margin)
fi, fs = self:findNearestSegment(new[i].secondPointX,
new[i].secondPointY, segs,
margin)
if not fs then
fi, fs = self:findNearestSegment(new[i].firstPointX,new[i].firstPointY,segs,margin)
fi, fs = self:findNearestSegment(new[i].firstPointX,
new[i].firstPointY, segs,
margin)
if not fs then
margin = margin + 1
@@ -680,36 +712,28 @@ end
function Level:getPerimeter(segments)
local p = 0
if not segments then
segments = self.segments
end
for k, s in pairs(segments) do
p = p + s:length()
end
if not segments then segments = self.segments end
for k, s in pairs(segments) do p = p + s:length() end
return p
end
function Level:findNearestSegment(x, y, segments, margin)
if not segments then
segments = self.segments
end
if not margin then
margin = 2
end
if not segments then segments = self.segments end
if not margin then margin = 2 end
-- find a touching segment within margins
for i, segment in pairs(segments) do
if segment.vertical then -- vertical line
if x >= segment.firstPointX - margin and x <= segment.firstPointX + margin and
y <= math.max(segment.firstPointY, segment.secondPointY) and
y >= math.min(segment.firstPointY, segment.secondPointY)
then
if x >= segment.firstPointX - margin and x <= segment.firstPointX +
margin and y <=
math.max(segment.firstPointY, segment.secondPointY) and y >=
math.min(segment.firstPointY, segment.secondPointY) then
return i, segment
end
elseif segment.horizontal then -- horizontal line
if y >= segment.firstPointY - margin and y <= segment.firstPointY + margin and
x <= math.max(segment.firstPointX, segment.secondPointX) and
x >= math.min(segment.firstPointX, segment.secondPointX)
then
if y >= segment.firstPointY - margin and y <= segment.firstPointY +
margin and x <=
math.max(segment.firstPointX, segment.secondPointX) and x >=
math.min(segment.firstPointX, segment.secondPointX) then
return i, segment
end
end
@@ -719,13 +743,12 @@ end
function Level:containsBalls(segments)
-- returns numbere of balls inside the bounds passed in or self.segments
if not segments then
segments = self.segments
end
if not segments then segments = self.segments end
local counter = 0
for k, b in pairs(self.balls) do
if self:insideBounds(b.x + 4, b.y + 4,segments) or self:pointOnEdge(b.x+4, b.y+4, seegments) then
if self:insideBounds(b.x + 4, b.y + 4, segments) or
self:pointOnEdge(b.x + 4, b.y + 4, seegments) then
counter = counter + 1
end
end

View File

@@ -71,3 +71,46 @@ function print_s(segments)
s:debug(tostring(k))
end
end
-- convert a list of points forming a polygon {x1, y1, x2, y2, ...} into a mesh
function poly2mesh(points)
-- remove duplicates???
local polypts = love.math.triangulate(points)
local tlist
local vnums = {}
local vcoords = {}
do
local verthash = {}
local n = 0
local v
-- use unique vertices by using a coordinate hash table
for i = 1, #polypts do
for j = 1, 3 do
local px = polypts[i][j * 2 - 1]
local py = polypts[i][j * 2]
if not verthash[px] then verthash[px] = {} end
if not verthash[px][py] then
n = n + 1
verthash[px][py] = n
vcoords[n * 2 - 1] = px
vcoords[n * 2] = py
v = n
else
v = verthash[px][py]
end
vnums[(i - 1) * 3 + j] = v
end
end
end
local mesh = love.graphics.newMesh(#vcoords, "triangles", "static")
for i = 1, #vcoords / 2 do
local x, y = vcoords[i * 2 - 1], vcoords[i * 2]
-- Here's where the UVs are assigned
mesh:setVertex(i, x, y, x / 50, y / 50, .5, .5, .9, 1)
end
mesh:setVertexMap(vnums)
return mesh
end

View File

@@ -21,7 +21,7 @@ function PlayState:enter()
end
function PlayState:update(dt)
-- self.level:update(dt)
self.level:update(dt)
self.player:update(dt)
self:updateBalls(dt, self.level)
end