What is a 3rd dimensional array?

Started by
10 comments, last by LennyLen 9 years, 9 months ago

I've looked this up but I don't quite get it. My game uses a 1 dimensional array to render levels and this was cool but it not enough anymore. I know how a 2 Dimensional array work similar to a grid of rolls and columns but this new concept or 3 doesn't speak to me. Most example I find only explain 2 dimensional arrays.

How does a 3rd dimensional array work? How should I use it. I really need to know before I start testing one 2nd dimensional arrays.

Advertisement

I think a code snippet is the easiest way to explain:





#include <iostream>

int main() {

    int array3d[4][3][2] = {{{2,3}, {1,7}, {8,9}},
                            {{4,2}, {5,0}, {2,5}},
                            {{7,7}, {6,8}, {4,9}},
                            {{1,5}, {2,7}, {3,2}}};

    for (int l = 0; l < 4; l++) {
        for (int m = 0; m <3; m++) {
            for (int n = 0; n < 2; n++) {
                std::cout << array3d[l][m][n] << std::endl;
            }
        }
    }

    return 0;
}

edit:
If you want help visualizing it spatially, think of a grid of rows and columns, with another grid in front of it, and another grid in front of that, and so on... something like this:

22fig03.jpg

Think of a 3d array like a cube.

This diagram might help:http://www.kevblog.co.uk/blog/97/as3_2D_3D_arrays.gif

Stay gold, Pony Boy.

I meant to add earlier, that depending on how your levels are designed, a 3D array might be inefficient.

A naive approach to a tilemap with multiple layers is to use a 3D array to store each layer as a 2D grid. But unless every tile uses every layer (not very likely), you end up allocating a whole lot of space that doesn't get used. A more efficient approach is to have a 2D array where each entry is a vector or list of layers for that tile. You could also design your own structure to store layers and have a 2D array of those.

I tried to run you code lennylen but I couldn't. Anyways the first part of the array hold the height of the array than the other 2 holds the length and width or is it the other way around?

My game is isometric. It still a 2D game but I've included this imaginary concept of something changing a variable named z and passing the value along parameter. My functions interact directly base on base of their position + the z value + 1/2 their height or y axis.

The 3rd dimensional array seems simple compare to your other method you mentioned.


The 3rd dimensional array seems simple compare to your other method you mentioned.

Simpler is sometimes better, but not always.

Imagine your map was 100 x 100 tiles, and then you decided to add two more layers to it. If you were storing this as an array, you would now need to be allocating enough memory for 30,000 values. If only 20 of the items on the map used these two extra layers, you've allocated memory for an extra 20,000 values when you've only added 40 values. That is extremely wasteful.

Alright I see.

I already had plans to test out a 2D array tomorrow but I was just wondering about the possibility of a 3rd dimensional array. I guess I know what is a 3rd dimensional array now so this place is done. By the way you wouldn't have to know anything about isometric games?

My game uses a 1 dimensional array to render levels and this was cool but it not enough anymore.

I find 1D arrays easy and more powerful than 2D, 3D, or 4D arrays. Sometimes simpler tools are more effective. wink.png

How does a 3rd dimensional array work? How should I use it.

You probably shouldn't use it - it's likely not something you need. Technically, you can have a six or seven dimensional array. For 99.9999% of programmers, they'll never need to use a 6D array.

Note: Just because we offer refer to 3D in terms of space: width, height, and depth. We're actually describing space using dimensions. The dimensions just describe data. The 4th array of a 4D array doesn't mean 'time', even if some physicists use the 4D to describe time.

Memory in your computer is 1-dimensional. Your RAM is 1-dimensional. When you create a 2D array, you aren't creating a 2D block of memory - you're just mapping [y][x] to a 1D block of memory.

If you create your own 1D array that is large enough to hold your data (width * height), then index into it using similar math to what C++ is using behind the scenes ((y * width) + x), you get all the advantages of a 2D array, and all the advantages of a 1D array.


std::vector<char> myArray;
myArray.resize(Width * Height, 'X');

//Indexing as a 1D array:
for(int i = 0; i < (Width * Height); ++i)
{
   myArray[i] = (i % 3)? 'O':'X';
}

//And also indexing as if it were a 2D array:
for(int y = 0; y < Height; ++y)
{
   for(int x = 0; x < Width; ++x)
   {
       std::cout << myArray[Index(x, y)] << " ";
   }

   std::cout << std::endl;
}

[Test the code]

Also, just incase you weren't aware, std::vector< std::vector<int> > is not a 2D vector. It is a 'vector of vectors', with each vector potentially having a different length - probably not what you want. That used to confuse me. happy.png

Alright then I'll just have to create prototypes for rendering using a 1D and 2D and see which one gives the most desired results. I'll still have questions later if that's okay.

This topic is closed to new replies.

Advertisement