/// <summary>
        /// separationBehavior.Update infuences the owning animal to move away from
        /// the otherAnimal is it’s too close, in this case if it’s inside 
        /// AIParameters.separationDistance.
        /// </summary>
        /// <param name="otherAnimal">the Animal to react to</param>
        /// <param name="aiParams">the Behaviors' parameters</param>
        public override void Update(Animal otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 pushDirection = Vector2.Zero;
            float weight = aiParams.PerMemberWeight;

            if (Animal.ReactionDistance > 0.0f && 
                Animal.ReactionDistance <= aiParams.SeparationDistance)
            {
                //The otherAnimal is too close so we figure out a pushDirection 
                //vector in the opposite direction of the otherAnimal and then weight
                //that reaction based on how close it is vs. our separationDistance

                pushDirection = Animal.Location - Animal.ReactionLocation;
                Vector2.Normalize(ref pushDirection, out pushDirection);

                //push away
                weight *= (1 - 
                    (float)Animal.ReactionDistance / aiParams.SeparationDistance);

                pushDirection *= weight;

                reacted = true;
                reaction += pushDirection;
            }
        }
        /// <summary>
        /// CohesionBehavior.Update infuences the owning animal to move towards the
        /// otherAnimal that it sees as long as it isn’t too close, in this case 
        /// that means inside the separationDist in the passed in AIParameters.
        /// </summary>
        /// <param name="otherAnimal">the Animal to react to</param>
        /// <param name="aiParams">the Behaviors' parameters</param>
        public override void Update(Animal otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 pullDirection = Vector2.Zero;
            float weight = aiParams.PerMemberWeight;

            //if the otherAnimal is too close we dont' want to fly any
            //closer to it
            if (Animal.ReactionDistance > 0.0f
                && Animal.ReactionDistance > aiParams.SeparationDistance)
            {
                //We want to make the animal move closer the the otherAnimal so we 
                //create a pullDirection vector pointing to the otherAnimal bird and 
                //weigh it based on how close the otherAnimal is relative to the 
                //AIParameters.separationDistance.
                pullDirection = -(Animal.Location - Animal.ReactionLocation);
                Vector2.Normalize(ref pullDirection, out pullDirection);

                weight *= (float)Math.Pow((double)
                    (Animal.ReactionDistance - aiParams.SeparationDistance) /
                        (aiParams.DetectionDistance - aiParams.SeparationDistance), 2);

                pullDirection *= weight;

                reacted = true;
                reaction = pullDirection;
            }
        }
        /// <summary>
        /// separationBehavior.Update infuences the owning animal to move away from
        /// the otherAnimal is it’s too close, in this case if it’s inside
        /// AIParameters.separationDistance.
        /// </summary>
        /// <param name="otherAnimal">the Animal to react to</param>
        /// <param name="aiParams">the Behaviors' parameters</param>
        public override void Update(Animal otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 pushDirection = Vector2.Zero;
            float   weight        = aiParams.PerMemberWeight;

            if (Animal.ReactionDistance > 0.0f &&
                Animal.ReactionDistance <= aiParams.SeparationDistance)
            {
                //The otherAnimal is too close so we figure out a pushDirection
                //vector in the opposite direction of the otherAnimal and then weight
                //that reaction based on how close it is vs. our separationDistance

                pushDirection = Animal.Location - Animal.ReactionLocation;
                Vector2.Normalize(ref pushDirection, out pushDirection);

                //push away
                weight *= (1 -
                           (float)Animal.ReactionDistance / aiParams.SeparationDistance);

                pushDirection *= weight;

                reacted   = true;
                reaction += pushDirection;
            }
        }
        /// <summary>
        /// CohesionBehavior.Update infuences the owning animal to move towards the
        /// otherAnimal that it sees as long as it isn’t too close, in this case
        /// that means inside the separationDist in the passed in AIParameters.
        /// </summary>
        /// <param name="otherAnimal">the Animal to react to</param>
        /// <param name="aiParams">the Behaviors' parameters</param>
        public override void Update(Animal otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 pullDirection = Vector2.Zero;
            float   weight        = aiParams.PerMemberWeight;

            //if the otherAnimal is too close we dont' want to fly any
            //closer to it
            if (Animal.ReactionDistance > 0.0f &&
                Animal.ReactionDistance > aiParams.SeparationDistance)
            {
                //We want to make the animal move closer the the otherAnimal so we
                //create a pullDirection vector pointing to the otherAnimal bird and
                //weigh it based on how close the otherAnimal is relative to the
                //AIParameters.separationDistance.
                pullDirection = -(Animal.Location - Animal.ReactionLocation);
                Vector2.Normalize(ref pullDirection, out pullDirection);

                weight *= (float)Math.Pow((double)
                                          (Animal.ReactionDistance - aiParams.SeparationDistance) /
                                          (aiParams.DetectionDistance - aiParams.SeparationDistance), 2);

                pullDirection *= weight;

                reacted  = true;
                reaction = pullDirection;
            }
        }
Beispiel #5
0
        /// <summary>
        /// React to an Animal based on it's type
        /// </summary>
        /// <param name="animal"></param>
        public void ReactTo(Animal animal, ref AIParameters AIparams)
        {
            if (animal != null)
            {
                //setting the the reactionLocation and reactionDistance here is
                //an optimization, many of the possible reactions use the distance
                //and location of theAnimal, so we might as well figure them out
                //only once !
                Vector2 otherLocation = animal.Location;
                ClosestLocation(ref location, ref otherLocation,
                                out reactionLocation);
                reactionDistance = Vector2.Distance(location, reactionLocation);

                //we only react if theAnimal is close enough that we can see it
                if (reactionDistance < AIparams.DetectionDistance)
                {
                    Behaviors reactions = behaviors[animal.AnimalType];
                    foreach (Behavior reaction in reactions)
                    {
                        reaction.Update(animal, AIparams);
                        if (reaction.Reacted)
                        {
                            aiNewDir += reaction.Reaction;
                            aiNumSeen++;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// AlignBehavior.Update infuences the owning animal to move in same the
        /// direction as the otherAnimal that it sees.
        /// </summary>
        /// <param name="otherAnimal">the Animal to react to</param>
        /// <param name="aiParams">the Behaviors' parameters</param>
        public override void Update(Animal otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            if (otherAnimal != null)
            {
                reacted  = true;
                reaction = otherAnimal.Direction * aiParams.PerMemberWeight;
            }
        }
Beispiel #7
0
        /// <summary>
        /// update bird position, wrapping around the screen edges
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime, ref AIParameters aiParams)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            Vector2 randomDir = Vector2.Zero;

            randomDir.X = (float)random.NextDouble() - 0.5f;
            randomDir.Y = (float)random.NextDouble() - 0.5f;
            Vector2.Normalize(ref randomDir, out randomDir);

            if (aiNumSeen > 0)
            {
                aiNewDir = (direction * aiParams.MoveInOldDirectionInfluence) +
                    (aiNewDir * (aiParams.MoveInFlockDirectionInfluence / 
                    (float)aiNumSeen));
            }
            else
            {
                aiNewDir = direction * aiParams.MoveInOldDirectionInfluence;
            }

            aiNewDir += (randomDir * aiParams.MoveInRandomDirectionInfluence);
            Vector2.Normalize(ref aiNewDir, out aiNewDir);
            aiNewDir = ChangeDirection(direction, aiNewDir, 
                aiParams.MaxTurnRadians * elapsedTime);
            direction = aiNewDir;

            if (direction.LengthSquared() > .01f)
            {
                Vector2 moveAmount = direction * moveSpeed * elapsedTime;
                location = location + moveAmount;

                //wrap bird to the other side of the screen if needed
                if (location.X < 0.0f)
                {
                    location.X = boundryWidth + location.X;
                }
                else if (location.X > boundryWidth)
                {
                    location.X = location.X - boundryWidth;
                }

                location.Y += direction.Y * moveSpeed * elapsedTime;
                if (location.Y < 0.0f)
                {
                    location.Y = boundryHeight + location.Y;
                }
                else if (location.Y > boundryHeight)
                {
                    location.Y = location.Y - boundryHeight;
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// update bird position, wrapping around the screen edges
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime, ref AIParameters aiParams)
        {
            float   elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            Vector2 randomDir   = Vector2.Zero;

            randomDir.X = (float)random.NextDouble() - 0.5f;
            randomDir.Y = (float)random.NextDouble() - 0.5f;
            Vector2.Normalize(ref randomDir, out randomDir);

            if (aiNumSeen > 0)
            {
                aiNewDir = (direction * aiParams.MoveInOldDirectionInfluence) +
                           (aiNewDir * (aiParams.MoveInFlockDirectionInfluence /
                                        (float)aiNumSeen));
            }
            else
            {
                aiNewDir = direction * aiParams.MoveInOldDirectionInfluence;
            }

            aiNewDir += (randomDir * aiParams.MoveInRandomDirectionInfluence);
            Vector2.Normalize(ref aiNewDir, out aiNewDir);
            aiNewDir = ChangeDirection(direction, aiNewDir,
                                       aiParams.MaxTurnRadians * elapsedTime);
            direction = aiNewDir;

            if (direction.LengthSquared() > .01f)
            {
                Vector2 moveAmount = direction * moveSpeed * elapsedTime;
                location = location + moveAmount;

                //wrap bird to the other side of the screen if needed
                if (location.X < 0.0f)
                {
                    location.X = boundryWidth + location.X;
                }
                else if (location.X > boundryWidth)
                {
                    location.X = location.X - boundryWidth;
                }

                location.Y += direction.Y * moveSpeed * elapsedTime;
                if (location.Y < 0.0f)
                {
                    location.Y = boundryHeight + location.Y;
                }
                else if (location.Y > boundryHeight)
                {
                    location.Y = location.Y - boundryHeight;
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// Setup the flock boundaries and generate individual members of the flock
        /// </summary>
        /// <param name="tex"> The texture to be used by the birds</param>
        /// <param name="screenWidth">Width of the screen</param>
        /// <param name="screenHeight">Height of the screen</param>
        /// <param name="flockParameters">Behavior of the flock</param>
        public Flock(Texture2D tex, int screenWidth, int screenHeight,
            AIParameters flockParameters)
        {
            boundryWidth = screenWidth;
            boundryHeight = screenHeight;

            birdTexture = tex;

            flock = new List<Bird>();
            flockParams = flockParameters;

            ResetFlock();
        }
        public FlockingSample()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            inputState = new InputState();

            flock = null;
            cat   = null;

            flockParams = new AIParameters();
            ResetAIParams();
        }
Beispiel #11
0
        /// <summary>
        /// Setup the flock boundaries and generate individual members of the flock
        /// </summary>
        /// <param name="tex"> The texture to be used by the birds</param>
        /// <param name="screenWidth">Width of the screen</param>
        /// <param name="screenHeight">Height of the screen</param>
        /// <param name="flockParameters">Behavior of the flock</param>
        public Flock(Texture2D tex, int screenWidth, int screenHeight,
                     AIParameters flockParameters)
        {
            boundryWidth  = screenWidth;
            boundryHeight = screenHeight;

            birdTexture = tex;

            flock       = new List <Bird>();
            flockParams = flockParameters;

            ResetFlock();
        }
Beispiel #12
0
        public FlockingSample()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

#if WINDOWS_PHONE || IOS
            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime     = TimeSpan.FromTicks(333333);
            graphics.IsFullScreen = true;
#endif
            inputState = new InputState();

            flock = null;
            cat   = null;

            flockParams = new AIParameters();
            ResetAIParams();
        }
Beispiel #13
0
        public override void Update(Animal otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 dangerDirection = Vector2.Zero;

            //Vector2.Dot will return a negative result in this case if the 
            //otherAnimal is behind the animal, in that case we don’t have to 
            //worry about it because we’re already moving away from it.
            if (Vector2.Dot(
                Animal.Location, Animal.ReactionLocation) >= -(Math.PI / 2))
            {
                //set the animal to fleeing so that it flashes red
                Animal.Fleeing = true;
                reacted = true;

                dangerDirection = Animal.Location - Animal.ReactionLocation;
                Vector2.Normalize(ref dangerDirection, out dangerDirection);
                
                reaction = (aiParams.PerDangerWeight * dangerDirection);
            }
        }
Beispiel #14
0
        public override void Update(Animal otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 dangerDirection = Vector2.Zero;

            //Vector2.Dot will return a negative result in this case if the
            //otherAnimal is behind the animal, in that case we don’t have to
            //worry about it because we’re already moving away from it.
            if (Vector2.Dot(
                    Animal.Location, Animal.ReactionLocation) >= -(Math.PI / 2))
            {
                //set the animal to fleeing so that it flashes red
                Animal.Fleeing = true;
                reacted        = true;

                dangerDirection = Animal.Location - Animal.ReactionLocation;
                Vector2.Normalize(ref dangerDirection, out dangerDirection);

                reaction = (aiParams.PerDangerWeight * dangerDirection);
            }
        }
Beispiel #15
0
 /// <summary>
 /// Abstract function that the subclass must impliment. Figure out the 
 /// Behavior reaction here.
 /// </summary>
 /// <param name="otherAnimal">the Animal to react to</param>
 /// <param name="aiParams">the Behaviors' parameters</param>
 public abstract void Update(Animal otherAnimal, AIParameters aiParams);
        public FlockingSample()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

#if WINDOWS_PHONE || IOS
            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);
            graphics.IsFullScreen = true;
#endif
            inputState = new InputState();

            flock = null;
            cat = null;

            flockParams = new AIParameters();
            ResetAIParams();
        }
 /// <summary>
 /// Abstract function that the subclass must impliment. Figure out the
 /// Behavior reaction here.
 /// </summary>
 /// <param name="otherAnimal">the Animal to react to</param>
 /// <param name="aiParams">the Behaviors' parameters</param>
 public abstract void Update(Animal otherAnimal, AIParameters aiParams);
Beispiel #18
0
        /// <summary>
        /// React to an Animal based on it's type
        /// </summary>
        /// <param name="animal"></param>
        public void ReactTo(Animal animal, ref AIParameters AIparams)
        {
            if (animal != null)
            {
                //setting the the reactionLocation and reactionDistance here is
                //an optimization, many of the possible reactions use the distance
                //and location of theAnimal, so we might as well figure them out
                //only once !
                Vector2 otherLocation = animal.Location;
                ClosestLocation(ref location, ref otherLocation, 
                    out reactionLocation);
                reactionDistance = Vector2.Distance(location, reactionLocation);

                //we only react if theAnimal is close enough that we can see it
                if (reactionDistance < AIparams.DetectionDistance)
                {
                    Behaviors reactions = behaviors[animal.AnimalType];
                    foreach (Behavior reaction in reactions)
                    {
                        reaction.Update(animal, AIparams);
                        if (reaction.Reacted)
                        {
                            aiNewDir += reaction.Reaction;
                            aiNumSeen++;
                        }
                    }
                }
            }
        }