public override Vector2 Steer(ISteeringTarget target)
        {
            var force         = Vector2.Zero;
            var neighborCount = 0;

            var result = CheckNearestFunc.Invoke(this);

            foreach (var entity in result)
            {
                force += entity.Position - SteeringEntity.Position;
                neighborCount++;
            }

            if (neighborCount != 0)
            {
                force /= neighborCount;
                force *= -1;
            }

            if (force != Vector2.Zero)
            {
                force.Normalize();
            }
            force *= MaxSeparation;

            return(force);
        }
Example #2
0
        public override Vector2 Steer(ISteeringTarget target)
        {
            var dv = SteeringEntity.Velocity;

            if (dv != Vector2.Zero)
            {
                dv.Normalize();
            }
            dv *= MaxAvoidAhead * SteeringEntity.Velocity.Length() / SteeringEntity.MaxVelocity;

            _ahead = SteeringEntity.Position + dv;

            // BUG: It really likes to get stuck on edges (rectangle colliders), probably need to check field of view, not just a ray.
            var collision       = Physics.Linecast(SteeringEntity.Position, _ahead, 2);
            var mostThreatening = collision.Collider;

            if (mostThreatening != null && collision.Collider.Entity != Entity)
            {
                _avoidance = _ahead - mostThreatening.AbsolutePosition;
                _avoidance.Normalize();
                _avoidance *= AvoidForce;
            }
            else
            {
                _avoidance *= 0;
            }

            return(_avoidance);
        }
        public static Vector2 Seek(ISteeringTarget target, ISteeringEntity steeringEntity)
        {
            var dv = target.Position - steeringEntity.Position;

            dv.Normalize();

            steeringEntity.DesiredVelocity = dv;

            return((steeringEntity.DesiredVelocity * steeringEntity.MaxVelocity) - steeringEntity.Velocity);
        }
        public static Vector2 Flee(ISteeringTarget target, ISteeringEntity steeringEntity)
        {
            var dv = (target.Position - steeringEntity.Position);

            dv.Normalize();
            dv *= steeringEntity.MaxVelocity;

            steeringEntity.DesiredVelocity = -dv;

            return(steeringEntity.DesiredVelocity - steeringEntity.Velocity);
        }
Example #5
0
 private void AcquireConfigObjects()
 {
     if (this.sampler == null)
     {
         this.sampler = new AdaptiveVelocitySampler();
     }
     if (this.target == null)
     {
         this.target = new DirectionTarget();
     }
     if (this.characteristics == null)
     {
         this.characteristics = new DefaultAgentCharacteristics();
     }
 }
        public SteeringBuilder(Vector2 position, ISteeringTarget target = null)
        {
            _entity = new SteeringEntity
            {
                Name     = "steering-" + _currId,
                Position = position,
                Tag      = 123
            };

            var smc = _entity.AddComponent(new SteeringManagerComponent(target));

            _entity.ResetEvent += (sender, args) => smc.UpdateComponents();

            _currId++;
        }
Example #7
0
        public override Vector2 Steer(ISteeringTarget target)
        {
            var     distance     = (target.Position - SteeringEntity.Position).Length();
            var     updatesAhead = distance / SteeringEntity.MaxVelocity;
            Vector2 futurePos;

            if (target is ISteeringEntity steeringTarget)
            {
                futurePos = target.Position + steeringTarget.Velocity * updatesAhead;
            }
            else
            {
                futurePos = target.Position;
            }

            return(NestedBehavior == null
                ? BehaviorMath.Flee((Vector2SteeringTarget)futurePos, SteeringEntity)
                : NestedBehavior.Steer((Vector2SteeringTarget)futurePos));
        }
        public override Vector2 Steer(ISteeringTarget target)
        {
            var dv    = Leader.Velocity;
            var force = Vector2.Zero;

            dv.Normalize();
            dv    *= LeaderBehindDist;
            _ahead = Leader.Position + dv;

            dv     *= -1;
            _behind = Leader.Position + dv;

            //if (IsOnLeaderSight(_ahead))
            //    force += _evade.Steer(Leader as ISteeringTarget);

            ISteeringBehavior nestedBehavior = NestedBehavior ?? _arrival;

            return(force + nestedBehavior.Steer((Vector2SteeringTarget)_behind));
        }
Example #9
0
        public override Vector2 Steer(ISteeringTarget target)
        {
            var v        = SteeringEntity.Velocity;
            var brake    = Vector2.Zero;
            var neighbor = _getNeighborAheadFunc.Invoke(SteeringEntity);

            if (neighbor != null)
            {
                brake  = -SteeringEntity.Steering * 0.8f;
                v     *= -1;
                brake += v;

                if (Vector2.Distance(SteeringEntity.Position, neighbor.Position) <= _maxQueueRadius)
                {
                    SteeringEntity.Velocity *= 0.3f;
                }
            }

            return(brake);
        }
Example #10
0
        public override Vector2 Steer(ISteeringTarget target)
        {
            if (_target == null)
            {
                _target = target;
            }

            SteeringEntity.DesiredVelocity = target.Position - SteeringEntity.Position;
            var distance = SteeringEntity.DesiredVelocity.Length();

            if (distance < SlowingRadius)
            {
                SteeringEntity.DesiredVelocity = SteeringEntity.DesiredVelocity.Normalized() * SteeringEntity.MaxVelocity * (distance / SlowingRadius);
            }
            else
            {
                SteeringEntity.DesiredVelocity = SteeringEntity.DesiredVelocity.Normalized() * SteeringEntity.MaxVelocity;
            }

            return(SteeringEntity.DesiredVelocity - SteeringEntity.Velocity);
        }
Example #11
0
        public override Vector2 Steer(ISteeringTarget target)
        {
            var circleCenter = SteeringEntity.Velocity;

            circleCenter.Normalize();
            circleCenter *= CircleDistance;

            var displacement = new Vector2(0, -1);

            displacement *= CircleRadius;

            displacement = SetAngle(displacement, WanderAngle);

            var nxt = Random.RNG.NextFloat(-AngleChange, AngleChange);

            WanderAngle += nxt;

            var wanderForce = circleCenter + displacement;

            SteeringEntity.DesiredVelocity = wanderForce.Normalized() * SteeringEntity.MaxVelocity;
            return(SteeringEntity.DesiredVelocity - SteeringEntity.Velocity);
        }
Example #12
0
		private void AcquireConfigObjects()
		{
			if (this.sampler == null)			this.sampler = new AdaptiveVelocitySampler();
			if (this.target == null)			this.target = new DirectionTarget();
			if (this.characteristics == null)	this.characteristics = new DefaultAgentCharacteristics();
		}
 public ConditionArgs(ISteeringEntity entity, ISteeringTarget target)
 {
     Entity = entity;
     Target = target;
 }
Example #14
0
 public override Vector2 Steer(ISteeringTarget target)
 {
     return(BehaviorMath.Seek(target, SteeringEntity));
 }
 public abstract Vector2 Steer(ISteeringTarget target);
Example #16
0
        public override Vector2 Steer(ISteeringTarget target)
        {
            if (!target.IsActual) // We've completed the path, need to stop now
            {
                return(-SteeringEntity.Velocity);
            }

            var pathComp = target as PathComponent;

            if (pathComp == null)
            {
                throw new Exception("Incorrect pathing target (use PathComponent)");
            }

            var path = pathComp.Path;

            if (path.NodeCount == 0)
            {
                return(-SteeringEntity.Velocity);
            }

            if (PathFollowingMode == PathFollowingMode.Patrol)
            {
                var targetNode = path[_currentNode];

                if (targetNode != null)
                {
                    /*var distance = (SteeringEntity.Position - targetNode.Target).Length();
                     *
                     * if (distance <= targetNode.TargetRadius)*/
                    if (IsWithinTarget(targetNode))
                    {
                        _currentNode += _pathDir;

                        if (_currentNode >= path.NodeCount || _currentNode < 0)
                        {
                            _pathDir     *= -1;
                            _currentNode += _pathDir;
                        }

                        targetNode = path[_currentNode];
                    }

                    return(_pathFollowingBehavior.Steer((Vector2SteeringTarget)targetNode.Target));
                }
            }
            else if (PathFollowingMode == PathFollowingMode.Circular)
            {
                var targetNode = path[_currentNode % path.NodeCount];

                if (targetNode != null)
                {
                    /*var distance = (SteeringEntity.Position - targetNode.Target).Length();
                     *
                     * if (distance <= targetNode.TargetRadius)*/
                    if (IsWithinTarget(targetNode))
                    {
                        _currentNode++;
                        targetNode = path[_currentNode % path.NodeCount];
                    }

                    return(_pathFollowingBehavior.Steer((Vector2SteeringTarget)targetNode.Target));
                }
            }
            else if (PathFollowingMode == PathFollowingMode.OneWay)
            {
                var targetNode = path.GetTargetNode();
                if (targetNode != null)
                {
                    /*var distance = (target.Position - SteeringEntity.Position).Length();
                     *
                     * if (distance <= targetNode.TargetRadius)*/
                    if (IsWithinTarget(targetNode))
                    {
                        path.RemoveTargetNode();
                    }

                    return(_pathFollowingBehavior.Steer((Vector2SteeringTarget)targetNode.Target));
                }
            }

            return(-SteeringEntity.Velocity);
        }