Sign in to follow this  
Steedie

Useless 2D Array Problem

Recommended Posts

Hey guys, I'm trying to split my openGL window up into a grid, so I can later use it for BFS and DFS algorithms. However I'm trying to set the array first to contain all the coordinates for it I've set up 4 Constant Values as follows
//Screen Width
#define SCREEN_WIDTH	60
#define HALF_WIDTH	30

//Screen Height
#define SCREEN_HEIGHT	56
#define HALF_HEIGHT	28


Then these are used when I try and fill the array, which I do by:
void gameWorld::initGraph(sMap *simMap)
{	
	for(int i = HALF_HEIGHT - SCREEN_HEIGHT; i < HALF_HEIGHT; i++)
	{
		for(int j = HALF_WIDTH - SCREEN_WIDTH; j < HALF_WIDTH; j++)
		{
			simMap->iMap[i = i][j = j];
		}
	} 
}

So I'm looking for it to go from -30 to 30 in [i], and -28 to 28 in the [j] However when I print out the array, all the values are set to -858993460, which I'm assuming is what an int is set to when its not been initialised. I'm assuming its to do with this line:
simMap->iMap[i = i][j = j];

//I'm thinking this wouldnt work either
simMap->iMap[i][j] = i, j


But not sure how it should be, simple thing that someone will no doubt fix in a second, but its been a long day and I've hit a mental block lol Any help will be appreciated, cheers

Share this post


Link to post
Share on other sites
Sounds like you want two arrays, not a 2D array.

Each element of a 2D array only holds one value, like:

int x[10][10];
x[2][3]=25;

If, as you say, you want one array running -30 to +30 and another one running -28 to +28, you need:


int a[61];
int b[57];

for(int i=0;i<61;++i) a[i]=i-30;
for(int i=0;i<57;++i) b[i]=i-28;


But then given how easy it is to compute the contents based on the index, why would you want to store these in an array?

I mean, for the first one, doing:

int v=a[i];

is equivalent to doing

int v=i-30;

and there is probably no performance gain at all from doing lookup on a simple operation like that.

2D arrays are normally used to store grid-like structures, such as cells in a map, where you want to index a single value by a row and column.

Share this post


Link to post
Share on other sites
lol thought that might not explain it properly

Right, I'll do my best to explain it fully. I've got a GL window open thats running several objects moving around in a 30x28 space.

I'm trying to work out the best way to implement some AI search algorithms, and thinking the best way to start off going about it is by splitting the space that the objects move about in, into a grid.

So I want to make an array with all the possible points in that the objects could be at, hence wanting to go on the X value, between -30 and 30. Then on the Y axis -28 to 28.

Once I've got this set up, I can then go about using this array with all the points in for the search algorithm implementation.

I hope that explains it better, explaining things has never been my strong point :P

Share this post


Link to post
Share on other sites
I think I understand what you mean.

If you want a 2D array of co-ordinates, you can create a structure:


struct Coord
{
Coord(int x=0,int y=0) : X(x),Y(y) { }

int X,Y;
};


then create a 2D array of these structures:


Coord Map[10][10];


then assign to each one in a loop like:


for(int Y=0;Y<10;++Y)
{
for(int X=0;X<10;++X) Map[X][Y]=Coord(X-5,Y-8); // or whatever
}


HTH

Share this post


Link to post
Share on other sites
Quote:
Original post by Steedie
lol thought that might not explain it properly

Right, I'll do my best to explain it fully. I've got a GL window open thats running several objects moving around in a 30x28 space.

I'm trying to work out the best way to implement some AI search algorithms, and thinking the best way to start off going about it is by splitting the space that the objects move about in, into a grid.

So I want to make an array with all the possible points in that the objects could be at, hence wanting to go on the X value, between -30 and 30. Then on the Y axis -28 to 28.

Once I've got this set up, I can then go about using this array with all the points in for the search algorithm implementation.

I hope that explains it better, explaining things has never been my strong point :P


What is the search algorithm you are planning to use? (Is it a well known one, with a name that could be looked up in Wikipedia, say?)

What should the size of the array be? Why?

What should the element type of the array be? Why? (I.e., what kind of thing do you want to store within each position in the array?)

For a given element of the array, what value should be there? Why? (I.e., for each column of each row of the array, ask the question: "which thing, of the kind from the previous question, should be here?")




Also, those #defines are NOT "constant values". C++ (and, for some time now, C) offers a built-in language feature for defining constant values, which looks almost exactly like you'd expect...


//Screen Width
const int SCREEN_WIDTH = 60;
const int HALF_WIDTH = SCREEN_WIDTH / 2;

//Screen Height
const int SCREEN_HEIGHT = 56;
const int HALF_HEIGHT = SCREEN_HEIGHT / 2;



and the results have (a) a type and (b) a scope, because they are real values - real *concepts in your program* - as opposed to mere #defines, which are instructions to a text search-and-replace engine.

You'll notice I defined the HALF_ constants in terms of the other values. This is for two reasons here:

1) Because you should normally do things like this anyway: when two constants should have a mathematical relationship, expose that relationship when you set them. That way, (a) if you need to change the constants, you can't forget to update one when you update the other; (b) you document the relationship - it's right there, so the person reading the code can see what it is and whether it makes sense.

2) To illustrate that you can do this just fine with real constants, without any mucking around with extra parentheses or anything. Macros have problems with these because they are just instructions to a text search-and-replace engine (without extra parentheses, things can get messed up because of order of operations, along with the context into which the "constant" is inserted).

Don't use automated text search-and-replace hacks. Use a built-in feature of the language. Please. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this