0
\$\begingroup\$

I need a little help with structuring my map. I want to make it efficient but I'm not sure if there's a better way to do this:

public class Map
{
    Chunk[][] Chunks;
    Object[] Objects;
}

public class Chunk
{
    public int[][] Tiles;
    public List<int> Objects;
}

This is the current structure of my map. I basically load and unload chunks if the player is too far from them. Every chunk has the same size, and has a list of objects. For each integer in the list of integers in a chunk, the corresponding Object will be updated in the parent map.

This way, I can handle collision detection without comparing two objects that are far apart: I only compare objects to other objects that are in the same chunk, or neighboring chunks.

However, I'm wondering if there is a better way to structure my map. Faster runtime. Less processing. More efficient.

This may seem like an opinion based question but for those who perceive it that way, the question is how can I improve my map to make it more efficient?

\$\endgroup\$
2
\$\begingroup\$

If your chunks are square, you could align Tiles into a single array, and use a little math to find you indices.

For example:

class Chunk {
  final int SIZE = 8;
  int[] tiles = new int[SIZE * SIZE];

  int getTile(int col, int row) {
    return tiles[row * SIZE + col;
  }
}

The same this could be applied to Map. I'm not sure how much more efficient this is, but I would speculate quite a bit. Instead of making size references to size arrays, you would have just one array. The computer wouldn't have to track down where each array is in memory.

\$\endgroup\$
  • \$\begingroup\$ Thanks for reminding. I had used this for a previous prototype but I had forgotten. \$\endgroup\$ – Dave Apr 11 '14 at 22:35
  • \$\begingroup\$ @Davud If this answer has satisfied your needs, I encourage you to "accept it". \$\endgroup\$ – Vaughan Hilts Apr 12 '14 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.