Make AI Not Run Into Each Other?

So, In my game, I have enemies that will follow you using the angle between the player and the enemy. Often the enemies will get bunched up and follow the same path, like so:
From this

Over time to this (It is really the 4 different enemies you saw in the first pic touched into the same spot, since they follow me by the same angle…)

The Code:


		// Get differences from player to enemy.
		Player target = World.getPlayer();
		float dx = target.x - x;
		float dy = target.y - y;
		float dist = (float) Math.sqrt((dx * dx) + (dy * dy));

		rot = MTrig.atan2(dx, dy);
		
		// Just to make sure it stops before it gets too close to me.
		if (dist <= MUNCHING_DISTANCE) {
			return;
		}

		// Move according to the angle
		move(MTrig.cos(rot) * speed, MTrig.sin(rot) * speed, delta);

What I want is to make sure they do not bunch up into ‘one enemy’ like that.

Thanks!

EDIT:
Oh, and I know what the problem is caused by… They are running on identical algorithms. I want to know what I should do to solve it.

maybe have each enemy pick a random offset for the angle when the targeting starts or have each zombie generate a random path to the player each targeting phase

Have collision detection between each of them that will automatically push each other into different spaces. A good example that I can think of is in Starcraft II. When air units are moved, they can bunch up but when they stop, they slowly expand and get their own space.

Watch from about 0:30 - 0:40 if you wan to visually see what I mean.

DkDJe60gHw8

Another option is that until an enemy gets close enough to the player, it randomly chooses an offset relative to the player that it is aiming for.

Honestly, the way I would do it is if a zombie is on a walkable tile, add it to the closed list when finding a path to the player.

Edit:
Oh, you’re not using path finding. Alright, then yeah just generate random angles and only move through the tile if there isn’t an entity already in it. Otherwise generate a new angle.

Edit 2:
In response to your latest question, I would say generate an angle and then maybe use some trig to create sort of a circular path to the player? Maybe?

Found this article, may you to resolve the problem:
Introduction to AI Programming for Games

And you should consider to start using vectors (if you don’t already do), with them all these things are easier to do.

I though also about using collision detection and A star pathfinding, but they could be an overkill for this situation.

Maybe when they bunch together get them to sense each other so that if one is moving and infront of it is another zombie that the one moving goes left or right just to move out of the way.