Пример #1
0
    void Evaluate()
        {
            duration -= Engine.timeStep;
            if (duration <= 0) ResetStrategy();

            bool targetIsGroup = false;
            int j = -1; // id del agente o grupo con el ttc mas bajo
            min_ttc = Mathf.Infinity;

            foreach(var key in ttc.Keys) {
                if (ttc[key] < min_ttc)
                {
                    j = key;
                    min_ttc = ttc[key];
                }
            }

            foreach (var key in group_ttc.Keys)
            {
                if (group_ttc[key] < min_ttc)
                {
                    j = key;
                    min_ttc = group_ttc[key];
                    targetIsGroup = true;
                }
            }

            if (j != -1 && min_ttc < timeHorizon)
            {
                if (!targetIsGroup) neighbor = Engine.Instance.GetAgent(j);
                else neighbor = Engine.Instance.GetVirtualAgent(j);
                ApplyStrategy();
            }
            else { velocity = Behaviours.GetSteering(position, goal, prefSpeed); }
        }
Пример #2
0
        public bool DoStep()
        {
            Vector2 dir = goal - position;
            float distSqToGoal = dir.sqrMagnitude;

            if (distSqToGoal <= goalRadius * goalRadius)
            {
                velocity = Vector2.zero;
                return true;
            }

            velocity = Behaviours.GetSteering(position, goal, prefSpeed);

            ttc.Clear();
            DetectingNeighbors(neighborDist, dir, viewAngle);
            DetectingNeighbors(personalSpace, -dir, 360 - viewAngle);

            if (useGroups) DetectingGroups();
            Evaluate();

            // Limit velocity to prefSpeed of agent
            if (velocity.sqrMagnitude > (prefSpeed * prefSpeed))
            {
                velocity.Normalize();
                velocity *= prefSpeed;
            }

            position += velocity * Engine.timeStep;
            UpdateDB();

            MoveInRealWorld();
            return false;
        }
Пример #3
0
        void DetermineStrategy(int[] s, bool frontal2 = false)
        {
            bool sameStrategy = false;
            foreach (var ss in s)
            {
                if (curStrategy == (strategies)ss)
                {
                    sameStrategy = true;
                    break;
                }
            }

            if (!sameStrategy && System.Array.IndexOf(s, curStrategy) == -1)
            {
                ResetStrategy();
                int i = Random.Range(0, s.Length);
                curStrategy = (strategies)s[i];
                if (debugLog) Debug.Log(id + " , The current strategy is: " + curStrategy);
            }

            switch (curStrategy)
            {
                case strategies.DCC:
                    velocity = Behaviours.GetSteering(position, goal, prefSpeed);
                    velocity = Behaviours.DecelerationStrategy(min_ttc, velocity);
                    break;

                case strategies.CH:
                    int turnTo = TurnTo;
                    velocity = Behaviours.GetSteering(position, goal, prefSpeed);
                    velocity = Behaviours.ChangeDirectionStrategy((velocity,
                        neighbor.position - position, min_ttc, timeHorizon, neighbor.TurnTo, out turnTo, frontal2);
                    TurnTo = turnTo;
                    break;

                case strategies.F:
                    velocity = Behaviours.GetSteering(position, goal, prefSpeed);
                    velocity = Behaviours.FollowStrategy(radius, prefSpeed, position, 
                        velocity, neighbor.position, neighbor.velocity);
                    break;

                case strategies.A:
                    velocity += Behaviours.CollisionAvoidance(position, velocity, 
                    Behaviours.GetSteering(position, goal, prefSpeed), timeHorizon, ttc, group_ttc);
                    break;

                case strategies.N:
                    velocity = Behaviours.GetSteering(position, goal, prefSpeed);
                    break;
            }


            if (debugLog) Debug.DrawRay(new Vector3(position.x, 1.5f, position.y),
                new Vector3(velocity.x, 0, velocity.y), Color.green);
        }