Archived

This topic is now archived and is closed to further replies.

TheBlackJester

I hate 2D Arrays!

Recommended Posts

Ok, I''ve had this problem over and over again when trying to create an arbitrary terrain. I want to pass the x and z values to a 2d array so that my terrain can be whatever size I need. This doesn''t work..
  

#define SMALL_MAP 32
// In my terrain class

CVector3 *m_pVertices;


// In my Build function

m_pVertices = new CVector3[SMALL_MAP][SMALL_MAP];

  
Now even if I could get that to work, its still not ideal. I want to be able to pass the size from my function without having to use #define constants. Anyone have a better way of working with 2D arrays??? Thanx ahead of time


"With my feet upon the ground I lose myself between the sounds and open wide to suck it in, I feel it move across my skin. I''m reaching up and reaching out. I''m reaching for the random or what ever will bewilder me, what ever will bewilder me. And following our will and wind we may just go where no one''s been. We''ll ride the spiral to the end and may just go where no one''s been." - Maynard James Keenan
[TheBlackJester ]
[Wildfire Studios ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Two ways that I can think of that you can deal with this problem:

1. Have a 1D array simulate a 2D array. That is, just create an array SMALL_MAP * SMALL_MAP in size and calculate each element as row * SMALL_MAP + col. Disadvantage here is the extra operation required to figure out the corresponding 1D index.

2. To do what I think you want to do, simply have an array of pointers (SMALL_MAP in length). Then loop over these and new each pointer in the array to be SMALL_MAP in size. I believe this is what you intended.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
1. Have a 1D array simulate a 2D array. That is, just create an array SMALL_MAP * SMALL_MAP in size and calculate each element as row * SMALL_MAP + col. Disadvantage here is the extra operation required to figure out the corresponding 1D index.

For the record, the above method is actually the more efficient approach on today's CPUs-- if nit-picky efficiency is of any concern to anyone anymore, that is. Plus, I personally think it's easier to read and understand when constructed that way, especially if you care to add a simple macro to your code:

#define vertidx(x,y) ( (y * m_cVerticesSize.x) + x )

Also, while you probably do this already, it is worth mentioning that to use either of the methods our anonymous friend pointed out, you need to add two storage variables to your class to hold the x and y sizes of the map (as seen in the macro there). Have fun.

- Air

[edited by - Air on September 2, 2002 1:44:34 PM]

Share this post


Link to post
Share on other sites