Kevin
Kevin

Reputation: 775

Get away from an object in a 2D-grid

I'm developing a small game in python. I am using a 2D rectangular grid. I know that for pathfinding I can use A* and the likes, I know how this works, but the problem I have is a bit different.

Let's say we have a computer controlled human and some computer controlled zombies. When the human spots a zombie, it should get away from this as far as he can. At the moment, to test everything I just turn around 180° and run away, until I spot another zombie and repeat.

Obviously this is not very smart (and can cause problems if there is a zombie on both sides).

I was wondering if there was a smarter way to do this? Something like using Dijkstra to find a "safe zone" where I can run to? Alternatives are always welcome, I can't seem to figure it out.

Upvotes: 0

Views: 278

Answers (2)

Skolor
Skolor

Reputation: 424

Just off the top of my head, you'll probably be able to do some vector math and have the human run in the normal vector to the zombies.

I don't know how well this will work (or how it will scale to the number of zombies you have), but you could do something like:

  • For each zombie, compute the distance to the human and the direction it is from the human.

  • Create a vector for each zombie (or some subset of the close zombies), using the direction and the inverse of the distance, since the closer the zombie is the more important it is to run away.

  • Find the sum of all the vectors.

  • Make the human run in the normal vector to your result.

I'm not sure how resource intensive this would be, but it seems like the most logical way to prioritize where to run.

Upvotes: 0

Matt Esch
Matt Esch

Reputation: 22966

You could suppose that the zombies can see everything within a particular range (radius or perhaps be more clever) and then have the human look for a spot that he thinks the zombies can't see. Pick the closest spot the zombie can't see and use the A* algorithm to find a path if one exists, else try a different one. Look out when there's nowhere to run. Alternatively you could weight all of the spots in your visibility region with a value based on how far away you would be from the zombies if you chose that spot.

Upvotes: 1

Related Questions