Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to interpolate between two rotation matrices?

Credits go to Evert, Kirr, and miran for helping out!
This thread is locked; no one can reply to it. rss feed Print
How to interpolate between two rotation matrices?
nonnus29
Member #2,606
August 2002
avatar

Right, so I've been reading up on skeletal animation and it all makes sense except for when it comes to applying the movement. If a model is keyframed then you have to apply the matrices to the vertices incrementally or it'd look like crap. So how do you interpolate a 4x4 matrix? Is it as simple as interpolating each of the elements of the matrix between the start and end matrix elements?

Erm, if that doesn't make sense let me know...

Edit; I gave credit once but it won't let me again? ???

Kirr
Member #5,060
September 2004
avatar

exquat.c

--
"Go to the NW of Stonemarket Plaza to the Blue Glyph Keeper Library door and enter."
- Lunabean's Thief Deadly Shadows Walkthrough, day eight

miran
Member #2,407
June 2002

Yeah, you can't just interpolate between matrices like that. You'd get totally unpredictable results. The first thing you'd think to do would be to interpolate between Euler angles and construct intermediate matrices at every step. This is very simple and easy to code, but has some negative side effects so you unless you want to see what those are, you shouldn't even try to implement it. Instead you should use quaternions, as Kirr said. Allegro conveniently has all the functions you need, and even an example...

--
sig used to be here

Evert
Member #794
November 2000
avatar

Quote:

So how do you interpolate a 4x4 matrix? Is it as simple as interpolating each of the elements of the matrix between the start and end matrix elements?

No. What you want to do is interpolating between a set of rotation angles (typically two or three for a rotation in three-dimensional space). Interpolating between the matrix elements is a bad idea because they are not independent (although if they are `near to eachother' in the sense that the rotation angles are `close' (modulo 2pi) you should not introduce too big an error).

You can look into Euler angles, but a better idea would indeed be to use quaternions instead: they can be interpolated more easily and directly.

nonnus29
Member #2,606
August 2002
avatar

Ah, so this is what quaternions are used for....

Quote:

Allegro conveniently has all the functions you need, and even an example...

:)

Tobias Dammers
Member #2,604
August 2002
avatar

Yep. You could interpolate matrices, but the result would be quite different from what you expect. Imagine you have two versions of your object, one before and one after the rotation. You interpolate between these two per vertex; a bit like morphing and not at all desirable. Each vertex will move to its new position on a straight line.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

gillius
Member #119
April 2000

Everyone is right somewhat. It is impossible to "interpolate" a matrix in the general case and get a rational result. Tobias says you could do it, and he is right, but the result would be bad, as he said.

So Evert is also right in the general case, although you can convert a matrix to a quanternion if and only if you know that the matrix is precisely a rotation matrix only. This is important if your keyframe generator generated only matrices as keyframes. You might be able to pull out the components needed to generate quanternions, which should be possible if you know that there is no scaling and only rotation and translation.

Gillius
Gillius's Programming -- http://gillius.org/

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

You might be able to pull out the components needed to generate quanternions, which should be possible if you know that there is no scaling and only rotation and translation.

...which is what allegro's quat_to_matrix() does for you. It extracts rotation information from the matrix and transforms it into a quaternion. You can manually grab and interpolate the translation part, but you won't be able to use the scaling part.
Although for skeleton animation, Euler angles might be just fine. Maybe consider storing the joint status as angles rather than matrices?

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

nonnus29
Member #2,606
August 2002
avatar

That's probably what I'll end up doing; this will be for the lwjgl 16k contest. So what I'm planning on doing is loading one set of vertices for the character, then tranform each joint for an animation frame and store the resulting set of vertices. And then simply morph the mesh between frames. This has the benefit that I'll at most need 2 angles per joint which can be stored in a byte (1 byte per angle). Aren't I clever?

Go to: