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?
chunk index structure, flat or matrix?
- parallax7d
- Citizen
- Posts: 82
- Joined: Wed Jul 02, 2014 11:44 pm
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: chunk index structure, flat or matrix?
I thought chunck was a data structure like a quad tree for example
Re: chunk index structure, flat or matrix?
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),
That said, this idea has a certain elegance to it:
Code: Select all
index = 1 + ((Y-1) * 256) + (X-1)
chunk[index] = chunk_data
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.parallax7d wrote:chunk['1,-5'] = { chunkX = 1, chunkY = -5, ... chunk data ... } -- flat
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: chunk index structure, flat or matrix?
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.
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.
When I write def I mean function.
- parallax7d
- Citizen
- Posts: 82
- Joined: Wed Jul 02, 2014 11:44 pm
Re: chunk index structure, flat or matrix?
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.
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests