示例#1
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;
            }
        }
示例#2
0
        public void Execute(ArchetypeChunk batchInChunk, int batchIndex)
        {
            var boidChunk      = batchInChunk.GetNativeArray(boidTypeHandle);
            var constrainChunk = batchInChunk.GetNativeArray(constrainTypeHandle);

            for (int i = 0; i < batchInChunk.Count; i++)
            {
                Constrain con = constrainChunk[i];
                Boid      b   = boidChunk[i];

                Vector3 force    = Vector3.zero;
                Vector3 toTarget = positions[b.boidId] - centre;
                if (toTarget.magnitude > radius)
                {
                    force = Vector3.Normalize(toTarget) * (radius - toTarget.magnitude);
                }

                /*
                 * float xDist = positions[b.boidId].x - centre.x;
                 * if (Mathf.Abs(xDist) > radius)
                 * {
                 *  force.x +=  radius - xDist;
                 * }
                 * float yDist = positions[b.boidId].y - centre.y;
                 * if (Mathf.Abs(yDist) > radius)
                 * {
                 *  force.y +=  radius - yDist;
                 * }
                 * float zDist = positions[b.boidId].z - centre.z;
                 * if (Mathf.Abs(zDist) > radius)
                 * {
                 *  force.z +=  radius - zDist;
                 * }
                 */
                con.force         = force * weight;
                constrainChunk[i] = con;
            }
        }
示例#3
0
        public void Execute(ArchetypeChunk batchInChunk, int batchIndex)
        {
            var boidChunk         = batchInChunk.GetNativeArray(boidTypeHandle);
            var translationsChunk = batchInChunk.GetNativeArray(translationTypeHandle);
            var rotationsChunk    = batchInChunk.GetNativeArray(rotationTypeHandle);


            for (int i = 0; i < batchInChunk.Count; i++)
            {
                Translation p = translationsChunk[i];
                Rotation    r = rotationsChunk[i];
                Boid        b = boidChunk[i];
                int         neighbourStartIndex = maxNeighbours * b.boidId;
                int         neighbourCount      = 0;


                if (usePartitioning)
                {
                    int surroundingCellCount = (int)Mathf.Ceil(neighbourDistance / cellSize);

                    // Are we looking above and below?
                    int sliceSurrounding = threedcells ? surroundingCellCount : 0;
                    for (int slice = -sliceSurrounding; slice <= sliceSurrounding; slice++)
                    {
                        for (int row = -surroundingCellCount; row <= surroundingCellCount; row++)
                        {
                            for (int col = -surroundingCellCount; col <= surroundingCellCount; col++)
                            {
                                Vector3 pos  = positions[b.boidId] + new Vector3(col * cellSize, slice * cellSize, row * cellSize);
                                int     cell = PartitionSpaceJob.PositionToCell(pos, cellSize, gridSize);

                                NativeMultiHashMapIterator <int> iterator;
                                int boidId;
                                if (cells.TryGetFirstValue(cell, out boidId, out iterator))
                                {
                                    do
                                    {
                                        if (boidId != b.boidId)
                                        {
                                            if (Vector3.Distance(positions[b.boidId], positions[boidId]) < neighbourDistance)
                                            {
                                                neighbours[neighbourStartIndex + neighbourCount] = boidId;
                                                neighbourCount++;
                                                if (neighbourCount == maxNeighbours)
                                                {
                                                    b.taggedCount = neighbourCount;
                                                    return;
                                                }
                                            }
                                        }
                                    } while (cells.TryGetNextValue(out boidId, ref iterator));
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < positions.Length; j++)
                    {
                        if (j != b.boidId)
                        {
                            if (Vector3.Distance(positions[b.boidId], positions[j]) < neighbourDistance)
                            {
                                neighbours[neighbourStartIndex + neighbourCount] = j;
                                neighbourCount++;
                                if (neighbourCount == maxNeighbours)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                b.taggedCount = neighbourCount;
                boidChunk[i]  = b;
            }
        }
示例#4
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);
        }