I've been keeping my eye on this lib for an idea for a 3D game possibly. Something along the lines of a voxel cube based game, minecraft-like, but with a
huge twist that I won't disclose just yet.
I read somewhere that to transverse the z-axis in lua hampers performance quite a bit, so I had an idea. Why not use a 2D array and store the z-axis information as a number?
IE.
z-axis is 10 cubes long. Let's say that you can only insert 3 different cube types. (air, dirt, grass) -- assuming a minecraft like example
air = 1,
dirt = 2,
grass = 3,
So at map[y][x] = num
Code: Select all
num = 1
[x][y][1] = 1 -- air
[x][y][2-10] = 1 -- rest of blocks are air
num = 2
[x][y][1] = 2 -- dirt
[x][y][2-10] = 1 -- rest of blocks are air
num = 3
[x][y][1] = 3 -- grass
[x][y][2-10] = 1 -- rest of blocks are air
num = 4
[x][y][1] = 1 -- air
[x][y][2] = 2 -- dirt
[x][y][3-10] = 1 -- rest of blocks are air
num = 5
[x][y][1] = 2 -- dirt
[x][y][2] = 2 -- dirt
[x][y][3-10] = 1 -- rest of blocks are air
num = 6
[x][y][1] = 3 -- grass
[x][y][2] = 2 -- dirt
[x][y][3-10] = 1 -- rest of blocks are air
Hopefully you can see the pattern here.
so you have 59049 (3^10) range of possibilities. Insert some fancy math division to determine what block type on a z-axis location is, and whoolah!
Code: Select all
[x][y][1] = (num % 3) + 1
[x][y][2] = math.floor( (num % 9)/3) + 1
[x][y][3] = math.floor( (num % 81)/3) + 1
...
-- or this basically,
[x][y][z] = math.floor( (num % 3^z)/3) + 1 -- unless [z] = 1 then omit the math.floor part
I'm not sure if this would be any faster than just using a z-axis table to look up this information. The math algorithm you use to break up the z-axis can be localized (since it will be called repeatedly) and be a little faster. So the question comes down to which way is faster to access the Z-axis: looking up a third [z] axis table or using a bit of math on a 2D array to figure out your [z] info? Not sure how to test this... Also the number of block types and the size of the z-axis have a limit. (25 block types with a 10-size z-axis OR 5 block types with a 20-size z-axis --- approaches the Lua rounding error limit)
hmmm... on second thought if you turned into a string that limit would be MUCH bigger, but I imagine it would require more math.