Prevent players from placing walls inside another wall

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.


The 2 walls here connect at 90 degrees, as you can see their Hitboxes are touching. This is fine, problem arises in image after

As you can see here, the hitbox is directly inside of this wall, meaning theres 2 wall models, even tho only 1 is needed.

So how can I get it to know if a hitbox is in the same spot (technically not same CFrame)

As intersecting hitboxes should be allowed (for example the 90 degree wall in the second pic, or if players wanna make a cross shape or something)

Just do a Region3, as I assume you can only do 90 degree rotations instead of increments like 45.

You can do any angle increment

Don’t know if it still works but worth a try.

While placing a wall, you could create a loop that will use Wall:GetTouchingParts() and check if another wall is detected inside the returned table.

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.

Yea that’s what Im trying to do, but I don’t know how

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?

1 Like