Esempio n. 1
0
    public override Vector2 CalculateLeadMove(FlockAgent_Follower agent,
                                              List <Transform> context,
                                              FlockWithLeader flock,
                                              Leader_Boidsynth07 leader)
    {
        /* If no neighbors maintain current alignment (heading)
         * In this case, if no neighbors are found, the agent will move forward
         * on its current heading. */
        if (leader != null)
        {
            return(Vector2.zero);
        }

        // get the average alignment of neighbors and face that direction
        Vector2 alignmentMove = Vector2.zero;

        foreach (Transform item in context)
        {
            alignmentMove += (Vector2)item.transform.up;
        }
        alignmentMove /= context.Count;

        // alignmentMove = Vector2.SmoothDamp(
        //      agent.transform.up,
        //      alignmentMove,
        //      ref agent.currentVelocity,
        //      agent.agentSmoothTime
        //      );

        return(alignmentMove);
        //return Seek(agent, agent.transform.position,alignmentMove);
    }
Esempio n. 2
0
    // Use this for initialization
    void Start()
    {
        // squareMaxSpeed = maxSpeed * maxSpeed;
        // squareNeighborRadius = neighborRadius * neighborRadius;
        // squareAvoidanceRadius = squareNeighborRadius * avoidanceRadiusMultiplier * avoidanceRadiusMultiplier;
        volumeScalar = 1 / startingCount;

        // Populate the scene with flock agents
        for (int i = 0; i < startingCount; i++)
        {
            Leader_Boidsynth07 newAgent = Instantiate(
                agentPrefab,
                Random.insideUnitCircle * 3,
                Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)),
                transform
                );
            newAgent.name = "Agent " + i;
            newAgent.GetComponent <AudioSource>().volume = 1.0f / startingCount;

            // agents.Add(newAgent);
        }

        // foreach (FlockAgent agent in agents)
        // {
        //  Debug.Log(1/startingCount);
        //  agent.audioSource.volume = 1/startingCount;
        // }
    }
Esempio n. 3
0
    public override Vector2 CalculateLeadMove(FlockAgent_Follower agent,
                                              List <Transform> context,
                                              FlockWithLeader flock,
                                              Leader_Boidsynth07 leader)
    {
        /* If we don't find any neighbors then make no adjustments.
         * That means to throw a vector with no magnitude. */
        if (context.Count == 0)
        {
            return(Vector2.zero);
        }

        // Add all points together and average. (trying to find a point in the middle of the group)
        Vector2 avoidanceMove = Vector2.zero;

        // How many agents are in our avoidance radius
        int nAvoid = 0;

        //go through each transform
        foreach (Transform item in context)
        {
            // calculate if the transform is within our avoidance radius
            if (Vector2.SqrMagnitude(item.position - agent.transform.position) < flock.SquareAvoidanceRadius)
            {
                nAvoid++;
                avoidanceMove += (Vector2)(agent.transform.position - item.position);
            }
        }
        if (nAvoid > 0)
        {
            avoidanceMove /= nAvoid;
        }

        return(avoidanceMove);
    }
Esempio n. 4
0
    // Use this for initialization
    void Start()
    {
        squareMaxSpeed        = maxSpeed * maxSpeed;
        squareNeighborRadius  = neighborRadius * neighborRadius;
        squareAvoidanceRadius = squareNeighborRadius * avoidanceRadiusMultiplier * avoidanceRadiusMultiplier;

        //Imstantiate the leader
        leader = Instantiate(
            leaderPrefab,
            Random.insideUnitCircle * 3,
            Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)),
            transform
            );

        // Populate the scene with flock agents
        for (int i = 0; i < startingCount; i++)
        {
            FlockAgent_Follower newAgent = Instantiate(
                agentPrefab,
                Random.insideUnitCircle * 3,
                Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)),
                transform
                );
            newAgent.name     = "Agent " + i;
            newAgent.noteMode = noteMode;
            newAgent.GetComponent <AudioSource>().volume = 1.0f / startingCount;


            agents.Add(newAgent);
        }
    }
    public override Vector2 CalculateLeadMove(
        FlockAgent_Follower agent,
        List <Transform> context,
        FlockWithLeader flock,
        Leader_Boidsynth07 leader)
    {
// Check if the amount of weights and behaviors is the same
        if (weights.Length != behaviors.Length)
        {
            // Throw and error and don't move
            Debug.LogError("Data mismatch in " + name, this);
            return(Vector2.zero);
        }

        // Set up Move
        Vector2 move = Vector2.zero;

        // Iterate through the behaviors
        for (int i = 0; i < behaviors.Length; i++)
        {
            Vector2 partialMove = behaviors[i].CalculateLeadMove(agent, context, flock, leader) * weights[i];

            // if movement is not zero
            if (partialMove != Vector2.zero)
            {
                if (partialMove.sqrMagnitude > weights[i] * weights[i])
                {
                    // normalize to a magnitude of 1 and multiply it by the weights
                    partialMove.Normalize();
                    partialMove *= weights[i];
                }

                move += partialMove;
            }
        }

        return(move);
    }
 void Awake()
 {
     synth  = this.GetComponentInParent <Hv_Boidsynth07_AudioLib>();
     agent  = this.GetComponentInParent <Leader_Boidsynth07>();
     angles = this.GetComponentInParent <AngleDetection>();
 }
Esempio n. 7
0
    public override Vector2 CalculateLeadMove(FlockAgent_Follower agent, List <Transform> context, FlockWithLeader flock, Leader_Boidsynth07 leader)
    {
        if (leader != null)
        {
            leaderPos = leader.transform.position;
            /* First let's face the agent towards the mouse */

            /*  subtract the position of agent from mouse position to get vector pointing from
             * the agent to the mouse */

            Vector2 vectorToLeader = new Vector2(
                leaderPos.x - agent.transform.position.x,
                leaderPos.y - agent.transform.position.y
                );

            agent.dist = vectorToLeader.magnitude;

            //Draw a line to mouse position
            Debug.DrawLine(
                agent.transform.position,
                leaderPos,
                Color.white,
                0.0f,
                true
                );

            return(vectorToLeader);
        }
        else
        {
            Debug.Log("Leader not found");
            return(Vector2.zero);
        }
    }
 public abstract Vector2 CalculateLeadMove(
     FlockAgent_Follower agent,
     List <Transform> context,
     FlockWithLeader flock,
     Leader_Boidsynth07 leader);