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:
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!
- 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!