Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I created an enemy class for my game. It works well, except for one dilemma. When I spawn more than one enemy, eventually they begin to "overlap" and look like one enemy. I was wondering if anyone had any ideas on how to create more differences and spontaneity between the enemies. All help would be greatly appreciated.

Here is the class:

using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Defender
public class Enemy

    Vector2 Location;
    public static Texture2D texture;

    float rotation;
    Rectangle rectangle;
    int shootTimer = 50;

    public void Draw(SpriteBatch spriteBatch)
        rectangle = new Rectangle((int)Location.X, (int)Location.Y, 32, 32);
        spriteBatch.Draw(texture, Location, null, Color.White, rotation, new Vector2(16, 16), 1, SpriteEffects.None, 0);


    public void Update(GameTime gameTime)


    public void Move(GameTime gameTime)
        if (Vector2.Distance(Location, Game.player.Location) > 75)

                Location.X += findDirection(Game.player.Location).X * (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 20);
                Location.Y += findDirection(Game.player.Location).Y * (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 20);



    private void Rotate()

            float XDistance = Location.X - Game.player.Location.X;
            float YDistance = Location.Y - Game.player.Location.Y;

            rotation = (float)Math.Atan2(YDistance, XDistance);


    private Vector2 findDirection(Vector2 target)

            Vector2 direction = target - Location;
            return direction;


    public void Shoot()
        if (shootTimer > 0)
        if (shootTimer == 0)

            //find slope

            //create bullet
            Bullet bullet = new Bullet(Location, findDirection(new Vector2(Game.player.Location.X, Game.player.Location.Y)));

            //reset timer 
            shootTimer = 50;



    public Enemy(Vector2 loc)

        Location = loc;



share|improve this question

There are a couple of things you can do to fix this.

First off, you can try implementing collision on enemies so that they don't walk through one another. Depending on the enemy type you're fighting, it may or may not make sense for them to easily pass through one another (flying bugs, ghosts, and small creatures might make sense, but who knows how those T-rexes are all running in the same spot).

Alternately, you can try and add some random factor to the pathing algorithm your enemies are using. A couple of people talked about this in more detail on this page: Making the AI take different paths to each other

share|improve this answer
I would add them both, collision and a random movement factor. You could even implement a flocking / swarming algorithm so all enemies will react to the others. – Thomas May 4 '12 at 6:33
This sounds like too much fun not to try. Guess I know what I'm doing with my weekend. – SomeGuy May 4 '12 at 7:14

Have a look at Steering Behaviors.

There are many different kinds of behaviors that could help your enemies navigate more realistically, but one behavior that relates to what you are looking for is a "Separation" type of behavior (often used in Flocking behaviors (or boids)). This behavior states that all entities under this "rule" try to keep a small distance between each other. Using this rule, your enemies wouldn't overlap, and you wouldn't need to check for collisions either.

Some pseudo-code can be found here as well as a bit more in-depth explanation of how it works and can be implemented.

share|improve this answer

Well, you can use random number generator to create a little differences between enemy characteristics (speed, initial position, etc.)

share|improve this answer
I understand this, but regardless of how long it takes, they will all pile up on each other once again. – user1162850 May 4 '12 at 4:01
Could you provide example how the problem looks like (maybe screenshot), and how you want it look like? – Petr Abdulin May 4 '12 at 4:02
I'm not exactly sure about how to post images, but I can try to explain it better. So, when I spawn enemies, they all start in different initial positions. As I move the player, the enemies all rotate exactly towards it and move towards it. As they do so, they also slowly get closer to each other. Finally, after a few seconds (30- one minute) they all pile up on each other. They all go to literally the exactly spot. In fact, when this happens, it seems that there is actually only one enemy.\ – user1162850 May 4 '12 at 4:05
I think you should consider what it is you would -like- the enemies to do. Should they surround the player? Stalk him (but keep distance?) Should they be allowed to pile on each other at all? It isn't clear exactly what you need done, so everyone's going to have to make a vague answer, or a lot of different answers. – SomeGuy May 4 '12 at 4:07
Just press image button then you edit your post (it's on the toolbar (rightmost in the second group). Also all additional explanation can be adder to post, not as a comment here. – Petr Abdulin May 4 '12 at 4:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.