#### Archived

This topic is now archived and is closed to further replies.

# Tweening using Matrices?

This topic is 5093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here's my situation: I have a static object that I want to move from its starting location/orientation to a target location/orientation. I have the matrices representing each, but I want to tween between them over a given time period (1000 milliseconds, for example). Is it possible to tween location and orientation using matrices? For example, could I do something like this (Semi-Pseudocode)
D3DXMATRIX StartMatrix;     //Where the object started

D3DXMATRIX CurrentMatrix;   //Where the object is now

D3DXMATRIX TargetMatrix;    //Where the object is going

D3DXMATRIX DifferenceMatrix;
long TweenStartTime;
long TweenEndTime;
float PercentComplete;        //How far complete is the tween?

PercentComplete = ((float)(TweenEndTime - TweenStartTime)/1000.0f);
DifferenceMatrix = TargetMatrix - StartMatrix;
CurrentMatrix = StartMatrix + (DifferenceMatrix * PercentComplete);

Does this look like it would work? If so, how would I do something like this in real code? Thanks in advance! [edited by - Teric on February 6, 2004 10:34:35 AM] [edited by - Teric on February 6, 2004 10:35:25 AM]

Anyone?

##### Share on other sites
You can''t intorpolate an orientation/transformation matrix.

What you can do is interpolate a quaternion using slerp.

You can represent your matricies as quaternions.

Hope that helps.

##### Share on other sites
Could you post an example of what you''re talking about?

You''re saying that if I convert my matrices to quaternions, then I can slerp between them?

##### Share on other sites
O.k, without getting deep into quaternions (there are good docs on this on gamedev, and other sites.)

But a quaternion records an axis and an orientation, so every matrix can be represented by a quaternion.

(A quick check in the docs pulled out this nice function)

D3DXQuaternionRotationMatrix

You transform your 2 rotation matricies to a quaternion. Now you can use D3DXQuaternionSlerp to interpolate between one and the other.

You can then use D3DXMatrixRotationQuaternion to get it back

This is why quaternions are powerful, you can't interpolate between two matricies, but you can convert a matrix to an equivalent quaternion, and interpolate between them.

Some pseudo code would look something like this

D3DXMATRIX StartMatrix;     //Where the object startedD3DXMATRIX CurrentMatrix;   //Where the object is nowD3DXMATRIX TargetMatrix;    //Where the object is goingD3DXQUATERNION startQ,endQ,interpQ;D3DXQuaternionRotationMatrix(&startQ,&StartMatrix);D3DXQuaternionRotationMatrix(&endQ,&EndMatrix);PercentComplete = ((float)(TweenEndTime - TweenStartTime)/1000.0f);D3DXQuaternionSlerp(&interpQ,&startQ,&endQ,PercentageComplete);D3DXMatrixRotationQuaternion(&TargetMatrix,&interpQ);

Hope this helps. (I just wrote the code out, I never compiled it, but something like this should work for you)

[edited by - ataru on February 6, 2004 6:03:44 PM]