pathfinding and chasing

Started by
14 comments, last by plusminus 18 years, 10 months ago
Yea, you have understand more less what i need i was trying to eimplement the A* to mke the ghosts chase them until they seem him, not all the time.

WeirdoFu: Thanks for your answer, it really made me think. I dont have to do the A* algorithm then, so i will try something like you are saying.

And well, you have gave me more ideas, but i still will use A* because i have 3 types of ghosts the blu ones, green ones and the red ones which are harder respectivivly so i plan that the blue one just chase you when he sees you in a short visible area, the green will more less know the map and go faster than blue but still not using the A* and the red one is the A* one which will go the same speed as green but instead of looking if pacman is seen, it will just chases him for some longer time.

Thats the idea and now thanks to you i more less ready to implement it.

Thanks people.
Any other commentary is welcom
Advertisement
Hi all, I am up to the same thing in my 3D pacman game (2D game play, pac, the walls, the dots and ghosts are 3D).

I gave Kylotan's post a go:

Quote:
if at junction:
if can see pacman:
follow pacman
else:
pick random direction


And this is what I came up with:

Note, in relation to area[][], 0 = nothing / clear, 1 = food / dots / points, and 2 = wall.

/* Control the ghost. */void Ghost(unit& ghost, unit& pac, int area[AREA][AREA]){   int intersection = 0;   bool crash = true;   bool see_pacman = false;   int a = (int) pac.x;   int b = (int) pac.z;   int c = 0;   int d = 0;   if (area[(int) ghost.x - 1][(int) ghost.z] == 0)   {      intersection++;   }   if (area[(int) ghost.x + 1][(int) ghost.z] == 0)   {      intersection++;   }   if (area[(int) ghost.x][(int) ghost.z - 1] == 0)   {      intersection++;   }   if (area[(int) ghost.x][(int) ghost.z + 1] == 0)   {      intersection++;   }   c = (int) ghost.x;   d = (int) ghost.z;   if (a == c)   {      while (area[c][d] < 2)      {         if (b == d)         {            ghost.x_direction = 0.0f;            ghost.z_direction = 1.0f;            see_pacman = true;         }         d++;      }   }   c = (int) ghost.x;   d = (int) ghost.z;   if (a == c)   {      while (area[c][d] < 2)      {         if (b == d)         {            ghost.x_direction =  0.0f;            ghost.z_direction = -1.0f;            see_pacman = true;         }         d--;      }   }   c = (int) ghost.x;   d = (int) ghost.z;   if (b == d)   {      while (area[c][d] < 2)      {         if (a == c)         {            ghost.z_direction = 0.0f;            ghost.x_direction = 1.0f;            see_pacman = true;         }         c++;      }   }   c = (int) ghost.x;   d = (int) ghost.z;   if (b == d)   {      while (area[c][d] < 2)      {         if (a == c)         {            ghost.z_direction =  0.0f;            ghost.x_direction = -1.0f;            see_pacman = true;         }         c--;      }   }   if (see_pacman != false && (rand() % 3) == 0)   {      ghost.x = ghost.x + ghost.x_direction;      ghost.z = ghost.z + ghost.z_direction;      crash = Crash(ghost, area); // Just a wall check.      if (crash != false)      {         ghost.x = ghost.x - ghost.x_direction;         ghost.z = ghost.z - ghost.z_direction;      }   }   else   {      while (crash != false)      {         if (intersection > 2) // If at 3 or 4 way interesction!         {            switch (rand() % 4)            {            case 0:               ghost.x_direction = 1.0f;               ghost.z_direction = 0.0f;               break;            case 1:               ghost.x_direction = -1.0f;               ghost.z_direction =  0.0f;               break;            case 2:               ghost.x_direction = 0.0f;               ghost.z_direction = 1.0f;               break;            case 3:               ghost.x_direction =  0.0f;               ghost.z_direction = -1.0f;               break;            }         }         ghost.x = ghost.x + ghost.x_direction;         ghost.z = ghost.z + ghost.z_direction;         crash = Crash(ghost, area); // Just a wall check.         if (crash != false)         {            ghost.x = ghost.x - ghost.x_direction;            ghost.z = ghost.z - ghost.z_direction;            intersection = 4; // Try Again...         }      }   }   return;}


It does work ok. But fails to have the "pac man feel". Any ideas to improve it? Or simplify the algorithm?

If anyone has done A* using a simple 2D int array, please post here or get in touch with me, 'cos I'd like to try it.

Dospro, if you get the "pac man feel" let me know!!!

If you want the 'Pacman feel' you probably need to look into how the original game did it! I can tell you that it didn't use any pathfinding. It's quite a bit simpler than that. The details are on the web somewhere.
In fact, someone posted this in one of the other forums just today:
http://www.gamedev.net/community/forums/topic.asp?topic_id=324890#2095993
People are always saying that "The details are on the web somewhere" in relation to Pac Man AI, but where? I've been googling. The topic of Pac Man AI will always keep coming up on this and other forums, 'cos no one can answer!
OK,.. pathfinding

When you test my game (Tank-Wars) (Tank-Wars-Thread) you will see that, when a tank sees a Extra-Item, which is behind a wall, he will drive straight around it and then collect the Item.
(By the way, that the game is written in VB-Only)
I did that, by giving the tank a "WANT" to collect this item.
BUT, if he would hit a wall, by moving towards the Item,the prog moves him to the side(he tries to find out the shortest way around the wall (Left-Way or Right-Way)). This Collision-Check has to have igher priority then the "WANT" of collecting the Item. Test this with: ONLY ONE Tank in (f.e.) TEAM GREEN and big SIGHT-RANGE! IF there were other Tanks there would be other "WANTS" :P

What does that mean to your PAC-Man-Game ...
Your Ghosts should have a "SIght-Radar" trough which they can see the "Pac-Man". IF they see you store the coordinates of the "Pac-Man". Moving towards this point will be the "WANT" of the Ghost. (Every want of every Ghost should be processed every single frame, so you have the "LIVE-WANT" of every Ghost.)
So the gohst will try to move towards the Pac-Man. You Calculate the Direction (if the pacman is more to the left or to the right. same for : Top/Bottom)the ghost SHOULD move. then you check if its POSSIBLE to move into that direction (Virtually move the ghost and then check collison with the walls). IF ther is no collison move the Ghost in REAL. IF Collison is true decide if it is shorter to walk around the left-Corner or around the right-Corner.(This DECISION can be done, just by testing, which edge the ghost is closer to.).

SO as you can see, our games are not very different. If i didn't explain some problems exactly, Just msg me (ICQ: 195-333-171) and if you want, i can send you some VB-6(!) example-Code.

Grtz Plus|minus

This topic is closed to new replies.

Advertisement