Archived

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

Pacman

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone tell me how I might get collision detection to work in a pacman game. I was wanting to make a simple pacman game but the col det is holding me back! thanks Brian

Share this post


Link to post
Share on other sites
Simple collision detection:

all objects in the game have a position in the grid that makes up the level, that means the walls, the player, the little dots, the enemies, everything. Now, whenever the player moves somewhere, check if he moved onto a square with something on it. If he did, take appropriate action. If he didn''t continue the game normally.



-----------------------------
Jappie
BabJap Productions

"There''s no such things as bugs; they''re just unintentional extra features"

Share this post


Link to post
Share on other sites
I''d probably recommend a slightly different technique to the one above. Most games generally do 2 types of collision detection.

The first is with static objects in the level (eg. walls etc.) and the second is with dynamic objects (like enemies... basically stuff that moves).

Pacman is a special case, because the level is essentially a grid with (optional) walls between each cell and its surrounding ones. If you store pacman''s previous position in each game cycle, then in your collision detection algorithm, you should do the following:

1. check if pacman is over two cells (almost always the case unless he''s perfectly in one cell). In theory he could be over 4 cells, but if you restrict his movement so that he must always be aligned with the middle of the cell in at least the X or Y position (like the original game did) then you won''t have to consider that extra case.

2. Check to see if there is a wall between the two cells pacman is currently between*. If there IS a wall, then pacman has collided, so set his position back to his previous one (to prevent him from going through the wall. If there ISN''T a wall, then you''re done.

To do collision with enemies, you just need to calculate the distance between pacman''s position and the enemy''s position. If you have these values stored as pacmanX, pacmanY, enemyX and enemyY, then the formula is:


distance = sqrt((enemyX - pacmanX)^2 + (enemyY - pacmanY)^2);


(This is inefficient because the sqrt takes about as much time as doing 7 multiplies), but you probably don''t need to worry about that if you''re doing a pacman game. Then all you do is compare the radius of pacman to the distance. If the distance between pacman and the enemy is less than pacman''s radius, then uh oh spaghettio, there''s a collision.

* Your data structure could look something like this:


typedef struct cell Cell;
struct cell
{
// These will be TRUE or FALSE (ie. 1 or 0)
int upWall, downWall, leftWall, rightWall;
};

Cell gameCell[width][height];


A lot of this info will be redundant though (since walls are stored twice by neighbouring cells). Should be enough to get you started, anyway...

Share this post


Link to post
Share on other sites