Example #1
0
    public override Vector2 CalculateSteering(MovingEntity vehicle)
    {
        Vector2 steeringForce = new Vector2();

        if (seek)
        {
            steeringForce += SteeringBehaviors.Seek(vehicle);
        }
        if (flee)
        {
            steeringForce += SteeringBehaviors.Flee(vehicle);
        }
        if (arrive)
        {
            steeringForce += SteeringBehaviors.Arrive(vehicle, SteeringBehaviors.Deceleration.normal);
        }
        if (separation)
        {
            steeringForce += SteeringBehaviors.Separation(vehicle) * 3;
        }
        if (pursuit)
        {
            steeringForce += SteeringBehaviors.Pursuit(vehicle);
        }

        return(steeringForce);
    }
    Vector3 PathFollowing(SimplePath path)
    {
        Vector3 predictedLocation = cachedTransform.position + velocity.normalized * predictionDistance;// maxSpeed;

        predictedLocationMarker.position = predictedLocation;

        float   shortestSqrMagnitude    = Mathf.Infinity;
        int     startIndexOfClosestLine = -1;
        Vector3 closestNormalPoint      = Vector3.zero;

        // Find the closest normal point by checking against all lines in path.
        for (int i = 0; i < path.points.Count - 1; i++)
        {
            Vector3 normalPoint = ComputeClampedNormalPoint(predictedLocation, path.points[i], path.points[i + 1]);

            float sqrMagnitudeAB = (path.points[i + 1] - path.points[i]).sqrMagnitude;

            // The normal cannot be between A & B if its distance to A or B exceeds the length of AB.
            if (sqrMagnitudeAB < (normalPoint - path.points[i]).sqrMagnitude)
            {
                normalPoint = path.points[i + 1];
            }
            else if (sqrMagnitudeAB < (normalPoint - path.points[i + 1]).sqrMagnitude)
            {
                normalPoint = path.points[i];
            }

            float sqrMagnitudeFromNormal = (predictedLocation - normalPoint).sqrMagnitude;
            if (sqrMagnitudeFromNormal < shortestSqrMagnitude)
            {
                shortestSqrMagnitude    = sqrMagnitudeFromNormal;
                startIndexOfClosestLine = i;
                closestNormalPoint      = normalPoint;
            }
        }

        // If no valid noraml could be found, redirect the boid to the start of the path.
        if (startIndexOfClosestLine == -1)
        {
            return(SteeringBehaviors.Seek(cachedTransform.position, velocity, maxSpeed, path.points[0]));
        }

        normalPointMarker.position = closestNormalPoint;

        Vector3 pathDirection    = path.points[startIndexOfClosestLine + 1] - path.points[startIndexOfClosestLine];
        Vector3 futurePathTarget = closestNormalPoint + pathDirection.normalized;// * predictionDistance;

        futurePathTargetMarker.position = futurePathTarget;

        if (shortestSqrMagnitude > path.radius * path.radius)
        {
            return(SteeringBehaviors.Seek(cachedTransform.position, velocity, maxSpeed, futurePathTarget));
        }
        else // No steering required
        {
            return(Vector3.zero);
        }
    }
Example #3
0
    private void SeekTarget(Vector3 targetPosition)
    {
        if (!IsAvoiding())
        {
            Vector3 steering = steeringBehaviors.Seek(body, targetPosition) * VELOCITY;
            movement.KeepConstantVelocity(body, VELOCITY);

            body.AddForce(steering);
        }
    }
Example #4
0
    public override Vector2 CalculateSteering(MovingEntity vehicle)
    {
        Vector2 steeringForce = Vector2.zero;
        Vector2 force;

        if (separation)
        {
            force = SteeringBehaviors.Separation(vehicle) * 1.0f;
            if (AccumulateForce(vehicle, ref steeringForce, force) && force != Vector2.zero)
            {
                return(steeringForce);
            }
        }
        if (seek)
        {
            force = SteeringBehaviors.Seek(vehicle) * 1.0f;
            if (AccumulateForce(vehicle, ref steeringForce, force))
            {
                return(steeringForce);
            }
        }
        if (flee)
        {
            force = SteeringBehaviors.Flee(vehicle) * 1.0f;
            if (AccumulateForce(vehicle, ref steeringForce, force))
            {
                return(steeringForce);
            }
        }
        if (arrive)
        {
            force = SteeringBehaviors.Arrive(vehicle) * 1.0f;
            if (AccumulateForce(vehicle, ref steeringForce, force))
            {
                return(steeringForce);
            }
        }
        if (pursuit)
        {
            force = SteeringBehaviors.Pursuit(vehicle) * 1.0f;
            if (AccumulateForce(vehicle, ref steeringForce, force))
            {
                return(steeringForce);
            }
        }

        return(steeringForce);
    }
Example #5
0
    private void FixedUpdate()
    {
        var steeringForce = new Vector2();

        if (Target != null && settings.Seek > 0)
        {
            steeringForce = settings.Seek * SteeringBehaviors.Seek(this, Target.position);
        }

        foreach (var boid in allBoids)
        {
            if ((object)boid != this)
            {
                if ((boid.GetPosition() - GetPosition()).sqrMagnitude < 1f)
                {
                    if (settings.Flee > 0)
                    {
                        steeringForce += settings.Flee * SteeringBehaviors.Flee(this, boid.GetPosition()) / ((Vector2)transform.position - boid.GetPosition()).magnitude;
                    }
                    if (settings.GetClose > 0)
                    {
                        steeringForce += settings.GetClose * SteeringBehaviors.Seek(this, boid.GetPosition());
                    }
                    ;
                    if (settings.Align > 0)
                    {
                        steeringForce += settings.Align * SteeringBehaviors.Aling(this, boid);
                    }
                }
            }
        }

        _rigidbody.AddForce(Vector2.ClampMagnitude(steeringForce, _maxForce));

        TurnTowardsVelocity();
    }
    public void UpdateSteering()
    {
        steeringVector = transform.forward;

        if (isSeeking && targetTransform != null)
        {
            steeringVector = SteeringBehaviors.Seek(cachedTransform.position, velocity, maxSpeed, targetTransform.position);
        }

        if (isFleeing && targetTransform != null)
        {
            steeringVector = SteeringBehaviors.Flee(cachedTransform.position, velocity, maxSpeed, targetTransform.position);
        }

        if (isPursuing && movingTarget != null)
        {
            steeringVector = SteeringBehaviors.Pursuit(cachedTransform.position, velocity, maxSpeed, movingTarget.transform.position, movingTarget.velocity);
        }

        if (isEvading && movingTarget != null)
        {
            steeringVector = SteeringBehaviors.Evasion(cachedTransform.position, velocity, maxSpeed, movingTarget.transform.position, movingTarget.velocity);
        }

        if (isArriving && targetTransform != null)
        {
            steeringVector = SteeringBehaviors.Arrival(cachedTransform.position, velocity, maxSpeed, targetTransform.position, arrivalSlowingDistance);
        }

        if (IsWandering)
        {
            steeringVector = Wander(0.5f, 0.1f, false);
        }

        if (isLeaderFollowing)
        {
            Vector3 leaderFollowPoint = leader.transform.position + leader.transform.forward * -2f;
            steeringVector = SteeringBehaviors.Arrival(cachedTransform.position, velocity, maxSpeed, leaderFollowPoint, arrivalSlowingDistance);
        }

        if (isPathFollowing)
        {
            Vector3 possibleSteeringVector = PathFollowing(simplePath);

            if (possibleSteeringVector != Vector3.zero)
            {
                steeringVector = possibleSteeringVector;
            }
        }

        if (isSeperating)
        {
            Vector3 repulsionLocation = cachedTransform.position + Seperation();

            if (repulsionLocation != cachedTransform.position)
            {
                Vector3 desiredVelocity = (repulsionLocation - cachedTransform.position).normalized * maxSpeed;

                steeringVector = desiredVelocity - velocity;
            }
        }

        if (isAvoidingCollision)
        {
            Vector3 collisionSteeringVector = CollisionAvoidance();

            if (collisionSteeringVector != velocity)
            {
                if (collisionSteeringVector == transform.forward * maxSpeed && steeringVector != transform.forward)
                {
                    // The previously applied steering vector should not be modified.
                }
                else
                {
                    steeringVector = collisionSteeringVector;
                }
            }
        }

        if (velocity != Vector3.zero) //Check is to prevent the log message "Look rotation viewing vector is zero"
        {
            transform.rotation = Quaternion.LookRotation(velocity);
        }

        UpdateMovement();
    }