Beispiel #1
0
        public static Vector2 Separation(Unit curBoid, List<Unit> Boids, float WorldSize)
        {
            //bool Reach = false; //this is to see if the curboid has been reached in the list
            Vector2 c = new Vector2(0);
            GameModel tempModel;

            Vector2 curPos = new Vector2(WorldSize - curBoid.Position.X, curBoid.Position.Z);

            for (int x = 0; x < Boids.Count; x++)
            {
                tempModel = Boids[x];
                if (!curBoid.Equals(tempModel))
                {
                    Vector2 temp = new Vector2(WorldSize - tempModel.Position.X, tempModel.Position.Z);
                    float distance = (float)Math.Sqrt(Math.Pow(temp.X - curPos.X, 2) + Math.Pow(temp.Y - curPos.Y, 2));
                    if (distance < 25)
                    {
                        c = c - (temp - curPos)/(distance);
                    }
                }

            }

            return (c);//new Vector3(c.X,Boids[curBoid].Position.Y,c.Y);
        }
Beispiel #2
0
        public static Vector2 Cohesion(Unit curBoid, List<Unit> Boids, float WorldSize)
        {
            GameModel tempModel;
            Vector2 pc = new Vector2(0);
            
            int div = 0;
            Vector2 curPos = new Vector2(WorldSize - curBoid.Position.X, curBoid.Position.Z);
            for (int x = 0; x < Boids.Count; x++)
            {

                tempModel = Boids[x];
                Vector2 temp = new Vector2(WorldSize - tempModel.Position.X, tempModel.Position.Z);
                double distance = Math.Sqrt(Math.Pow(temp.X - curPos.X, 2) + Math.Pow(temp.Y - curPos.Y, 2));

                if (distance < 300)
                {
                    pc += temp;
                    div++;
                }

            }

            if (div > 0)
                pc = pc / (div);

            pc = (pc - curPos) /800;
            //Vector3 pc3 = new Vector3(pc.X, Boids[curBoid].Position.Y, pc.Y);
            return pc;
        }
Beispiel #3
0
        public static Vector2 Alignment(Unit curBoid, List<Unit> Boids, float WorldSize)
        {
            GameModel tempModel;
            Vector2 pv = new Vector2(0);
            int div = 0;
            Vector2 curPos = new Vector2(WorldSize - curBoid.Position.X, curBoid.Position.Z);
            Vector2 curVel = new Vector2(0);
            curVel.X = 0 - (float)(Math.Cos(curBoid.RotationY + 90));
            curVel.Y = 0 - (float)(Math.Sin(curBoid.RotationY + 90));
            for (int x = 0; x < Boids.Count; x++)
            {

                tempModel = Boids[x];

                Vector2 velocity = new Vector2();
                velocity.X = 0 - (float)(Math.Cos(tempModel.RotationY + 90));
                velocity.Y = 0 - (float)(Math.Sin(tempModel.RotationY + 90));

                Vector2 temp = new Vector2(WorldSize - tempModel.Position.X, tempModel.Position.Z);
                double distance = Math.Sqrt(Math.Pow(temp.X - curPos.X, 2) + Math.Pow(temp.Y - curPos.Y, 2));

                    if (distance < 300)
                    {
                        pv += velocity;
                        div++;
                    }


            }

            if (div > 0)
                pv = pv / (div);

            pv = (pv - curVel)/2;
           //Vector3 pc3 = new Vector3(pc.X, Boids[curBoid].Position.Y, pc.Y);
            return pv;
        }
Beispiel #4
0
 public List<Unit> getNearbyUnits(Unit unit)
 {
     List<Unit> result = new List<Unit>();
     //get the zones
     Vector3 pos = (Vector3)Converters.WorldPositionToCartesianPosition(unit.Position, world.MaxX);
     Vector3 tl=new Vector3((float)(pos.X-(unit.ViewRange/2)),(float)(pos.Y+(unit.ViewRange/2)),0);
     Vector3 br=new Vector3((float)(pos.X+(unit.ViewRange/2)),(float)(pos.Y-(unit.ViewRange/2)),0);
     Rectangle rectangle = new Rectangle((int)tl.X, (int)tl.Y, (int)unit.ViewRange, (int)unit.ViewRange);
     List<Point> zones = getZonesInRectangle(rectangle);
     //check objects in each zone if in rectangle
     foreach (Point point in zones)
     {
         foreach (GameModel gameModel in Grid[point.X, point.Y])
         {   //check if gm in rectangle
             if (gameModel.Name != unit.Name)
             {
                 pos = (Vector3)Converters.WorldPositionToCartesianPosition(gameModel.Position, world.MaxX);
                 double top = rectangle.Top;
                 double bottom = rectangle.Top - rectangle.Height;
                 double left = rectangle.Left;
                 double right = rectangle.Right;
                 if ((pos.X >= left) && (pos.X < right))
                 {
                     if ((pos.Y >= bottom) && (pos.Y < top))
                     {
                         result.Add((Unit)gameModel);
                     }
                 }
             }
         }
     }
     return result;
 }