public static SteeringOutput GetSteering(KinematicState ownKS,
                                                 ref float targetOrientation,
                                                 float wanderRate   = 30f,
                                                 float wanderRadius = 10f, float wanderOffset = 20f)
        {
            // change target orientation (change location of surrogate target on unit circle)
            targetOrientation += wanderRate * Utils.binomial();

            // place surrogate target on circle of wanderRadius
            SURROGATE_TARGET.transform.position = Utils.OrientationToVector(targetOrientation) * wanderRadius;

            // place circle  "in front"
            SURROGATE_TARGET.transform.position += ownKS.position + Utils.OrientationToVector(ownKS.orientation) * wanderOffset;


            // show some gizmos before returning
            Debug.DrawLine(ownKS.position,
                           ownKS.position + Utils.OrientationToVector(ownKS.orientation) * wanderOffset,
                           Color.black);

            DebugExtension.DebugCircle(ownKS.position + Utils.OrientationToVector(ownKS.orientation) * wanderOffset,
                                       new Vector3(0, 0, 1),
                                       Color.red,
                                       wanderRadius);
            DebugExtension.DebugPoint(SURROGATE_TARGET.transform.position,
                                      Color.black,
                                      5f);



            // Seek the surrogate target
            return(Seek.GetSteering(ownKS, SURROGATE_TARGET));
        }
Example #2
0
        public static SteeringOutput GetSteering(KinematicState ownKS, SAlign info)
        {
            SURROGATE_TARGET.rotation = Quaternion.Euler(SURROGATE_TARGET.eulerAngles.x, MathExtent.VectorToAngle(info.m_target.position - ownKS.m_position), SURROGATE_TARGET.eulerAngles.z);
            info.m_target             = SURROGATE_TARGET;

            return(Align.GetSteering(ownKS, info));
        }
Example #3
0
        public static SteeringOutput GetSteering(KinematicState ownKS, SArrive info)
        {
            Vector3 l_distanceToTarget = info.m_target.position - ownKS.m_position;

            if (l_distanceToTarget.magnitude < info.m_closeEnoughRadius)
            {
                return(NULL_STEERING);
            }

            if (l_distanceToTarget.magnitude > info.m_slowDownRadius)
            {
                return(Seek.GetSteering(ownKS, info.m_target));
            }

            float   l_desiredSpeed    = ownKS.m_maxLinearSpeed * (l_distanceToTarget.magnitude / info.m_slowDownRadius);
            Vector3 l_desiredVelocity = l_distanceToTarget.normalized * l_desiredSpeed;

            Vector3 l_requiredAcceleration = (l_desiredVelocity - ownKS.m_linearVelocity) / info.m_timeToDesiredSpeed;

            l_requiredAcceleration = MathExtent.Clip(l_requiredAcceleration, ownKS.m_maxLinearAcceleration);

            SteeringOutput result = new SteeringOutput();

            result.m_linearAcceleration = l_requiredAcceleration;

            return(result);
        }
        public static SteeringOutput GetSteering(KinematicState ownKS)
        {
            SteeringOutput steering = new SteeringOutput();

            Vector3 desiredDirection = Vector3.zero;

            if (Input.GetKey(KeyCode.LeftArrow))
            {
                desiredDirection += Vector3.left;
            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                desiredDirection += Vector3.right;
            }
            if (Input.GetKey(KeyCode.UpArrow))
            {
                desiredDirection += Vector3.up;
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                desiredDirection += Vector3.down;
            }


            if (desiredDirection.magnitude < 0.01f)
            {
                return(NULL_STEERING);
            }
            else
            {
                steering.linearAcceleration = desiredDirection * ownKS.maxAcceleration;
            }

            return(steering);
        }
Example #5
0
        private SteeringOutput NaiveWander(KinematicState own)
        {
            // align with a surrogate target that has your new orientation and go there

            SteeringOutput align, goWYL;
            KinematicState surrogateTarget = new KinematicState();

            // "sligtly" change the orientation
            float desiredOrientation = own.orientation + wanderRate * binomial();

            // give that orientation to the surrogate target
            surrogateTarget.orientation = desiredOrientation;
            // align
            align = Align(own, surrogateTarget);

            // go where you look (move "forward");
            goWYL = goWhereYouLook(own);             //goWhereYouLook never returns null...

            // combine and return

            if (align != null && goWYL != null)
            {
                goWYL.angularAcceleration = align.angularAcceleration;
            }

            return(goWYL);
        }
Example #6
0
        private SteeringOutput Wander(KinematicState own, KinematicState wanderTarget)
        {
            // update orientation of wanderTarget
            wanderTarget.orientation = wanderTarget.orientation + wanderRate * binomial();


            // place the center of the wander circle
            wanderTarget.position = own.position + wanderDistance * OrientationToVector(own.orientation);
            // place the target somewhere in the wander circle
            wanderTarget.position = wanderTarget.position + wanderRadius * OrientationToVector(wanderTarget.orientation + own.orientation);

            /* why wanderTarget.orientation+own.orientation?
             * because wanderTarget.orintation is in local -own- system and must be in world system
             */


            // face the target
            SteeringOutput face = Face(own, wanderTarget);

            // go where you look (move "forward");
            SteeringOutput goWYL = goWhereYouLook(own);             //goWhereYouLook never returns null...

            // combine and return

            if (face != null && goWYL != null)
            {
                goWYL.angularAcceleration = face.angularAcceleration;
            }

            return(goWYL);
        }
Example #7
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target, float RequiredDsitance, float DesiredAngle)
        {
            Vector3 DirectionFromTarget;
            Vector3 DesiredPos;
            Vector3 OrientationVector;

            float targetOrientation = target.transform.eulerAngles.z;

            // Compute direction to target
            OrientationVector = Utils.OrientationToVector(targetOrientation + DesiredAngle);

            DirectionFromTarget = ((ownKS.position - target.transform.position)).normalized;

            DesiredPos = (target.transform.position + DirectionFromTarget * RequiredDsitance) + OrientationVector;


            //Matriz de rotacion 2d
            //DirectionFromTarget.y = OrientationVector.x * Mathf.Sin(Mathf.Deg2Rad * DesiredAngle) + OrientationVector.y * Mathf.Cos(Mathf.Deg2Rad * DesiredAngle);
            //DirectionFromTarget.x = OrientationVector.x * Mathf.Cos(Mathf.Deg2Rad * DesiredAngle) - OrientationVector.y * Mathf.Sin(Mathf.Deg2Rad * DesiredAngle);
            //DirectionFromTarget.Normalize();

            if (My_KeepDistanceVersatile.surrogateTarget == null)
            {
                My_KeepDistanceVersatile.surrogateTarget = new GameObject("Dummy (Surrogate Target for keep distance)");
            }
            My_KeepDistanceVersatile.surrogateTarget.transform.position = DesiredPos;


            return(Seek.GetSteering(ownKS, surrogateTarget));
        }
Example #8
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target)
        {
            SteeringOutput result = Seek.GetSteering(ownKS, target);

            result.linearAcceleration = -result.linearAcceleration;
            return(result);
        }
        public static SteeringOutput GetSteering(KinematicState ownKS,
                                                 float WanderRate, float wanderRadius, float wanderOffset, ref float targetOrientation,
                                                 bool showWhishker, float lookAheadLength, float avoidDistance, float secondaryWhiskerAngle, float secondaryWhiskerRatio,
                                                 ref bool avoidActive)
        {
            // give priority to obstacle avoidance
            SteeringOutput so = ObstacleAvoidance.GetSteering(ownKS, showWhishker, lookAheadLength,
                                                              avoidDistance, secondaryWhiskerAngle, secondaryWhiskerRatio);

            if (so == NULL_STEERING)
            {
                if (avoidActive)
                {
                    // if avoidance was active last frame, update target orientation (otherwise the object would tend to regain
                    // the orientation it had before avoiding a collision which would make it face the obstacle again)
                    targetOrientation = ownKS.orientation;
                }
                avoidActive = false;
                return(Wander.GetSteering(ownKS, ref targetOrientation, WanderRate, wanderRadius, wanderOffset));
            }
            else
            {
                avoidActive = true;
                return(so);
            }
        }
        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 #11
0
        public static SteeringOutput GetSteering(KinematicState ownKS, float wanderRate = 30f, float targetAngularRadius       = 2f,
                                                 float slowDownAngularRadius            = 10f, float timeToDesiredAngularSpeed = 0.1f)
        {
            // align with a surrogate target that has your new orientation and go there

            // slightly change the orientation
            float desiredOrientation = ownKS.orientation + wanderRate * Utils.binomial();

            // give that orientation to the surrogate target
            SURROGATE_TARGET.transform.rotation = Quaternion.Euler(0, 0, desiredOrientation);

            // align with the surrogate target
            SteeringOutput al = Align.GetSteering(ownKS, SURROGATE_TARGET, targetAngularRadius, slowDownAngularRadius, timeToDesiredAngularSpeed);

            // go where you look (looked, actually)
            SteeringOutput gwyl = GoWhereYouLook.GetSteering(ownKS);             // should never return null

            // combine, if possible
            if (al != null)
            {
                gwyl.angularActive       = true;
                gwyl.angularAcceleration = al.angularAcceleration;
            }

            return(gwyl);
        }
Example #12
0
        public static SteeringOutput GetSteering(KinematicState ownKS)
        {
            SteeringOutput steering = new SteeringOutput();

            Vector3 desiredDirection = Vector3.zero;

            if (Input.GetKey(KeyCode.LeftArrow))
            {
                desiredDirection += Vector3.left;
            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                desiredDirection += Vector3.right;
            }
            if (Input.GetKey(KeyCode.UpArrow))
            {
                desiredDirection += Vector3.up;
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                desiredDirection += Vector3.down;
            }


            // Beware, this part of the code tampers with speed...
            if (desiredDirection.magnitude < 0.01f)
            {
                ownKS.linearVelocity = Vector3.zero;                  // STOP
            }

            steering.linearAcceleration = desiredDirection * ownKS.maxAcceleration;
            return(steering);
        }
Example #13
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject repulsor, float scareRadius = 40f, float fleeWeight = 0.2f, string idTag = "BOID",
                                                 float cohesionThreshold = 40f, float repulsionThreshold = 10f,
                                                 float wanderRate        = 10f)
        {
            SteeringOutput fleeOutput;

            if ((ownKS.position - repulsor.transform.position).magnitude <= scareRadius)
            {
                fleeOutput = Flee.GetSteering(ownKS, repulsor);
            }
            else
            {
                fleeOutput = NULL_STEERING;
            }

            SteeringOutput result = Flocking.GetSteering(ownKS, idTag, cohesionThreshold, repulsionThreshold, wanderRate);

            // beware, Flocking may return NULL_STEERING. In that case, just apply flee
            if (result == NULL_STEERING)
            {
                return(fleeOutput);
            }

            result.linearAcceleration  = result.linearAcceleration * (1 - fleeWeight) + fleeOutput.linearAcceleration * fleeWeight;
            result.angularAcceleration = result.angularAcceleration * (1 - fleeWeight) + fleeOutput.angularAcceleration * fleeWeight;

            return(result);
        }
Example #14
0
        private SteeringOutput ScaredFish(KinematicState own)
        {
            SteeringOutput obstacleAvoidance = ObstacleAvoidance2D(own);

            //SteeringOutput flocking  = Flocking(own);
            //SteeringOutput collision = CollisionAvoidance (own, "PREDATOR");

            if (obstacleAvoidance == null)
            {
                GameObject     pred     = GameObject.FindGameObjectWithTag("PREDATOR");
                KinematicState targetKS = pred.GetComponent <Steerings2> ().ownKS;
                if ((own.position - targetKS.position).magnitude < 200)
                {
                    return(Flee(own, targetKS));
                }
                else
                {
                    return(Flocking(own));
                }
            }
            else
            {
                return(obstacleAvoidance);
            }
        }
        public static SteeringOutput GetSteering(KinematicState ownKS, SObstacleAvoidance info)
        {
            Ray        l_ray = new Ray(ownKS.m_position, ownKS.m_linearVelocity.normalized);
            RaycastHit l_hitInfo;

            if (Physics.Raycast(l_ray, out l_hitInfo, info.m_avoidDistance))
            {
                SURROGATE_TARGET.position = l_hitInfo.point + l_hitInfo.normal * info.m_avoidDistance;
                return(Seek.GetSteering(ownKS, SURROGATE_TARGET));
            }

            l_ray = new Ray(ownKS.m_position, MathExtent.AngleToVector(MathExtent.VectorToAngle(ownKS.m_linearVelocity.normalized) + info.m_AngleSecondWhisk));

            if (Physics.Raycast(l_ray, out l_hitInfo, info.m_primaryWhiskerLenght * info.m_RatioSecondWhisk))
            {
                SURROGATE_TARGET.position = l_hitInfo.point + l_hitInfo.normal * info.m_avoidDistance;
                return(Seek.GetSteering(ownKS, SURROGATE_TARGET));
            }

            l_ray = new Ray(ownKS.m_position, MathExtent.AngleToVector(MathExtent.VectorToAngle(ownKS.m_linearVelocity.normalized) - info.m_AngleSecondWhisk));

            if (Physics.Raycast(l_ray, out l_hitInfo, info.m_primaryWhiskerLenght * info.m_RatioSecondWhisk))
            {
                SURROGATE_TARGET.position = l_hitInfo.point + l_hitInfo.normal * info.m_avoidDistance;
                return(Seek.GetSteering(ownKS, SURROGATE_TARGET));
            }

            return(NULL_STEERING);
        }
Example #16
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target, float maxPredictionTime = 3f)
        {
            // we need to know the kinematic state of the target since we need to know its linear velocity

            // if target has no kinematic state "give up" and just seek
            KinematicState targetKS = target.GetComponent <KinematicState> ();

            if (targetKS == null)
            {
                Debug.Log("Pursue invoked with a target that has no kinematic state attached. Resorting to Seek");
                return(Seek.GetSteering(ownKS, target));
            }

            Vector3 directionToTarget = targetKS.position - ownKS.position;
            float   distanceToTarget  = directionToTarget.magnitude;
            float   currentSpeed      = ownKS.linearVelocity.magnitude;

            // determine the time it will take to reach the target
            float predictedTimeToTarget = distanceToTarget / currentSpeed;

            if (predictedTimeToTarget > maxPredictionTime)
            {
                predictedTimeToTarget = maxPredictionTime;
            }

            // now determine future (at predicted time) location of target
            Vector3 futurePositionOfTarget = targetKS.position + targetKS.linearVelocity * predictedTimeToTarget;

            // create surrogate target and place it at future location
            SURROGATE_TARGET.transform.position = futurePositionOfTarget;

            // delegate to seek
            return(Seek.GetSteering(ownKS, SURROGATE_TARGET));
            // could also delegate to Arrive if overshooting is an issue...
        }
Example #17
0
        private SteeringOutput Pursue(KinematicState own, KinematicState target)
        // INTERCEPT
        {
            Vector3 directionToTarget = target.position - own.position;
            float   distanceToTarget  = directionToTarget.magnitude;
            float   currentSpeed      = own.linearVelocity.magnitude;

            // determine the time it will take to reach the target
            float predictedTimeToTarget = distanceToTarget / currentSpeed;

            if (predictedTimeToTarget > maxPredictionTime)
            {
                predictedTimeToTarget = maxPredictionTime;
            }

            // now determine future (at predicted time) location of target
            Vector3 futurePositionOfTarget = target.position + target.linearVelocity * predictedTimeToTarget;

            // create surrogate target and place it at future location
            KinematicState surrogateTarget = new KinematicState();

            surrogateTarget.position = futurePositionOfTarget;

            // delegate to arrive
            return(Arrive(own, surrogateTarget));
            // could also delegate to seek... but we risk overshooting...
            // return Seek(own, surrogateTarget);
        }
        public static SteeringOutput GetSteering(KinematicState ownKS, float wanderRate = 10f)
        {
            // "slightly" change your own orientation
            ownKS.orientation += Utils.binomial() * wanderRate;

            // and go where you look
            return(GoWhereYouLook.GetSteering(ownKS));
        }
Example #19
0
        public static SteeringOutput GetSteering(KinematicState ownKS, Transform target)
        {
            SteeringOutput result = new SteeringOutput();

            result.m_linearAcceleration = (target.position - ownKS.m_position).normalized * ownKS.m_maxLinearAcceleration;

            return(result);
        }
Example #20
0
        public static SteeringOutput GetSteering(KinematicState ownKS, Transform target)
        {
            SteeringOutput result = Seek.GetSteering(ownKS, target);

            result.m_linearAcceleration *= -1;

            return(result);
        }
Example #21
0
        public static SteeringOutput GetSteering(KinematicState ownKS)
        {
            Vector3 l_direction = MathExtent.AngleToVector(ownKS.m_orientation);

            SURROGATE_TARGET.position = ownKS.m_position + l_direction;

            return(Seek.GetSteering(ownKS, SURROGATE_TARGET));
        }
Example #22
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target)
        {
            // invoke SEEK...
            SteeringOutput result = Seek.GetSteering(ownKS, target);

            // ... reverse direction and that's all
            result.linearAcceleration = -result.linearAcceleration;
            return(result);
        }
Example #23
0
        public static SteeringOutput GetSteering(KinematicState ownKS, float seekWeight, ref SWander wanderInfo, SSeek seekInfo)
        {
            SteeringOutput seekSteering = Seek.GetSteering(ownKS, seekInfo.m_target);
            SteeringOutput result       = Wander.GetSteering(ownKS, ref wanderInfo);

            result.m_linearAcceleration = result.m_linearAcceleration * (1f - seekWeight) + seekSteering.m_linearAcceleration * seekWeight;

            return(result);
        }
Example #24
0
        private SteeringOutput Flee(KinematicState own, KinematicState target)
        {
            SteeringOutput steering;

            steering = Seek(own, target);
            steering.linearAcceleration *= -1;             // reverse sense of acceleration

            return(steering);
        }
Example #25
0
        public static SteeringOutput GetSteering(KinematicState ownKS, ref SWander info)
        {
            info.m_targetOrientation           += info.m_wanderRate * MathExtent.Binomial();
            SURROGATE_TARGET.transform.position = MathExtent.AngleToVector(info.m_targetOrientation) * info.m_wanderRadius;

            SURROGATE_TARGET.transform.position += ownKS.m_position + MathExtent.AngleToVector(ownKS.m_orientation) * info.m_wanderOffset;

            return(Seek.GetSteering(ownKS, SURROGATE_TARGET));
        }
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target,
                                                 float targetAngularRadius       = 2f,
                                                 float slowDownAngularRadius     = 10f,
                                                 float timeToDesiredAngularSpeed = 0.1f)
        {
            SteeringOutput result = new SteeringOutput();

            result.linearActive  = false;            // this is not a linear steering
            result.angularActive = true;             // this is a rotational steering


            float requiredAngularSpeed;
            float targetOrientation = target.transform.eulerAngles.z;             // BEWARE...

            float requiredRotation = targetOrientation - ownKS.orientation;       // how many degs do we have to rotate?

            if (requiredRotation < 0)
            {
                requiredRotation = 360 + requiredRotation;                 // map to positive angles
            }
            if (requiredRotation > 180)
            {
                requiredRotation = -(360 - requiredRotation);                 // don't rotate more than 180 degs. just reverse rotation sense
            }
            // when here, required rotation is in [-180, +180]

            float rotationSize = Mathf.Abs(requiredRotation);

            if (rotationSize <= targetAngularRadius)             // if we're "there", no steering needed
            {
                return(NULL_STEERING);
            }


            if (rotationSize > slowDownAngularRadius)
            {
                requiredAngularSpeed = ownKS.maxAngularSpeed;
            }
            else
            {
                requiredAngularSpeed = ownKS.maxAngularSpeed * (rotationSize / slowDownAngularRadius);
            }

            // restablish sign
            requiredAngularSpeed = requiredAngularSpeed * Mathf.Sign(requiredRotation);

            // compute acceleration
            result.angularAcceleration = (requiredAngularSpeed - ownKS.angularSpeed) / timeToDesiredAngularSpeed;
            // clip if necessary
            if (Mathf.Abs(result.angularAcceleration) > ownKS.maxAngularAcceleration)
            {
                result.angularAcceleration = ownKS.maxAngularAcceleration * Mathf.Sign(result.angularAcceleration);
            }

            return(result);
        }
Example #27
0
        public static SteeringOutput GetSteering(KinematicState ownKS)
        {
            // just "seek" your own direction

            Vector3 myDirection = Utils.OrientationToVector(ownKS.orientation);

            SURROGATE_TARGET.transform.position = ownKS.position + myDirection;

            return(Seek.GetSteering(ownKS, SURROGATE_TARGET));
        }
        public static SteeringOutput GetSteering(KinematicState me, GameObject target, float distance, float angle)
        {
            float   desiredAngle = target.transform.eulerAngles.z + angle;
            Vector3 desiredDirectionFromTarget = Utils.OrientationToVector(desiredAngle).normalized;

            desiredDirectionFromTarget         *= distance;
            SURROGATE_TARGET.transform.position = desiredDirectionFromTarget + target.transform.position;

            return(Arrive.GetSteering(me, SURROGATE_TARGET, 0, 5, 0.1f));
        }
Example #29
0
        public static SteeringOutput GetSteering(KinematicState ownKS)
        {
            SteeringOutput result = new SteeringOutput();

            result.angularActive = true;
            Vector3 desiredDirection        = Vector3.zero;
            float   desiredAngularDirection = 0;

            // UP is MOVE FORWARD.
            // DOWN is MOVE BACKWARDS.
            if (Input.GetKey(KeyCode.UpArrow))
            {
                desiredDirection += Utils.OrientationToVector(ownKS.orientation);
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                desiredDirection += -Utils.OrientationToVector(ownKS.orientation);
            }



            if (Input.GetKey(KeyCode.LeftArrow))
            {
                desiredAngularDirection += 1f;
            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                desiredAngularDirection += -1f;
            }


            // Beware: this part of the code tampers with the speed...
            if (desiredAngularDirection == 0)
            {
                ownKS.angularSpeed = 0;                  // stop!!!
                if (desiredDirection.magnitude < 0.01f)
                {
                    ownKS.linearVelocity = Vector3.zero;                     // stop!!
                }
            }

            if (desiredAngularDirection == 0f)
            {
                result.angularActive = false;
            }
            if (desiredDirection.magnitude < 0.01f)
            {
                result.linearActive = false;
            }

            result.linearAcceleration  = desiredDirection * ownKS.maxAcceleration;
            result.angularAcceleration = desiredAngularDirection * ownKS.maxAngularAcceleration;

            return(result);
        }
Example #30
0
        public static SteeringOutput GetSteering(KinematicState ownKS)
        {
            SteeringOutput result = new SteeringOutput();

            result.angularAcceleration = ownKS.maxAngularAcceleration;
            SteeringOutput r2 = GoWhereYouLook.GetSteering(ownKS);

            result.linearAcceleration = r2.linearAcceleration;

            return(result);
        }