hello, i need some help with my quadtree, i’ve figured everything out but i can’t seem to find out how to fix it.
local function FindQuadrant(pos)
local function RecursiveTracking(times,lasttable)
times += 1
if times >= Max then
return lasttable
end
local Sorted = table.clone(lasttable)
Sorted.Position = nil
Sorted.Quad1 = nil
Sorted.Quad2 = nil
for index,Table in pairs(Sorted) do
if Table.Position then
local p1,p2,p3,p4,mid = UnpackArray(Table.Position)
local InPoly = GetPointInPolgyon(pos.X,pos.Z,unwrapper({p1,p2,p3,p4}))
for i,v in pairs(Table.Position) do
local part = Instance.new("Part")
local gui = game.ReplicatedStorage.SurfaceGui:Clone()
gui.Parent = part
gui.Adornee = part
gui.TextLabel.Text = i
part.Size = InPoly and Vector3.new(5,100,5) or Vector3.new(15,15,15)
part.Position = v
part.Anchored = true
part.Color = InPoly and Color3.new(0.14902, 1, 0) or Color3.new(1, 0, 0)
part.Parent = workspace.Folder
end
if InPoly then
return RecursiveTracking(times,Table)
end
else
return "Positions not here"
end
end
end
local Quadrant = RecursiveTracking(0,Grid)
return Quadrant
end
workspace.Part:GetPropertyChangedSignal("Position"):Connect(function()
workspace.Folder:ClearAllChildren()
FindQuadrant(workspace.Part.Position)
end)
local function GetPointInPolgyon(x,y,poly)
-- By Pedro Gimeno, donated to the public domain
local x1, y1, x2, y2
local len = #poly
x2, y2 = poly[len - 1], poly[len]
local wn = 0
for idx = 1, len, 2 do
x1, y1 = x2, y2
x2, y2 = poly[idx], poly[idx + 1]
if y1 > y then
if (y2 <= y) and (x1 - x) * (y2 - y) < (x2 - x) * (y1 - y) then
wn = wn + 1
end
else
if (y2 > y) and (x1 - x) * (y2 - y) > (x2 - x) * (y1 - y) then
wn = wn - 1
end
end
end
return wn ~= 0 -- even/odd rule
end
local function unwrapper(tbl)
local unwrapped = {}
for i,v in pairs(tbl) do
unwrapped[#unwrapped + 1] = v.X
unwrapped[#unwrapped + 1] = v.Z
end
return unwrapped
end
local function UnpackArray(array)
local dict = {}
for i,v in pairs(array) do
table.insert(dict,v)
end
return table.unpack(dict)
end
Don’t worry about the quadtree creation as i can confirm the points are correct, lmk if you want the grid structure though.