示例#1
0
        public static Vector2 SteerForce(Vector2 target, AutonomousAgent2D agent)
        {
            Vector2 agentPosition = agent.transform.position;
            Vector2 desired       = (target - agentPosition).normalized * agent.maxSpeed;

            return(desired - agent.rigid.velocity);
        }
示例#2
0
        public static Vector2 SteerForce(AutonomousAgent2D agent, EvadeSensor2D sensor)
        {
            // Nothing setup to avoid.
            if (sensor.avoid == null)
            {
                return(Vector2.zero);
            }

            Vector2 targetPosition = sensor.avoid.position;
            Vector2 targetVelocity = sensor.avoid.velocity;

            Vector2 toAvoider       = targetPosition - (Vector2)agent.transform.position;
            float   distanceToAvoid = toAvoider.magnitude;

            if (sensor.ShouldAvoid(distanceToAvoid))
            {
                float   lookAheadTime = distanceToAvoid / (agent.maxSpeed + targetVelocity.magnitude);
                Vector2 fleePosition  = targetPosition + targetVelocity * lookAheadTime;

                return(Flee2D.SteerForce(agent, fleePosition));
            }

            // Too far too evade
            else
            {
                return(Vector2.zero);
            }
        }
示例#3
0
        public static Vector2 SteerForce(AutonomousAgent2D agent, Collider2D[] colliders, int totalCount, float detectionRadius)
        {
            Vector2 forwardsSum   = Vector2.zero;
            Vector2 agentPosition = agent.transform.position;
            Vector2 agentForward  = agent.ForwardDirection;

            int   count    = 0;
            float radiusSq = detectionRadius * detectionRadius;

            for (int i = 0; i < totalCount; ++i)
            {
                var other = colliders[i].GetComponent <AutonomousAgent2D>();

                if (other == agent)
                {
                    continue;
                }

                if (agentPosition.WithinDistanceSq(other.transform.position, radiusSq))
                {
                    count       += 1;
                    forwardsSum += other.ForwardDirection;
                }
            }

            if (count > 0)
            {
                Vector2 averageForward = (forwardsSum / count) - agentForward;
                return(averageForward);
            }

            return(Vector2.zero);
        }
示例#4
0
        private static bool shouldStopCompletely(AutonomousAgent2D agent, ArriveSensor2D sensor)
        {
            var vel    = agent.rigid.velocity;
            var sqStop = sensor.stopSpeed * sensor.stopSpeed;

            return(!vel.IsZero() && vel.sqrMagnitude < sqStop);
        }
示例#5
0
        public static Vector2 SteerForce(AutonomousAgent2D agent, ArriveSensor2D sensor)
        {
            Vector2 desired          = sensor.target - (Vector2)agent.transform.position;
            float   distanceToTarget = desired.magnitude;

            desired.Normalize();

            // Stop completely.
            if (shouldStopSteering(distanceToTarget, sensor))
            {
                if (shouldStopCompletely(agent, sensor))
                {
                    agent.rigid.velocity = Vector2.zero;
                }
                return(Vector2.zero);
            }

            // Slow down.
            else if (shouldSlowDown(distanceToTarget, sensor))
            {
                float speed = agent.maxSpeed * (distanceToTarget / sensor.slowRadius);
                desired *= speed;
            }

            // Full speed.
            else
            {
                desired *= agent.maxSpeed;
            }

            return((desired - agent.rigid.velocity) * (1f / sensor.decelerationFactor));
        }
示例#6
0
        public static Vector2 SteerForce(AutonomousAgent2D agent, Collider2D[] colliders, int totalCount, float detectionRadius)
        {
            Vector2 positionsSum  = Vector2.zero;
            Vector2 steering      = Vector2.zero;
            Vector2 agentPosition = agent.transform.position;

            int   count    = 0;
            float radiusSq = detectionRadius * detectionRadius;

            for (int i = 0; i < totalCount; ++i)
            {
                var other = colliders[i].GetComponent <AutonomousAgent2D>();

                if (other == agent)
                {
                    continue;
                }

                if (agentPosition.WithinDistanceSq(other.transform.position, radiusSq))
                {
                    count        += 1;
                    positionsSum += (Vector2)other.transform.position;
                }
            }

            if (count > 0)
            {
                // The center "mass".
                Vector2 averagePosition = positionsSum / count;
                return(Seek2D.SteerForce(averagePosition, agent));
            }

            return(Vector2.zero);
        }
示例#7
0
        public static void DrawGizmos(Vector2 target, AutonomousAgent2D agent)
        {
            // Draw the arrow pointing to the target position.
            DrawUtil.DrawArrow(agent.transform.position, target, Color.green, 0.8f);

            // Emphasize the target position.
            DrawUtil.DrawCircle(target, 0.15f, Color.green);
        }
示例#8
0
        public static void DrawGizmos(AutonomousAgent2D agent, Vector2 target)
        {
            // Draw the arrow towards the target.
            DrawUtil.DrawArrow(agent.transform.position, target, Color.green, 0.8f, true);

            // Emphasize the fleeing position.
            DrawUtil.DrawCircle(target, 0.15f, Color.green);
        }
示例#9
0
        /// <summary>
        /// If the agent should evade the target rigid.
        /// </summary>
        /// <param name="agent"></param>
        /// <returns></returns>
        public bool ShouldAvoid(AutonomousAgent2D agent)
        {
            if (avoid != null)
            {
                return(ShouldAvoid(Vector2.Distance(agent.transform.position, avoid.position)));
            }

            // If there is nothing to avoid, then no need to avoid.
            return(false);
        }
示例#10
0
        private void cacheAgent()
        {
            if (_agent == null)
            {
                _agent = GetComponent <AutonomousAgent2D>();

                if (_agent == null)
                {
                    throw new UnityException("Autonamous Agent not found!");
                }
            }
        }
示例#11
0
        public static void DrawGizmos(AutonomousAgent2D agent, EvadeSensor2D sensor)
        {
            if (sensor.avoid != null)
            {
                if (sensor.bUseRange)
                {
                    DrawUtil.DrawCircle(agent.transform.position, sensor.avoidRange, Color.red);
                }

                DrawUtil.DrawArrow(agent.transform.position, sensor.avoid.position, Color.green, 1f, true);
            }
        }
示例#12
0
        public static void DrawGizmos(AutonomousAgent2D agent, ArriveSensor2D sensor)
        {
            // Draw the slow radius
            DrawUtil.DrawCircle(agent.transform.position, sensor.slowRadius, Color.green);

            // Draw the stop radius
            DrawUtil.DrawCircle(agent.transform.position, sensor.stopRadius, Color.green);

            // Draw a line between the agent and its target.
            DrawUtil.DrawArrow(agent.transform.position, sensor.target, Color.green, 1f);

            // Draw the target position
            DrawUtil.DrawCircle(sensor.target, 0.15f, Color.green);
        }
示例#13
0
        public static Vector2 SteerForce(AutonomousAgent2D agent, Collider2D[] colliders, int totalCount, float detectionRadius)
        {
            Vector2 steering      = Vector2.zero;
            Vector2 agentPosition = agent.transform.position;
            Vector2 agentVelocity = agent.rigid.velocity;

            int count = 0;

            for (int i = 0; i < totalCount; ++i)
            {
                var other = colliders[i].GetComponent <AutonomousAgent2D>();

                if (other == agent)
                {
                    continue;
                }

                Vector2 toAgent  = agentPosition - (Vector2)other.transform.position;
                float   distance = toAgent.magnitude;

                if (distance < detectionRadius)
                {
                    count += 1;

                    // Force is inversely proportional to the distance to the other agent.
                    steering += toAgent.normalized / distance;
                }
            }

            if (count > 0)
            {
                return((steering.normalized * agent.maxSpeed) - agentVelocity);
            }

            return(Vector2.zero);
        }
示例#14
0
        public static Vector2 SteerForce(AutonomousAgent2D agent, Vector2 target)
        {
            Vector2 desired = ((Vector2)agent.transform.position - target).normalized * agent.maxSpeed;

            return(desired - agent.rigid.velocity);
        }