/// <summary>
        /// Gets the tangent at a point along the curve.
        /// </summary>
        /// <returns>The tangent at the given time.</returns>
        /// <param name="coeffs">The precalculated coefficients of the curve.</param>
        /// <param name="t">
        /// The time along the curve, where t=0 is the start point, and t=1 is the end point. Note that t can be extrapolated
        /// beyond these bounds.
        /// </param>
        public static Vector3 GetTangent(this CubicBezierTangentCoefficients coeffs, double t)
        {
            Vector3 velocity = (float)(t * t) * coeffs.Coeff1 + ((float)t) * coeffs.Coeff2 + coeffs.Coeff3;

            // If the velocity is 0, we look at the acceleration of the path to determine which direction the path is about
            // to travel to.
            if (velocity.magnitude < float.Epsilon)
            {
                Vector3 acceleration = coeffs.GetAcceleration(t);
                // If acceleration is 0, the entire path is probably a single point.
                if (acceleration.magnitude < float.Epsilon)
                {
                    if (coeffs.Coeff1.magnitude < float.Epsilon)
                    {
                        return(Vector3.forward);
                    }
                    return(coeffs.Coeff1.normalized);
                }
                return(acceleration.normalized);
            }
            return(velocity.normalized);
        }
Exemple #2
0
        /// <summary>
        /// Gets a normal for a given segment. Note that there are an infinite number of possible normals, this method just
        /// produces a locally consistent normal.
        /// </summary>
        /// <returns>A normal at a given point.</returns>
        /// <param name="segment">The segment to calculate the tangent of.</param>
        /// <param name="segment">The precalculated coefficients of the curve.</param>
        /// <param name="t">
        /// The time along the curve, where t=0 is the start point, and t=1 is the end point. Note that t can be extrapolated
        /// beyond these bounds.
        /// </param>
        public static Vector3 GetNormal(this CubicBezierSegment segment, CubicBezierTangentCoefficients coeffs, double t)
        {
            Vector3 currentTangent = coeffs.GetTangent(t);

            Vector3 normal;

            if (currentTangent == Vector3.zero)
            {
                Vector3 acceleration = coeffs.GetAcceleration(t);
                currentTangent = acceleration;
            }

            // The tangent doesn't change, (such in the case of a straight line).
            // We pick a vector which shouldn't be parallel to the tangent, and
            // use the cross product with the tangent to find a normal.
            normal   = currentTangent;
            normal.x = -currentTangent.y;
            normal.y = -currentTangent.z;
            normal.z = currentTangent.x;

            normal = Vector3.Cross(currentTangent, normal).normalized;
            return(normal);
        }