PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Pursuit algorithm question

Hello all. I just had cause to write a very basic pursuit/tracking algorithm (a couple of zombie-type guys chasing your player on the screen). Here's what I did:

Code:

// zombie is a POINT struct which contains an x, y coordinate
// player is also a POINT struct
for (int i = 0; i < 3; i++) {
if (zombie[i].x > player.x) { zombie[i].x--; }
if (zombie[i].x < player.x) { zombie[i].x++; }
if (zombie[i].y > player.y) { zombie[i].y--; }
if (zombie[i].y < player.y) { zombie[i].y++; }
}

Not exactly brain surgery... Now, here's my problem/question...

If you implement this to try it out, you'll see that the zombies bunch up and eventually inhabit the same location on the screen at all times.

I assume I'm not the first guy to encounter this problem, but I don't see a way to stop it from happening.

I was thinking of introducing a random component to each movement, so it doesn't always track exactly... I can also have the zombie move randomly when the character isn't moving... The nature of the game is that the player pretty always has to be moving, but at least at the times they do pause, the zombie will go their own separate ways.

But there's obviously a flaw in my basic thinking, and I'm wondering if anyone has had the experience to give me an answer? Any suggestions besides are appreciated!
This is probably overkill for what you want, but check out Boids here, http://www.red3d.com/cwr/boids/

The basic idea being around Steering behavours. You have a behaviour where the zombies steer toward a common goal. What you also need is a behaviour where the zombies steer to avoid eachother.

One way is to check the position a zombie is about to move into and see if there is already a zombie there, or within some proximity. If there is, either don't move at all, or pick a new position in the same general direction, i.e. if x>y then add/subtract a random value to y and repeat the check.

Pam
Excellent info Pam, thank you!
You're very welcome.

Pam
You can also add a threshold such that the zombies only move towards the player's character once it falls within the zombie's "eyesite". This forces the player to choose paths that are a bit farther and not always moving on a straight line. :)
That's funny because I just finished putting in the code to do just that about 30 seconds ago! :)

Yes, that gave it just enough randomness to keep them apart, and also made the game a little more strategic in a way, so it's better all-around. Thanks for the suggestion!