So for starters, players can place down a wall, like below. The Wall model itself has a Hitbox, which will be used later for detecting if items are being placed within the wall.
GetTouchingParts would return if the walls intersected at the a cross section or stuff like that. I need it to only return if its being placed inside of another wall
I interpreted this as ‘Prevent players from placing walls inside other players’. I might just be dumb, but it’s worth considering rephrasing the title a little bit. ie ‘placing walls inside other walls’
You could reject an attempt to build a wall if it intersects an existing wall which is parallel to the new wall. Try using RotatedRegion3 or GetTouchingParts to detect the intersecting walls as shown above, then compare the wall directions. You could calculate the angle of each wall using math.atan2(lookvec.x, lookvec.z) and compare those to see if they are approximately equal or opposite.
The most robust way would be to do 2D line intersection tests. I just wrote this test yesterday for this thread:
local function isIntersecting(a, b, c, d)
local ab = b - a
local cd = d - c
local e = a - c
local t
local abm = ab.X / ab.Z
local cdm = cd.X / cd.Z
if math.abs(ab.X) > math.abs(ab.Z) then
t = (e.Z * cdm - e.X) / ab.X / (1 - cdm/abm)
else
t = (e.X / cdm - e.Z) / ab.Z / (1 - abm/cdm)
end
local u
if math.abs(cd.X) > math.abs(cd.Z) then
u = (e.X - ab.X * t) / cd.X
else
u = (e.Z - ab.Z * t) / cd.Z
end
print(t, u)
if t > 0 and t < 1 and u > 0 and u < 1 then
return true
end
end
If touching at the endpoints doesn’t count as intersecting then use < and > in the function above. If touching at the endpoints counts as an intersection then use <= and >=. I don’t think you’ll have issues with rounding error. a and b are the two points of the first wall and c and d are the points of the second wall. This also detects if walls are crossing not just contained in each other which I think is what you wanted, correct?