예제 #1
0
    ////////////////////////   LEADER WALKING METHODS  ////////////////////////

    // RandomWalk: Leader walks around randomly
    public void RandomWalk(List <Vector3> listPositions, float velocity, float minDistance, float AreaMin, float AreaMax)
    {
        Vector3 leaderPosition  = leader.transform.position;
        Vector3 randomDirection = CM.RandomVector(velocity);
        Vector3 newPosition     = leaderPosition + randomDirection;

        if (!CM.OutsideBoundaries(newPosition, AreaMin, AreaMax) &&
            !CM.WillCollide(minDistance, leaderPosition, newPosition, listPositions))
        {
            leader.transform.Translate(randomDirection);
            this.Position = newPosition;
        }
        else if (CM.OutsideBoundaries(newPosition, AreaMin, AreaMax))
        {
            Vector3 back = CM.BackToBoundaries(leaderPosition, velocity, AreaMin, AreaMax);
            leader.transform.Translate(back);
            this.Position = leaderPosition + back;
        }
        else
        {
            Vector3 colidingAgent = CM.ClosestAgent(leaderPosition, listPositions);
            Vector3 awayFromAgent = leaderPosition - colidingAgent;

            Vector3 correctiveVector = (awayFromAgent).normalized * velocity;
            leader.transform.Translate(correctiveVector);
            this.Position = leaderPosition + correctiveVector;
        }
    }
예제 #2
0
    ////////////////////////   MESSAGE PROPAGATION  ////////////////////////

    //MessagePropagation: Agents stop when they enter the area of Influence of a transmitting agent and become transmitters themselves.
    //                    Communication simulated using blinking colors
    IEnumerator MessagePropagation()
    {
        bool toggle = true;

        while (true)
        {
            toggle = !toggle;
            for (int i = 0; i < listAgents.Count; i++)
            {
                if (listAgents[i].tag == "Moving")
                {
                    Vector3        agentPosition  = listAgents[i].transform.position;
                    Vector3        newDirection   = CM.PerlinVector(steeringForces[i], agentPosition, velocity);
                    Vector3        newPosition    = agentPosition + newDirection;
                    List <Vector3> agentPositions = listAgents.Select(a => a.transform.position).ToList();

                    if (CM.Colliding(AreaInfluence, agentPosition, staticAgents))
                    {
                        listAgents[i].GetComponent <Renderer>().material = blueGlowMaterial;
                        listAgents[i].tag = "Transmitting";
                        staticAgents.Add(agentPosition);
                    }
                    else
                    if (!CM.WillCollide(minDistance, agentPosition, newPosition, agentPositions) &&
                        !CM.OutsideBoundaries(newPosition, AreaMin, AreaMax))
                    {
                        listAgents[i].transform.Translate(newDirection);
                        steeringForces[i] = newDirection;
                    }
                    else if (CM.OutsideBoundaries(newPosition, AreaMin, AreaMax))
                    {
                        Vector3 correctiveVector = newDirection * 0.2f + CM.BackToBoundaries(agentPosition, newDirection, velocity, AreaMin, AreaMax) * 0.8f;    // Use repellingForce too?
                        listAgents[i].transform.Translate(correctiveVector);
                        steeringForces[i] = correctiveVector;
                    }
                    else
                    {
                        Vector3 colidingAgent           = CM.ClosestAgent(agentPosition, agentPositions);
                        float   distanceToColidingAgent = Vector3.Distance(newPosition, colidingAgent);
                        Vector3 perpendicularToAgent    = CM.AvoidObstacle(agentPosition, colidingAgent, newDirection, velocity);
                        float   repellingForce          = distanceToColidingAgent / AreaInfluence;

                        Vector3 correctiveVector = newDirection * repellingForce + perpendicularToAgent * (1 - repellingForce);
                        listAgents[i].transform.Translate(correctiveVector);
                        steeringForces[i] = correctiveVector;
                    }
                }
                if (listAgents[i].tag == "Transmitting")
                {
                    var renderer = listAgents[i].GetComponent <Renderer>();
                    renderer.material = toggle ? redGlowMaterial : blueGlowMaterial;
                }
            }
            yield return(new WaitForSeconds(0.1f));
        }
    }