7
\$\begingroup\$

I am developing an AI simulation of predator and prey. I would like to simulate the AI hiding behind obstacles, if it is being chased. But I am still trying to figure out the best way to implement this.

I was thinking along the lines of checking on which side of the obstacle the predator is on and trying to go on the opposite side. Maybe using the A* path finding algorithm to ensure that it gets there using the shortest path.

Now the main reason I am writing is in case somebody is able to point me in the right direction of implementing this (maybe somebody has done this before) or have any other good ideas how to implement it. I have never done anything like this before in terms of programming AI or making any game.

All the obstacles are either horizontal or vertical squares/rectangles.

Additional Info: The map is based on x,y positions with each agent moving at a set velocity.So the map is not entirely tile based, also for that reason I am unsure if A* would actually work in this situation for me.

Here is an image of the situation:

enter image description here

Please also note that the red circle is the predator while the green circle is the prey being chased by the predator.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Is your obstacle map tile-based? \$\endgroup\$
    – Anko
    Jan 5, 2013 at 3:31
  • 1
    \$\begingroup\$ Um it's based on X and Y coordinates,not entirely tiles.Each agent changes it x,y position according to their velocity. \$\endgroup\$
    – Tohmas
    Jan 5, 2013 at 15:37

4 Answers 4

3
\$\begingroup\$

You can add this kind of behavior to any path-finding algorithm simply by adjusting the heuristic function. A typical heuristic for path-finding is just based on distance to the goal, however, you can factor in an estimate of how dangerous a given spot is:

// Typical heuristic function, lower values are better
float Heuristic(Vector position, Vector goal)
{
    return (goal - position).Length();
}



// New heuristic function
float Heuristic(Vector position, Vector goal)
{
    float value = (goal - position).Length();

    if(TestLineOfSight(enemy, position))
    {
        value += enemy.Level * 100.0f; // Higher-level enemies are more dangerous
    }

    // You can also add factors like environmental hazards, etc.

    return value;
}

This new heuristic will make your actors try to path-find around areas they perceive as dangerous. This technique is really great because it works with any heuristic-based path-finding algorithm.

Note: Path-finding algorithms like A* may need a little tweaking to allow the algorithm to back-track in case it comes across an area deemed too dangerous to cross.

\$\endgroup\$
1
\$\begingroup\$

You can make the Prey use an algotithm to determine that, at a given moment, the Predator will not directly "see" it if it hides behind an obstacle.

This is how it could be done:

  1. Prey finds an obstacle.
  2. In order to determine if the obstacle can be used for hiding, the Prey makes a list of possible spots it can stand around the found obstacle.
  3. For each of these spots, the Prey traces a line from the Predator to itself. Once it finds a line that crosses that obstacle, it means the obstacle is standing in the way between them. That's when the Prey found a hiding spot.

Then, you make the Prey move to that spot.

Edit: implying you're using a tile-based map.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ The limitation: There are infinitely many spots to stand around an obstacle. If you sample, you may miss potential good spots between your sample values. \$\endgroup\$
    – Anko
    Jan 5, 2013 at 3:27
  • 1
    \$\begingroup\$ I assumed it was a tile based map. In that case, there would be just a few possible spots, making it easy to test them quickly. \$\endgroup\$ Jan 5, 2013 at 14:48
  • \$\begingroup\$ That's true. The asker hasn't specified for sure, but since they're using A*, it seems likely it's a tilemap and hence this would work. \$\endgroup\$
    – Anko
    Jan 5, 2013 at 14:53
  • 1
    \$\begingroup\$ Ahh sorry about that, the map is based on X,Y axis so not entirely tile based.For that reason I was also unsure if I could get A* algorithm working with this sort of a map. \$\endgroup\$
    – Tohmas
    Jan 5, 2013 at 15:47
0
\$\begingroup\$

You can treat this as a max/min tree. The goal would be to minimize the "visibility" of the predator. You can compute values as something like:

  • Predator's direct line/range of sight: 100
  • Open areas, but not in predator's sight: 50
  • Not visible by predator's sight: 0

In this case, it's a simpler problem of "pick the next best move." That would mean, the next move that takes you towards getting into a zero-visibility area.

\$\endgroup\$
0
\$\begingroup\$

For the nearest obstacle that's both...

  1. Long or wide enough to hide behind.
  2. Further from the predator than it is from you.

See if you can get to the opposite side of the obstacle from the predator. Just:

xPosToGoTowards = (2 * xPos_Obstacle) - xPos_Predator
yPosToGoTowards = (2 * yPos_Obstacle) - yPos_Predator

If you can't get there without bumping into things, check the next closest obstacle. The first obstacle might pass the test later after a bit more running, but it will also keep the AI more interesting if it doesn't always hide behind the closest obstacle.

There are far more ingenious solutions out there, but that's a bad thing if you don't need it. Simple is good.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .