Engines/FlatRedBallXNA/FlatRedBall.Content/ModelAnimation/ContentUtil.cs
/// <summary>
/// Roughly decomposes two matrices and performs spherical linear interpolation
/// </summary>
/// <param name="start">Source matrix for interpolation</param>
/// <param name="end">Destination matrix for interpolation</param>
/// <param name="slerpAmount">Ratio of interpolation</param>
/// <returns>The interpolated matrix</returns>
Roughly decomposes two matrices and performs spherical linear interpolation
Roughly decomposes two matrices and performs spherical linear interpolation
public static Matrix SlerpMatrix(Matrix start, Matrix end,
            float slerpAmount)
        {
            Quaternion.CreateFromRotationMatrix(ref start, out qStart);
            Quaternion.CreateFromRotationMatrix(ref end, out qEnd);
            Quaternion.Lerp(ref qStart, ref qEnd, slerpAmount, out qResult);
            curTrans.X = start.M41;
            curTrans.Y = start.M42;
            curTrans.Z = start.M43;
            nextTrans.X = end.M41;
            nextTrans.Y = end.M42;
            nextTrans.Z = end.M43;
            Vector3.Lerp(ref curTrans, ref nextTrans, slerpAmount, out lerpedTrans);
            Matrix.CreateFromQuaternion(ref qStart, out startRotation);
            Matrix.CreateFromQuaternion(ref qEnd, out endRotation);
            curScale.X = start.M11 - startRotation.M11;
            curScale.Y = start.M22 - startRotation.M22;
            curScale.Z = start.M33 - startRotation.M33;
            nextScale.X = end.M11 - endRotation.M11;
            nextScale.Y = end.M22 - endRotation.M22;
            nextScale.Z = end.M33 - endRotation.M33;
            Vector3.Lerp(ref curScale, ref nextScale, slerpAmount, out lerpedScale);
            Matrix.CreateFromQuaternion(ref qResult, out returnMatrix);
            returnMatrix.M41 = lerpedTrans.X;
            returnMatrix.M42 = lerpedTrans.Y;
            returnMatrix.M43 = lerpedTrans.Z;
            returnMatrix.M11 += lerpedScale.X;
            returnMatrix.M22 += lerpedScale.Y;
            returnMatrix.M33 += lerpedScale.Z;
            return returnMatrix;
        }
public static Matrix SlerpMatrix ( Matrix start , Matrix end , float slerpAmount ) { Quaternion . CreateFromRotationMatrix ( ref start , out qStart ) ; Quaternion . CreateFromRotationMatrix ( ref end , out qEnd ) ; Quaternion . Lerp ( ref qStart , ref qEnd , slerpAmount , out qResult ) ; curTrans . X = start . M41 ; curTrans . Y = start . M42 ; curTrans . Z = start . M43 ; nextTrans . X = end . M41 ; nextTrans . Y = end . M42 ; nextTrans . Z = end . M43 ; Vector3 . Lerp ( ref curTrans , ref nextTrans , slerpAmount , out lerpedTrans ) ; Matrix . CreateFromQuaternion ( ref qStart , out startRotation ) ; Matrix . CreateFromQuaternion ( ref qEnd , out endRotation ) ; curScale . X = start . M11 - startRotation . M11 ; curScale . Y = start . M22 - startRotation . M22 ; curScale . Z = start . M33 - startRotation . M33 ; nextScale . X = end . M11 - endRotation . M11 ; nextScale . Y = end . M22 - endRotation . M22 ; nextScale . Z = end . M33 - endRotation . M33 ; Vector3 . Lerp ( ref curScale , ref nextScale , slerpAmount , out lerpedScale ) ; Matrix . CreateFromQuaternion ( ref qResult , out returnMatrix ) ; returnMatrix . M41 = lerpedTrans . X ; returnMatrix . M42 = lerpedTrans . Y ; returnMatrix . M43 = lerpedTrans . Z ; returnMatrix . M11 += lerpedScale . X ; returnMatrix . M22 += lerpedScale . Y ; returnMatrix . M33 += lerpedScale . Z ; return returnMatrix ; }
Roughly decomposes two matrices and performs spherical linear interpolation
Roughly decomposes two matrices and performs spherical linear interpolation
// Get rotation components and interpolate (not completely accurate but I don't want  // to get into polar decomposition and this seems smooth enough) // Get final translation components // Get final scale component // Create the rotation matrix from the slerped quaternions // Set the translation // And the lerped scale component
The interpolated matrix
The interpolated matrix
start
Matrix
Source matrix for interpolation
Source matrix for interpolation
end
Matrix
Destination matrix for interpolation
Destination matrix for interpolation
slerpAmount
float
Ratio of interpolation
Ratio of interpolation