# Why is 1 added to this terrain generation equation for positioning the water?

This is actually from @okeanskiy 's video on perlin noise & terrain generation. I’ve went ahead and wrote out the code fully to the point where it works, I understand everything except for this part.

``````water.Position = Vector3.new((maximumX + 1) * 5 / 2, -5, (maximumZ + 1) * 5 / 2) + Vector3.new(0, 50, 0)
``````

Why are we adding 1 to maximumX and maximumZ? I get that otherwise the waterpart is somewhat off, but where does this 1 come from?

Full code:

``````local maximumX = 100
local maximumZ = 100

local gridLayout = {}

for x = 1, maximumX do
gridLayout[x] = {}
for z = 1, maximumZ do
gridLayout[x][z] = math.noise(x/10, z/10) * 15
end
end

for x = 1, maximumX do
for z = 1, maximumZ do
local part = Instance.new("Part", workspace)
part.Size = Vector3.new(5,30,5)
part.Position = Vector3.new(x*5, gridLayout[x][z], z*5) + Vector3.new(0, 50, 0)
part.Anchored = true
end
end

local water = Instance.new("Part", workspace)
water.Anchored = true
water.Size = Vector3.new(maximumX * 5, 30, maximumZ * 5)
water.Position = Vector3.new((maximumX + 1) * 5 / 2, -5, (maximumZ + 1) * 5 / 2) + Vector3.new(0, 50, 0)
water.Transparency = 0.6
``````

This is because x and z start at 1 in the loop. What that means is the first position is at (5,5) for the first tile and the last tile is at position (500, 500). The water part is a single piece that is as big as the whole generated terrain, meaning it’s important it’s placed in the center. To get the center we get the average of the points (500+5)/2. You can rewrite that as (100 + 1) * 5 / 2. That’s why there is a + 1. If the loop started at 0, this wouldn’t be necessary since the first point would be zero and wouldn’t effect the sum, however then you would have to have a -1 on the loop number to have it spawn the correct amount.

1 Like