예제 #1
0
    // Cohesion is a behavior that causes agents to steer towards a "center of mass"
    Vector3 Cohesion(PedestrianBehavior agent)
    {
        Vector3 forceVector    = Vector3.zero;
        Vector3 centerOfMass   = Vector3.zero;
        uint    neighbourCount = 0;

        List <GameObject> neighbours = agent.GetNeighbours();

        for (int i = 0; i < neighbours.Count; i++)
        {
            // Discard self checking
            if (neighbours[i] != agent.gameObject)
            {
                centerOfMass += neighbours[i].transform.position;
                neighbourCount++;
            }
        }

        if (neighbourCount > 0)
        {
            centerOfMass /= neighbourCount;

            centerOfMas = centerOfMass;

            forceVector = centerOfMass - agent.gameObject.transform.position;
            forceVector.Normalize();
        }
        return(forceVector);
    }
예제 #2
0
    Vector3 Separation(PedestrianBehavior agent)
    {
        Vector3 forceVector = Vector3.zero;

        List <GameObject> neighbours = agent.GetNeighbours();

        for (int i = 0; i < neighbours.Count; i++)
        {
            // Discard self checking
            if (neighbours[i] != agent.gameObject)
            {
                // Distance squared
                float distance = Mathf.Pow(Vector3.Distance(neighbours[i].transform.position, agent.transform.position), 2.0f);
                if (distance < SeparationDistance)
                {
                    // Calculate the heading vector between the source entity and its neighbour
                    Vector3 headingVector = agent.transform.position - neighbours[i].transform.position;

                    // Calculate the scale value
                    float scale = headingVector.magnitude / (float)Mathf.Sqrt(SeparationDistance);

                    //The closer we are the more intense the force vector will be
                    forceVector = Vector3.Normalize(headingVector) / scale;
                }
            }
        }
        return(forceVector);
    }
예제 #3
0
    // Alignment is a behavior that causes a particular agent to line up with agents close by
    Vector3 Alignment(PedestrianBehavior agent)
    {
        Vector3 forceVector     = Vector3.zero;
        int     neighboursCount = 0;

        List <GameObject> neighbours = agent.GetNeighbours();

        for (int i = 0; i < neighbours.Count; i++)
        {
            // Discard self checking
            if (neighbours[i] != agent.gameObject)
            {
                forceVector += neighbours[i].GetComponent <PedestrianBehavior>().Velocity;
                neighboursCount++;
            }
        }

        if (neighboursCount > 0)
        {
            forceVector /= neighboursCount;
            forceVector.Normalize();
        }

        return(forceVector);
    }
예제 #4
0
    public List <GameObject> GetNeighbours()
    {
        if (IsLeader())
        {
            return(mNeighbours);
        }

        else if (GetLeader() != null)
        {
            PedestrianBehavior leader = GetLeader().GetComponent <PedestrianBehavior>();
            return(leader.GetNeighbours());
        }

        return(null);
    }