12 Replies - 7920 Views - Last Post: 12 October 2009 - 01:15 PM Rate Topic: -----

#1 vAlexv  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 12-December 08

Pacman collision detection?

Posted 10 September 2009 - 06:08 PM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: Pacman collision detection?

#2 Richy321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 26-June 09

Re: Pacman collision detection?

Posted 11 September 2009 - 07:36 AM

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.
Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Pacman collision detection?

Posted 11 September 2009 - 08:00 AM

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:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Pacman collision detection?

Posted 11 September 2009 - 01:10 PM

I think that the collision detection would be the same and pixel-perfect collision wouldn't really be necessary.
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Pacman collision detection?

Posted 11 September 2009 - 02:53 PM

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"

:D
Was This Post Helpful? 0
  • +
  • -

#6 vAlexv  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 12-December 08

Re: Pacman collision detection?

Posted 11 September 2009 - 08:06 PM

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.


		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 September 2009 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#7 Telestia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 12-January 09

Re: Pacman collision detection?

Posted 12 September 2009 - 09:08 AM

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

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.

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.

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



I hope this helps!
Was This Post Helpful? 0
  • +
  • -

#8 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: Pacman collision detection?

Posted 14 September 2009 - 01:12 PM

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!
Was This Post Helpful? 0
  • +
  • -

#9 AndrewVD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-October 09

Re: Pacman collision detection?

Posted 09 October 2009 - 09:09 AM

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.
Was This Post Helpful? 0
  • +
  • -

#10 Theaegd  Icon User is offline

  • Hater & Lover

Reputation: -125
  • View blog
  • Posts: 944
  • Joined: 15-August 09

Re: Pacman collision detection?

Posted 09 October 2009 - 04:09 PM

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.
Was This Post Helpful? 0
  • +
  • -

#11 bobjob  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 163
  • Joined: 29-March 08

Re: Pacman collision detection?

Posted 09 October 2009 - 10:58 PM

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: 09 October 2009 - 11:01 PM

Was This Post Helpful? 0
  • +
  • -

#12 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Pacman collision detection?

Posted 12 October 2009 - 09:39 AM

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
Was This Post Helpful? 0
  • +
  • -

#13 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Pacman collision detection?

Posted 12 October 2009 - 01:15 PM

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....mandossier.html
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1