I have a wall generator, that generates walls between 2 points, like so
I want to be able to know if said walls are intersecting one another, and if so, be able to place a point at the intersection, and update the walls to have new end points. So the idea would be, instead of 2 walls here, it’d be split into 4 walls (color coded) and have their start and end points changed to match the intersecting pole
When a wall is generated, this is how I store the pole and wall positions
local function StorePole(pole)
if not AllPoles[pole] then
AllPoles[pole] = {}
end
end
local function StoreWall(wall, startPoint, endPoint)
if not AllWalls[wall] then
AllWalls[wall] = {}
end
table.insert(AllPoles[startPoint], wall)
table.insert(AllPoles[endPoint], wall)
AllWalls[wall] = {
Start = startPoint,
End = endPoint
}
end
-- Generate wall
StorePole(Pole1.Position)
StorePole(Pole2.Position)
StoreWall(WallModel, Pole1.Position, Pole2.Position)
So you basically store the poles position, and the pole has any walls that are attached to it added to their table, and the walls have the start and end pole added.
And then I tried this function, which I found here
function intersection_point(line_1_start, line_1_end, line_2_start, line_2_end)
print(line_1_start, line_1_end, line_2_start, line_2_end)
local line_1_m = (line_1_end.Z - line_1_start.Z) / (line_1_end.X - line_1_start.X)
local line_2_m = (line_2_end.Z - line_2_start.Z) / (line_2_end.X - line_2_start.X)
local line_1_b = line_1_start.Z - (line_1_m * line_1_start.X)
local line_2_b = line_2_start.Z - (line_2_m * line_2_start.X)
local intersect_x = (line_2_b - line_1_b) / (line_1_m - line_2_m)
local intersect_z = (line_1_m * intersect_x) + line_1_b
return Vector3.new(intersect_x, line_1_start.Y, intersect_z)
end
for wall, polePositions in pairs(AllWalls) do
local Intersect = intersection_point(
Pole1.Position, -- Pole1 and Pole2 are the most recent poles of the wall being added
Pole2.Position,
polePositions.Start,
polePositions.End
)
if Intersect then
print(Intersect)
end
end
However, Intersect returns nan, 6, nan