customBoundaries = {
-- this boundary is between the big brown building(s) and the skatepark on the road in LS
["test"] = {
{x = 1842.2908935547, y = -1412.469360351},
{x = 1856.8596191406, y = -1412.555664062},
{x = 1851.5925292969, y = -1436.5225830078},
{x = 1842.0438232422, y = -1424.5217285156},
},
}
local colorOfBounds = tocolor(255, 0, 0)
bindKey("e", "down",
function ()
if isElementWithinBoundary(localPlayer, "test") then
colorOfBounds = tocolor(0, 255, 0)
else
colorOfBounds = tocolor(255, 0, 0)
end
end
)
addEventHandler("onClientRender", getRootElement(),
function ()
local localX, localY, localZ = getElementPosition(localPlayer)
local boundary = "test"
for i = 1, #customBoundaries[boundary] do
local currentPoint = customBoundaries[boundary][i]
local nextPoint = customBoundaries[boundary][i + 1]
if currentPoint then
if i == #customBoundaries[boundary] then
nextPoint = customBoundaries[boundary][1]
dxDrawLine3D(currentPoint.x, currentPoint.y, localZ, nextPoint.x, nextPoint.y, localZ, colorOfBounds, 3)
end
dxDrawLine3D(currentPoint.x, currentPoint.y, localZ, nextPoint.x, nextPoint.y, localZ, colorOfBounds, 3)
end
end
end
)
function isElementWithinBoundary(element, boundary)
if isElement(element) and boundary and customBoundaries[boundary] and #customBoundaries[boundary] >= 3 then
local pointX, pointY, pointZ = getElementPosition(element)
local intersections = 0
for i = 1, #customBoundaries[boundary] do
local currentPoint = customBoundaries[boundary][i]
if currentPoint then
local nextPoint = customBoundaries[boundary][i + 1]
local previousPoint = customBoundaries[boundary][i - 1]
if i == 1 then
previousPoint = customBoundaries[boundary][1]
elseif i == #customBoundaries[boundary] then
nextPoint = customBoundaries[boundary][1]
end
if areIntersecting(pointX, pointY, previousPoint.x, previousPoint.y, currentPoint.x, currentPoint.y, nextPoint.x, nextPoint.y) then
intersections = intersections + 1
end
end
end
if intersections > 0 then
return true
end
end
return false
end
function sign(x1, y1, x2, y2, x3, y3)
return (x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3)
end
function areIntersecting(pointX, pointY, x1, y1, x2, y2, x3, y3)
local b1 = sign(pointX, pointY, x1, y1, x2, y2) < 0
local b2 = sign(pointX, pointY, x2, y2, x3, y3) < 0
local b3 = sign(pointX, pointY, x3, y3, x1, y1) < 0
return b1 == b2 and b2 == b3
end
Tested. Works with 3 or more points.
Yes, you can check the element within the the polygon.