Пример #1
0
    /// <summary>
    /// Should the force be calculated?
    /// </summary>
    /// <returns>
    /// A <see cref="Vector3"/>
    /// </returns>
    protected override Vector3 CalculateForce()
    {
        if (_path == null)
        {
            return(Vector3.zero);
        }

        // our goal will be offset from our path distance by this amount
        float pathDistanceOffset = (int)_direction * _predictionTime * Vehicle.Speed;

        // predict our future position
        Vector3 futurePosition = Vehicle.PredictFuturePosition(_predictionTime);

        // measure distance along path of our current and predicted positions
        float nowPathDistance    = _path.mapPointToPathDistance(Vehicle.Position);
        float futurePathDistance = _path.mapPointToPathDistance(futurePosition);

        // are we facing in the correction direction?
        bool rightway = ((pathDistanceOffset > 0) ? (nowPathDistance < futurePathDistance) : (nowPathDistance > futurePathDistance));

        // find the point on the path nearest the predicted future position
        // XXX need to improve calling sequence, maybe change to return a
        // XXX special path-defined object which includes two Vector3s and a
        // XXX bool (onPath,tangent (ignored), withinPath)
        mapReturnStruct tStruct = new mapReturnStruct();

        _path.mapPointToPath(futurePosition, ref tStruct);


        // no steering is required if (a) our future position is inside
        // the path tube and (b) we are facing in the correct direction
        if ((tStruct.outside < 0) && rightway)
        {
            // all is well, return zero steering
            return(Vector3.zero);
        }
        else
        {
            // otherwise we need to steer towards a target point obtained
            // by adding pathDistanceOffset to our current path position

            float   targetPathDistance = nowPathDistance + pathDistanceOffset;
            Vector3 target             = _path.mapPathDistanceToPoint(targetPathDistance);

            // return steering to seek target on path
            return(Vehicle.GetSeekVector(target));
        }
    }
    protected override Vector3 CalculateForce()
    {
        if (this._path == null)
        {
            return(Vector3.get_zero());
        }
        float           num             = (float)this._direction * this._predictionTime * base.Vehicle.Speed;
        Vector3         point           = base.Vehicle.PredictFuturePosition(this._predictionTime);
        float           num2            = this._path.mapPointToPathDistance(base.Vehicle.Position);
        float           num3            = this._path.mapPointToPathDistance(point);
        bool            flag            = (num <= 0f) ? (num2 > num3) : (num2 < num3);
        mapReturnStruct mapReturnStruct = default(mapReturnStruct);

        this._path.mapPointToPath(point, ref mapReturnStruct);
        if (mapReturnStruct.outside < 0f && flag)
        {
            return(Vector3.get_zero());
        }
        float   pathDistance = num2 + num;
        Vector3 target       = this._path.mapPathDistanceToPoint(pathDistance);

        return(base.Vehicle.GetSeekVector(target));
    }