• ### Popular Now

• 14
• 12
• 9
• 10
• 13

#### 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 5134 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

high, ive recently finished my pong clone, and now im thinking about making a pac man. ive been going through in my head all the stuff i will have to do. anyway, in pong, collision detection was probably the hardest thing to do/get perfect, and im thinking how im going to do it. how do i lay the walls out onto the game? do i use a bunch of different wall pics (like 10 wall "strips" that i lay out all throughout the map to make look like a maze?). also, after i get these walls set up, how will i detect collsion against them? do i give EACH wall an x,y,w,h, and then do collision detection for EACH individual wall piece? this is the way im thinking about doing it, but it seems like a lot of work, and maybe theres something im missing and there is some easier way of doing collision detection/setting up the maze also, in my pong clone, it looked SOMETHING like this: if (ball.x == paddle.x && balls top left corner was lower then the paddles top left corner and higher then the paddls bottom left corner) (efectively putting it somewhere in the middle of the paddle) then if it was true, i would send the ball across the screen. or for walls.. if (ball.x < 0) // went through the top wall ball.x == 0 //this would make it HUG the wall. now in pong i would normally make it bounce at this point, but for pac man i want it to hug the wall. so this is how i would do it in pac-man. so, do i have to make a bunch of rectagular blocks in paint, then display them to the screen in a maze-like situation, THEN i would give each image an x,y, w,h, and in each game-loop i would check if the pac-man/monsters collided with each one of these images? or, am i totally stupid and going about it the wrong way? thanks for any help im thinking maybe i could make it so each wall piece is generic, and then i wouldnt have to check EACH individual wall/each idividual wall''s coordinated, but how could i make a generic function like this if each wall piece will have different dimensions? hmmm..... guess ill have to think about this more. thanks for any help

##### 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!!!