Exemplo n.º 1
0
        // Update is called once per frame
        void Update()
        {
            for (int b = 0; b < boids.Length; b++)
            {
                BoidData boid = boids[b];
                if (IsHeadingForCollision(boid.position, boid.boundsRadius, boid.forward, boid.collisionAvoidDst, boid.layerMask))
                {
                    boid.collAvoidDir = boid.forward;
                }

                boids[b] = boid;
            }
        }
Exemplo n.º 2
0
        public void Execute(int index)
        {
            int numFlockMates = 0;

            BoidData boida = boids[index];

            for (int i = 0; i < boids.Length; i++)
            {
                if (index != i)
                {
                    BoidData boidb = boids[i];

                    float3 offset = boidb.position - boida.position;
                    float  sqrDst = offset.x * offset.x + offset.y * offset.y + offset.z * offset.z;

                    if (sqrDst < viewRadius * viewRadius)
                    {
                        numFlockMates++;
                        boida.flockHeading += boidb.direction;
                        boida.flockCenter  += boidb.position;

                        if (sqrDst < avoidRadius * avoidRadius)
                        {
                            boida.separationHeading -= offset / sqrDst;
                        }
                    }
                }
            }

            float3 acceleration = float3.zero;

            if (numFlockMates > 0)
            {
                boida.flockCenter /= numFlockMates;

                float3 offsetToFlockmatesCentre = (boida.flockCenter - boida.position);

                var alignmentForce  = SteerTowards(boida.flockHeading, boida.velocity) * alignWeight;
                var cohesionForce   = SteerTowards(offsetToFlockmatesCentre, boida.velocity) * cohesionWeight;
                var seperationForce = SteerTowards(boida.separationHeading, boida.velocity) * seperateWeight;

                acceleration += alignmentForce;
                acceleration += cohesionForce;
                acceleration += seperationForce;
            }

            boida.acceleration = acceleration;

            boids[index] = boida;
        }