#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# 3D-array

edited May 2016

heey, i want to make a 3D-array with some floats and i've this:

``````float[][][] players = {
{
random(500), random(500), random(500)
}
,
{
random(500), random(500), random(500)
}
,
{
random(2), random(2), random(2)
}
};
``````

but it doesn't work well. Why?

Tagged:

## Answers

• This example is only 2D now, but it works

``````float[][] players = {
{
random(500), random(500), random(500)
}
,
{
random(500), random(500), random(500)
}
,
{
random(2), random(2), random(2)
}
};

for (int i = 0; i < players.length; i++) {
println (players[i] );
println ("");
}
``````
• you declared float[][][] players which makes it 3D

but the content is only 2D - so yours shouldn't work at all.

In my example above (which works) we have a 2D array with the content you posted (2D) - it's all 2D.

• this is all 3D

``````float[][][] players = { // the whole array
{ // one chess board
{ // one column
random(500), random(500), random(500)
}
,
{ // one column
random(500), random(500), random(500)
}
,
{ // one column
random(500), random(500), random(500)
}
} // --------------------------------------
,
{  // 2nd chess board ------------------------
{ // one column
random(500), random(500), random(500)
}
,
{
random(500), random(500), random(500)
}
,
{
random(500), random(500), random(500)
}
} // --------------------------------------
,
{ // 3rd chess board
{ // one column
random(500), random(500), random(500)
}
,
{
random(500), random(500), random(500)
}
,
{
random(500), random(500), random(500)
}
} // --------------------------------------
};

for (int i = 0; i < players.length; i++) {
println ("i is " + i);
for (int j = 0; j < players.length; j++) {
println (players[i][j] );
println ("");
}
}
``````
• please don't confuse 3D with a dataset that has 3 items. Totally different.

In fact, we seldom need 3D arrays.

look, a chess game board is a 2D grid / array: you need to say E2 (you need 2 parameters or e.g. 5,2 as indexes / indices) to reach one field ( = one random number in your example).

a 3D array / grid would be 8 chess boards on top of each other and you would need 3 indexes to reach one field (Etage plus E2, or Etage, column, row)

;-)

• a typical 3D grid is defined not with a code like yours with lots of {{{}}}

but in a triple for loop - see function defineBoxes() here

(besides: a 3D grid as displayed can have as the underlying internal data structure also a 1D array, when each elements in this list holds its color and its position in a 3D space (x y z). The advantage of a 3D grid stored as a 3D array - as it is shown here - is that each cell knows its neighbours: x+1 or x-1, z+1 or z-1 and y+1 or y-1 and so on. So when you have a game to move stones in a 3D grid, it makes this easier, in my opinion.)

;-)

``````// boxes
BoxClass[][][] boxes = new BoxClass;

// camera
PVector camPos;     // camera vectors
PVector camLookAt;
PVector camUp;

// camera rotation
float camCurrentAngle;       // for camera rotation around center
float camRadius;             //

// --------------------------------------------------------
// main funcs

void setup() {
size(800, 800, OPENGL);

// set camera vectors
camPos    = new PVector(width/2.0, height/2.0, 600);
camLookAt = new PVector(width/2.0, height/2.0, -300);
camUp     = new PVector( 0, 1, 0 );

// boxes
defineBoxes();
background(111);
println ("Any key to reset.");
//
} // func

void draw() {
// clear background
background(111);

// set lights
lights();

// set cam pos
camera (camPos.x, camPos.y, camPos.z,
camLookAt.x, camLookAt.y, camLookAt.z,
camUp.x, camUp.y, camUp.z);

// show boxes
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
for (int k = 0; k < boxes[i][j].length; k++) {
boxes[i][ j][k].show();
}
}
}

// camera rotation
camCurrentAngle++;
lookAtAngle();
//
} // func draw()

// ----------------------------------------------------
// input funcs

void keyPressed () {
// reset
defineBoxes();
}

// ----------------------------------------------------
// misc funcs

void defineBoxes() {
// define boxes
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
for (int k = 0; k < boxes[i][j].length; k++) {
// prepare values
color currentCol = color (random(255), random(255), random(255));
boolean exist;
// the percentage
if (random(100) > 60)
exist = true;
else
exist = false;
// create a box
boxes[i][ j][k] = new BoxClass( 158 + i*(height/10),
158 + j*(height/10),
- k*(height/10),
currentCol,
exist);
}
}
}
} // func

void lookAtAngle() {
// rotation in the plane : camera
camRadius = camLookAt.dist (camPos);
camPos.x = camRadius * cos (radians(camCurrentAngle)) + camLookAt.x;
camPos.z = camRadius * sin (radians(camCurrentAngle)) + camLookAt.z;
} // func

// =====================================================
// class

class BoxClass {

// this class represents one Box / Cell

float x;  // pos
float y;
float z;

color col;  // color
boolean exist = true; // display it yes/no

// constr
BoxClass(float x_, float y_, float z_,
color col_,
boolean exist_ ) {
x = x_;
y = y_;
z = z_;
col = col_;
exist = exist_;
} // constr

void show() {
if (exist) {
noStroke();
fill(col);
pushMatrix();
translate(x, y, z);
box(55);
popMatrix();
} // if
} // method
} // class
//
``````
• ;-) Sign In or Register to comment.