Exemplo n.º 1
0
    public List <GameObject> GetSeenBoids()
    {
        List <GameObject> seenBoids = new List <GameObject>();

        /*
         * Collider[] boids = Physics.OverlapSphere(rb.transform.position, overlapSphereRadius, LAYER_BOID);
         * int n = (maxSeenBoidsToStore <= 0) ? boids.Length : Mathf.Min(boids.Length, maxSeenBoidsToStore);
         * for (int i = 0; i < n; i++)
         * {
         *  if (boids[i].gameObject != this.gameObject) seenBoids.Add(boids[i].gameObject);
         * }
         */

        //System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();

        List <GameObject> boids = new List <GameObject>();

        if (useFastHashCheck)
        {
            boids = hash.Get(transform.position);
        }
        else
        {
            boids = hash.GetByRadius(transform.position, overlapSphereRadius);
        }

        int n = (maxSeenBoidsToStore <= 0) ? boids.Count : Mathf.Min(boids.Count, maxSeenBoidsToStore);

        for (int i = 0; i < n; i++)
        {
            if (boids[i] != this.gameObject)
            {
                seenBoids.Add(boids[i]);
            }
        }

        //watch.Stop();
        //if(Random.Range(0f, 1f) >= 0.9f) Debug.Log("time to for hash.GetByRadius(): " + watch.ElapsedTicks + " ticks");

        //ADAPTIVE OVERLAP SPHERE: if current pass didn't find enough boids, increase overlap sphere size; if it did, reduce it
        if (useAdaptiveOverlapSphere)
        {
            if (seenBoids.Count < maxSeenBoidsToStore && overlapSphereRadius < maxAdaptiveOverlapRadius)
            {
                overlapSphereRadius += adaptiveOverlapSphereInc;
            }
            else if (overlapSphereRadius > minAdaptiveOverlapRadius)
            {
                overlapSphereRadius -= adaptiveOverlapSphereInc;
            }
        }

        return(seenBoids);
    }
Exemplo n.º 2
0
    public void UpdateSeenBoids()
    {
        SeenBoids.Clear();

        //System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();

        List <GameObject> boids = new List <GameObject>();

        if (useFastHashCheck)
        {
            boids = hash.GetNRandom(transform.position, maxSeenBoidsToStore);
        }
        else
        {
            //boids = hash.GetNByRadius(transform.position, overlapSphereRadius, maxSeenBoidsToStore);
            boids = hash.GetByRadius(transform.position, overlapSphereRadius);
        }

        foreach (GameObject boid in boids)
        {
            if (boid != this.gameObject && IsWithinVisionAngle(boid.transform.position))
            {
                SeenBoids.Add(boid);
            }
        }

        /*
         * int n = (maxSeenBoidsToStore <= 0) ? boids.Count : Mathf.Min(boids.Count, maxSeenBoidsToStore);
         * for (int i = 0; i < n; i++)
         * {
         *  if (boids[i] != this.gameObject && IsWithinVisionAngle(boids[i].transform.position))
         *  {
         *      SeenBoids.Add(boids[i]);
         *  }
         * }
         */

        //watch.Stop();
        //if(Random.Range(0f, 1f) >= 0.9f) Debug.Log("time to get seen boids (fast hash check = " + useFastHashCheck + "): " + watch.ElapsedMilliseconds + " ms");

        //ADAPTIVE OVERLAP SPHERE: if current pass didn't find enough boids, increase overlap sphere size; if it did, reduce it
        if (useAdaptiveOverlapSphere)
        {
            if (SeenBoids.Count < maxSeenBoidsToStore && overlapSphereRadius < maxAdaptiveOverlapRadius)
            {
                overlapSphereRadius += adaptiveOverlapSphereInc;
            }
            else if (overlapSphereRadius > minAdaptiveOverlapRadius)
            {
                overlapSphereRadius -= adaptiveOverlapSphereInc;
            }
        }
    }