Page 1 of 1

chunk index structure, flat or matrix?

Posted: Mon Apr 20, 2015 1:03 am
by parallax7d
I'm splitting my game world into chunks, and was wondering if anyone had an opinion on the merits of a 'flat' store for the chunk's index, or if nested (2d matrix) style was fine?

chunk[1][-5] = { ... chunk data ...) -- nested

or

chunk['1,-5'] = { chunkX = 1, chunkY = -5, ... chunk data ... } -- flat

I'm kind of leaning towards the 2d matrix style, but don't want to make a vital mistake since it's a little more complex of a data structure. Like it might make things relatively slower or something?

Re: chunk index structure, flat or matrix?

Posted: Mon Apr 20, 2015 3:15 am
by bobbyjones
I thought chunck was a data structure like a quad tree for example

Re: chunk index structure, flat or matrix?

Posted: Tue Apr 21, 2015 3:17 am
by Inny
It's traditional to do a 'Strided Array', as in you do the arithmetic to turn a 2d array into a 1d array. So, if you have a 256x256 2d space, the math to make it a lua array (and I'm assuming X and Y are in the range 1-256),

Code: Select all

index = 1 + ((Y-1) * 256) + (X-1)
chunk[index] = chunk_data
That said, this idea has a certain elegance to it:
parallax7d wrote:chunk['1,-5'] = { chunkX = 1, chunkY = -5, ... chunk data ... } -- flat
Though lua can be a bit temperamental about how many strings you create, since it has to intern them and then later garbage collect them.

Re: chunk index structure, flat or matrix?

Posted: Tue Apr 21, 2015 8:54 am
by kikito
If your data looks like a 2d matrix, use a 2d matrix. The kinds of loops you will have to do to update and draw align better with that structure.

An id-based structure can make sense if the data is very sparse (i.e. if you are modelling a space-based game, where most of the space is empty), but that's about it.

Re: chunk index structure, flat or matrix?

Posted: Thu Apr 23, 2015 4:47 pm
by parallax7d
Cool, I'll stick with the simple matrix for now then. Striding the matrix sounds like a nice optimization, but I want to have unlimited sized maps, at least at this stage.

Has anyone had troubles updating their worlds in 'rows' (x, y) when 'columns' (y, x) would have been preferable? I'm thinking if objects are 'falling' vertically very quickly it would be better to have the chunks laid out in columns.