Jump to content
  • Advertisement
Sign in to follow this  
Andy474

[XNA] Problems Creating Terrain

This topic is 3349 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

Hi there, i am Trying to Create a 3D Tilemap, and im having some Problem with either working out the Verticies Positions, o the Index's. This map is flat along the X and Z Axis, Y = 0 all the time; First a few Variables
MapWidth = the Width of the Map (in tiles)
MapDepth = the depth of the map (in tiles)

TileWidth = the Width of a tile(in Pixels)
TileDepth = the Depth of a Tile(in Pixels)
this is how I create the Vertices:
ia_mapLayout = SetBlankArray(0); //sets it all to 0

            for (int x = 0; x < MapWidth; x++)
            {
                for (int z = 0; z < MapDepth; z++)
                {
                    Color staticColor = Color.White;

                    Vector3 bottomLeft  = new Vector3(x,  0,  z);                          // 0 [Bottom Left]
                    Vector3 bottomRight = new Vector3(x + 1,  0,  z);               // 1 [Bottom Right]
                    Vector3 topLeft     = new Vector3(x,  0,  z + 1);                  // 2 [Top Left]
                    Vector3 topRight    = new Vector3(x + 1,  0,  z + 1);              // 3 [Top Right]

                    l_Vertex.Add(new VertexPositionColor(topRight, staticColor));
                    l_Vertex.Add(new VertexPositionColor(bottomRight, staticColor));
                    l_Vertex.Add(new VertexPositionColor(bottomLeft, staticColor));

                    l_Vertex.Add(new VertexPositionColor(topRight, staticColor));
                    l_Vertex.Add(new VertexPositionColor(topLeft, staticColor));
                    l_Vertex.Add(new VertexPositionColor(bottomLeft, staticColor));
                }
            }




The Idea is, at each 0 in the array, create 2 new Triangles this is how I create the Indices:
ia_Indices = new int[(MapWidth - 1) * (MapDepth - 1) * 6];
            int counter = 0;
            for (int x = 0; x < MapWidth - 1; x++)
            {
                for (int z = 0; z < MapDepth - 1; z++)
                {
                    int IndexZero = x;
                    int IndexOne = x + 1;
                    int IndexTwo = (x + 1) + MapWidth ;
                    int IndexThree = IndexTwo + 1;

                    ia_Indices[counter++] = IndexThree;
                    ia_Indices[counter++] = IndexOne;
                    ia_Indices[counter++] = IndexZero;

                    ia_Indices[counter++] = IndexThree;
                    ia_Indices[counter++] = IndexTwo;
                    ia_Indices[counter++] = IndexZero;
                }
            }




in this example, MapSize = 50x50, and tiles or 3.0fx3.0f Now, Once I have done that, I move these to the Index and Vertex Buffers, and the draw the Prims. Ok, so now all the Code is out of the way, here is the Problem: A Picture of them Problem (this is my personal Server hosted by BT) From what I can tell, its a problem with the Indices, and they all draw along the First X plane only (when x = 0 && x =1) .... and some link themselves back to x = 0 and z = 0; However, I find it encouraging that there are some Squares in there :) if anyone could perhaps show me where I am going wrong, in my head (and paper) the maths for the indices works out (I think) although my knowlage of them maynot be great. Thanks in Advanced .... Andy^ [Edited by - jpetrie on October 14, 2009 3:03:47 PM]

Share this post


Link to post
Share on other sites
Advertisement
The first problem I see is that you're creating 6 vertices for each square, you only need 4 when you have indices to build the triangles.

Share this post


Link to post
Share on other sites
Ok, so I now Only create the 4 Vertices


Vector3 bottomLeft = new Vector3(x, 0, z); // 0 [Bottom Left]
Vector3 bottomRight = new Vector3(x + 1, 0, z); // 1 [Bottom Right]
Vector3 topLeft = new Vector3(x, 0, z + 1); // 2 [Top Left]
Vector3 topRight = new Vector3(x + 1, 0, z + 1); // 3 [Top Right]

l_Vertex.Add(new VertexPositionColor(bottomLeft, staticColor));
l_Vertex.Add(new VertexPositionColor(bottomRight, staticColor));
l_Vertex.Add(new VertexPositionColor(topLeft, staticColor));
l_Vertex.Add(new VertexPositionColor(topRight, staticColor));




However the map when drawn is still getting almost the same result


However, I'm not sure if the Problem is within the Indicies, or in the Verticies.

Share this post


Link to post
Share on other sites
OK, I think i've Fixed this now.

I Simply got rid of Indices, and created the vertex, at(x,0,-z). This does mean I need 6 vertices for each sq, but I suppose it wont affect performance to much, if it does i can always attempt to add them in later

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!