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:
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); } } }
source share