示例#1
0
    //Finds the middle point between neighbours and tries to move there
    public override Vector3 CalculateMove(FlockAgent3D agent, List <Transform> context, Flock3D flock)
    {
        //If no neighbors return no adjustment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        //Add all points together and avarage
        Vector3          cohesionMove  = Vector3.zero;
        List <Transform> fiteredContex = (filter == null) ? context : filter.Filter(agent, context);

        foreach (Transform item in fiteredContex)
        {
            cohesionMove += item.position;
        }

        cohesionMove /= context.Count;



        //create offset from agent position
        cohesionMove -= (Vector3)agent.transform.position;

        return(cohesionMove);
    }
    public override Vector3 CalculateMove(FlockAgent3D agent, List <Transform> context, Flock3D flock)
    {
        //How far is the agent from the center and steer towards th center
        Vector3 centerOffset = center - agent.transform.position;

        //tells us where we are, if t i 0 we are at the center if t is 1 we are at the end of the radius.
        float t = centerOffset.magnitude / (radius * 3.14f);

        //if we are iside the 0.9 radius let the agent continue their journey
        if (t < 0.9f)
        {
            return(Vector3.zero);
        }


        return(centerOffset * t * t / radius);
    }
示例#3
0
    public override List <Transform> Filter(FlockAgent3D agent, List <Transform> original)
    {
        List <Transform> Filtered = new List <Transform>();



        foreach (Transform item in original)
        {
            //if the item is on one of the mask layers.
            if (mask == (mask | (1 << item.gameObject.layer)))
            {
                Filtered.Add(item);
            }
        }

        return(Filtered);
    }
示例#4
0
    List <Transform> GetNearbyObjects(FlockAgent3D agent)
    {
        List <Transform> context = new List <Transform>();

        //gets all nearby objects inside the a circle of the agent. To do this in 3d Use collider 3d and a overlapSphereall
        Collider[] contextCollider = Physics.OverlapSphere(agent.transform.position, neighborRadius);

        //Put all the colliders found inside the circle and put itside the list
        foreach (Collider c in contextCollider)
        {
            //if the collider isnt our own collider
            if (c != agent.AgentCollider)
            {
                //adds the transform of the collider found inside the neighborRadius
                context.Add(c.transform);
            }
        }
        return(context);
    }
示例#5
0
    public override List <Transform> Filter(FlockAgent3D agent, List <Transform> original)
    {
        List <Transform> Filtered = new List <Transform>();



        foreach (Transform item in original)
        {
            FlockAgent3D itemAgent = item.GetComponent <FlockAgent3D>();


            //If there are a flock component attached and they are in the same flock then add it to the fiterd list
            if (itemAgent != null && itemAgent.AgentFlock3D == agent.AgentFlock3D)
            {
                Filtered.Add(item);
            }
        }

        return(Filtered);
    }
示例#6
0
    //Finds the middle point between neighbours and tries to move there
    public override Vector3 CalculateMove(FlockAgent3D agent, List <Transform> context, Flock3D flock)
    {
        //If no neighbors, maintain current heading
        if (context.Count == 0)
        {
            return(agent.transform.up);
        }

        //Add all points together and avarage
        Vector3          AlignmentMove = Vector3.zero;
        List <Transform> fiteredContex = (filter == null) ? context : filter.Filter(agent, context);

        foreach (Transform item in fiteredContex)
        {
            AlignmentMove += item.transform.up;
        }

        //returns a normalized value
        AlignmentMove /= context.Count;

        return(AlignmentMove);
    }
示例#7
0
    // Start is called before the first frame update
    void Start()
    {
        squareMaxSpeed        = maxSpeed * maxSpeed;
        squareNeighborRadius  = neighborRadius * neighborRadius;
        squareAvoidanceRadius = squareNeighborRadius * avoidanceRadiusMultiplier * avoidanceRadiusMultiplier;


        for (int i = 0; i < startingCount; i++)
        {
            FlockAgent3D NewAgent = Instantiate(
                agentPrefab,
                (Random.insideUnitSphere * startingCount * AgentDenstity),
                Quaternion.Euler(Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360)),
                transform

                );
            NewAgent.name = "Agent" + i;

            //Now it knows what flock it belong to
            NewAgent.initialize(this);
            agents3D.Add(NewAgent);
        }
    }
    //Finds the middle point between neighbours and tries to move there
    public override Vector3 CalculateMove(FlockAgent3D agent, List <Transform> context, Flock3D flock)
    {
        //If no neighbors return no adjustment
        if (context.Count == 0)
        {
            return(Vector3.zero);
        }

        //Add all points together and avarage
        Vector3 AvoidanceMove = Vector3.zero;

        //How many inside the avodance radius.
        int nAvoid = 0;
        List <Transform> fiteredContex = (filter == null) ? context : filter.Filter(agent, context);

        foreach (Transform item in fiteredContex)
        {
            Vector3 closestPoint = item.gameObject.GetComponent <Collider>().ClosestPoint(agent.transform.position);

            //squared distance between the item and the agent
            if (Vector3.SqrMagnitude(closestPoint - agent.transform.position) < flock.SquareAvoidanceRadius)
            {
                //gives the offset
                AvoidanceMove += (agent.transform.position - closestPoint);

                //adds
                nAvoid++;
            }
        }

        if (nAvoid > 0)
        {
            AvoidanceMove /= nAvoid;
        }

        return(AvoidanceMove);
    }
    public override Vector3 CalculateMove(FlockAgent3D agent, List <Transform> context, Flock3D flock)
    {
        //Handle data missmatch
        if (weights.Length != behaviours.Length)
        {
            Debug.Log("Error: Data missmactch in" + name, this);
            return(Vector3.zero);
        }

        // setup Move
        Vector3 move = Vector3.zero;

        //iterate through behaviours
        for (int i = 0; i < behaviours.Length; i++)
        {
            //middle man passing through the different beaviours
            Vector3 partialMove = behaviours[i].CalculateMove(agent, context, flock) * weights[i];


            //is some movement being returned?
            if (partialMove != Vector3.zero)
            {
                //does this movent exceed the wieight?
                if (partialMove.sqrMagnitude > weights[i] * weights[i])
                {
                    //if it does, normalize it back to magnitude of 1 and multiply it by the weigth so it set perfectly at the maximum of the weight
                    partialMove.Normalize();
                    partialMove *= weights[i];
                }

                move += partialMove;
            }
        }

        return(move);
    }
示例#10
0
 public abstract Vector3 CalculateMove(FlockAgent3D agent3D, List <Transform> context3D, Flock3D flock3D);
示例#11
0
 public abstract List <Transform> Filter(FlockAgent3D agent, List <Transform> original);