• Advertisement
Sign in to follow this  

What is a 3rd dimensional array?

This topic is 1275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by LAURENT*

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by LennyLen

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Not in a place to do anything but my curiosity couldn't keep me away.

 

I have many ideas since last night, actually couldn't sleep. First I need to know Is manipulating the values of arrays a good idea? I don't want to be a bad coder.

Share this post


Link to post
Share on other sites


First I need to know Is manipulating the values of arrays a good idea? I don't want to be a bad coder

 

Are you referring to the values stored in the arrays?  If so, then it's perfectly fine to change them. A program that doesn't change data isn't going to accomplish much.

 

If you meant some other values, please explain.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement