How can I get all adjacent floor tiles in a room?

Hey developers!

I’m making a grid building game and I am trying to detect how much of a specific object is inside each room. I first used flood filling to seperate enclosed rooms with walls.

local grid_size = 2

local function createPart(pos)
	local newPart = Instance.new("Part")
	newPart.Name = "FloorDetection"
	newPart.Anchored = true
	newPart.Size = Vector3.new(grid_size, 0.25, grid_size)
	newPart.Position = pos
	newPart.CollisionGroup = "FloorDetection"
	newPart.Parent = folder
	
	local sgui = Instance.new("SurfaceGui")
	sgui.Parent = newPart
	
	local tl = Instance.new("TextLabel")
	tl.BackgroundTransparency = 1
	tl.TextScaled = true
	tl.Size = UDim2.new(1, 0, 1, 0)
	tl.Parent = sgui

	local touchingParts = newPart:GetTouchingParts()
	
	if touchingParts[1] then
		tl.Text = touchingParts[1].Name
	else
		tl.Text = "None"
		newPart.Color = non_collision_color
	end
end

for n = top - grid_size, bottom + grid_size, -grid_size do
	for n2 = left - (grid_size * 2), right + (grid_size * 2), grid_size do
		createPart(Vector3.new(n, 1, n2))
	end
end

I got the following results below:

As you can see above, tiles that have a wall on top of it is highlighted as grey, whereas tiles with no walls on top of it are highlighted as blue.

I want to make it so that a script can merge all the adjacent blue tiles to one model. For example, in the first image, I want all the blue tiles circled in red parented all together under a new model.

However, I’m not sure how to code this at all, or even where to get started. I’ve searched on the DevForum and the solutions I’ve found were unrelated. Any ideas?

1 Like

Perhaps you’re looking for a greedymeshing algorithm.

1 Like

Perfect, thanks! This was exactly what I was looking for, but I couldn’t remember what it was for whatever reason.

For anyone looking at this topic in the foreseeable future, take a look at this module, it’s really useful for greedy meshing:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.