Infinite 2D map data ?
Posted: June 4th, 2019, 9:35 pm
Hi,
I'm planning a game with an potentially infinite 2D map, made out of Cells. Each cell is an object with numerous members.
Class Cell
{
....
}
The goal is to access very quickly any of these Cells by its coordinate (x,y).
So I'm thinking about dividing the map into "chunks" which would be a fixed sized array of WxH Cells.
I'm thinking about storing chunks into a std::map. I first tried this :
Class Chunk
{
Cell* cells; // pointer to a [CHUNK_SIZE*CHUNK_SIZE] array
}
typedef std::pair<int, int> Point; // x,y coordinates of a Cell
Class Map
{
std::map<Point, Chunk*> chunks;
Cell* GetCell(int x, int y);
}
The GetCell member will be used to read of write the Cell.
But after some testings, it's quite slow because I need of lot of access to the map contents. I guess the (x,y)-key is not optimal.
So I'm thinking about a map<int, map<int, Chunk> > (a list of "rows" of "columns" of Cells)
Or maybe using pointers, like
map<int, (map<int,*Chunk>)* >
...so that copying/moving a "column" of Chunk would avoid duplicating any data, but only pointers...
What do you think ?
-----------------------------------------
My other idea is that, to enlighten computation work, I do not have to compute the logic of the game every graphical tick : I do not need to be so precise time-speaking.
So I was thinking about using a thread for the sync timed graphic loop, and another thread, more relaxed, that would constantly calculate the logic of the game, at its own rythm, only passing "generic" orders to the graphic engine, like "entity N moves from x1,y1 to x2,y2 in S seconds".
Do you think about another solution ?
I'm planning a game with an potentially infinite 2D map, made out of Cells. Each cell is an object with numerous members.
Class Cell
{
....
}
The goal is to access very quickly any of these Cells by its coordinate (x,y).
So I'm thinking about dividing the map into "chunks" which would be a fixed sized array of WxH Cells.
I'm thinking about storing chunks into a std::map. I first tried this :
Class Chunk
{
Cell* cells; // pointer to a [CHUNK_SIZE*CHUNK_SIZE] array
}
typedef std::pair<int, int> Point; // x,y coordinates of a Cell
Class Map
{
std::map<Point, Chunk*> chunks;
Cell* GetCell(int x, int y);
}
The GetCell member will be used to read of write the Cell.
But after some testings, it's quite slow because I need of lot of access to the map contents. I guess the (x,y)-key is not optimal.
So I'm thinking about a map<int, map<int, Chunk> > (a list of "rows" of "columns" of Cells)
Or maybe using pointers, like
map<int, (map<int,*Chunk>)* >
...so that copying/moving a "column" of Chunk would avoid duplicating any data, but only pointers...
What do you think ?
-----------------------------------------
My other idea is that, to enlighten computation work, I do not have to compute the logic of the game every graphical tick : I do not need to be so precise time-speaking.
So I was thinking about using a thread for the sync timed graphic loop, and another thread, more relaxed, that would constantly calculate the logic of the game, at its own rythm, only passing "generic" orders to the graphic engine, like "entity N moves from x1,y1 to x2,y2 in S seconds".
Do you think about another solution ?