3 Dimensional Grid Construction Help

So basically Im trying to implement the marching squares algorithm and i need a 3 Dimensional grid to do some testing on but I’ve hit a fork in the road I cant seem to configure the X and Y axis properly it stops a bit short and thats rather irritating but I feel im close

Ive tried tinkering with numbers but it only pushes me further from the desired goal

one of many attempts ^
On the bright side this is the closest

	local Number = 0
	for Y = 0, NumberAmountTable.Y*Scale do
		for X = 0, NumberAmountTable.X*Scale do
			for Z = 0, (NumberAmountTable.Z)*Scale do
				local Clone = Object:Clone()
				local Xmath = (Object.CFrame.RightVector  * (NumberAmountTable.X) * X/NumberAmountTable.X)/Scale
				local Zmath = (Object.CFrame.LookVector * NumberAmountTable.Z * Z/NumberAmountTable.Z)/Scale
				local Ymath = (Object.CFrame.UpVector  * -(NumberAmountTable.Y) * Y/NumberAmountTable.Y)/Scale
				Clone.Size = Vector3.new(.5, .5, .5)
				Clone.Position = Object.Position + Xmath + Zmath  + Ymath  
				Clone.BillboardGui:Destroy()
				Clone.Parent = game.Workspace.MarchingCubes
				Clone.Name = "GridPoint"..Number
				--print(Clone.Name)
				Clone.Shape = Enum.PartType.Ball
				if Clone.Position == Object.Position or Clone.Position == OppPos then
					Clone:Destroy()
				end
				Number = Number + 1
				--wait()
			end
			--wait()
		end
		wait()
	end
end
local Part5 = script.Parent.Part5
local Part7 = script.Parent.Part7
local Part4 = script.Parent.Part4
local Part3 = script.Parent.Part3
local NumberAmountTable = {}
NumberAmountTable.Z = (Part7.Position - Part4.Position).magnitude
NumberAmountTable.X = (Part7.Position - Part5.Position).magnitude
NumberAmountTable.Y = (Part7.Position - Part3.Position).magnitude 
Grid(Part7, NumberAmountTable, .1)```

I’m not certain, but I believe your problem lies in your for loops.

The for loops you have designed will loop through every whole number that is one tenth of the distance towards your goal (at least, that is what I think the Scale variable is).

Reading this I assume you want a “Preferred distance” between the objects, sadly user input do not allow for a perfect distance modifier.

However, it is possible to make something almost perfect by rounding numbers.

The code below should work for what I think you want… Note; I don’t really know if this is what you wanted or something else is

local function Grid(Object, Distances, PreferredDistance) -- Distances is NumberAmountTable
	local AmountX = math.floor(Distances.X / PreferredDistance + 0.5)
	local AmountY = math.floor(Distances.Y / PreferredDistance + 0.5)
	local AmountZ = math.floor(Distances.Z / PreferredDistance + 0.5)
	
	local RealDistance = {X = Distances.X/AmountX, Y = -Distances.Y/AmountY, Z = Distances.Z/AmountZ}
	
	for X = 0, AmountX do
		for Y = 0, AmountY do
			for Z = 0, AmountZ do
				local Offset = Vector3.new(RealDistance.X*X, RealDistance.Y*Y, RealDistance.Z*Z)
				-- Other stuff
				Clone.Position = Object.CFrame * Offset -- See https://developer.roblox.com/en-us/articles/CFrame-Math-Operations
				-- Other stuff
			end
		end
	end
end

-- The same as before

Grid(Part7, NumberAmountTable, 10) -- Scale is now Preferred number of studs between objects.

If you wanted exact number of objects instead of a preferred distance then you can change

	local AmountX = math.floor(Distances.X / PreferredDistance + 0.5)
	local AmountY = math.floor(Distances.Y / PreferredDistance + 0.5)
	local AmountZ = math.floor(Distances.Z / PreferredDistance + 0.5)

to

	local AmountX = -- AmountOfXObjects
	local AmountY = -- AmountOfYObjects
	local AmountZ = -- AmountOfZObjects
1 Like

I figured it out

local function Grid(Object, NumberAmountTable, Scale)
	local Number = 0
	for Y = 0, NumberAmountTable.Y*Scale do
		for X = 0, NumberAmountTable.X*Scale do
			for Z = 0, (NumberAmountTable.Z)*Scale do
				local Clone = Object:Clone()
				local Xmath = (Object.CFrame.RightVector * (NumberAmountTable.X) * X/NumberAmountTable.X)/Scale
				local Zmath = (Object.CFrame.LookVector * (NumberAmountTable.Z) * Z/NumberAmountTable.Z)/Scale
				local Ymath = (Object.CFrame.UpVector * -(NumberAmountTable.Y) * Y/NumberAmountTable.Y)/Scale--]]
				Clone.Size = Vector3.new(.5, .5, .5)
				Clone.Position = Clone.Position:Lerp(Object.Position + Xmath + Zmath  + Ymath, 1)
				--Clone.Position = Object.Position + Xmath + Zmath  + Ymath  
				Clone.BillboardGui.Frame.TextLabel.Text = tostring(Number)
				Clone.Parent = game.Workspace.MarchingCubes
				Clone.Name = "GridPoint"..Number
				Clone.Shape = Enum.PartType.Ball
				Number = Number + 1
			end
			wait()
		end
		wait()
	end
end
local Part7 = script.Parent.Part7
local NumberAmountTable = {}
local Dist = 32
NumberAmountTable.Z = (Part7.Position - (Part7.Position + Vector3.new(0,0,Dist))).magnitude
NumberAmountTable.X = (Part7.Position - (Part7.Position + Vector3.new(Dist,0,0))).magnitude
NumberAmountTable.Y = (Part7.Position - (Part7.Position - Vector3.new(0,Dist,0))).magnitude 
Grid(Part7, NumberAmountTable, .1)```

Oh yeaaaa i keep forgetting mb