Thank you very much for your help, to be able to properly merge the two polygons I found a function that could have done it properly in the Lua HC module (HC.polygon -
https://github.com/vrld/HC/blob/master/polygon.lua)
I therefore recovered the two functions
mergedWith() and
getSharedEdge() which I adapted as follows:
Code: Select all
local getSharedEdge = function(p,q)
local pindex = setmetatable({}, {__index = function(t,k)
local s = {}
t[k] = s
return s
end})
for i = 1,#p do
pindex[p[i][1]][p[i][2]] = i
end
local i,k = #q,1
for k = 1,#q do
local v,w = q[i], q[k]
if pindex[v[1]][v[2]] and pindex[w[1]][w[2]] then
return pindex[w[1]][w[2]], k
end
i = k
end
end
local mergePolygons = function(poly1, poly2)
local p,q = getSharedEdge(poly1, poly2)
assert(p and q, "Polygons do not share an edge")
local ret = {}
for i = 1,p-1 do
ret[#ret+1] = {poly1[i][1], poly1[i][2]}
end
for i = 0,#poly2-2 do
i = ((i-1 + q) % #poly2) + 1
ret[#ret+1] = {poly2[i][1], poly2[i][2]}
end
for i = p+1,#poly1 do
ret[#ret+1] = {poly1[i][1], poly1[i][2]}
end
return ret
end
return mergePolygons
But it still fails when calling
getSharedEdge() because it doesn't find any shared edge...
I'm still thinking about it, I tried a lot of other things but I always end up with points inside the polygon which causes me collision problems.
I'm thinking of directly using HC.polygon because maybe I'm adapting it badly...
Update:
I hadn't seen this answer, so I tried to adapt it for "mesh vertices" but without success, so I then abandoned the idea of using meshes, I rewrote everything to display the polygons with
love.graphics.polygon() and be able to use the mentioned library directly. I first tried using
polybool() which sometimes works and other times not...
Code: Select all
self.zone = polybool(self.zone, self.snakeVerts, "or")
'Error: Number of vertex components must be a multiple of two'
I also tried the "and" and "not" arguments but I still got the error above.
And using the
polygon.concaveHull() function:
Code: Select all
for _, p in pairs(self.snakeVerts) do
table.insert(self.zone, p)
end
self.zone = polygon.concaveHull(0.5, self.zone)
'Error: Need at least three vertices to draw a polygon'
Then testing the function
polygon.convexHull() I noticed that it didn't really work, it removes pieces of the polygon for no reason...
I'm still looking for a solution...