Exemplo n.º 1
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...
        }
        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);
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target)
        {
            SteeringOutput result = Seek.GetSteering(ownKS, target);

            result.linearAcceleration = -result.linearAcceleration;
            return(result);
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
0
        public static SteeringOutput GetSteering(KinematicState ownKS, Transform target)
        {
            SteeringOutput result = Seek.GetSteering(ownKS, target);

            result.m_linearAcceleration *= -1;

            return(result);
        }
Exemplo n.º 8
0
        protected override SteeringOutput GetSteering()
        {
            SteeringOutput result = Seek.GetSteering(m_ownKS, m_info.m_target);

            base.ApplyFacingPolicy(m_info.m_facingPolicy, result);

            return(result);
        }
Exemplo n.º 9
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));
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
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));
        }
Exemplo n.º 13
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));
        }
Exemplo n.º 14
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject attractor, float seekWeight,
                                                 ref float targetOrientation, float wanderRate = 30f,
                                                 float wanderRadius = 10f, float wanderOffset = 20f)
        {
            SteeringOutput seekOutput = Seek.GetSteering(ownKS, attractor);
            SteeringOutput result     = Wander.GetSteering(ownKS, ref targetOrientation, wanderRate, wanderRadius, wanderOffset);

            result.linearAcceleration = result.linearAcceleration * (1 - seekWeight) + seekOutput.linearAcceleration * seekWeight;
            // result.angularAcceleration = result.angularAcceleration * (1 - seekWeight) + seekOutput.angularAcceleration * seekWeight;

            return(result);
        }
Exemplo n.º 15
0
        public override SteeringOutput GetSteering()
        {
            SteeringOutput result = Seek.GetSteering(this.ownKS, this.target);

            if (ownKS.linearVelocity.magnitude > 0.001f)
            {
                transform.rotation = Quaternion.Euler(0, VectorToOrientation(ownKS.linearVelocity), 0);
                ownKS.orientation  = transform.rotation.eulerAngles.y;
            }
            result.angularActive = false;

            return(result);
        }
Exemplo n.º 16
0
        public static SteeringOutput GetSteering(KinematicState me, SKeepPosition info)
        {
            float targetOrientation = info.m_target.transform.eulerAngles.y;

            targetOrientation += info.m_requiredAngle;

            Vector3 finalTargetPosition = MathExtent.AngleToVector(targetOrientation).normalized;

            finalTargetPosition *= info.m_requiredDistance;

            SURROGATE_TARGET.position = info.m_target.position + finalTargetPosition;

            return(Seek.GetSteering(me, SURROGATE_TARGET));
        }
        public static SteeringOutput GetSteering(KinematicState ownKS, SSimpleObstacleAvoidance 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))
            {
                return(NULL_STEERING);
            }

            SURROGATE_TARGET.position = l_hitInfo.point + l_hitInfo.normal * info.m_avoidDistance;

            return(Seek.GetSteering(ownKS, SURROGATE_TARGET));
        }
Exemplo n.º 18
0
        public static SteeringOutput GetSteering(KinematicState ownKS, SIntercept info)
        {
            if (info.m_targetLinearVelocity == null)
            {
                return(NULL_STEERING);
            }

            float l_predictedTimeToTarget = (info.m_target.position - ownKS.m_position).magnitude / ownKS.m_linearVelocity.magnitude;

            l_predictedTimeToTarget = MathExtent.Clip(l_predictedTimeToTarget, info.m_maxPredictionTime);

            SURROGATE_TARGET.position = info.m_target.position + (info.m_targetLinearVelocity * l_predictedTimeToTarget);

            return(Seek.GetSteering(ownKS, SURROGATE_TARGET));
        }
Exemplo n.º 19
0
        public override SteeringOutput GetSteering()
        {
            // no KS? get it
            if (this.ownKS == null)
            {
                this.ownKS = GetComponent <KinematicState>();
            }

            if (this.target == null)
            {
                Debug.Log("Null target in Seek of " + this.gameObject);
            }

            return(Seek.GetSteering(this.ownKS, this.target));
        }
Exemplo n.º 20
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target,
                                                 float targetRadius = 5f, float slowDownRadius = 20f, float timeToDesiredSpeed = 0.1f)
        {
            SteeringOutput steering = new SteeringOutput();
            Vector3        directionToTarget;
            float          distanceToTarget;
            float          desiredSpeed;
            Vector3        desiredVelocity;
            Vector3        requiredAcceleration;

            // compute direction and distance to target
            directionToTarget = target.transform.position - ownKS.position;
            distanceToTarget  = directionToTarget.magnitude;

            // if we're already there, no steering required
            if (distanceToTarget < targetRadius)
            {
                return(null);
            }

            // if we're are far away from target, let's go with full acceleration (SEEK)
            // if we're getting closer speed has to be inversely proportional to distance
            if (distanceToTarget > slowDownRadius)
            {
                return(Seek.GetSteering(ownKS, target));
            }


            // if we're getting closer speed has to be inversely proportional to distance
            desiredSpeed = ownKS.maxSpeed * (distanceToTarget / slowDownRadius);

            // desired velocity is towards the target
            desiredVelocity = directionToTarget.normalized * desiredSpeed;

            // compute the acceleration required to get desiredVelocity in timeToDesiredSpeed
            // take into account that we already have a velocity
            requiredAcceleration = (desiredVelocity - ownKS.linearVelocity) / timeToDesiredSpeed;

            // if required acceleration is too high, clip it
            if (requiredAcceleration.magnitude > ownKS.maxAcceleration)
            {
                requiredAcceleration = requiredAcceleration.normalized * ownKS.maxAcceleration;
            }

            steering.linearAcceleration = requiredAcceleration;

            return(steering);
        }
Exemplo n.º 21
0
        public static SteeringOutput GetSteering(KinematicState ownKS, ref float targetOrientation, float wanderRate, float wanderRadius, float wanderOffset)
        {
            targetOrientation += wanderRate * (Random.value - Random.value);

            if (surrogateTarget)
            {
                surrogateTarget.transform.position = OrientationToVector(targetOrientation) * wanderRadius;
            }

            if (surrogateTarget)
            {
                surrogateTarget.transform.position += ownKS.position + OrientationToVector(ownKS.orientation) * wanderOffset;
            }

            return(Seek.GetSteering(ownKS, surrogateTarget));
        }
Exemplo n.º 22
0
        public static SteeringOutput GetSteering(KinematicState ownKS)
        {
            // just "seek" your own direction

            if (GoWhereYouLook.surrogateTarget == null)
            {
                GoWhereYouLook.surrogateTarget = new GameObject("Surrogate target for go where you look");
            }

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

            GoWhereYouLook.surrogateTarget.transform.position = ownKS.position + myDirection;


            return(Seek.GetSteering(ownKS, surrogateTarget));
        }
Exemplo n.º 23
0
        public override SteeringOutput GetSteering()
        {
            SteeringOutput seekOutput = Seek.GetSteering(ownKS, attractor);
            SteeringOutput result     = base.GetSteering();

            // beware, Flocking may return null. In that case, just apply seek

            if (result == null)
            {
                return(seekOutput);
            }

            result.linearAcceleration  = result.linearAcceleration * (1 - seekWeight) + seekOutput.linearAcceleration * seekWeight;
            result.angularAcceleration = result.angularAcceleration * (1 - seekWeight) + seekOutput.angularAcceleration * seekWeight;

            return(result);
        }
Exemplo n.º 24
0
        public static SteeringOutput GetSteering(KinematicState ownKS, Path path, ref int currentWaypointIndex, float wayPointReachedRadius)
        {
            // path shouldn't be neither null nor erroneous
            if (path == null)
            {
                Debug.LogError("PathFollowing invoked with null path");
                return(NULL_STEERING);
            }
            if (path.error)
            {
                Debug.LogError("PathFollowing invoked with null path");
                return(NULL_STEERING);
            }

            // if currentWaypoint is not valid, end of path has been reached
            if (path.vectorPath.Count == currentWaypointIndex)
            {
                return(NULL_STEERING);
            }

            // if we're "close" to the current waypoint try going to the next one
            float distance = (ownKS.position - path.vectorPath[currentWaypointIndex]).magnitude;

            if (distance <= wayPointReachedRadius)
            {
                currentWaypointIndex++;
            }

            if (path.vectorPath.Count == currentWaypointIndex)
            {
                return(NULL_STEERING);
            }

            SURROGATE_TARGET.transform.position = path.vectorPath[currentWaypointIndex];

            if (currentWaypointIndex == path.vectorPath.Count - 1)
            {
                // use arrive for the last waypoint
                return(Arrive.GetSteering(ownKS, SURROGATE_TARGET, wayPointReachedRadius / 2, wayPointReachedRadius * 2));
            }
            else
            {
                return(Seek.GetSteering(ownKS, SURROGATE_TARGET));
            }
        }
Exemplo n.º 25
0
        public static SteeringOutput GetSteering(KinematicState ownKS, string tag, float cohesionThreshold = 20f)
        {
            Vector3 centerOfMass = Vector3.zero;
            int     count        = 0;
            float   distanceToMate;

            // get all your mates (potential targets)
            GameObject [] mates = GameObject.FindGameObjectsWithTag(tag);

            // iterate to compute center of mass
            for (int i = 0; i < mates.Length; i++)
            {
                // skip yourself...
                if (mates [i] == ownKS.gameObject)
                {
                    continue;
                }

                // Only consider close mates. Disregard far ones
                distanceToMate = (mates[i].transform.position - ownKS.position).magnitude;
                if (distanceToMate < cohesionThreshold)
                {
                    centerOfMass = centerOfMass + mates [i].transform.position;
                    count++;
                }
            }


            if (count == 0)
            {
                return(null);
            }

            centerOfMass = centerOfMass / count;

            // generate a surrogate target and delegate to seek or arrive...
            if (surrogateTarget == null)
            {
                surrogateTarget = new GameObject("Surrogate target for cohesion");
            }
            surrogateTarget.transform.position = centerOfMass;

            return(Seek.GetSteering(ownKS, surrogateTarget));
        }
Exemplo n.º 26
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target, float RequiredDsitance)
        {
            Vector3 DirectionFromTarget;
            Vector3 DesiredPos;

            // Compute direction to target
            DirectionFromTarget = (ownKS.position - target.transform.position).normalized;
            DesiredPos          = target.transform.position + DirectionFromTarget * RequiredDsitance;


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


            return(Seek.GetSteering(ownKS, surrogateTarget));
        }
Exemplo n.º 27
0
        public override SteeringOutput GetSteering()
        {
            // no KS? get it
            if (this.ownKS == null)
            {
                this.ownKS = GetComponent <KinematicState>();
            }

            if (this.target == null)
            {
                Debug.Log("Null target in Seek of " + this.gameObject);
            }


            SteeringOutput result = Seek.GetSteering(this.ownKS, this.target);

            base.applyRotationalPolicy(rotationalPolicy, result, this.target);
            return(result);
        }
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject target,
                                                 float requieredDistance, float alpha)
        {
            // Y axis of the target
            Vector3 targetUp = target.transform.up;
            // We rotate the Y vector alpha angles on the Z axis
            Vector3 rotatedDir = Quaternion.Euler(0, 0, alpha) * targetUp;
            // We add the position of the target to the direction multiplied
            // by the distance to keep from the target
            Vector3 desiredPos = target.transform.position + (rotatedDir * requieredDistance);

            if (KeepDistanceVersatile.surrogateTarget == null)
            {
                KeepDistanceVersatile.surrogateTarget = new GameObject("surrogateee");
            }
            KeepDistanceVersatile.surrogateTarget.transform.position = desiredPos;

            return(Seek.GetSteering(ownKS, surrogateTarget));
        }
Exemplo n.º 29
0
        public static SteeringOutput GetSteering(KinematicState ownKS, GameObject attractor, float seekWeight = 0.2f, 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 = 023f)
        {
            SteeringOutput seekOutput = Seek.GetSteering(ownKS, attractor);
            SteeringOutput result     = Flocking.GetSteering(ownKS, idTag, cohesionThreshold, repulsionThreshold, wanderRate);

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

            result.linearAcceleration  = result.linearAcceleration * (1 - seekWeight) + seekOutput.linearAcceleration * seekWeight;
            result.angularAcceleration = result.angularAcceleration * (1 - seekWeight) + seekOutput.angularAcceleration * seekWeight;

            return(result);
        }
Exemplo n.º 30
0
        public static SteeringOutput GetSteering(KinematicState me, GameObject target, float distance, float angle)
        {
            // get the target's orientation (as an angle)...
            float targetOrientation = target.transform.rotation.eulerAngles.z;

            // ... add the required angle
            float requiredOrientation = targetOrientation + angle;

            // convert the orientation into a direction (convert from angle to vector)
            Vector3 requiredDirection = Utils.OrientationToVector(requiredOrientation).normalized;

            // determine required position
            Vector3 requiredPosition = target.transform.position + requiredDirection * distance;

            // place surrogate target in required position
            SURROGATE_TARGET.transform.position = requiredPosition;

            return(Seek.GetSteering(me, SURROGATE_TARGET));
            //return Arrive.GetSteering(me, SURROGATE_TARGET, 2, 5);
        }