• Create Account

# How do i implement linear interpolation

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Ronan085  Members   -  Reputation: 169

Like
0Likes
Like

Posted 12 May 2013 - 02:04 PM

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.

### #2Álvaro  Crossbones+   -  Reputation: 13914

Like
2Likes
Like

Posted 12 May 2013 - 02:49 PM

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.

### #3Squared'D  Members   -  Reputation: 2258

Like
1Likes
Like

Posted 12 May 2013 - 07:38 PM

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, 12 May 2013 - 07:38 PM.

Learn all about my current projects and watch some of the game development videos that I've made.

Squared Programming Home

New Personal Journal

### #4Ronan085  Members   -  Reputation: 169

Like
0Likes
Like

Posted 13 May 2013 - 12:28 PM

@Á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

### #5slicer4ever  Crossbones+   -  Reputation: 3983

Like
1Likes
Like

Posted 13 May 2013 - 04:22 PM

@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, 13 May 2013 - 04:23 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

### #6Ronan085  Members   -  Reputation: 169

Like
0Likes
Like

Posted 14 May 2013 - 06:39 AM

@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

### #7JTippetts  Moderators   -  Reputation: 8652

Like
1Likes
Like

Posted 14 May 2013 - 06:49 AM

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".

### #8Álvaro  Crossbones+   -  Reputation: 13914

Like
3Likes
Like

Posted 14 May 2013 - 07:21 AM

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).

### #9belfegor  Crossbones+   -  Reputation: 2719

Like
0Likes
Like

Posted 14 May 2013 - 07:41 AM

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?

### #10Ronan085  Members   -  Reputation: 169

Like
0Likes
Like

Posted 14 May 2013 - 11:43 AM

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

### #11Álvaro  Crossbones+   -  Reputation: 13914

Like
0Likes
Like

Posted 14 May 2013 - 12:14 PM

[...] 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.

### #12BGB  Crossbones+   -  Reputation: 1554

Like
0Likes
Like

Posted 15 May 2013 - 01:12 PM

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, 15 May 2013 - 01:35 PM.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS