public override Vector2 Calculate()
        {
            bool IsNear(BaseGameEntity entity, float range) => Vector2.DistanceSquared(this.Entity.Position, entity.Position) < range * range;

            bool InAlignmentRange(MovingEntity entity) => IsNear(entity, this.alignmentRadius);
            bool InCohesionRange(MovingEntity entity) => IsNear(entity, this.cohesionRadius);
            bool InSeparationRange(MovingEntity entity) => IsNear(entity, this.separationRadius);

            const int          amountOfNeighbours = 10;
            IEnumerable <Bird> birds = this.world.Entities.OfType <Bird>()
                                       .OrderBy(bird => Vector2.DistanceSquared(bird.Position, this.Entity.Position))
                                       .Where(entity => entity != this.Entity)
                                       .Take(amountOfNeighbours)
                                       .ToArray();

            //  When no neighbours nearby, wander
            if (!birds.Any())
            {
                return(this.innerWander.Calculate());
            }

            Vector2 alignment  = SteeringBehaviours.Alignment(birds.Where(InAlignmentRange));
            Vector2 cohesion   = SteeringBehaviours.Cohesion(this.Entity, birds.Where(InCohesionRange));
            Vector2 separation = SteeringBehaviours.Separation(this.Entity, birds.Where(InSeparationRange));

            Vector2 target = alignment * this.alignmentWeight +
                             cohesion * this.cohesionWeight +
                             separation * this.separationWeight;

            return(target * this.Strength);
        }
Example #2
0
        public override Vector2 Calculate()
        {
            Vector2 steeringForce = new Vector2(0, 0);

            DynamicEntity.entityManager.TagNeighbours(DynamicEntity, 100);
            DynamicEntity.entityManager.EnforceNonPenetrationConstraint(DynamicEntity);
            List <DynamicGameEntity> entities = DynamicEntity.entityManager.GetMovingEntities();

            steeringForce += Vector2.Multiply(SteeringBehaviours.Cohesion(DynamicEntity, entities), cohesionAmount);
            steeringForce += Vector2.Multiply(SteeringBehaviours.Alignment(DynamicEntity, entities), alignmentAmount);
            steeringForce += Vector2.Multiply(SteeringBehaviours.Separation(DynamicEntity, entities), separationAmount);
            steeringForce += Vector2.Multiply(SteeringBehaviours.Wander(DynamicEntity, 20, 5, 5), wanderAmount);

            return(steeringForce.Truncate(maxSteeringForce));
        }
Example #3
0
    // Draw gizmos when this agent is selected in the scene view
    private void OnDrawGizmosSelected()
    {
        if (_steering == null)
        {
            return;
        }
        var currentPosition = transform.position;
        // calculate steering
        Vector3 seek = Vector3.zero, arrival = Vector3.zero, flee = Vector3.zero, hide = Vector3.zero;
        var     alignment         = alignmentFactor * _steering.Alignment(_neighbors);
        var     cohesion          = cohesionFactor * _steering.Cohesion(currentPosition, _neighbors);
        var     separation        = separationFactor * _steering.Separation(currentPosition, _neighbors);
        var     obstacleAvoidance = obstacleAvoidanceFactor * _steering.ObstacleAvoidance(currentPosition, _rigidbody.velocity, _obstacles);
        var     wander            = wanderFactor * _steering.Wander(currentPosition, transform.forward);

        if (_isarrivalTargetNotNull)
        {
            arrival = arrivalFactor * _steering.Arrival(currentPosition, arrivalTarget.position);
        }
        if (_isseekTargetNotNull)
        {
            seek = seekFactor * _steering.Seek(currentPosition, seekTarget.position);
        }
        if (_isfleeTargetNotNull)
        {
            flee = fleeFactor * _steering.Flee(currentPosition, fleeTarget.position);
        }
        if (_ishideTargetNotNull)
        {
            hide = hideFactor * _steering.Hide(currentPosition, hideTarget.position, _obstacles);
        }
        // calculate acceleration (combine steering behaviours and limit acceleration)
        Vector3 acceleration = alignment + cohesion + separation + obstacleAvoidance + wander + arrival + seek + flee + hide;

        if (acceleration.magnitude > maxAcceleration)
        {
            acceleration = acceleration.normalized * maxAcceleration;
        }
        if (acceleration.magnitude < minAcceleration)
        {
            acceleration = acceleration.normalized * minAcceleration;
        }
        // draw vector gizmos
        Gizmos.color = Color.red;
        Gizmos.DrawRay(currentPosition, cohesion);
        Gizmos.color = Color.green;
        Gizmos.DrawRay(currentPosition, separation);
        Gizmos.color = Color.blue;
        Gizmos.DrawRay(currentPosition, alignment);
        Gizmos.color = Color.white;
        Gizmos.DrawRay(currentPosition, obstacleAvoidance);
        Gizmos.color = Color.grey;
        Gizmos.DrawRay(currentPosition, flee);
        Gizmos.color = Color.black;
        Gizmos.DrawRay(currentPosition, hide);
        Gizmos.color = Color.yellow;
        Gizmos.DrawRay(currentPosition, wander);
        Gizmos.DrawRay(currentPosition, arrival);
        Gizmos.DrawRay(currentPosition, seek);
        Gizmos.color = Color.magenta;
        Gizmos.DrawRay(currentPosition, acceleration);
    }