Am I declaring this 2D array correctly?

Started by
11 comments, last by adam23 17 years, 11 months ago
I am attempting to make a 2D array that I will use for collision detection. The idea is that if the player is moving into a spot located by a 1 he will bump into an object. 20 rows and 28 columns make up my game world. each part represents a 32 pixels x 32 pixels const int mapTravel[20][28] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, //[0] 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, //[1] 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1, 1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; //[19] Now I am having a rabbit run around the town, but I can't get the collsion to work. void NPC::update() { //updating bunny bunny.x += bunny.movex; bunny.y += bunny.movey; bunnyTravel += bunnySpeed; if(bunny.movey > 0) { if(bunnyTravel > 32) { bunnyPos[0]+=1; bunnyTravel = 0; } } if(bunny.movey < 0) { if(bunnyTravel > 32) { bunnyPos[0] -= 1; bunnyTravel = 0; } } if(bunny.movex > 0) { if(bunnyTravel > 32) { bunnyPos[1] += 1; bunnyTravel = 0; } } if(bunny.movex < 0 ) { if(bunnyTravel>32) { bunnyPos[1]-=1; bunnyTravel = 0; } } if(++bunny.animcount > bunny.animdelay) { bunny.animcount = 0; ++bunny.curframe; if(bunny.curframe > bunnyStop) { bunny.curframe = bunnyStart; } } int j; if(mapTravel[19][0]==1) { ::MessageBoxA(0,"In if", "bunny.bmp", MB_OK); j = (int)(4 * rand()/(RAND_MAX+1.0)); switch(j) { case 1: bunnyStart = 8; bunnyStop = 15; bunny.curframe = 8; bunny.movex = bunnySpeed; bunny.movey = 0; break; case 2: bunnyStart = 16; bunnyStop = 23; bunny.curframe = 16; bunny.movex = 0; bunny.movey = bunnySpeed; break; case 3: bunnyStart = 40; bunnyStop = 47; bunny.curframe = 40; bunny.movex = 0; bunny.movey = -bunnySpeed; break; case 4: bunnyStart = 64; bunnyStop = 71; bunny.curframe = 64; bunny.movex = -bunnySpeed; bunny.movey = 0; break; } } } Note the bunnyStart and bunnyStop are the start and stop frames of my bitmap file.
Adamhttp://www.allg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0,0,0,0,1,1,	 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,	 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,	 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; //[19]Now I am having a rabbit run around the town, but I can't get the collsion to work.void NPC::update(){	//updating bunny	bunny.x += bunny.movex;	bunny.y += bunny.movey;	bunnyTravel += bunnySpeed;	if(bunny.movey &gt; 0)	{		if(bunnyTravel &gt; 32)		{			bunnyPos[0]+=1;			bunnyTravel = 0;		}	}	if(bunny.movey &lt; 0)	{		if(bunnyTravel &gt; 32)		{			bunnyPos[0] -= 1;			bunnyTravel = 0;		}	}	if(bunny.movex &gt; 0)	{		if(bunnyTravel &gt; 32)		{			bunnyPos[1] += 1;			bunnyTravel = 0;		}	}	if(bunny.movex &lt; 0 )	{		if(bunnyTravel&gt;32)		{			bunnyPos[1]-=1;			bunnyTravel = 0;		}	}	if(++bunny.animcount &gt; bunny.animdelay)	{		bunny.animcount = 0;		++bunny.curframe;		if(bunny.curframe &gt; bunnyStop)		{			bunny.curframe = bunnyStart;		}	}	int j;	if(mapTravel[19][0]==1)	{		::MessageBoxA(0,"In if", "bunny.bmp", MB_OK);		j = (int)(4 * rand()/(RAND_MAX+1.0));		switch(j)		{		case 1:			bunnyStart = 8;			bunnyStop = 15;			bunny.curframe = 8;			bunny.movex = bunnySpeed;			bunny.movey = 0;			break;		case 2:			bunnyStart = 16;			bunnyStop = 23;			bunny.curframe = 16;			bunny.movex = 0;			bunny.movey = bunnySpeed;			break;		case 3:			bunnyStart = 40;			bunnyStop = 47;			bunny.curframe = 40;			bunny.movex = 0;			bunny.movey = -bunnySpeed;			break;		case 4:			bunnyStart = 64;			bunnyStop = 71;			bunny.curframe = 64;			bunny.movex = -bunnySpeed;			bunny.movey = 0;			break;		}	}}

Note the bunnyStart and bunnyStop are the start and stop frames of my bitmap file.

Beginner in Game Development?  Read here. And read here.

 

How do you format it that way?
Adamhttp://www.allgamedevelopment.com
Just offering a little suggestion, load the array in through a file. This allows you just to go in and edit the file when you need to and not change your code, if you put the size of your array in the file you can also change the size of things without changing your code.

theTroll
Well the column you declared for each is only has 23 elements long, not 28. Plus your starting at the beginning of the array, just because you put a newline there doesn't mean it's going to magically move to the next index.

Try something like this:

const int mapTravel[20][28] =
{
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, //[0]
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1 }, //[1]
{ 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 }, //[2]
//etc
};

Edit: Nevermind, I forgot how to count. Still the above method is a lot cleaner.
Quote:Original post by Scet
Well the column you declared for each is only has 23 elements long, not 28. Plus your starting at the beginning of the array, just because you put a newline there doesn't mean it's going to magically move to the next index.

Try something like this:

const int mapTravel[20][28] =
{
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, //[0]
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1 }, //[1]
{ 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 }, //[2]
//etc
};

Edit: Nevermind, I forgot how to count. Still the above method is a lot cleaner.


Thanks I thought there was another way to declare it. I like your method better :)

Adamhttp://www.allgamedevelopment.com
Now I have a screen width of 897 and a screen height of 600. Everything in my game is moving 3 pixels at a time, should I create a larger array mapTravel[298][200].
Which is 897/3 and 600/3.
Would that take up to much memory or is there a better way to do this type of collision?
Adamhttp://www.allgamedevelopment.com
That is not going to take up to much memory. Most games have dozens of 1024x1024 textures at 32bits per pixel, so that's going to be alot more than your array. Speaking of which, you might want to actually load it in from a monochrome bitmap image instead of hard-coding it. It will be easier to visualize and edit.
Quote:Original post by Ezbez
That is not going to take up to much memory. Most games have dozens of 1024x1024 textures at 32bits per pixel, so that's going to be alot more than your array. Speaking of which, you might want to actually load it in from a monochrome bitmap image instead of hard-coding it. It will be easier to visualize and edit.



Thank you for helping me out, that isn't a bad idea using an external file.

Adamhttp://www.allgamedevelopment.com
If I'm correct your NPC::update() makes the bunny move and then choses a new direction. (based on a random function (Not sure if it can pass the cases or if it'll always choose a new direction))
btw. Why is "if(mapTravel[19][0]==1)" in your code? It equals to true with the current map setting.
So what you need to do when you're about to move the bunny is check if it will enter a new square and if it does you'll need to test if the array contains a 1 or a 0 at that place.
Im guessing that bunnyPos[0] is the Y position of the bunny corresponding to the array. And bunnyPos[1] the X position. Personally I'd name 'm bunnyPosY and bunnyPosX, but ok.
Now it is pretty common to maintain these X and Y positions along with an offset which would be a value between 0 and 32 (tilesize).
Using this, when you move your bunny, you'll get something like this for collision:

//Moving bunny//We're not going to move it yet, first we check if it may move.int xi; // temporary valueif (bunny.movex > 0){ // Bunny is moving to the rightxi = ((bunny.x + bunny.movex + bunny.ImageX) / 32) // Because you need to check if the right side of the bunny is going// to enter a new tile, you'll need the X-size of the bunnytile, try it // without and you'll see what I mean, you'll have to do same when the // bunny is moving down, only then with Y-size of the bunnytile (probably // the same value as X-size, assuming its squareif (MapTravel[bunnyPos[0]][xi + bunnyPos[1]] == 1){ // The bunny cannot move to the right}else{// the bunny may safely move, without bumping into an object}}


Now the same for when it moves left, up or down.
When moving to the left you need to test if the left side of the bunny enters a new tile, so you won't need to use X-size of the bunnytile.
Hope I gave you an Idea of how to do it.

This topic is closed to new replies.

Advertisement