Example #1
0
            public void Execute(int index)
            {
                var    v     = velocities[index];
                var    accel = accelerations[index];
                Float3 velocity;

                velocity.X = v.X + accel.X * dt;
                velocity.Y = v.Y + accel.Y * dt;
                velocity.Z = v.Z + accel.Z * dt;
                var speed = Float3.Magnitude(ref velocity);

                velocity.Scale(1f / speed);
                var r = Quaternion.LookRotation(velocity, Vector3.up);

                speed = MathFast.Clamp(speed, minSpeed, maxSpeed);
                velocity.Scale(speed);
                var pos = positions[index];

                pos.Add(velocity.X * dt, velocity.Y * dt, velocity.Z * dt);
                positions[index] = pos;
                accel.Set(0f, 0f, 0f);
                accelerations[index] = accel;
                velocities[index]    = velocity;

                matrices[index] = Matrix4x4.TRS(pos, r, scale);
            }
Example #2
0
        /// <summary>
        /// Our worker callback for processing entities.
        /// Important: always use static methods as workers - you cant touch any instance data except method parameters.
        /// </summary>
        void Worker(EcsFilter <BoidEntityData> filter, int from, int to)
        {
            // Important: you can't iterate over filter with foreach-loop
            // and should use for-loop with "from" / "to" bounds.
            var dt       = _timeSystem.DeltaTime;
            var minSpeed = MinSpeed;
            var maxSpeed = MaxSpeed;
            var scale    = BoidScale;

            var wallScale = WallScale;
            var thresh    = WallDistance;
            var weight    = WallWeight;

            var prodThresh       = NeighborFov;
            var distThresh       = NeighborDistance;
            var separationWeight = SeparationWeight;
            var alignmentWeight  = AlignmentWeight;
            var cohesionWeight   = CohesionWeight;

            Float3  velocity = Float3.Zero;
            Vector3 up       = Vector3.up;

            for (var i = from; i < to; i++)
            {
                var c = filter.Components1[i];
                Wall(c, wallScale, thresh, weight);
                var neighbours   = Neighbours[i];
                var currentCount = DetectNeighbour(c, distThresh, prodThresh, filter.Components1, filter.EntitiesCount, neighbours);
                if (currentCount > 0)
                {
                    Separation(c, separationWeight, currentCount, neighbours);
                    Alignment(c, alignmentWeight, currentCount, neighbours);
                    Cohesion(c, cohesionWeight, currentCount, neighbours);
                }
                velocity.X = c.velocity.X + c.acceleration.X * dt;
                velocity.Y = c.velocity.Y + c.acceleration.Y * dt;
                velocity.Z = c.velocity.Z + c.acceleration.Z * dt;
                var speed = Float3.Magnitude(ref velocity);
                velocity.Scale(1f / speed);
                var r = Quaternion.LookRotation(velocity, up);
                speed = MathFast.Clamp(speed, minSpeed, maxSpeed);
                velocity.Scale(speed);
                c.position.Add(velocity.X * dt, velocity.Y * dt, velocity.Z * dt);
                c.acceleration.Set(0f, 0f, 0f);
                c.velocity = velocity;

                BoidsVisualisationSystem2._matrices[i] = Matrix4x4.TRS(c.position, r, scale);
            }
        }