-1
\$\begingroup\$

I have a 2D array called map which holds the coordinates to my level like this:

float[,] map = 
{
    {1, 0, 1, 0, 1},
    {0, 1, 0, 1, 0},
    {1, 0, 1, 0, 1}, 
    {0, 1, 0, 1, 0}, 
    {1, 0, 1, 0, 1}
};

I'm trying to use this to create a cube at each value. In this map, each value in the area holds the height of my cube. So value {0, 0} is equal to 1 and value {2, 3} is equal to 0.

The line that creates my cube is simple enough I think. It creates it based on a Vector3 value.

My question is, how can I create a for loop to set each value in the Vector3 related to the map array?

For example, for value {0, 0}, the Vector3 would be (0, 1, 0).

Feel free to ask questions if you're confused.

Edit: this is what I'm doint at the moment

for (int x = 0; x < 5; x++)
{
    mapX[x] = x;
}

for (int z = 0; z < 5; z++)
{
    mapZ[z] = z;
}

for (int x = 0; x < 5; x++)
{
    for (int z = 0; z < 5; z++)
    {
        cubes.Add(new Vector3(mapX[x], 0, mapZ[z]), Matrix.Identity, grass);      
    }
}

The y value is staying a zero because I can't figure this stupid problem out...

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Can you post some of your code? Also, I'm not sure it makes sense to represent a cube with a single Vector3 - what are you trying to represent here? \$\endgroup\$ Jun 12, 2013 at 7:49
  • \$\begingroup\$ I'm confused. I took the liberty to edit your post to try to clarify it a bit, feel free to re-edit if I messed up something. But I'm still confused. As Andrew said, it doesn't make sense to represent a cube with a Vector3. So this Vector3 has to be the position of the cube, not its dimensions. In other words, if you want a cube with an height of zero ("each value in the area holds the height of my cube", and you have zeros in your map), this means your cube is just a point, as its width and depth will also be zero. So did you mean parallelepiped? \$\endgroup\$ Jun 13, 2013 at 9:44
  • \$\begingroup\$ Also what is this cubes object? What does its Add method do? \$\endgroup\$ Jun 13, 2013 at 9:44
  • \$\begingroup\$ Each element in the array represents a height (as in the starting point of the cube) and is completely built inside the .Add() method. so at map[0, 0] the cube would be 'grown' from that start point. There would be an offset built into the method to keep spacing. --This was a guess on my part-- \$\endgroup\$
    – Dialock
    Jun 13, 2013 at 15:17
  • \$\begingroup\$ Yeah sorry for the mishap in words. The Vector3 does represent the position of each cube. The zeros in my map array represent the height of each cube, 0 being at ground level and so on. Cubes is a list that holds each cube. The Add method just adds a Cube object to the list at the specified position in the map \$\endgroup\$ Jun 15, 2013 at 3:47

1 Answer 1

0
\$\begingroup\$

First off, if you plan on using larger sized maps, I would recommend using jagged arrays instead of multidimensional arrays. Jagged arrays have faster element access time. Although the use of jagged or multidimensional can be easily debated.

Second, you don't appear to be using your map variable anywhere in your posted code. You instead appear to be using two separate arrays, why?

Third, you want to perform the outer loop on the rows, and the inner loop on the columns of the array. And then you pull the height value from the array.

So, assuming height is represented in the Z dimension in your application:

int[][] map;

// You'd initialize the map here with data
map = new int[5][];
for(int i = 0; i < map.Length; i++){ map[i] = new int[5]; }

// Also, assuming a cube width/height/depth of 1
for(int y = 0; y < map.Length; y++){
    for(int x = 0; x < map[y].Length; x++){
        cubes.Add(new Vector3(x, y, map[y][x]), Matrix.Identity, grass);
    }
}

Or with multidimensional arrays:

int[,] map;
int height = 5;
int width = 5;

// You'd initialize the map here with data
map = new int[height, width];

// Also, assuming a cube width/height/depth of 1
for(int y = 0; y < height; y++){
    for(int x = 0; x < width; x++){
        cubes.Add(new Vector3(x, y, map[y,x]), Matrix.Identity, grass);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Well the reason I'm using a multidimensional is because the map is always going to be a square. Eventually the map will be 1000x1000 \$\endgroup\$ Jun 13, 2013 at 3:29
  • \$\begingroup\$ I would very much so recommend using jagged arrays then. A rectangular shape is easily implemented (no different than multidimensional), and the access time for each element will be much faster. \$\endgroup\$ Jun 13, 2013 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .