I'm trying to detect if a point is inside a mesh.
The point is the player and the mesh corresponds to a circle at the start and will change shape as the execution progresses.
So I wrote a function that looks like this:
Code: Select all
function pointInPolygon(pos, mesh) -- vc : current vertice | vn : next vertice
local inPolygon = false
local countVerts = mesh:getVertexCount()
for ivc= 1, countVerts do
local ivn = ivc + 1
local xc, yc = mesh:getVertex(ivc) -- current
if ivn > countVerts then ivn = 1 end
local xn, yn = mesh:getVertex(ivn)
if (yc <= pos.y and pos.y < yn) or (yn <= pos.y and pos.y < yc)
and (pos.x < (xn - xc) * (pos.y - yc) / (yn - yc) + xc) then
inPolygon = not inPolygon
end
end
return inPolygon
end