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;
        }
示例#2
0
        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;
        }
        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;
        }
        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;
        }
示例#5
0
        public virtual void Move(Flock flock)
        {
            if (flock == null)
            {
                throw new ArgumentNullException(nameof(flock));
            }

            foreach (Boid boid in flock.Boids)
            {
                this.MoveBoid(flock, boid);
            }
        }
示例#6
0
 private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
 {
     if (e.Button == MouseButtons.Left)
     {
         flock = CreateFlock(e.Location);
         pictureBox1.Invalidate();
         timer1.Start();
     }
     else if (e.Button == MouseButtons.Right)
     {
         timer1.Stop();
         RemoveFlock();
     }
 }
示例#7
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;
        }
        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;
        }
示例#9
0
 private void RemoveFlock()
 {
     flock = null;
     pictureBox1.Invalidate();
 }
示例#10
0
        protected override void Initialize()
        {
            base.Initialize();

            flock = new Flock();
        }
示例#11
0
 private void Awake()
 {
     _globalFlock = GameObject.FindGameObjectWithTag("GameController").GetComponent <Flock>();
 }