1
\$\begingroup\$

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)
    {
        Rotate();
        Shoot();
        Move(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;
            direction.Normalize();
            return direction;

    }

    public void Shoot()
    {
        if (shootTimer > 0)
        {
            shootTimer--;
        }
        if (shootTimer == 0)
        {

            //find slope


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

            //reset timer 
            shootTimer = 50;


        }

    }



    public Enemy(Vector2 loc)
    {

        Location = loc;
    }


}

}

\$\endgroup\$

3 Answers 3

2
\$\begingroup\$

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

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Thomas
    Commented May 4, 2012 at 6:33
  • \$\begingroup\$ This sounds like too much fun not to try. Guess I know what I'm doing with my weekend. \$\endgroup\$
    – SomeGuy
    Commented May 4, 2012 at 7:14
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
5
  • \$\begingroup\$ I understand this, but regardless of how long it takes, they will all pile up on each other once again. \$\endgroup\$ Commented May 4, 2012 at 4:01
  • \$\begingroup\$ Could you provide example how the problem looks like (maybe screenshot), and how you want it look like? \$\endgroup\$
    – pabdulin
    Commented May 4, 2012 at 4:02
  • \$\begingroup\$ 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.\ \$\endgroup\$ Commented May 4, 2012 at 4:05
  • \$\begingroup\$ 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. \$\endgroup\$
    – SomeGuy
    Commented May 4, 2012 at 4:07
  • \$\begingroup\$ 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. \$\endgroup\$
    – pabdulin
    Commented May 4, 2012 at 4:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .