Pacman ghost ai
I've just implemented a first concept AI for the ghosts in my pacman game.
Here's what I do currently:
- each frame check what directions are available for the ghost, when no collision would occur (up/down/left/right)
- predict movement in each of the available directions and store distance from predicted ghost pos to player (aka pacman)
- sort them and choose the direction moving closer to pacman (shortest distance), and when the ghosts are vulnerable, take the direction moving away from pacman (farthest)
- if the ghosts moves out of his quarter of the maze, invert the direction
Basically this works, but with a draw back:
- ghosts stall when they reach an edge of their quarter of the maze
Another option could be to only check available directions after a collision would occur, based on the current's ghost direction. This would solve the "stalling" problem partially.
A major drawback from this approach is that ghosts will always pass "T" crosses and In this case always will continue in their current direction (since there's no collision).
Really like to hear your thoughts and advice on how to approach this.
Any input is appreciated.
Crealysm game & engine development: http://www.crealysm.com
Looking for a passionate, disciplined and structured producer? PM me
Instead of checking for direction every frame, you could base the checking on a probability based on e.g. perlin noise.
You could also make the gost move 'physically' i.e. it takes force to change speed and momentum &c.
But if you want it to look like the original Pacman, these suggestions are probably not very helpful ;-)
Good luck !
Cheers,
/Thomas
I might have found a solution:
- if player is within some distance of the quarter, execute the follow pacman logics
- if not just follow a predefined path
Crealysm game & engine development: http://www.crealysm.com
Looking for a passionate, disciplined and structured producer? PM me
The real question is: how do you want the ghosts to behave? Define the results you're trying to achieve first. Then you can work backwards from the desired outcome to find a solution.
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]
In a pacman game i did the ghost did move like this. Note: Does not work in open areas only hallway like levels.
If there is a path shorter then 30 steps to the player.
Walk to the position the player was at.
Else
Pick a left or right turn at random.
If possible to move in the selected direction
Move
elseif possible to move in old direction
Move
else
Reverse direction
Move
@spinningcubes | Blog: Spinningcubes.com | Gamedev notes: GameDev Pensieve | Spinningcubes on Youtube
Assigning a quadrant of the playfield to each ghost seems wrong; they are supposed to surround and chase Pacman, clustering around him until they are not only all in the same quadrant but close together. Barring wrong turns and lack of dexterity on the player's part, teamwork is the only way to become a threat.
You don't really need a good performance from your ghosts, Pacman needs a fighting chance. The ghosts need to follow naive and suboptimal rules, like the original ones.
For example:
- At every crossing, go straight if possible, else take the shortest path towards Pacman.
- At every crossing, choose randomly among the paths that don't lead away from Pacman
- Always the shortest path to Pacman
- When choosing a direction at an intersection, exclude from consideration as if obstructed all corridors sections already containing a ghost (so that ghosts spread apart on alternate routes)
- Follow a predictable patrol route until Pacman comes close (e.g. in the ghost's field of view)
- Shortest path to the closest or second closest intersection to Pacman, to somewhere ahead of Pacman's movement, etc.
Note that the actual difficulty of the game depends very strongly on maze design.
Omae Wa Mou Shindeiru
Thanks, I never thought of it that way. That ghosts can overlap/ go to other quarters.
I've been thinking a lot about it, and for now came up with this design/approach:
1. INITIAL: GIVE EACH GHOST 4 POSSIBLE TARGET 'TILES' / POSITIONS (for each level)
2. GHOST UPDATE: MOVE TOWARDS TARGET (ALWAYS)
-- CHECK POSSIBLE DIRECTIONS
-- PICK PREDICTED POSSIBLE DIRECTION WITH LOWEST DISTANCE TO TARGET
3. PLAYER SWITCHES QUARTER:
-- OLD GHOST: PICK RANDOM TARGET
-- NEW GHOST (IN CURRENT QUARTER):
-- IF FLEEING -> TARGET FARTHEST FROM PLAYER
-- IF FOLLOWING -> TARGET CLOSEST TO PLAYER
4. TARGET REACHED + PLAYER OUTSIDE QUARTER --> PICK (OTHER) RANDOM TARGET
+ PLAYER INSIDE, FLEEING --> PICK TARGET FARTHEST FROM PLAYER
+ PLAYER INSIDE, FOLLOWING --> PICK TARGET CLOSEST TO PLAYER
This assumes that a ghost will stay in it's 'own quarter'.
I'll give your other approaches some thoughts. Thanks again
Crealysm game & engine development: http://www.crealysm.com
Looking for a passionate, disciplined and structured producer? PM me