Nick Gravelyn

I'm trying to create a grid for my program, but I can't get it to work. Right now it just draws real long lines instead of a nice grid. I understand the buffers, but I feel like I'm calculating either the vertices or indices incorrectly. Any ideas


int numVerts = (size * 2) * (size * 2);
int numVertsRoot = (size * 2);
verts = new VertexPositionColor[numVerts];

int numIndices = numVerts * 2;
indices = new short[numIndices];
int index = 0;

for (int i = 0; i < numVertsRoot; i++)
{
for (int j = 0; j < numVertsRoot; j++)
{
verts[i * numVertsRoot + j] = new VertexPositionColor(
new Vector3(i - size, 0, j - size),
Color.White
);

if (j > 0)
{
indices[index++] = (short)(i * numVertsRoot + j - 1);
indices[index++] = (short)(i * numVertsRoot + j);
}
}
}


Re: XNA Game Studio Express Generating Grid With Loops

waruwaru

Sorry, I don't understand what your variables mean.  Are they suppose to represent the endpoints of each line in the grid   If that is the case, it might be easier to break into 2 loops, 1 loop to calculate the endpoints for the vertical lines, and 2nd look to calculate the end points for the horizontal lines.  Something like (psuedo code), modify accordingly if you don't want border

counter = 0;

for (x=0; x<width; x+=space_between_vert_lines) {

  end_point[counter][top].x = x;

  end_point[counter][top].y = 0;

  end_point[counter][bottom].x = x;

  end_point[counter][bottom].y = (height-1);

  // draw_line( end_point[counter][top].x, end_point[counter][top].y, end_point[counter][bottom].x, end_point[counter][bottom].y );

  ++counter;

}

counter = 0;

for (y=0; y<height; y+=space_between_horiz_lines) {

  end_point[counter][left].x = 0;

  end_point[counter][left].y = y;

  end_point[counter][right].x = (width-1);

  end_point[counter][right].y = y;

  // draw_line( end_point[counter][left].x, end_point[counter][left].y, end_point[counter][right].x, end_point[counter][right].y );

  ++counter;

}






Re: XNA Game Studio Express Generating Grid With Loops

Jim Perry

One thing that struck me was the creation of the Vector3:

new Vector3(i - size, 0, j - size)

perhaps you meant to do:

new Vector3(i - size, j - size, 0)

If that's not the problem I can take a further look at it (unless someone else beats me to it )






Re: XNA Game Studio Express Generating Grid With Loops

Nick Gravelyn

Nope. I wanted to leave Y 0 so I can have a plane grid. I am in 3D space for the grid.

The variables represent a few things. size is simply the number of lines to draw in each direction from the origin. That is why I have to double it (one for each side of the axis) and square it (because I have both the X and Z axis to use). The numVerts is simply this value. I have the numVertsRoot which is the number of vertices in each row/column of the grid. I then try to go through and place all the vertices in the proper places on the plane.

Hope that helps somebody help me.




Re: XNA Game Studio Express Generating Grid With Loops

Nick Gravelyn

Ah. I took a good look at my code and figured that my problem may be in the drawing code. Sure enough, I was incorrectly calculating the number of lines to draw (d'oh!). So it's working just fine now. Thanks for the help anyway.