Public Group

#### Archived

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

# question about collision detection in a small 2d game(like pacman)

This topic is 5404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
You could do a huge array[x][y] where some of the x,y is wall ... if you get me. Since you will be moving in a direction, it is just a matter of testing if it''s ok to move from (x,y)1 to (x,y)2 ...

thats the simplest way ... unless your going 3d and some insane maps

##### Share on other sites
are you saying to make an array the size of the screen? for example if i was doing 800x600 it would be

bool map[800][600];

then i would do a for loop, initializing every one of those values to false(if true was a wall, and false was open space)?

then for each one of my images(wall pictures), say i had a wall that was at (50,75) and was 20 pixels wide and 80 pixels tall, i would do

for (int x= 50; x<=50 + width,x++){   for (int y=75; y<=75 + height,y++)      {            map[x][y] = true;       }}now all those positions (pixels) of the wall image, they will be set to true. then when i move my pacman i will doif (map[pacman_X][pacman_y])move_up()

im not sure if what i just did will work or not. is this what you were talking about? its actually a pretty cool idea, on how to make it so the map is one big thing, instead of a bunch of pieces of map images(which was what i was planning). but im not even sure if my forumals (above) will work. do you see any problems? what do you think of this system? was this even what you were talking about?? thanks for any help!!!

##### Share on other sites
so has anyone done anyting like this? i set up a bunch of walls and a character you can control, using the method i talked about here. for some reason, i walk through walls going down and right, but going up and left i wont colide with walls. maybe you can spot what im doing wrong? heres the part of the code that initialzes the map

void Init_Map() //this initializes all of a maps pixels (800x600) to false (open spot){	for (int x = 0 ; x<=800 ; x++)	{		for (int y = 0 ; y<=600 ; y++)		{			data.map[x][y] = OPEN_SPACE;		}	}	}void Load_Walls(){	Make_Wall(top_wall);	Make_Wall(bottom_wall);	Make_Wall(left_wall);	Make_Wall(right_wall);	Make_Wall(middle_wall);}	void Make_Wall(Objects &object) //this function receives an object (wall) and fills in its spots {	for (int x= object.xPos; x<=object.xPos + object.w;x++)	{   		for (int y=object.yPos; y<=object.yPos + object.h;y++)      		{            			data.map[x][y] = WALL;       		}	}	}

do you think it has to do with me storing my coordinates as floats, and then type casting them to integers when i check for collision?> (because arrays dont like floats). anyone ever do anything like this? can anyone help? im thinking of scrapping the idea, i knew it sounded to good to be true

##### Share on other sites
The easiest way to do collsion destection in a pacman-like game is to use a tile map.

Don't do array[800][600]. Pick a tile size, for instance 32 x 32. You want to make a tile map like this:

const int MAP_Y = 18; // 576 / 32;
const int MAP_X = 18;

int map[MAP_Y][MAP_X];

where MAP_Y is the height of the map in tiles and MAP_X is the width of the map in tiles.

For each tile type, make a corresponding number for the map array:

const int WALL = 0;
const int BLANK = 1;
...

Fill your map array with these values. One way is ...

int map[MAP_Y][MAP_X] = {   {0, 0, 0, 0, 0},  // 0 is WALL and 1 is BLANK   {0, 1, 1, 1, 0},   {0, 1, 1, 1, 0},   {0, 1, 1, 1, 0},   {0, 0, 0, 0, 0}};

When drawing be sure to remember that the tile size is 32.
void draw_map(map[MAP_Y][MAP_X]){   for (int i = 0; i < MAP_Y; i++)   {       for (int j = 0; j < MAP_X; j++)       {            switch (map[i][j])            {                 case WALL:                             draw_tile(WALL, j * 32, i * 32);                             break;            ....

For collision detection, test the x and y coordinates of pacman with the map values at the next square he will move to.

For example, in your event loop you could do...
switch (key){   case UP:            if (map[pac_y - 1][pac_x] == BLANK)                 pac_y--;            break;   case DOWN:            ...};
I can't remember if this is upside-down so try it out and see!

Edit: tags...

[edited by - falsk on February 28, 2004 5:00:26 AM]

##### Share on other sites
thanks for your help! but im still confused, you say use a 32x32 tilesize, but your map array is 18x18. i saw you divide it by 576 or something, but why?

how many of these map arrays will you have per game/screen/?

could you give me some more detailed info?

thanks for your help, i appreciate it

[edited by - graveyard filla on February 28, 2004 2:57:36 PM]

##### Share on other sites
well you could EXTREMELY easily change the 18''s to 32s and have a 32x32 map depending on how big you draw each square, you could use just 1 32x32 section, heck, nothing says you couldnt use just one and draw one square to the whole screen and make the camera jump every movement, but of course that would suck... lol...
and also, he didnt divide anything by 576, thats a comment
/*this is a comment too*/

//so is this
this is not

anyways, hes saying that 18 is 576/18, im not sure the significance of either number, but my guess is he just didnt want to deal with a 32x32 array , anoher thing i would do... is make 0 represent an empty square... and 1 represent a wall, 2 represent a pit, and so on... much more intuitive if you ask me...
my two cents
-Dan

##### Share on other sites
but how does this all work exactly? do i make many of these map arrays, enough to fill up an 800/600 screen? what about the graphical side of this, do i make a bunch of tiles and draw them all to the screen, or is this stuff only for the back-end collision stuff? i dunno i just cant picture all this in my head. need a run down on whats going on. thanks

##### Share on other sites
please someone help. do i make enough of these 32x32 arrays to fill up a 800x600 screen, or what? cant anyone give more details.... argh!!!

##### Share on other sites
ok lol, basically, you could load, say, 3 textures, one for ground, one for wall, and one for pits, (and yes, each element in the array would represent one tile) and so you could loop through your array, and if the current element was say, 0, then you would draw the appropriate texture there, (you could generate the coordinates to draw at by x*texturewidth and y*texturewidth hope THAT makes sense lol
-Dan

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013710
×