|
-
April 10th, 2010, 06:48 AM
#1
[RESOLVED] Multidimensional Array
I´m messing around a bit with procedurally generating terrain for a game I´m working on. The terrain itself is simply represented by a 2 dimensional array which containts height values, basicaly a heightmap.
The problem for me is, how do I implement this array. The array will be accessed and written to thousands of times in several loops and algorithms.
The test code I had written used a simple 2 dimensional array (float[][]). But I need a way to determine the size of the array in the constructor of the Terrain object, and this isn't possible when I have to determine the size of the array when I declare it.
So I tried a vector<vector<float>> setup. I have always used vectors over arrays in my code, and they have never cost me much performance wise. But when I used it here it slowed things down noticably. Perhaps it is because I have never extensivly accessed a vector of vectors before like I am trying now.
At the moment I have another solution and my situation looks a bit like below (psuedocode):
Code:
Class Terrain
{
float* terrainArray;
Terrain(int lenght, int width)
{
terrainArray = new float[lenght*width];
}
};
This is currently working fine for me. But I don't think its the best solution, because everytime I access or write to the array, I need to do it like so terrainArray[lenght*width]. Personally I would like to see stuff like terrainArray[lenght][width] or even terrainArray.set(lenght, width, value). Perhaps I am nitpicking and I should simply keep using the current implementation. But there are some other options that I am considering and I would like some second opinions on the matter.
The current options I'm considering:
- Keep using float* terrainArray = new float[lenght*width]
- Write a thin wrapper class (called Array2D) around float* terrainArray = new float[lenght*width] to add some syntactic sugar.
- Use a well known library such as boost::multiarray (I don't know about its performance versus a vector or my personal solution)
Keep it constructive please, I don't consider myself a C++ veteran or anything. Do feel free to suggest other options as well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|