# Matrix Interpolation

## Recommended Posts

Phy    139
Can anyone help me out? I need to interpolate two rotation matrices linearly, and the only data I have are the two matricies and a delta. I've been trying to convert them to quaternions and then back, but everything I read on teh subject is kinda grey and hard to understand.

##### Share on other sites
John Schultz    811
If you aren't ready for quaternions (there's plenty of example source code on the internet), you can convert the matrices to axis-angle format, resulting in a vector and a scalar, which you can interpolate directly.

An even simpler method is to interpolate two rows or columns of the matrices as vectors, then create a new matrix from the resulting two interpolated vectors: cross (product) the two vectors to get the missing third vector, then orhonormalize (normalize/cross or dot-project (Gram-Schmidt)) the vectors to create an orthogonal matrix (3 orthonormal vectors make up a rotation matrix).

The first method may produce better results, and quaternions give more options for interpolation (lerp (linear), slerp (spherical linear), squad (cubic)).

##### Share on other sites
Phy    139
Well I've tried to do it wih quaternions and axis-angles, but it looks like im toying with glScale on different axis :( I don't know what I'm doing wrong either.

##### Share on other sites
dodecahedron    152
If you have two 4x4 matricies X1 and X2, then you can linearly interpolate them by making 4x4=16 linear equations. Let x1 and x2 be the elements in the ith row and jth column of X1 and X2 respectively. Then with the parameter t, you have the equation:

f(t)=x1 + (x2 - x1)t

With such an equation for each pair of elements, you will have X1 when t=0 and X2 when t=1.

##### Share on other sites
jyk    2094
Here's another option to consider. You can get the same interpolation as you would with quaternion slerp (albeit at greater expense) by finding the matrix that rotates from A to B, extracting the axis and angle from this matrix, scaling the angle, and then recomposing the matrix from the axis and angle and multiplying with A.

I'm at work now, but if you're interested in this method I can post details later (or perhaps someone else will in the meantime).

##### Share on other sites
John Schultz    811
Quote:
 Original post by jykHere's another option to consider. You can get the same interpolation as you would with quaternion slerp (albeit at greater expense) by finding the matrix that rotates from A to B, extracting the axis and angle from this matrix, scaling the angle, and then recomposing the matrix from the axis and angle and multiplying with A.I'm at work now, but if you're interested in this method I can post details later (or perhaps someone else will in the meantime).

Linear interpolation (Vectors, scalars):
delta = b - a;
// alpha = [0..1]
c = a + delta*alpha

3x3 rotation matrix form (right-hand element evaluated first):

delta = b * transpose(a) // transpose(a) followed by b.
delta.getAxisAngle(axis,deltaAngle)
// alpha = [0..1]
c = axisAngleToMatrix(axis,deltaAngle*alpha) * a

inline void interpolate(const Mat3 & a,const Mat3 & b,flt alpha,Mat3 & c) {  Mat3 delta = b ^ !a; // ^ = matrix product, ! = matrix transpose.  Vec3 axis;  flt deltaAngle;  delta.getAxisAngle(axis,deltaAngle);  Mat3 rm(axis,deltaAngle*alpha);  c = rm ^ a;} // interpolate

##### Share on other sites
Phy    139
Thanks guys, that did it!