AI enemies converge during the game

I created enemies that chase the player around the game, but there is a problem. Enemies are too perfect and quickly converge with each other when they approach the player. This is because they simply move toward the player each time the game is updated.

I would like to introduce some randomness into the enemies, perhaps with an angle of movement. This is what I have so far (I have not optimized it since it has not been executed yet, so I know about the high overhead):

Angle = (float)Math.Atan2((HeroPosition - Position).Y, (HeroPosition - Position).X)// Positions are Vector2s GlobalForce Propellant = new GlobalForce((float)Math.Cos(Angle) / 2, (float)Math.Sin(Angle) / 2); ApplyForce(Propellant); 

If I try to add a random number to the corner, several problems arise:

  • All of them are updated so quickly after each other that the seed time is the same for all of them.

  • The random number is so different from each update that the angle of the enemy jumps randomly.

So what I want to know is: how do most games get around this? How to make enemies use different paths (without access to the list of other enemies)?

EDIT:

This is the code that I use after the sentences below for future passers-by:

 Angle = (float)Math.Atan2((HeroPosition - Position).Y, (HeroPosition - Position).X); GlobalForce Propellant = new GlobalForce((float)Math.Cos(Angle) / 2, (float)Math.Sin(Angle) / 2); ApplyForce(Propellant); foreach (Enemy e in OtherEnemies) { if (e != this) { if ((e.Position - Position).Length() < 64) { float angleBetween = MathHelper.TwoPi-(float)Math.Atan2((e.Position-Position).Y, (e.Position-Position).X); GlobalForce avoidance = new GlobalForce((float)Math.Cos(angleBetween)*2, (float)Math.Sin(angleBetween)*2); ApplyForce(avoidance); } } } 
+4
source share
4 answers

Most games go around when enemies block each other, so that they do not move into the same space.

This can be implemented as a hard restriction (for example, only one enemy is allowed on each square, no other enemies can move into an already occupied square) or a soft restriction (some kind of hidden "force" that pushes the enemies apart if they are too close to each other to friend).

+2
source

A significant part of AI tactics (as well as BTW human tactics) is not only knowledge and action on where the enemy is, but also knowledge and action on where your allies are. Most of the simplest and most famous maneuvers are impossible without this (think about the environment).

Most of this comes down to being "not too close to an ally if you can avoid it."

So, basically, what you need to do is manage your power in such a way that it is not only attracted to the enemy, but also rejected by an ally (even less so). Combine this with a two-stage navigator, and you're done.

+4
source

The middle solution is to have different strategies for different AIs. for example, one AI that lives where the player is now, another AI that lives where the player is, another that usually tries to hang several units south of the player, unless the player moves directly to him ... .

This is not as good as a strategy in which your AI takes into account where their friends are, but (if spelled correctly) they will behave differently.

+1
source

Last year I had to encode a PACMAN game for one of the courses that I took

I used the A-Star search algorithm for enemies

Execution of this algorithm will directly lead to perfect enemies.

The trick is to add some randomness to the result of the algorithm - to make the enemy "make mistakes" as the difficulty level decreases (that is, go in a different direction than the algorithm, when the difficulty level drops, enemas make more "mistakes ")

0
source

Source: https://habr.com/ru/post/1390195/


All Articles