# PACMAN GAME - COLLISION DETECTION WITH MAZE WALLS

Hello,
I am currently still trying to develop my own version of the pacman arcade game.
The problem I have is to do with the movement of pacman within the maze.
My maze is made up of tiles which are 8x8 pixels each.
Pacman is a sprite which is 16x16 pixels in size, same with the ghosts.
Note that a tile containing part of a wall also has "moving space" which means that pacman can move onto some of the tile even although the tile is classed as a wall tile.
This can be observed by placing an 8x8 grid over a snapshot of the arcade version.

I have tried a method which will detect whether the tile is a wall or not, if it is a wall then pacman will continue to move until he is halfway into the tile before stopping.

Another method is to use a counter which counts 8 pixels from pacmans previous position (note that I am using the centre of pacman sprite as the position reference).

I have downloaded versions of pacman with source code but these versions only seem to have pacman as an 8x8 sprite as with the tiles and pacman's movement is restricted to one tile at a time.

I need pacman to be able to move smoothly one pixel at a time between tiles and be able to move back and forward at will without having to wait until movement onto next tile before changing direction.

Can anyone suggest a way of solving this problem.
I would be grateful for any advice that can be provided.

Thanks
Spcman

• Hey Spcman,
Without really knowing how your code and tile bitmaps look, it's hard to suggest a good collision algorithm, but here's something that might help.

1) Depending on pacman's direction of motion, calculate the position of a pixel on his "leading edge" (in other words, if he's moving up, calculate the coordinate of a point with a y-value along his top edge, and x value in his center. If he's moving right, use the x of his right edge, and y of his center, etc). Call this Pstart.

2) Calculate a second point that accounts for the total distance he'll move this frame (again, if he's going to move up 5 pixels this frame, take the original point and subtract 5 from its y-value, leaving the x value the same). Call this Pend.

3) Now loop from Pstart to Pend, generating pixel coordinates for each step. For each of these pixels, check the corresponding point on the board. If it falls in an empty tile, go to the next step in the loop. If it falls on a wall tile, go to step 4.

4) If one of the points in the loop falls on a wall tile, you need to do extra calculation. Based on the type of wall tile, you can figure out if the point falls on a wall or an empty pixel. For example, if pacman is moving toward a top edge tile, the bottom few pixels are empty (i.e., the 'move space'). You should be able to subtract the y-coordinate of the tile's bottom edge from the y value of the current "movement check point" to get a value that represents how far past the tile's bottom edge the current move point falls. If that value is less than the number of "move spaces" in the tile, it's valid for pacman to continue moving. If not, exit the loop. The current counter of the loop represents the distance pacman can move, plus 1 pixel. Subtract 1 and move pacman ahead by the remaining amount.

6) If you loop through without hitting any pixels, allow pacman to move the full distance.

That's it. It probably doesn't make much sense without diagrams. If it's not helpful, let me know and I'll try to explain it more clearly.

Good luck!

Kreitler

: Hello,
: I am currently still trying to develop my own version of the pacman arcade game.
: The problem I have is to do with the movement of pacman within the maze.
: My maze is made up of tiles which are 8x8 pixels each.
: Pacman is a sprite which is 16x16 pixels in size, same with the ghosts.
: Note that a tile containing part of a wall also has "moving space" which means that pacman can move onto some of the tile even although the tile is classed as a wall tile.
: This can be observed by placing an 8x8 grid over a snapshot of the arcade version.
:
: I have tried a method which will detect whether the tile is a wall or not, if it is a wall then pacman will continue to move until he is halfway into the tile before stopping.
:
: Another method is to use a counter which counts 8 pixels from pacmans previous position (note that I am using the centre of pacman sprite as the position reference).
:
: I have downloaded versions of pacman with source code but these versions only seem to have pacman as an 8x8 sprite as with the tiles and pacman's movement is restricted to one tile at a time.
:
: I need pacman to be able to move smoothly one pixel at a time between tiles and be able to move back and forward at will without having to wait until movement onto next tile before changing direction.
:
: Can anyone suggest a way of solving this problem.
: I would be grateful for any advice that can be provided.
:
: Thanks
: Spcman
:
:

• I forsee one massive problem, besides having different sized tiles and sprites:
Assuming pacman's the full 16 pixels wide, with 8 wide tiles, I assume he'll be centered on one.
This means he extends 4 pixels onto each of the adjacent tiles. Either he can't move since he's stuck in a wall, you can't draw the wall, or he can move on one side of a wall, but not the other.

To make it look good, with 8 wide tiles, the walls would have to be 2 pixels wide, one on each side of the center of the tile.
so, you have 3 pixels on each side of each wall, and the 8 in the middle. This adds up to 14 pixels, pacman can't be any bigger than this. You should also have a single pixel on each side of padding, where nothing is, reducing the size available to 12 pixels.

: Hey Spcman,
: Without really knowing how your code and tile bitmaps look, it's hard to suggest a good collision algorithm, but here's something that might help.
:
: 1) Depending on pacman's direction of motion, calculate the position of a pixel on his "leading edge" (in other words, if he's moving up, calculate the coordinate of a point with a y-value along his top edge, and x value in his center. If he's moving right, use the x of his right edge, and y of his center, etc). Call this Pstart.
:
: 2) Calculate a second point that accounts for the total distance he'll move this frame (again, if he's going to move up 5 pixels this frame, take the original point and subtract 5 from its y-value, leaving the x value the same). Call this Pend.
:
: 3) Now loop from Pstart to Pend, generating pixel coordinates for each step. For each of these pixels, check the corresponding point on the board. If it falls in an empty tile, go to the next step in the loop. If it falls on a wall tile, go to step 4.
:
: 4) If one of the points in the loop falls on a wall tile, you need to do extra calculation. Based on the type of wall tile, you can figure out if the point falls on a wall or an empty pixel. For example, if pacman is moving toward a top edge tile, the bottom few pixels are empty (i.e., the 'move space'). You should be able to subtract the y-coordinate of the tile's bottom edge from the y value of the current "movement check point" to get a value that represents how far past the tile's bottom edge the current move point falls. If that value is less than the number of "move spaces" in the tile, it's valid for pacman to continue moving. If not, exit the loop. The current counter of the loop represents the distance pacman can move, plus 1 pixel. Subtract 1 and move pacman ahead by the remaining amount.
:
: 6) If you loop through without hitting any pixels, allow pacman to move the full distance.
:
: That's it. It probably doesn't make much sense without diagrams. If it's not helpful, let me know and I'll try to explain it more clearly.
:
: Good luck!
:
: Kreitler
:
:
: : Hello,
: : I am currently still trying to develop my own version of the pacman arcade game.
: : The problem I have is to do with the movement of pacman within the maze.
: : My maze is made up of tiles which are 8x8 pixels each.
: : Pacman is a sprite which is 16x16 pixels in size, same with the ghosts.
: : Note that a tile containing part of a wall also has "moving space" which means that pacman can move onto some of the tile even although the tile is classed as a wall tile.
: : This can be observed by placing an 8x8 grid over a snapshot of the arcade version.
: :
: : I have tried a method which will detect whether the tile is a wall or not, if it is a wall then pacman will continue to move until he is halfway into the tile before stopping.
: :
: : Another method is to use a counter which counts 8 pixels from pacmans previous position (note that I am using the centre of pacman sprite as the position reference).
: :
: : I have downloaded versions of pacman with source code but these versions only seem to have pacman as an 8x8 sprite as with the tiles and pacman's movement is restricted to one tile at a time.
: :
: : I need pacman to be able to move smoothly one pixel at a time between tiles and be able to move back and forward at will without having to wait until movement onto next tile before changing direction.
: :
: : Can anyone suggest a way of solving this problem.
: : I would be grateful for any advice that can be provided.
: :
: : Thanks
: : Spcman
: :
: :
:
:

• There are versions online you can examine to check out the detection methods etc.
I like this [link=http://www.thepcmanwebsite.com/media/pacman_flash/]pacman[/link] version it is like the original.
• This post has been deleted.
• This post has been deleted.
• i think use a counter which counts 8 pixels from pacmans previous position (note that I am using the centre of pacman sprite as the position reference). sorry i still newbie in programming. [link=http://poles-marmer-jasa-poles-marmer.com/]jasa poles marmer[/link]
• i think use a counter which counts 8 pixels from pacmans previous position (note that I am using the centre of pacman sprite as the position reference). sorry i still newbie in programming. [link=http://poles-marmer-jasa-poles-marmer.com/]jasa poles marmer[/link]