I sorted it out eventually. However there's a lot of happening.
Since the ObjectPolygon doesn't return updated coordinates after the object is moved with ObjectOffset i created a function which returns updated coordinates based on the current offset.
function sortPolygon(obj)
local polygonTable = {}
for i = 1, #obj.ObjectPolygon do
table.insert(polygonTable, { x = obj.ObjectPolygon[i].x + obj.ObjectOffset.x, y = obj.ObjectPolygon[i].y + obj.ObjectOffset.y})
end
return polygonTable
end
For some reason the isPointInsidePolygon fuction was returning false to me, so i created the same function based on stackowerflow ( the function is crazy, i have no idea how it works )
function pointInPolygon(polygon, x, y)
local i, j = #polygon, #polygon
local oddNodes = false
for i = 1, #polygon do
if ((polygon[i].y = y
or polygon[j].y = y) and (polygon[i].x <= x
or polygon[j].x <= x)) 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
return oddNodes
end
And then finally a function to check if a polygon is inside of a polygon
function polygonInPolygon(polygon, object)
for i =1, #object do
if pointInPolygon(polygon, object[i].x, object[i].y) then
--anything here // collision
return true
end
end
return false
end
Well and since i want to check a more polygons
function polygonInPolygons(polygons, object)
for i = 1, #polygons do
if polygonInPolygon(sortPolygon(Objects[polygons[i]]), object) then
break
end
end
end
+ i need a table of the polygons i want to check
polygons = {"OBJ_heart_01", "OBJ_heart_02", "OBJ_heart_03", "OBJ_heart_04", "OBJ_heart_05"}
That would be all for the definition script part. At the beginning of the scene where i want to run this i need to run a loop for setting a position for the symbol representing a cursor
function setSymbol()
Objects.OBJ_symbol.ObjectOffset = { x = ( getCursorPos().x - 35 ), y = ( getCursorPos().y - 39 )}
end
registerEventHandler("mainLoop", "setSymbol")
And then i'm calling a Visionaire action which is also a loop but runs once in a 100ms becase it takes a while to run all the code. All i need there is:
The object i'm checking if is inside of a polygon
local object = sortPolygon(Objects.OBJ_symbol)
And finally run the main function
polygonInPolygons(polygons, object)
I also have a loop that animates the objects with the :to() function, it runs once in a 500ms.
Also i reduced the amount of objects from 10 to 5 to make it faster.