Jump to content

  • Log In with Google      Sign In   
  • Create Account

What is a 3rd dimensional array?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 LAURENT*   Members   -  Reputation: 218

Like
0Likes
Like

Posted 23 July 2014 - 06:31 PM

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*, 23 July 2014 - 06:43 PM.


Sponsor:

#2 LennyLen   Crossbones+   -  Reputation: 3783

Like
8Likes
Like

Posted 23 July 2014 - 06:41 PM

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


Edited by LennyLen, 23 July 2014 - 06:45 PM.


#3 minibutmany   Members   -  Reputation: 1615

Like
0Likes
Like

Posted 23 July 2014 - 06:47 PM

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.

#4 LennyLen   Crossbones+   -  Reputation: 3783

Like
0Likes
Like

Posted 23 July 2014 - 07:10 PM

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.



#5 LAURENT*   Members   -  Reputation: 218

Like
0Likes
Like

Posted 23 July 2014 - 07:11 PM

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?



#6 LAURENT*   Members   -  Reputation: 218

Like
0Likes
Like

Posted 23 July 2014 - 07:29 PM

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.



#7 LennyLen   Crossbones+   -  Reputation: 3783

Like
0Likes
Like

Posted 23 July 2014 - 07:38 PM


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, 23 July 2014 - 07:38 PM.


#8 LAURENT*   Members   -  Reputation: 218

Like
0Likes
Like

Posted 23 July 2014 - 07:49 PM

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?



#9 Servant of the Lord   Crossbones+   -  Reputation: 19560

Like
3Likes
Like

Posted 23 July 2014 - 08:01 PM

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


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#10 LAURENT*   Members   -  Reputation: 218

Like
0Likes
Like

Posted 23 July 2014 - 08:15 PM

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.



#11 LAURENT*   Members   -  Reputation: 218

Like
0Likes
Like

Posted 24 July 2014 - 08:20 AM

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.



#12 LennyLen   Crossbones+   -  Reputation: 3783

Like
0Likes
Like

Posted 24 July 2014 - 08:26 AM


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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS