Hello, guys. Iām making mining game. For it I wanted to use chunk system, with chunk size = 16.
Chunk have blocks, starting from 1 and ending with 16th.
To get block chunk position, I used formula:
Can someone help me with making furmula, which will also work with negative numbers, because IDK what I need to do to work it with negative numbers with hard-coded if-else statements?
No. My system uses integer values, to determine XYZ position of block, like 92, 14, -12
Also, blocks are placed inside chunks, which are 16x16x16 blocks. First chunk have block from 1-16. Second one - 17-32. third - 33-48. Zero chunk have -15 to 0, Minus first chunk have -31 to -16 blocks.
That block positions are only code accessible, so I need to get specific block data:
Blockgrid[ChunkX][ChunkY][ChunkZ][BlockX][BlockY][BlockZ] = Block data
local function getBlockPosition(x)
local bp
if x >= 0 then
bp = (x-1) % 16 + 1
else
bp = (math.fmod(x-1, 16) + 16) % 16 + 1
end
return bp
end
print(getBlockPosition(15)) -- 15
print(getBlockPosition(24)) -- 8
print(getBlockPosition(-20)) -- 12
For āa % bā, the remainder will be negative when b is negative.
For āmath.fmod(x, y)ā, the remainder will be negative when x is negative.
In your example, you are using āa % bā where b is 16, so it will always return a positive remainder.
-- a % b is negative only when b is negative
(Position - 1) % 16 + 1
In the case where you are using āmath.fmod(x, y)ā, it is better to define a function that adds absolute value of y to the remainder when it is negative.
function positiveMod(x, y)
local remainder = math.fmod(x, y)
if remainder < 0 then
remainder += math.abs(y)
end
return remainder
end
-- the formula will then be
positiveMod(Position - 1, 16) + 1