School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,141 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,761 people online right now. Registration is fast and FREE... Join Now!




Pacman collision detection?

 

Pacman collision detection?

vAlexv

10 Sep, 2009 - 05:08 PM
Post #1

New D.I.C Head
Group Icon

Joined: 12 Dec, 2008
Posts: 24



Thanked: 1 times
Dream Kudos: 100
My Contributions
I recently began with game programming, after finishing tetris clone and a pong clone, but the collision detection was easier.
How is the collision detection implemented in pacman?

I really appreciated.

User is offlineProfile CardPM
+Quote Post


Richy321

RE: Pacman Collision Detection?

11 Sep, 2009 - 06:36 AM
Post #2

New D.I.C Head
*

Joined: 26 Jun, 2009
Posts: 22

I can't see how there would be much difference between pong collision detection and pacman collision detection.

Perhaps you will need to use some bounding boxes as the ghosts/pacman sprites arent perfect squares/circles but the same principle applies.

User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Pacman Collision Detection?

11 Sep, 2009 - 07:00 AM
Post #3

lives.ToCode();
Group Icon

Joined: 18 Oct, 2008
Posts: 3,066



Thanked: 171 times
Dream Kudos: 775
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks.
User is offlineProfile CardPM
+Quote Post

e_barroga

RE: Pacman Collision Detection?

11 Sep, 2009 - 12:10 PM
Post #4

D.I.C Regular
Group Icon

Joined: 16 Feb, 2009
Posts: 435



Thanked: 24 times
Dream Kudos: 825
My Contributions
I think that the collision detection would be the same and pixel-perfect collision wouldn't really be necessary.
User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: Pacman Collision Detection?

11 Sep, 2009 - 01:53 PM
Post #5

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,404



Thanked: 53 times
Dream Kudos: 300
My Contributions
How did you implement the collision in your pong game? what libraries are you using?

Its quite hard to just say "Oh, well pacman collision is implemented like so... blah blah blah"

biggrin.gif


User is offlineProfile CardPM
+Quote Post

vAlexv

RE: Pacman Collision Detection?

11 Sep, 2009 - 07:06 PM
Post #6

New D.I.C Head
Group Icon

Joined: 12 Dec, 2008
Posts: 24



Thanked: 1 times
Dream Kudos: 100
My Contributions
SFML, Is the library im using.

This is a way I tried but it cant move, I think may be because the box of the pacman always collides with the box of another tile, I could post all my code but I think this was the most important part of the collision
I hope the code is at least understandable.

CODE


        for(int i = 0; i < maze.GetWidth(); ++i)
        {
            for(int j = 0; j < maze.GetHeight(); ++j)
            {
                const sf::FloatRect PacmanBox(
                    pacman.GetPosition().x,
                    pacman.GetPosition().y,
                    pacman.GetPosition().x + TileWidth ,
                    pacman.GetPosition().y + TileHeight);

                if(PacmanBox.Intersects(maze.GetTile(i,j).GetBoundingBox()))
                {
                    if(maze.GetTile(i,j).GetTileType() != Dot &&
                        maze.GetTile(i,j).GetTileType() != Energy  &&
                        maze.GetTile(i,j).GetTileType() != Black)
                    {
                        //Delta is the speed * frametime
                        if(pacmanDirection == Right)
                            pacman.Move(-Delta, 0);
                        if(pacmanDirection == Up)
                            pacman.Move(0,Delta);
                        if(pacmanDirection == Left)
                            pacman.Move(Delta, 0);
                        if(pacmanDirection == Down)
                            pacman.Move(0,-Delta);
                    }
                }
            }
        }



This post has been edited by vAlexv: 11 Sep, 2009 - 07:21 PM
User is offlineProfile CardPM
+Quote Post

Telestia

RE: Pacman Collision Detection?

12 Sep, 2009 - 08:08 AM
Post #7

New D.I.C Head
*

Joined: 12 Jan, 2009
Posts: 24


My Contributions
First of all I don't think that you should be calculating Pacman's collision box inside the nested for loops.

I think that your problem is your logic in your if check

CODE

if(maze.GetTile(i,j).GetTileType() != Dot &&
                        maze.GetTile(i,j).GetTileType() != Energy  &&
                        maze.GetTile(i,j).GetTileType() != Black)

{
                        //Delta is the speed * frametime
                        if(pacmanDirection == Right)
                            pacman.Move(-Delta, 0);
                        if(pacmanDirection == Up)
                            pacman.Move(0,Delta);
                        if(pacmanDirection == Left)
                            pacman.Move(Delta, 0);
                        if(pacmanDirection == Down)
                            pacman.Move(0,-Delta);
                    }



The way I'm reading it is that Pacman can only move if he's not on a dot, energy, or empty space, but this is just me assuming things about your code. I'm thinking you should change the !=s to ==s and the &&s to ||s as such.

CODE

if(maze.GetTile(i,j).GetTileType() == Dot ||
                        maze.GetTile(i,j).GetTileType() == Energy  ||
                        maze.GetTile(i,j).GetTileType() == Black)


Or to optimize a little you should only check to see if the tile is not a wall.

CODE

if (maze.GetTile(i,j).GetTileType() != Wall)


I hope this helps!

User is offlineProfile CardPM
+Quote Post

wildgoose

RE: Pacman Collision Detection?

14 Sep, 2009 - 12:12 PM
Post #8

D.I.C Regular
Group Icon

Joined: 29 Jun, 2009
Posts: 430



Thanked: 56 times
Dream Kudos: 25
Expert In: Assembly,C,C++

My Contributions
You should be using the center-point of your objects for collision detection. You seem do be using their position (which I'm assuming is their upper left corner and then adding their width and height).
It really should be mid-point +/- width>>1 height>>1, for proper collision detection!
User is offlineProfile CardPM
+Quote Post

AndrewVD

RE: Pacman Collision Detection?

9 Oct, 2009 - 08:09 AM
Post #9

New D.I.C Head
*

Joined: 9 Oct, 2009
Posts: 1

The original pacman did not do collision detection. The game is based on a non scrolling tile map. Each position on the map is in a XY matrix.
As each sprite moves a quick is done to see if there are any other sprites in the adjacent cell for the direction in which the pacman is moving.
There is no check for ghost on ghost action. Only pacman vs ghost checks are done. The same is done for checking whether a maze wall exists or
not.

Pacman is such a simple game, the original game was less than 1k.

If you are using XNA or Direct-X there are very useful samples on the XNA website.
User is offlineProfile CardPM
+Quote Post

Theaegd

RE: Pacman Collision Detection?

9 Oct, 2009 - 03:09 PM
Post #10

D.I.C Regular
***

Joined: 15 Aug, 2009
Posts: 309



Thanked: 4 times
My Contributions
REALLY? REALLY?! Is it really that hard for people?
1. Ghosts may not even need collision detection, other than colliding with pacman, considering that they will (should) have predetermined paths that are randomly chosen, so that the game is random each time.
2. Per-Pixel Collision would not suit this type of game, so if the origanal graphic is a square but you make the backround transperant, then it will still act as though it were a square but looks like it isnt. Then simply make it so when "wall" contacts "pacman" then stop pacman from moving.
I dont know if i explained well... this is the most basic of basic collision detection, and if your using C# with XNA, then I know that its help file could explain this perfectly to you.
User is offlineProfile CardPM
+Quote Post

bobjob

RE: Pacman Collision Detection?

9 Oct, 2009 - 09:58 PM
Post #11

D.I.C Head
**

Joined: 29 Mar, 2008
Posts: 115



Thanked: 8 times
My Contributions
use basic rectangle collisions

define a basic rectangle object that has the following variables
x1, y1, x2, y2

fill the level up with these rectangle objects

when moving the character move a dummy object into the desired position. If the desired position is a pre-difened rectange, then its a collision remove the dummy and continue the game. if there isnt a collision set the characters position to the dummy position.

its actually a very simple collision test.


edit: or even better define the rectange object as
startX, startY, width, height
that way you can make the startX and Y always the lower point. It may make the logic less confusing
if (character.x >= startX && character.x <= startX+width) //collision on X

This post has been edited by bobjob: 9 Oct, 2009 - 10:01 PM
User is offlineProfile CardPM
+Quote Post

Aeternalis

RE: Pacman Collision Detection?

12 Oct, 2009 - 08:39 AM
Post #12

D.I.C Regular
***

Joined: 13 Jul, 2009
Posts: 273



Thanked: 25 times
My Contributions
If I remember correctly.. the arcade version did not use per pixel collision, you could actually touch the ghosts or they could touch the pacman and overlap just a tiny bit without a death occuring. The collision test used some smaller object to check the collision taking up about maybe 1/2 of the sprite size.. 1/4 of the sprite on each size overlap was possible (or close to that).

Also.. just FYI, the ghosts were not on random paths. They may have seemed that way but it was possible to navigate a specific path through the maze every time and not get killed, well .. at least on Ms. Pacman it was. The ghosts took the same route every time, if YOU took the same route.


Retro games rock,
keep making em!

Aet

User is offlineProfile CardPM
+Quote Post

janne_panne

RE: Pacman Collision Detection?

12 Oct, 2009 - 12:15 PM
Post #13

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 531



Thanked: 107 times
My Contributions
Yep, the actual collision detection isn't pixel per pixel and if everything went perfectly, pacman was able to go through the ghost without actually dying.

Here is a documentation of pacman, I think everyone who wants to be involved in game programming should read it:
http://home.comcast.net/~jpittman2/pacman/pacmandossier.html
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:30PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month