コード例 #1
0
ファイル: Flock.cs プロジェクト: niteshbhatia008/flock-test
    Vector3 flockForces()
    {
        Vector3 sep      = Vector3.zero;
        Vector3 ali      = Vector3.zero;
        Vector3 coh      = Vector3.zero;
        int     visCount = 0;
        int     sepCount = 0;

        for (int i = 0; i < fc.flockSize(); i++)
        {
            Flock boid = fc.getBoid(i);
            if (boid && boid != this)
            {
                float dist = Vector3.Distance(transform.position, boid.transform.position);
                if (dist < SEP_DIST)                                                             //The separation force does not take into account view angle
                {
                    sep += (transform.position - boid.transform.position) / Mathf.Pow(dist, 2f); //Force becomes exponentially larger as we get closer to the other Flock
                    sepCount++;
                }
                if (canSee(boid))
                {
                    ali += boid.velocity;                     //Point us to the average velocity
                    coh += boid.transform.position;           //Find the average position
                    visCount++;
                }
            }
        }
        if (sepCount != 0)          //Avoid division by zero when we are alone
        {
            sep /= sepCount;
        }
        if (visCount != 0)          //The count given by the separation check may be different than this check
        {
            coh /= visCount;
            coh  = coh - transform.position;            //Now we have the average position, but we need to convert it to a relative vector

            ali /= visCount;
        }
        return(sep * 3f + ali * .8f + coh * .5f);       //Return the sum of our inter-bird forces, weighted
    }
コード例 #2
0
    private Vector3 hunt()       //Hunt down the prey
    {
        Vector3 force = Vector3.zero;

        for (int i = 0; i < fc.flockSize(); i++)
        {
            if (fc.getBoid(i))
            {
                Vector3 delta = fc.getBoid(i).transform.position - transform.position;
                float   dist  = Vector3.Distance(fc.getBoid(i).transform.position, transform.position);
                float   B     = 160f;
                float   m     = 2f;

                float magnitude = (B / Mathf.Pow(dist, m));
                if (printInfo)
                {
                    Debug.DrawRay(transform.position, delta.normalized * magnitude);
                }
                force += delta.normalized * magnitude;
            }
        }
        return(force);
    }