chunk index structure, flat or matrix?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

chunk index structure, flat or matrix?

Post 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?
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: chunk index structure, flat or matrix?

Post by bobbyjones »

I thought chunck was a data structure like a quad tree for example
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: chunk index structure, flat or matrix?

Post 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.
User avatar
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?

Post 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.
When I write def I mean function.
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: chunk index structure, flat or matrix?

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 94 guests