Pacman 'eating' problem

Hi there,

Im a fairly new programmer, literally picked up Java 12weeks ago on my course, and now Ive been told to make a game resembling pacman- where the character eats the enemies.

The game basically involves a pacman-like character (a filled arc) and puts him in a space invaders like situation- with falling/advancing enemies- and I want the pacman to eat the enemies.

Sounds simple enough but I just cant get my head around it. The enemies have a coordinates for their upper left x/y position and have a height/width in relation to the sprite they are displaying (e.g. 10x10pixels). The pacman also has an X/Y position, is set to a size of 38 and is user controlled by the keyboard.

Ive tried code such as

if ((monsterXPos == munchieXPos) && (monsterYPos == munchieYPos))

and then make the enemy disappear off screen. I thought this would detect the collision between the two and then allow the pacman to 'eat' the enemy, but it seems to be a random occurence and if the pacmans x or y position matches the enemies then it will eat it, and not only in a situation where the 2 characters meet- they could be on opposite sides of the screen and if the coordinates match then it will 'eat' the enemy.

Does anybody have any ideas on how to solve it? Should I post my source? Its really bugging me now!

[1380 byte] By [Senor-cojonesa] at [2007-11-26 13:08:32]
# 1

Dear Senor Cojones (lol :-)

usually collisions are detected by collisions of bounding boxes. In your situation I would simply check whether the monster box hits the center point of the munchie. Means:

if ((monsterXPos<munchieXPos && monsterXPos+monsterWidth>munchieXPos) && same for height...)

Where munchieXPos means the center X coordinate of your munchie.

That will detect collisions for characters whos discrete speed is smaller than a width or height of the bounding boxes.

I doubt the fact that if you compare coordinates and they match that the two characters will be on oposite sides of the screen. Then your render routine might not be flawless. ;-)

Hope that helps,

stephan

StephanTheNumba at 2007-7-7 17:20:01 > top of Java-index,Other Topics,Java Game Development...
# 2
Thanks very much for the help Stephan, thats solved my problem and now my game is well on its way to being completed! BTW ive had to re-sign up as apparently none of my details exist!
5enorcojone5a at 2007-7-7 17:20:01 > top of Java-index,Other Topics,Java Game Development...
# 3
R u from staffordshire university ?
xtremefactora at 2007-7-7 17:20:01 > top of Java-index,Other Topics,Java Game Development...