public static SteeringOutput GetSteering(KinematicState ownKS,
                                                 GameObject target, float distance, float angle,
                                                 string tag, float repulsionTh,
                                                 float wlr /* COMPLETE */)
        {
            // compute both steerings
            SteeringOutput lr = LinearRepulsion.GetSteering(ownKS, tag, repulsionTh);
            SteeringOutput kp = KeepPosition.GetSteering(ownKS, target, distance, angle);

            // blend
            // (if one is SteeringBehaviour.NULL_STEERING return the other.
            // if none is SteeringBehaviour.NULL_STEERING blend with weights wlr and 1-wlr)
            /* COMPLETE */

            if (lr == SteeringBehaviour.NULL_STEERING)
            {
                return(kp);
            }
            if (kp == SteeringBehaviour.NULL_STEERING)
            {
                return(lr);
            }


            // "concoct" the blending on lr
            lr.linearAcceleration = wlr * lr.linearAcceleration + (1 - wlr) * kp.linearAcceleration;
            return(lr);
        }
Example #2
0
        public float repulsionThreshold = 20f;           // at which distance does repulsion start?

        public override SteeringOutput GetSteering()
        {
            // no KS? get it
            if (this.ownKS == null)
            {
                this.ownKS = GetComponent <KinematicState>();
            }

            return(LinearRepulsion.GetSteering(this.ownKS, this.idTag, this.repulsionThreshold));
        }
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target, float predictionTime = 3.0f, string tag = "REPULSIVE", float repulsionTh = 8.0f)
        {
            SteeringOutput so = LinearRepulsion.GetSteering(ownKS, tag, repulsionTh);

            if (so != NULL_STEERING)
            {
                return(so);
            }

            return(Pursue.GetSteering(ownKS, target, predictionTime));
        }
        public float repulsionThreshold = 20f;           // at which distance does repulsion start?

        public override SteeringOutput GetSteering()
        {
            // no KS? get it
            if (this.ownKS == null)
            {
                this.ownKS = GetComponent <KinematicState>();
            }

            SteeringOutput result = LinearRepulsion.GetSteering(this.ownKS, this.idTag, this.repulsionThreshold);

            base.applyRotationalPolicy(rotationalPolicy, result, null);
            return(result);
        }
Example #5
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target, float distance = 10.0f, float angle = 180.0f, string tag = "REPULSIVE", float repulsionTh = 8.0f)
        {
            SteeringOutput so = LinearRepulsion.GetSteering(ownKS, tag, repulsionTh);

            // Give priority to linear repulsion
            // (if linear repulsion is not SteeringBehaviour.NULL_STEERING return linear repulsion
            if (so != NULL_STEERING)
            {
                return(so);
            }

            // else return Keep Position
            return(KeepPosition.GetSteering(ownKS, target, distance, angle));
        }
Example #6
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target, float closeEnoughRadius, float slowDownRadius, float timeToDesiredSpeed, float lookAheadLength, float avoidDistance, float secondaryWhiskerAngle, float secondaryWhiskerRatio, LayerMask avoidLayers, SphereCollider scanner, string repulsionTag, float repulsionThreshold, float arriveWeight)
        {
            SteeringOutput steeringOutput = ObstacleAvoidance.GetSteering(ownKS, lookAheadLength, avoidDistance, secondaryWhiskerAngle, secondaryWhiskerRatio, avoidLayers, scanner);

            if (steeringOutput == nullSteering)
            {
                SteeringOutput arrive          = Arrive.GetSteering(ownKS, target, closeEnoughRadius, slowDownRadius, timeToDesiredSpeed);
                SteeringOutput linearRepulsion = LinearRepulsion.GetSteering(ownKS, repulsionTag, repulsionThreshold);
                arrive.linearAcceleration = arrive.linearAcceleration * arriveWeight + linearRepulsion.linearAcceleration * (1 - arriveWeight);
                return(arrive);
            }

            return(steeringOutput);
        }
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target, float distance = 10.0f, float slowRadius = 20.0f,
                                                 float disredSpeed = 0.1f, bool showWhishker        = true, float lookAheadLength = 10f, float avoidDistance = 10f, float secondaryWhiskerAngle = 30f, float secondaryWhiskerRatio = 0.7f,
                                                 string tag        = "REPULSIVE", float repulsionTh = 8.0f, float repulsionWeight = 0.5f)
        {
            SteeringOutput linearRepulsion = LinearRepulsion.GetSteering(ownKS, tag, repulsionTh);
            SteeringOutput arrive          = ArrivePlusAvoid.GetSteering(ownKS, target, distance, slowRadius, disredSpeed, showWhishker, lookAheadLength, avoidDistance, secondaryWhiskerAngle, secondaryWhiskerRatio);

            SteeringOutput result = new SteeringOutput();

            if (linearRepulsion == NULL_STEERING)
            {
                return(arrive);
            }
            else if (arrive == NULL_STEERING)
            {
                return(linearRepulsion);
            }

            result.linearAcceleration = arrive.linearAcceleration * (1 - repulsionWeight) + linearRepulsion.linearAcceleration * repulsionWeight;
            return(result);
        }
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target, string idTag, float RequiredDistance, float DesiredAngle, float RepulsionThreshold)
        {
            SteeringOutput rp  = LinearRepulsion.GetSteering(ownKS, idTag, RepulsionThreshold);
            SteeringOutput Kep = My_KeepDistanceVersatile.GetSteering(ownKS, target, RequiredDistance, DesiredAngle);

            if (rp == null)
            {
                rp = new SteeringOutput();
            }

            SteeringOutput result = new SteeringOutput();

            result.linearAcceleration = rp.linearAcceleration * 0.6f + Kep.linearAcceleration * 0.4f;

            if (result.linearAcceleration.magnitude > ownKS.maxAcceleration)
            {
                result.linearAcceleration = result.linearAcceleration.normalized * ownKS.maxAcceleration;
            }

            return(result);
        }
Example #9
0
        public override SteeringOutput GetSteering()
        {
            SteeringOutput result = LinearRepulsion.GetSteering(this.ownKS, this.idTag, this.repulsionThreshold);

            if (!surrogateTarget)
            {
                return(null);
            }

            if (ownKS.linearVelocity.magnitude > 0.001f)
            {
                surrogateTarget.transform.rotation = Quaternion.Euler(0, 0, VectorToOrientation(ownKS.linearVelocity));
                SteeringOutput st = Align.GetSteering(ownKS, surrogateTarget);
                result.angularAcceleration = st.angularAcceleration;
                result.angularActive       = st.angularActive;
            }
            else
            {
                result.angularActive = false;
            }

            return(result);
        }
Example #10
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target, float distance = 10.0f, float angle = 180.0f, string tag = "REPULSIVE", float repulsionTh = 8.0f, float repulsiveWeight = 0.5f)
        {
            // compute both steerings
            SteeringOutput lr = LinearRepulsion.GetSteering(ownKS, tag, repulsionTh);
            SteeringOutput kp = KeepPosition.GetSteering(ownKS, target, distance, angle);

            // blend result
            SteeringOutput result = new SteeringOutput();

            // (if one is SteeringBehaviour.NULL_STEERING return the other
            if (lr == NULL_STEERING)
            {
                return(kp);
            }
            else if (kp == NULL_STEERING)
            {
                return(lr);
            }

            // if none is SteeringBehaviour.NULL_STEERING blend with weights wlr and 1-wlr)
            result.linearAcceleration = kp.linearAcceleration * (1 - repulsiveWeight) + lr.linearAcceleration * repulsiveWeight;
            return(result);
        }
Example #11
0
        public static SteeringOutput GetSteering(KinematicState ownKS, string idTag = "BOID",
                                                 float cohesionThreshold            = 40f, float repulsionThreshold = 10f,
                                                 float wanderRate = 10f,
                                                 float vmWeight   = 0.08f, float rpWeight = 0.46f, float coWeight = 0.23f, float wdWeight = 0.23f)
        {
            float          distanceToBoid;
            KinematicState boidKS;
            Vector3        averageVelocity = Vector3.zero;
            int            count           = 0;
            SteeringOutput result          = new SteeringOutput();

            // get all the other boids
            GameObject [] boids = GameObject.FindGameObjectsWithTag(idTag);


            // ... and iterate to find average velocity
            foreach (GameObject boid in boids)
            {
                // skip yourself
                if (boid == ownKS.gameObject)
                {
                    continue;
                }

                boidKS = boid.GetComponent <KinematicState> ();
                if (boidKS == null)
                {
                    // this should never happen but you never know
                    Debug.Log("Incompatible mate in flocking. Flocking mates must have a kinematic state attached: " + boid);
                    continue;
                }

                // disregard distant boids
                distanceToBoid = (boidKS.position - ownKS.position).magnitude;
                if (distanceToBoid > Math.Max(cohesionThreshold, repulsionThreshold))
                {
                    continue;
                }

                averageVelocity = averageVelocity + boidKS.linearVelocity;
                count++;
            }             // end of iteration to find average velocity

            if (count > 0)
            {
                averageVelocity = averageVelocity / count;
            }
            else
            {
                // if no boid is close enough (count==0) there's no flocking to be performed so return NULL_STEERING
                // or just apply some wandering...
                return(NaiveWander.GetSteering(ownKS, wanderRate));
            }


            SURROGATE_TARGET.GetComponent <KinematicState> ().linearVelocity = averageVelocity;

            SteeringOutput vm = VelocityMatching.GetSteering(ownKS, SURROGATE_TARGET);             // (in normal conditions) this does NOT return NULL_STEERING
            SteeringOutput rp = LinearRepulsion.GetSteering(ownKS, idTag, repulsionThreshold);     // this MAY return NULL_STEERING
            SteeringOutput co = Cohesion.GetSteering(ownKS, idTag, cohesionThreshold);             // this MAY return NULL_STEERING


            result.linearAcceleration = vm.linearAcceleration * vmWeight +
                                        rp.linearAcceleration * rpWeight + // if rp==NULL_STEERING linearAcceleration is Vector3.zero
                                        co.linearAcceleration * coWeight;  // id for co.

            // and now let's add some wandering to make things less predictable (yet more stable)

            SteeringOutput wd = NaiveWander.GetSteering(ownKS, wanderRate);

            result.linearAcceleration += wd.linearAcceleration * wdWeight;

            // adjust to maxAcceleration
            result.linearAcceleration = result.linearAcceleration.normalized * ownKS.maxAcceleration;

            return(result);
        }
Example #12
0
        public static SteeringOutput GetSteering(KinematicState ownKS, string idTag = "BOID",
                                                 float cohesionThreshold            = 40f, float repulsionThreshold = 10f,
                                                 float wanderRate = 10f)
        {
            float          distanceToBoid;
            KinematicState boid;
            Vector3        averageVelocity = Vector3.zero;
            int            count           = 0;

            // get all the other boids
            GameObject [] boids = GameObject.FindGameObjectsWithTag(idTag);


            // ... and iterate to find average velocity
            for (int i = 0; i < boids.Length; i++)
            {
                // skip yourself
                if (boids[i] == ownKS.gameObject)
                {
                    continue;
                }

                boid = boids [i].GetComponent <KinematicState> ();
                if (boid == null)
                {
                    // this should never happen but you never know
                    Debug.Log("Incompatible mate in flocking. Flocking mates must have a kinematic state attached: " + boids[i]);
                    continue;
                }

                // disregard distant boids
                distanceToBoid = (boid.position - ownKS.position).magnitude;
                if (distanceToBoid > Math.Max(cohesionThreshold, repulsionThreshold))
                {
                    continue;
                }

                averageVelocity = averageVelocity + boid.linearVelocity;
                count++;
            }             // end of iteration to find average velocity

            if (count > 0)
            {
                averageVelocity = averageVelocity / count;
            }
            else
            {
                return(null);
            }
            // if no boid is close enough (count==0) there's no flocking to be performed so return null
            // could also apply some wandering


            if (surrogateTarget == null)
            {
                surrogateTarget = new GameObject("surrogate target for Flocking");
                surrogateKS     = surrogateTarget.AddComponent <KinematicState> ();
            }

            surrogateKS.linearVelocity = averageVelocity;
            SteeringOutput vm = VelocityMatching.GetSteering(ownKS, surrogateTarget);              // (in normal conditions) this does NOT return null
            SteeringOutput rp = LinearRepulsion.GetSteering(ownKS, idTag, repulsionThreshold);     // this MAY return null
            SteeringOutput co = Cohesion.GetSteering(ownKS, idTag, cohesionThreshold);             // this MAY return null

            // avoid nasty problems due to null references
            if (rp == null)
            {
                rp = new SteeringOutput();
            }
            if (co == null)
            {
                co = new SteeringOutput();
            }

            SteeringOutput result = new SteeringOutput();

            result.linearAcceleration = vm.linearAcceleration * 0.4f +
                                        rp.linearAcceleration * 2f +
                                        co.linearAcceleration * 1f;

            // and now let's add some wandering to make things less predictable
            SteeringOutput wd = VeryNaiveWander.GetSteering(ownKS, wanderRate);

            result.linearAcceleration += wd.linearAcceleration * 1f;


            // clip if necessary
            if (result.linearAcceleration.magnitude > ownKS.maxAcceleration)
            {
                result.linearAcceleration = result.linearAcceleration.normalized * ownKS.maxAcceleration;
            }

            return(result);
        }