Im trying to create parts and assign them into grid formation. I know about math.floor but idk how to use it.
Here is my result the problem is its only on the x axis and there is 50 parts here.
Im trying to create parts and assign them into grid formation. I know about math.floor but idk how to use it.
Here is my result the problem is its only on the x axis and there is 50 parts here.
math.floor returns smallest integer of given number
print(math.floor(2.9)) -- will print 2
print(math.floor(2.1)) -- will print 2
print(math.floor(2)) -- will print 2 (ofc)
print(math.floor(2.99)) -- still prints 2
You could easily check how it works in output
(Actually I do not seem to understand what you meant, so please explain better)
I knew that but i dont understand how to use it with vector3s. I made it follow the x axis so far but its still 50 parts overlapping and not turning out like a grid.
Im trying to make my part layout like this:
Isn’t that layout just a for loop with a constant increment?
local function CreatePart(Position: Vector3): Part
local Part = Instance.New("Part")
Part.Position = Position
Part.Anchored = true
Part.Parent = Workspace
return Part
end
for i = 1, 10 do
for j = 1, 10 do
for k = 1, 10 do
local Position = Vector3.new(i * 5, j * 5, k * 5)
CreatePart(Position)
end
end
end
I did use a loop to create the parts then inside the loop i would assign the parts position. I dont think its a increment since a constant increment would make it a diagonal straight line while im trying to make it a grid pattern.
I got this result.
local Increment = 10
local function CreatePart(Position: Vector3): Part
local Part = Instance.new("Part")
Part.Position = Position
Part.Anchored = true
Part.Size = Vector3.new(1, 1, 1)
Part.Parent = workspace
return Part
end
for i = 1, 10 do
for j = 1, 10 do
for k = 1, 10 do
local Position = Vector3.new(i * Increment, j * Increment, k * Increment)
CreatePart(Position)
end
end
end
Oh thats great, thanks i found a video with only one loop but i dont understand how math.floor is used here tbh, and im not trying to use 3 loops if i can use 1.
The nested loop approach would be slightly easier but if you want to use one loop then I would recommend first using the loop variable (which can be thought of as an index in an imaginary zero-based 1-dimensional array) to calculate indices in an imaginary zero-based 3-dimensional array. Then you can use those indices (xi, yi, zi) to calculate the position. The //
operator that I use in the code is floor division operator. a // b
is equivalent to math.floor(a / b)
.
local function createParts(firstPartPos: Vector3, lastPartPos: Vector3, numbersOfConsecutivePartsInDimensions: Vector3, functionToCreatePart: (position: Vector3) -> Part): {Part}
local numPartsInXDir: number, numPartsInYDir: number, numPartsInZDir: number = numbersOfConsecutivePartsInDimensions.X, numbersOfConsecutivePartsInDimensions.Y, numbersOfConsecutivePartsInDimensions.Z
-- :Max(Vector3.one) is used to avoid division by zero when the number of parts in a dimension is 1.
local positionIncrements: Vector3 = (lastPartPos - firstPartPos) / (numbersOfConsecutivePartsInDimensions - Vector3.one):Max(Vector3.one)
local numberOfParts: number = numPartsInXDir * numPartsInYDir * numPartsInZDir
local parts: {Part} = table.create(numberOfParts)
for i: number = 0, numberOfParts - 1 do
local xi: number = i // (numPartsInYDir * numPartsInZDir)
local yi: number = i % (numPartsInYDir * numPartsInZDir) // numPartsInZDir
local zi: number = i % numPartsInZDir
local partPos: Vector3 = firstPartPos + Vector3.new(xi, yi, zi) * positionIncrements
local part: Part = functionToCreatePart(partPos)
table.insert(parts, part)
end
return parts
end
The way xi
, yi
and zi
are calculated in the above loop can also be written in a way that shows a pattern.
local xi: number = i % (numPartsInXDir * numPartsInYDir * numPartsInZDir) // (numPartsInYDir * numPartsInZDir * 1)
local yi: number = i % ( numPartsInYDir * numPartsInZDir) // ( numPartsInZDir * 1)
local zi: number = i % ( numPartsInZDir) // ( 1)
There’s also an iterative way for calculating the indices. It can easily be used for any number of dimensions but when the number of dimensions is constant and small, the code above is more practical.
-- index1D should be an integer in the range [0, array1DLength - 1].
-- To convert the index of a Luau array to such an index, just do luauArrayIndex - 1.
local function getIndicesInDimensionsFrom1DArrayIndex(index1D: number, arrayLengthsInDimensions: {number}): {number}
local indicesInDimensions: {number} = table.create(#arrayLengthsInDimensions)
for dimension: number = #arrayLengthsInDimensions, 1, -1 do
local lengthInDimension: number = arrayLengthsInDimensions[dimension]
indicesInDimensions[dimension] = index1D % lengthInDimension
index1D //= lengthInDimension
end
return indicesInDimensions
end
-- firstPartPos should be the position of the part whose indices are xi = 0, yi = 0, zi = 0.
-- and lastPartPos should be the position of the part whose indices are xi = numberOfConsecutivePartsInDimensions[1] - 1,
-- yi = numberOfConsecutivePartsInDimensions[2] - 1, zi = numberOfConsecutivePartsInDimensions[3] - 1.
local function createPartsV2(firstPartPos: Vector3, lastPartPos: Vector3, numbersOfConsecutivePartsInDimensions: Vector3, functionToCreatePart: (position: Vector3) -> Part): {Part}
local positionIncrements: Vector3 = (lastPartPos - firstPartPos) / (numbersOfConsecutivePartsInDimensions - Vector3.one):Max(Vector3.one)
local arrayOfNumbersOfPartsInDimensions = {numbersOfConsecutivePartsInDimensions.X, numbersOfConsecutivePartsInDimensions.Y, numbersOfConsecutivePartsInDimensions.Z}
local lengthOf1DArray: number = 1
for _, length: number in arrayOfNumbersOfPartsInDimensions do
lengthOf1DArray *= length
end
local axes: {Enum.Axis} = Enum.Axis:GetEnumItems()
local parts: {Part} = table.create(lengthOf1DArray)
for iPart: number = 0, lengthOf1DArray - 1 do
local indicesInDimensions: {number} = getIndicesInDimensionsFrom1DArrayIndex(iPart, arrayOfNumbersOfPartsInDimensions)
local partPosition: Vector3 = Vector3.zero
for dimension: number = 1, 3 do
local indexInDimension: number = indicesInDimensions[dimension]
local positiveAxisUnitVector: Vector3 = Vector3.FromAxis(axes[dimension])
partPosition += indexInDimension * positionIncrements:Dot(positiveAxisUnitVector) * positiveAxisUnitVector
end
local part: Part = functionToCreatePart(partPosition)
parts[iPart + 1] = part
end
return parts
end
local partColor: Color3 = Color3.new(1, 0, 0)
local partSize: Vector3 = Vector3.new(1, 1, 1)
local numbersOfConsecutivePartsInDimensions: Vector3 = Vector3.new(10, 10, 10)
local firstPartPos: Vector3 = Vector3.new(10, 0, 20)
local lastPartPos: Vector3 = Vector3.new(60, 50, 70)
local partFolder: Folder = Instance.new("Folder")
partFolder.Name = "PartGrid"
local function createPart(position: Vector3): Part
local part: Part = Instance.new("Part")
part.Color = partColor
part.BottomSurface, part.TopSurface = Enum.SurfaceType.Smooth, Enum.SurfaceType.Smooth
part.Anchored = true
part.Position = position
part.Size = partSize
part.Parent = partFolder
return part
end
createParts(firstPartPos, lastPartPos, numbersOfConsecutivePartsInDimensions, createPart)
--createPartsV2(firstPartPos, lastPartPos, numbersOfConsecutivePartsInDimensions, createPart)
partFolder.Parent = workspace
Using a single loop has the advantage that you can use it regardless of how many dimensions the grid or multi-dimensional array has but when the number of dimensions is constant and small, a nested loop would be slightly more simple.
In that image, the grid is two dimensional. There will be 20 consecutive parts in x direction and 10 consecutive parts in z direction.
math.floor((i - 1) / 10)
can be thought of as index in x-direction. When 1 <= i <= 10, math.floor((i - 1) / 10) will be 0. When 11 <= i <= 20, math.floor((i - 1) / 10) will be 1. When 191 <= i <= 200, `math.floor((i - 1) / 10) will be 19. So the x index will have integer values in the interval [0, 19].(i - 1) % 10
can be though of as index in z direction. When i is 1, 11, 21, 31, …, 111, 121, …, 181 or 191, (i - 1) % 10
will be 0. When i is 2, 12, 22, 32, …, 112, 122, …, 182 or 192, (i - 1) % 10
will be 1. When i is 10, 20, 30, 40, …, 120, 130, …, 190 or 200, (i - 1) % 10
will be 9. So the z index will have integer values in the interval [0, 9].yPos
the code is equivalent to my code with the following parameter values:firstPartPos = Vector3.new(10, yPos, 10)
lastPartPos = Vector3.new(10 + 20 * 10, yPos, 10 + 10 * 10)
numbersOfConsecutivePartsInDimensions = Vector3.new(20, 1, 10)
i would help if you showed your code but its important to note that you have to use the modulo (%) operator if you want to make a grid layout
Thanks i understood it more and using something i made myself after reading this post i got what i wanted:
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.