# How do i implement linear interpolation

## Recommended Posts

Ronan085    169

Hey

Does anybody know how to properly implement linear interpolation for skeletal animation i just cant seem to get it to work

Ive been getting some very interesting results when ive tried different ways eg model exploding apart when the animations starts, random translations and rotations happening

If anybody wants to see any of the codes from my attempts at it just ask

Any help would be greatly appreciated.

##### Share on other sites
alvaro    21247
How are you representing the transformation that brings you from the frame of reference of the parent bone to the frame of reference of this bone? You probably want to use quaternions for the rotation part, and then use slerp or nlerp.

##### Share on other sites
Squared'D    2427

The equation for linear interpolation is quite easy.

x = x_start + ((x_final - x_start) * time)

where time is a number between 0 and 1.

However; as Alvaro said, this equation doesn't work well with rotations. For rotations, you should look into quaternions.

Here's an article with some information.

Edited by Squared'D

##### Share on other sites
Ronan085    169

@Álvaro The file format im using is based off collada so the transformations are represented by 4x4 matrixs

@Squared'D ya ive been using that equation but the result are not what im expecting, my file file format has specified that all transformation interpolation should be linear

##### Share on other sites
slicer4ever    6760

@Squared'D ya ive been using that equation but the result are not what im expecting, my file file format has specified that all transformation interpolation should be linear

rotating around something is not linear, since a rotation imply's movement in an arc around a point, rather than a straight line. doing slerp(sperhical linear interpolation) isn't easy with matrixs, quaternions simplify the mathematics(but are a bit hard to understand if you don't know complex numbers(still it can be a bit difficult to comprehend even then imo)) Edited by slicer4ever

##### Share on other sites
Ronan085    169

@Squared'D ya ive been using that equation but the result are not what im expecting, my file file format has specified that all transformation interpolation should be linear

rotating around something is not linear, since a rotation imply's movement in an arc around a point, rather than a straight line. doing slerp(sperhical linear interpolation) isn't easy with matrixs, quaternions simplify the mathematics(but are a bit hard to understand if you don't know complex numbers(still it can be a bit difficult to comprehend even then imo))

How are you representing the transformation that brings you from the frame of reference of the parent bone to the frame of reference of this bone? You probably want to use quaternions for the rotation part, and then use slerp or nlerp.

Ok so where do i get this rotation information if the only transformation information i have is stored in one matrix

##### Share on other sites
JTippetts    12950
Some googling for "matrix to quaternion conversion" turned up http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/ which goes through the math and presents Java and C++ code for building a quaternion from a rotation matrix. Similar googling can be performed for "quaternion slerp" and "quaternion to matrix conversion".

##### Share on other sites
alvaro    21247

Ok so where do i get this rotation information if the only transformation information i have is stored in one matrix

The 4x4 matrix of an affine transformation is compose of a 3x3 matrix that represents a linear mapping (in the case of posture data this is a rotation) and a translation vector (a column to the right of the 3x3 matrix or a row below it, depending on whether you are using column vectors or row vectors). The other four components are always "0 0 0 1".

Extract the rotation and the translation separately and convert the rotation to a quaternion (see JTippetts's post above).

##### Share on other sites
belfegor    2834

I have noobish questions if you don't mind me learn something, since this is somehow related.

1.

Affine transformation matrix is like "world" matrix used to transform vertices?

For example i want to transform some point with that kind of matrix (dx, row major):

pout->x = pm->m[0][0] * pin->x + pm->m[1][0] * pin->y + pm->m[2][0] * pin->z + pm->m[3][0];
pout->y = pm->m[0][1] * pin->x + pm->m[1][1] * pin->y + pm->m[2][1] * pin->z + pm->m[3][1];
pout->z = pm->m[0][2] * pin->x + pm->m[1][2] * pin->y + pm->m[2][2] * pin->z + pm->m[3][2];


2.

Non-affine is like projection (and view?) matrix?

So to transform point with that it is bit different:

FLOAT norm;
norm = 1.0f / (pm->m[0][3] * pin->x + pm->m[1][3] * pin->y + pm->m[2][3] *pin->z + pm->m[3][3]);
pout->x = (pm->m[0][0] * pin->x + pm->m[1][0] * pin->y + pm->m[2][0] * pin->z + pm->m[3][0]) * norm;
pout->y = (pm->m[0][1] * pin->x + pm->m[1][1] * pin->y + pm->m[2][1] * pin->z + pm->m[3][1]) * norm;
pout->z = (pm->m[0][2] * pin->x + pm->m[1][2] * pin->y + pm->m[2][2] * pin->z + pm->m[3][2]) * norm;


3. For directions/vectors i can remove adding translation part of matrix?

##### Share on other sites
Ronan085    169

Some googling for "matrix to quaternion conversion" turned up http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/ which goes through the math and presents Java and C++ code for building a quaternion from a rotation matrix. Similar googling can be performed for "quaternion slerp" and "quaternion to matrix conversion".

really nice article thanks, thankfully XNAMath has a nice function XMQuaternionSlerp() so that will save me a ton of work;

Ok so where do i get this rotation information if the only transformation information i have is stored in one matrix

The 4x4 matrix of an affine transformation is compose of a 3x3 matrix that represents a linear mapping (in the case of posture data this is a rotation) and a translation vector (a column to the right of the 3x3 matrix or a row below it, depending on whether you are using column vectors or row vectors). The other four components are always "0 0 0 1".

Extract the rotation and the translation separately and convert the rotation to a quaternion (see JTippetts's post above).

thanks i was very clueless about how the different information was sorted in the matrix, any idea where the scaling information is

##### Share on other sites
alvaro    21247

[...] any idea where the scaling information is

It's also in the 3x3 matrix. That matrix can be any linear mapping, which besides rotations also allow for things like scalings, taking mirror images and shearing mappings. But I prefer to force them to be rotations for most situations.

##### Share on other sites
cr88192    1570
FWIW: one trick I had used before for interpolating matrices directly was to identify a common rotation axis, and then rotate all of the vectors in the matrix (as points) along this axis.

typically this axis was found/approximated by finding which unit axis changed the least during the movement, interpolating this axis (by treating it as an arc along the plane formed by its 2 positions and the origin), and then using this interpolated axis to rotate the matrix by (trying to rotate the source-matrix into the destination matrix).

(there may be better ways to calculate this axis though).

the math gets hairy, and there may be a better and more accurate ways to do this, but in my uses it seemed to work ok (much better than linearly interpolating them at least...).

(I can imaging a few ugly hacks though to possibly boost accuracy, but decided against elaborating on them). Edited by cr88192