#### Archived

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

# 2D Maze collision detection

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

## Recommended Posts

Hi, im in the midst of making a 2d pacman directdraw thing as my very first game in c++ - yeah, im a newbie Anyway, ive implemented a system of collision detection where every wall in the maze is mapped out in several arrays. I have one of the X coordinates in one array, the corresponding Y coordinate in another array and the length of the wall in pixels in another array ( i have one of these arrays for each direction, vertical and horizontal ). Hmm... i dunno if ive explained this very well... anyway, the problem im having is that you can only move through a "tunnel" (two walls stuck together) unless you get it pixel perfect.... im hope im making sense... heres my collision detection code: Where: pacmove is the flag in which defines the pacman sprite is moving in. 0 = left NUM_Y_WALLS is defined as the number of total vertical walls in the maze. NUM_X_WALLS same for the horizontal walls. levelwall[] is a little struct i made - dont worry about it wy_length = length of wall (vertical) wy_xpos = x position of wall (vertical) wy_ypos = y position of wall (vertical) wx_ypos = y position of wall (horizontal) and so on... pacypos is pacmans current y position pacxpos is pacmans current x position void CollisionDetect() { int i, j, b; bool pacleft = false, pacright = false; bool pacup = false, pacdown = false; bool pacdown2 = false, pacleft2 = false; bool pacright2 = false, pacup2 = false; if (pacmove == 0) { for (j=0;j+20) pacleft2 = true; } } } } if (pacleft2 == true) if (pacleft == false) pacxpos--; } } which is repeated for each direction and each of the ghosty characters I''ld really appreciate any help... im pulling my hair out on this one

##### Share on other sites
argghhh it cut out my greater than and smaller than signs!!! Grrr HTML tags!

Here it is again:

void CollisionDetect()
{
if (pacmove == 0)
{
for (j=0;j{LESS THAN)NUM_Y_WALLS;j++)
{
for (i=0;i(LESS THAN)levelwall[0].wy_length[j];i++)
{
if (pacypos == levelwall[0].wy_ypos[j] + i)
{
if (pacxpos == levelwall[0].wy_xpos[j]+20) pacleft = true;

for (b=0;b(LESS THAN) NUM_X_WALLS;b++)
{
if (pacypos == levelwall[0].wx_ypos+20) pacleft2 = true;
}
}
}
}
if (pacleft2 == true) if (pacleft == false) pacxpos--;
}
}

##### Share on other sites
Hi.

If I understand correctly, this is what I would do:

for X = 0 to VerticalWallCount
if WallX = (ScreenPlayerX + ScreenPlayerSize) then ScreenPlayerRight = true
if (WallX+WallXSize) = ScreenPlayerX then ScreenPlayerLeft = true
next x

and something similar for Y
true means that there is an obstacle in that direction

But tha way I did it in my PacMan clone is:

I divided the whloe maze in small squares. If the screen player is vertically misaligned with the squares, he can only move vertically. Same holds true for the horizontalwise direction.

ScreenPlayerLeft = true
ScreenPlayerRight = true
ScreenPlayerUp = true
ScreenPlayerDown = true

if (ScreenPlayerX/HorizontalXBlockSize) <> int(ScreenPlayerX/HorizontalXBlockSize) then
ScreenPlayerLeft = false
ScreenPlayerRight = false
end if
(same for Y)

If he is aligned in both axis, then I need to see if in the square where he would end up to a particular direction had an obstacle or not in order to allow movement. This way it is not necessary to test against *ALL* of the walls.

if both axis are aligned then
if (function that returns if (top) block has an obstacle) = false then
ScreenPlayerUp = false
end if
(same for left, right, and down)
end if

And now we should have the flags indicatig to which direction the screen player can go.

Topgoro

PS: I made this example in BASIC looking code because its my best second language, and I don''t think jumble wants to see the code example in assembler

PSS: But if you already have designed the mazes, I don''t think you want to redo all over again

##### Share on other sites
wow... thanks a lot. As with my current method im having to check against every wall im worrid about it using too much CPU power... i''ll try cutting the maze into squares (ive only designed one so far) and using your method. Thinking about it, using squares will also help with the movement of the ghosty things as they only change direction once they hit a complete stop - which is a bum.

Cheers

jumble

##### Share on other sites
Maybe you''ve already been helped, but....

I use a two-dimensional array, and when I draw the map, I put a 1 in the array where I can''t walk. For example, if I have a , and a block at (0,0), then array[0][0] = 1;
Then, when the player hits a key (eg, up), it will add check if your array[Ypos+1][Xpos] = 1, and if it does, it won''t move you. Else, it will, and then refresh the screen and all that junk.

Hope I helped, probably didn''t.
-Sponge99

##### Share on other sites
Hi all.

Yes anonimous, you did help a bit, but in my game I add more functionality to the array. Instead of ''1'' or ''0'', I put a code indicating which blos is there: a regular dot, a power dot, a vertical wall, horizontal wall, a corner, a mostar cage wall, moster cage door, a emptu space, etc. That way I can use the array for both collision detection and to draw it.

Jumble, if you wanted I can send you source for both of my pac-man clones, both run in DOS (one of them uses a DOS extender, so it will not work in NT/2000). The only may be bad thing for you is that both are writen in assembler.

I will chack, may be I can send you the sprite/maze editors I made while developing these games.

Topgoro

##### Share on other sites
Thanks for the offer, but i think i''ve got it going now I wouldn''t understand assembler anyway.

jumble
---------------
"Witty comment needed for signature. Current owner has no sense of humour"

• 15
• 13
• 35
• 39