Esempio n. 1
0
    /** Generates a steering that dinamically aligns the orientation to the target orientation  */
    public static Steering Align(Kinematic source, Steering sourceSteering, float targetOrientation, AlignOptions opts)
    {
        Steering steering = new Steering();

        // Define objectives
        float rotation     = Kinematic.OrientDiff(source.orientation, targetOrientation);
        float rotationSize = System.Math.Abs(rotation);
        float targetRotation;

        // Define target rotation
        if (rotationSize < opts.targetRadius)
        {
            return(steering);
        }
        if (rotationSize > opts.slowRadius)
        {
            targetRotation = opts.maxRotation;
        }
        else
        {
            targetRotation = rotationSize * opts.maxRotation / opts.slowRadius;
        }

        // Define steering based on target rotation
        steering.angular  = Math.Min((targetRotation * Mathf.Sign(rotation) - sourceSteering.rotation) / opts.timeToTarget, opts.maxAngular);
        steering.rotation = sourceSteering.rotation + steering.angular * Time.deltaTime;

        return(steering);
    }