Inheritance: Interfaces.Boid_Interface
コード例 #1
0
        public void Affect(Boid boid)
        {
            float dst = (boid.Position - position).Length();

            if (Type == EType.Approach && dst < 200)
            {
                boid.Seek(position);
            }
            else if (Type == EType.Flee && dst < 80)
            {
                boid.Flee(position);
            }
            else if (Type == EType.Arrive && dst < 200)
            {
                boid.Arrive(position, 100);
            }
            else if (Type == EType.Pursuit && dst < 150)
            {
                boid.Pursuit(position, velocity);
            }
            else if (Type == EType.Wander && dst < 100 )
            {
                if (BoidsScreen.RANDOM.NextDouble() < 0.001f || (wanderTarget - position).Length() > 50)
                {
                    wanderTarget = new Vector2((float)Math.Cos(BoidsScreen.RANDOM.NextDouble() * Math.PI * 2), (float)Math.Sin(BoidsScreen.RANDOM.NextDouble() * Math.PI * 2));
                    wanderTarget *= (float)(BoidsScreen.RANDOM.NextDouble() * 50);
                    wanderTarget += position;
                }

                boid.Wander(wanderTarget);
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: tsimafei-markhel/boids
        private Flock CreateFlock(Point targetLocation)
        {
            Flock newFlock = new Flock();

            int minBoids = 2;// 1;
            int maxBoids = 20;// 2;
            float positionDelta = 50;
            int velocityDelta = 10;

            int numberOfBoids = random.Next(minBoids, maxBoids);
            float minX = targetLocation.X - positionDelta;
            float maxX = targetLocation.X + positionDelta;
            float minY = targetLocation.Y - positionDelta;
            float maxY = targetLocation.Y + positionDelta;

            float deltaX = Math.Abs(maxX - minX);
            float deltaY = Math.Abs(maxY - minY);

            for (int i = 0; i < numberOfBoids; i++)
            {
                Boid boid = new Boid
                {
                    Velocity = new Vector2((float)random.Next(-velocityDelta, velocityDelta), (float)random.Next(-velocityDelta, velocityDelta)),
                    Position = new Vector2(minX + (float)random.NextDouble() * deltaX, minY + (float)random.NextDouble() * deltaY)
                };

                newFlock.Boids.Add(boid);
            }

            return newFlock;
        }
コード例 #3
0
        public Vector2 GetVelocity(Boid targetBoid, Flock flock)
        {
            if (targetBoid == null)
            {
                throw new ArgumentNullException(nameof(targetBoid));
            }

            if (flock == null)
            {
                throw new ArgumentNullException(nameof(flock));
            }

            if (!flock.Boids.Contains(targetBoid)) // TODO: Time-consuming?..
            {
                throw new InvalidOperationException();
            }

            if (flock.Boids.Count < 2)
            {
                return Vector2.Zero;
            }

            Vector2 centerOfMass = Vector2.Zero;
            foreach (Boid boid in flock.Boids)
            {
                if (targetBoid != boid)
                {
                    centerOfMass += boid.Position;
                }
            }

            centerOfMass /= flock.Boids.Count - 1;
            return (centerOfMass - targetBoid.Position) * this.shiftCoefficient;
        }
コード例 #4
0
        public Vector2 GetVelocity(Boid targetBoid, Flock flock)
        {
            if (targetBoid == null)
            {
                throw new ArgumentNullException(nameof(targetBoid));
            }

            Vector2 shift = Vector2.Zero;

            if (Math.Abs(this.AllowedArea.Left - targetBoid.Position.X) < this.proximityTreshold)
            {
                shift += new Vector2(-targetBoid.Velocity.X, 0.0f);
            }

            if (Math.Abs(this.AllowedArea.Right - targetBoid.Position.X) < this.proximityTreshold)
            {
                shift += new Vector2(-targetBoid.Velocity.X, 0.0f);
            }

            if (Math.Abs(this.AllowedArea.Top - targetBoid.Position.Y) < this.proximityTreshold)
            {
                shift += new Vector2(0.0f, -targetBoid.Velocity.Y);
            }

            if (Math.Abs(this.AllowedArea.Bottom - targetBoid.Position.Y) < this.proximityTreshold)
            {
                shift += new Vector2(0.0f, -targetBoid.Velocity.Y);
            }

            return shift;
        }
コード例 #5
0
        private IList<int> GetIdForObj(Boid obj)
        {
            var bucketsObjIsIn = new List<int>();

            Vector2 min = new Vector2(
               Math.Max(Math.Min( obj.Position.X - (Boid.RADIUS), SceneWidth - 1), 1),
                 Math.Max(Math.Min(obj.Position.Y - (Boid.RADIUS), SceneHeight -1), 1));

            Vector2 max = new Vector2(
                Math.Max(Math.Min(obj.Position.X + (Boid.RADIUS), SceneWidth - 1), 1),
               Math.Max(Math.Min(obj.Position.Y + (Boid.RADIUS), SceneHeight - 1), 1));

            float width = Cols;

            //TopLeft
            AddBucket(min, width, bucketsObjIsIn);

            //TopRight
            AddBucket(new Vector2(max.X, min.Y), width, bucketsObjIsIn);

            //BottomRight
            AddBucket(new Vector2(max.X, max.Y), width, bucketsObjIsIn);

            //BottomLeft
            AddBucket(new Vector2(min.X, max.Y), width, bucketsObjIsIn);

            return bucketsObjIsIn;
        }
コード例 #6
0
        public Vector2 GetVelocity(Boid targetBoid, Flock flock)
        {
            if (targetBoid == null)
            {
                throw new ArgumentNullException(nameof(targetBoid));
            }

            if (flock == null)
            {
                throw new ArgumentNullException(nameof(flock));
            }

            if (!flock.Boids.Contains(targetBoid)) // TODO: Time-consuming?..
            {
                throw new InvalidOperationException();
            }

            if (flock.Boids.Count < 2)
            {
                return Vector2.Zero;
            }

            Vector2 velocity = new Vector2();
            foreach (Boid boid in flock.Boids)
            {
                if (targetBoid != boid)
                {
                    velocity += boid.Velocity;
                }
            }

            velocity /= flock.Boids.Count - 1;
            return (velocity - targetBoid.Velocity) * this.correctionCoefficient;
        }
コード例 #7
0
 public void AddObject(Boid obj)
 {
     var cellIds = GetIdForObj(obj);
     foreach (var item in cellIds)
     {
         Buckets[(item)].Add(obj);
     }
 }
コード例 #8
0
 public void SpawnBoids(int n = 1)
 {
     for (int i = 0; i < n; i++)
     {
         Boid boid = new Boid(this, Game1.BOID_TEXTURE);
         Boids.Add(boid);
     }
 }
コード例 #9
0
 //static int width = 0, height = 75;
 static void Main()
 {
     fillArray();
        boidArray = new Boid[20];  //creates an array of boids to fill
        for (int i = 0; i < 20; i++)  //loops to create 20 boids
        {
        isEmpty = false;
        int boidNum = i + 1;  //gives each boid a number
        Random rand = new Random();
        int x = rand.Next(0, 20);  //sets each boid in a random position
        int y = rand.Next(0, 20);
        if (grid[x, y] == empty) //checks if it's empty
        {
            isEmpty = true;
        }
        else
        {
            while (isEmpty == false) //leeps making new randoms until it's in an empty position
            {
                x = rand.Next(0, 20);
                y = rand.Next(0, 20);
                if (grid[x, y] == empty)
                {
                    isEmpty = true;
                }
                else
                {
                    isEmpty = false;
                }
            }
        }
        boidArray[i] = new Boid(x, y, grid, boidNum); //creates boid in the random position
        boidArray[i].setPos(); //sets the position
        }
        Console.WriteLine("Welcome to Boids");
        Console.WriteLine("Press enter to continue, or enter x at any time to exit");
        input = Console.ReadLine();
        Console.Clear();
        readArray();
        input = Console.ReadLine();
        //main function loop, exits if x is entered
        while (input != "x")
        {
        Console.Clear(); //clears console from previous
        readArray(); //reads the array in its current state
        for (int i = 0; i < boidArray.Length; i++) //runs through search and move methods for each boid in the array
        {
            boidArray[i].search(boidArray);
            boidArray[i].Move();
        }
        input = Console.ReadLine();
        }
 }
コード例 #10
0
    private void Window_Loaded(object sender, EventArgs e) {
      int Xmargin = (int)Math.Round(Space.SizeX / 10.0);
      int Ymargin = (int)Math.Round(Space.SizeY / 10.0);
      int Zmargin = (int)Math.Round(Space.SizeZ / 10.0);
      Rect3D aviary = new Rect3D((double)Xmargin, (double)Ymargin, (double)Zmargin, Space.SizeX - (2 * Xmargin), Space.SizeY - (2 * Ymargin), Space.SizeZ - (2 * Zmargin));
      Random random = new Random();
      Vector3D place = new Vector3D(Space.SizeX / 2.0, Space.SizeY / 2.0, Space.SizeZ / 2.0);
      for (var i = 0; i < NumBoids; i++) {
        boids[i] = new Boid(aviary, place, NumBoids, i, this);
        boids[i].position = new Vector3D((double)random.Next(Xmargin, (int)Math.Round((double)(Space.SizeX - (2 * Xmargin)))), (double)random.Next(Ymargin, (int)Math.Round((double)(Space.SizeY - (2 * Ymargin)))), (double)random.Next(Zmargin, (int)Math.Round((double)(Space.SizeZ - (2 * Zmargin)))));
        boids[i].velocity = new Vector3D(0.0, 0.1, 0.0);
      }

      foreach (Boid boid in boids) {
        boid.Start();
      }
    }
コード例 #11
0
        public Flock(int flockSize = 100)
        {
            FlockSize = flockSize;
            Boids.Clear();
            for (var i = 1; i <= FlockSize; i++)
            {
                var boid = new Boid();
                boid.Position = new Vector3(
                    (float)Program.RANDOM.NextDouble() * (-5 - 5) + 5,
                    (float)Program.RANDOM.NextDouble() * (-5 - 5) + 5,
                    (float)Program.RANDOM.NextDouble() * (-5 - 5) + 5);

                boid.Velocity = new Vector3(
                    Program.RANDOM.Next(2) * 2 - 1,
                    Program.RANDOM.Next(2) * 2 - 1,
                    Program.RANDOM.Next(2) * 2 - 1);
                Boids.Add(boid);
            }
        }
コード例 #12
0
ファイル: Flock.cs プロジェクト: leefarley/Boids
        /// <summary>
        /// the constructor is responsible for creating the boids and give them values within the boid area
        /// </summary>
        public Flock(int flockSize, Random rand, List<Interfaces.BoidRule_Interface> rules,int width,int height)
        {
            this.rand = rand;
            boids = new List<Interfaces.Boid_Interface>();
            this.rules = rules;
            mapwidth = width;
            mapHeight = height;
            for (int i = 0; i < flockSize; i++)
            {
                int x = rand.Next(mapwidth);
                int y = rand.Next(mapHeight);
                int speedX = rand.Next(10);
                int speedY = rand.Next(10);
                Boid newBoid = new Boid(new Vector2(x, y), new Vector2(speedX, speedY));
                boids.Add(newBoid);
            }

            BoidColor = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
        }
コード例 #13
0
        protected virtual void MoveBoid(Flock flock, Boid boid)
        {
            if (flock == null)
            {
                throw new ArgumentNullException(nameof(flock));
            }

            if (boid == null)
            {
                throw new ArgumentNullException(nameof(boid));
            }

            Vector2 aggregateVelocity = boid.Velocity;
            foreach (IFlockMoveRule rule in this.MoveRules)
            {
                aggregateVelocity += rule.GetVelocity(boid, flock);
            }

            boid.Velocity = aggregateVelocity;
            boid.Position += aggregateVelocity;
        }
コード例 #14
0
        private void BoundPosition(Boid boid, int maxX, int maxY)
        {
            if (boid.Position.X < 2)
            {
                boid.Velocity = boid.Velocity.WithX(2);
            }

            if (boid.Position.X >= maxX)
            {
                boid.Velocity = boid.Velocity.WithX(-2);
            }

            if (boid.Position.Y < 2)
            {
                boid.Velocity = boid.Velocity.WithY(2);
            }

            if (boid.Position.Y >= maxY)
            {
                boid.Velocity = boid.Velocity.WithY(-2);
            }
        }
コード例 #15
0
        // === Get Items in neighborhood ===
        public List <Boid> FindNeighbors(Boid self)
        {
            List <Boid> neighbors    = new List <Boid>();
            Rectangle   neighborhood = self.GetNeighborhood();

            // Compare each item in population
            for (int other = 0; other < count; other++)
            {
                // Ignore self
                if (self != population[other])
                {
                    // Check if other is in neighboorhood
                    if (Geometry.PointFDistance(self.position, population[other].position) < Boid.SIGHT_RANGE)
                    {
                        neighbors.Add(population[other]);
                    }
                }
            }


            return(neighbors);
        }
コード例 #16
0
        public Vector2 GetVelocity(Boid targetBoid, Flock flock)
        {
            if (targetBoid == null)
            {
                throw new ArgumentNullException(nameof(targetBoid));
            }

            if (flock == null)
            {
                throw new ArgumentNullException(nameof(flock));
            }

            if (!flock.Boids.Contains(targetBoid)) // TODO: Time-consuming?..
            {
                throw new InvalidOperationException();
            }

            if (flock.Boids.Count < 2)
            {
                return Vector2.Zero;
            }

            Vector2 shift = Vector2.Zero;
            foreach (Boid boid in flock.Boids)
            {
                if (targetBoid != boid)
                {
                    if (Vector2.Distance(boid.Position, targetBoid.Position) < this.distanceTreshold)
                    {
                        shift -= boid.Position - targetBoid.Position;
                    }
                }
            }

            return shift;
        }
コード例 #17
0
        private void Hunt(List <Boid> boids)
        {
            var  range = float.MaxValue;
            Boid prey  = null;

            foreach (var boid in boids)
            {
                if (!boid.Zombie)
                {
                    float distance = Distance(Position, boid.Position);
                    if (distance < sight && distance < range)
                    {
                        range = distance;
                        prey  = boid;
                    }
                }
            }
            if (prey != null)
            {
                // Move towards closest prey.
                dX += prey.Position.X - Position.X;
                dY += prey.Position.Y - Position.Y;
            }
        }
コード例 #18
0
 public void AddBoid(Boid b)
 {
     Boids.Add(b);
 }
コード例 #19
0
ファイル: Boid.cs プロジェクト: kenny0894/boidsAIAssignment
        //searches the array of boids and finds any boids that within range
        //need to make this search otherside if they are within range (i.e. x=17 needs to search 12-->2)
        public void search(Boid[] boidArray)
        {
            double dist = 0;
            inView = new List<Boid>(); //large search, so they don't see the whole array
            neighbors = new List<Boid>(); //just the nearby neighbours, to check for any hits
            foreach (Boid b in boidArray)
            {
                if (b != this)//so it doesn't add itself to the list
                {
                    if (y-b.y <= 3 && y-b.y >= -3 && x+b.x >= 16) //overlap search
                    {
                        if (!inView.Contains(b))
                        {
                            inView.Add(b);
                        }
                        if (y - b.y <= 1 && y - b.y >= -1 && ((x==0 && b.x==19)||(x==19 && b.x == 0)))
                        {
                            if (!neighbors.Contains(b))
                            {
                                neighbors.Add(b);
                            }
                        }
                    }
                    if (x-b.x <= 3 && x-b.x >= -3 && y+b.y >= 16)
                    {
                        if (!inView.Contains(b))
                        {
                            inView.Add(b);
                        }
                        if (x - b.x <= 1 && x - b.x >= -1 && ((y==0 && b.y==19)||(y==19 &&b.y == 0)))
                        {
                            if (!neighbors.Contains(b))
                            {
                                neighbors.Add(b);
                            }
                        }
                    }
                    if (((x-b.x <= 3) && (x-b.x >= -3)) && ((y-b.y <= 3) && (y-b.y >= -3)))//searches a 7x7 square
                    {
                        if (!inView.Contains(b))
                        {
                            inView.Add(b);
                        }
                        if ((x - b.x <= 1) && (x - b.x >= -1) && (y - b.y <= 1) && (y - b.y >= -1)) //range = 1 (neighbouring points)
                        {
                            if (!neighbors.Contains(b))
                            {
                                neighbors.Add(b);
                            }
                        }
                    }

                }
            }
        }
コード例 #20
0
 public Boid[] GetPossibleColliders(Boid obj)
 {
     var objects = new List<Boid>();
     var bucketIds = GetIdForObj(obj);
     foreach (var item in bucketIds)
     {
         objects.AddRange(Buckets[item]);
     }
     return objects.Distinct().ToArray();
 }
コード例 #21
0
 // === Add new Boid to population ===
 public void Add()
 {
     population[count] = new Boid(this, rng, new Point(rng.Next(panel.Width), rng.Next(panel.Height)), graphics, pen);
     count++;
 }
コード例 #22
0
 double DistanceBetween(Boid B)
 {
     return(Math.Pow(B.position.X - position.X, 2) + Math.Pow(B.position.Y - position.Y, 2));
 }
コード例 #23
0
 public Vector2 AvoidFBINodes(Boid currentBoid)
 {
     return(getDangerAngleFromPosition(currentBoid.position, currentBoid));
 }
コード例 #24
0
ファイル: BoidLeader.cs プロジェクト: Banananaman91/DeathGame
        private List <Transform> GetNearbyObjects(Boid agent)
        {
            var neighbourColliders = Physics.OverlapSphere(agent.transform.position, _neighbourRadius);

            return((from c in neighbourColliders where c != agent.BoidCollider select c.transform).ToList());
        }
コード例 #25
0
        public Vector2 getDangerAngleFromPosition(Vector2 position, Boid currentBoid)
        {
            Point closest = gridPosFromPosition(position);

            //gridpos
            Point leftUp     = new Point(closest.X - 1, closest.Y - 1);
            Point leftMiddle = new Point(closest.X - 1, closest.Y);
            Point leftDown   = new Point(closest.X - 1, closest.Y + 1);

            Point middleUp   = new Point(closest.X, closest.Y - 1);
            Point middleDown = new Point(closest.X, closest.Y + 1);

            Point rightUp     = new Point(closest.X + 1, closest.Y - 1);
            Point rightMiddle = new Point(closest.X + 1, closest.Y);
            Point rightDown   = new Point(closest.X + 1, closest.Y + 1);


            //data
            float leftUpFloat     = getGridInformation(leftUp);
            float leftMiddleFloat = getGridInformation(leftMiddle);
            float leftDownFloat   = getGridInformation(leftDown);

            float middleUpFloat   = getGridInformation(middleUp);
            float middleDownFloat = getGridInformation(middleDown);

            float rightUpFloat     = getGridInformation(rightUp);
            float rightMiddleFloat = getGridInformation(rightMiddle);
            float rightDownFloat   = getGridInformation(rightDown);


            //dangervect
            Vector2 leftUpVect     = new Vector2(leftUpFloat, leftUpFloat);
            Vector2 leftMiddleVect = new Vector2(leftMiddleFloat, 0);
            Vector2 leftDownVect   = new Vector2(leftDownFloat, -leftDownFloat);

            Vector2 middleUpVect   = new Vector2(0, middleUpFloat);
            Vector2 middleDownVect = new Vector2(0, -middleDownFloat);

            Vector2 rightUpVect     = new Vector2(-rightUpFloat, rightUpFloat);
            Vector2 rightMiddleVect = new Vector2(-rightMiddleFloat, 0);
            Vector2 rightDownVect   = new Vector2(-rightDownFloat, -rightDownFloat);


            //pos
            Vector2 leftUpPos     = vectPosFromPoint(leftUp);
            Vector2 leftMiddlePos = vectPosFromPoint(leftMiddle);
            Vector2 leftDownPos   = vectPosFromPoint(leftDown);

            Vector2 middleUpPos   = vectPosFromPoint(middleUp);
            Vector2 middleDownPos = vectPosFromPoint(middleDown);

            Vector2 rightUpPos     = vectPosFromPoint(rightUp);
            Vector2 rightMiddlePos = vectPosFromPoint(rightMiddle);
            Vector2 rightDownPos   = vectPosFromPoint(rightDown);

            //distance
            float leftUpDist     = Vector2.Distance(position, leftUpPos);
            float leftMiddleDist = Vector2.Distance(position, leftMiddlePos);
            float leftDownDist   = Vector2.Distance(position, leftDownPos);

            float middleUpDist   = Vector2.Distance(position, middleUpPos);
            float middleDownDist = Vector2.Distance(position, middleDownPos);

            float rightUpDist     = Vector2.Distance(position, rightUpPos);
            float rightMiddleDist = Vector2.Distance(position, rightMiddlePos);
            float rightDownDist   = Vector2.Distance(position, rightDownPos);

            if (leftUpDist != 0.0f)
            {
                leftUpVect = Vector2.Divide(leftUpVect, leftUpDist);
            }

            if (leftMiddleDist != 0.0f)
            {
                leftMiddleVect = Vector2.Divide(leftMiddleVect, leftMiddleDist);
            }

            if (leftDownDist != 0.0f)
            {
                leftDownVect = Vector2.Divide(leftDownVect, leftDownDist);
            }


            if (middleUpDist != 0.0f)
            {
                middleUpVect = Vector2.Divide(middleUpVect, middleUpDist);
            }

            if (middleDownDist != 0.0f)
            {
                middleDownVect = Vector2.Divide(middleDownVect, middleDownDist);
            }


            if (rightUpDist != 0.0f)
            {
                rightUpVect = Vector2.Divide(rightUpVect, rightUpDist);
            }

            if (rightMiddleDist != 0.0f)
            {
                rightMiddleVect = Vector2.Divide(rightMiddleVect, rightMiddleDist);
            }

            if (rightDownDist != 0.0f)
            {
                rightDownVect = Vector2.Divide(rightDownVect, rightDownDist);
            }


            Vector2 dangerAngle = leftUpVect + leftMiddleVect + leftDownVect + middleUpVect + middleDownVect +
                                  rightUpVect + rightMiddleVect + rightDownVect;

            if (dangerAngle.Length() > 1)
            {
                dangerAngle.Normalize();
                dangerAngle = Vector2.Multiply(dangerAngle, Globals.boidLBIMaxSpeed);
                dangerAngle = Vector2.Subtract(dangerAngle, currentBoid.velocity);
            }


            return(dangerAngle);
        }