Ejemplo n.º 1
0
        public static EmergentAgent NewAgent(EmergentBehaviour behaviour)
        {
            EmergentAgent a = new EmergentAgent();

            a.currentBehaviour = behaviour;
            a.position         = Vector3.zero;
            a.direction        = Vector3.zero;
            return(a);
        }
        public override void UpdateAgent(EmergentAgent a)
        {
            List <EmergentAgent> ns = _group.GetNeighbours(a, radiusOfNeighbourhood);


            if (ns.Count > minGroupSize)
            {
                universalCohesion = Mathf.Sin(Time.time) + universalCohesionBias;

                Vector3 groupAlignment  = Vector3.zero;
                Vector3 groupCenter     = _group.transform.position;
                Vector3 groupSeparation = default(Vector3);
                Vector3 groupCohesion   = default(Vector3);


                for (int i = 0; i < ns.Count; i++)
                {
                    groupAlignment  += ns[i].direction;
                    groupCenter     += ns[i].position;
                    groupSeparation += CalcSeparation(a, ns[i]);
                }

                //Alignment: Average all neighbours direction
                groupAlignment /= ns.Count;
                groupAlignment.Normalize();


                //Cohesion: Direction towards the center of mass
                groupCenter  /= ns.Count;
                groupCenter  += (_group.transform.position - groupCenter) * universalCohesion;
                groupCohesion = (groupCenter - a.position).normalized;

                a.direction +=
                    groupAlignment * Time.deltaTime * alignmentWeight
                    + groupCohesion * Time.deltaTime * cohesionWeight
                    + groupSeparation * Time.deltaTime * separationWeight;

                a.direction.Normalize();
            }
            else
            {
                //if we don't have enough friends then make a sad and introspective journey towards the center of the universe..
                a.direction = (_group.transform.position - a.position).normalized;
            }


            a.position += a.direction * a.speedMetersPerSecond * Time.deltaTime;

            if (a.transform != null)
            {
                a.transform.position = a.position;
            }
        }
        Vector3 CalcSeparation(EmergentAgent agent, EmergentAgent other)
        {
            Vector3 heading = agent.position - other.position;

            if (heading.magnitude < separationDistance)
            {
                float scale = heading.magnitude / separationDistance;
                return(heading.normalized / scale);
            }
            else
            {
                return(Vector3.zero);
            }
        }
        void CreateAgents(float speedmps, EmergentBehaviour behaviour)
        {
            _agents = new List <EmergentAgent>();
            for (int i = 0; i < numberOfAgents; i++)
            {
                Vector3       d = UnityEngine.Random.onUnitSphere;
                Vector3       p = transform.position + UnityEngine.Random.insideUnitSphere * startRadiusOfAllAgents;
                EmergentAgent a = EmergentAgent.NewAgent(behaviour, p, d, agentSpeed, agentPrefab);


                a.transform.SetParent(this.transform);
                _agents.Add(a);
            }
        }
Ejemplo n.º 5
0
        public static EmergentAgent NewAgent(EmergentBehaviour behaviour, Vector3 position, Vector3 direction, float speedMetersPerSecond, GameObject prefab)
        {
            EmergentAgent a = new EmergentAgent();

            a.currentBehaviour = behaviour;

            GameObject g = GameObject.Instantiate <GameObject>(prefab, position, Quaternion.identity);

            a.transform            = g.transform;
            a.position             = position;
            a.direction            = direction;
            a.speedMetersPerSecond = speedMetersPerSecond;


            return(a);
        }
        public List <EmergentAgent> GetNeighbours(EmergentAgent agent, float neighbourhoodRadius)
        {
            List <EmergentAgent> ns = new List <EmergentAgent>();

            for (int i = 0; i < _agents.Count; i++)
            {
                if (agent == _agents[i])
                {
                    continue;
                }
                if ((agent.position - _agents[i].position).magnitude <= neighbourhoodRadius)
                {
                    ns.Add(_agents[i]);
                }
            }
            return(ns);
        }
 public abstract void UpdateAgent(EmergentAgent a);