예제 #1
0
        public void Execute(ArchetypeChunk batchInChunk, int batchIndex)
        {
            var wanderChunk       = batchInChunk.GetNativeArray(wanderTypeHandle);
            var boidChunk         = batchInChunk.GetNativeArray(boidTypeHandle);
            var translationsChunk = batchInChunk.GetNativeArray(translationTypeHandle);
            var rotationsChunk    = batchInChunk.GetNativeArray(rotationTypeHandle);
            var seperationChunk   = batchInChunk.GetNativeArray(seperationTypeHandle);
            var cohesionChunk     = batchInChunk.GetNativeArray(cohesionTypeHandle);
            var alignmentChunk    = batchInChunk.GetNativeArray(alignmentTypeHandle);
            var constrainChunk    = batchInChunk.GetNativeArray(constrainTypeHandle);
            var oaChunk           = batchInChunk.GetNativeArray(obstacleTypeHandle);

            for (int i = 0; i < batchInChunk.Count; i++)
            {
                Wander            w   = wanderChunk[i];
                Translation       p   = translationsChunk[i];
                Rotation          r   = rotationsChunk[i];
                Boid              b   = boidChunk[i];
                Seperation        s   = seperationChunk[i];
                Cohesion          c   = cohesionChunk[i];
                Alignment         a   = alignmentChunk[i];
                Constrain         con = constrainChunk[i];
                ObstacleAvoidance oa  = oaChunk[i];

                b.force = AccululateForces(ref b, ref oa, ref s, ref a, ref c, ref w, ref con) * b.weight;

                b.force = Vector3.ClampMagnitude(b.force, b.maxForce);
                Vector3 newAcceleration = (b.force * b.weight) * (1.0f / b.mass);
                newAcceleration.y *= limitUpAndDown;
                b.acceleration     = Vector3.Lerp(b.acceleration, newAcceleration, dT);
                b.velocity        += b.acceleration * dT;
                b.velocity         = Vector3.ClampMagnitude(b.velocity, b.maxSpeed);

                float speed = b.velocity.magnitude;
                speeds[b.boidId] = speed;

                if (speed > 0)
                {
                    Vector3 tempUp = Vector3.Lerp(b.up, (Vector3.up) + (b.acceleration * banking), dT * 3.0f);
                    rotations[b.boidId] = Quaternion.LookRotation(b.velocity, tempUp);
                    b.up = rotations[b.boidId] * Vector3.up;

                    positions[b.boidId] += b.velocity * dT;
                    b.velocity          *= (1.0f - (damping * dT));
                }
                b.force      = Vector3.zero;
                boidChunk[i] = b;
            }
        }
예제 #2
0
        public void Execute(ArchetypeChunk batchInChunk, int batchIndex)
        {
            var boidChunk         = batchInChunk.GetNativeArray(boidTypeHandle);
            var translationsChunk = batchInChunk.GetNativeArray(translationTypeHandle);
            var seperationChunk   = batchInChunk.GetNativeArray(seperationTypeHandle);

            for (int i = 0; i < batchInChunk.Count; i++)
            {
                Vector3     force = Vector3.zero;
                Translation p     = translationsChunk[i];
                Seperation  s     = seperationChunk[i];
                Boid        b     = boidChunk[i];

                for (int j = 0; j < b.taggedCount; j++)
                {
                    int neighbourStartIndex = maxNeighbours * b.boidId;

                    Vector3 myPosition = positions[b.boidId];


                    int neighbourId = neighbours[neighbourStartIndex + j];
                    if (neighbourId == b.boidId)
                    {
                        continue;
                    }
                    Vector3 toNeighbour = positions[b.boidId] - positions[neighbourId];
                    float   mag         = toNeighbour.magnitude;

                    if (mag > 0) // Need this check otherwise this behaviour can return NAN
                    {
                        force += (Vector3.Normalize(toNeighbour) / mag);
                    }
                    else
                    {
                        // same position, so generate a random force
                        Vector3 f = random.NextFloat3Direction();
                        force += f * b.maxForce;
                    }
                }
                s.force            = force * weight;
                seperationChunk[i] = s;
            }
        }
예제 #3
0
        private Vector3 AccululateForces(ref Boid b, ref ObstacleAvoidance oa, ref Seperation s, ref Alignment a, ref Cohesion c, ref Wander w, ref Constrain con)
        {
            Vector3 force = Vector3.zero;


            force += oa.force;
            if (force.magnitude >= b.maxForce)
            {
                force = Vector3.ClampMagnitude(force, b.maxForce);
                return(force);
            }

            force += b.fleeForce;
            if (force.magnitude >= b.maxForce)
            {
                force = Vector3.ClampMagnitude(force, b.maxForce);
                return(force);
            }


            force += s.force;
            if (force.magnitude >= b.maxForce)
            {
                force = Vector3.ClampMagnitude(force, b.maxForce);
                return(force);
            }
            force += a.force;
            if (force.magnitude >= b.maxForce)
            {
                force = Vector3.ClampMagnitude(force, b.maxForce);
                return(force);
            }

            force += c.force;
            if (force.magnitude >= b.maxForce)
            {
                force = Vector3.ClampMagnitude(force, b.maxForce);
                return(force);
            }

            force += w.force;
            if (force.magnitude >= b.maxForce)
            {
                force = Vector3.ClampMagnitude(force, b.maxForce);
                return(force);
            }

            force += con.force;
            if (force.magnitude >= b.maxForce)
            {
                force = Vector3.ClampMagnitude(force, b.maxForce);
                return(force);
            }

            force += b.seekForce;
            if (force.magnitude >= b.maxForce)
            {
                force = Vector3.ClampMagnitude(force, b.maxForce);
                return(force);
            }

            return(force);
        }