View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Matrix math help

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.

17 replies to this topic

### #1Hawkblood  Members

Posted 17 January 2014 - 01:17 PM

I can't believe I haven't needed this before..... How would you lerp between 2 matrix's???

Is there a function already out there or would I need to make my own? If I make my own, how?

### #2ApochPiQ  Moderators

Posted 17 January 2014 - 02:28 PM

POPULAR

There is no well-defined operation for "lerping between matrices" that universally preserves the semantics of the matrix itself. (If you're curious why, think about how matrix multiplication is not commutative, and how linear interpolation relies on multiplications.)

There might be well-defined operations for lerping between specific types of data which are represented in matrix form, though, such as affine transforms. However, you generally need to decompose the matrix and lerp individual subtransforms to make it work. For example, lerping a rotation in Euler angles is not necessarily the same as lerping a quaternion, and both might give you undesired behavior in certain corner cases. So (assuming you want to lerp transforms) you need to decide how you want each subtransform (translation, rotation, scaling, shearing, etc.) to be handled, and do so individually and reconstruct the final resultant matrix when you're done.

Wielder of the Sacred Wands

### #3Álvaro  Members

Posted 17 January 2014 - 02:50 PM

Well, ApochPiQ answer is probably what you needed to hear, but in fact lerping between 2 matrices is a perfectly well-defined operation. Matrices can be added, subtracted and scaled (i.e., multiplied by constants). Verify a couple of axioms more, and it turns out they are a vector space. You can always compute t*v + (1-t)*w, and that's what lerp is. Notice that the non-commutative nature of matrix multiplication doesn't enter the picture, because we are only multiplying the matrices by scalars, and that works just fine.

Posted 17 January 2014 - 02:58 PM

You can't guarantee (i.e. it usually won't be true) that the matrix will be orthonormal (each row/column perpendicular and normalised), orthogonal (each row/column perpendicular) or each row or column linearly independent though. You normally have to re-orthonormalise your matrix after a lerp (e.g. via Gram-Schmidt) but then you may as well use quaternion/scale/translation components then.

Edited by Paradigm Shifter, 17 January 2014 - 02:59 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #5TheComet  Members

Posted 17 January 2014 - 03:55 PM

It'd be interesting to know what your intentions are.

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

### #6ApochPiQ  Moderators

Posted 17 January 2014 - 03:56 PM

Well, ApochPiQ answer is probably what you needed to hear, but in fact lerping between 2 matrices is a perfectly well-defined operation. Matrices can be added, subtracted and scaled (i.e., multiplied by constants). Verify a couple of axioms more, and it turns out they are a vector space. You can always compute t*v + (1-t)*w, and that's what lerp is. Notice that the non-commutative nature of matrix multiplication doesn't enter the picture, because we are only multiplying the matrices by scalars, and that works just fine.

Yes, lerping matrices is defined, but it will violate the semantics of the matrix in the general case, as Paradigm Shifter elaborated on.
Wielder of the Sacred Wands

### #7Álvaro  Members

Posted 17 January 2014 - 04:08 PM

The original question didn't say anything about orthonormal matrices. It is very likely that the OP is interested in that case, which is why I said that ApochPiQ's answer is probably what he needed to hear.

There are contexts where the naive lerp between matrices is a perfectly reasonable thing to use.

Edited by Álvaro, 17 January 2014 - 04:09 PM.

### #8Hawkblood  Members

Posted 17 January 2014 - 04:19 PM

It'd be interesting to know what your intentions are.

Animation. I created a function to load a mesh (.x) with animation matrices. I need a way to move/rotate the object smoothly. Given that each frame has its own matrix, if I want it to animate very slowly would pose a problem. The difference between, say, frame0 and frame1 could be a rotation of 22.5 degrees, but I want it to move at 0.001 degree increments. Without a way to lerp, I could only show frame0 or frame1. With lerping, I could show incremental rotation of 0.001 each frame (smooth)......

orthonormal

Wow. I think I understand what that means (wiki). I am still not sure I need to worry about it (considering how I want to lerp). I'm pretty sure I can lerp the location part of the matrix, but the rotation I may have a problem with. As I understand, the "rotation" parts of the matrix is -1 to +1. Could I simply make a quaternions (after removing the "location"), lerp between the quaternions and then assemble the final matrix?

Posted 17 January 2014 - 04:20 PM

Yes, use quaternions for that.

EDIT: Kind of. This bit doesn't make sense: As I understand, the "rotation" parts of the matrix is -1 to +1

The rotation part (assuming no scale/shear/other shenanigans) is the upper 3x3 matrix of a 4x4, if it is orthonormal you can extract the rotation as a quaternion and slerp.

Edited by Paradigm Shifter, 17 January 2014 - 04:24 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #10Hawkblood  Members

Posted 17 January 2014 - 04:23 PM

Scaling and location can be a simple vector lerp, right?

Posted 17 January 2014 - 04:26 PM

Kind of. Non-uniform scaling (i.e. scale x, y, z by different amounts) can be a pain.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #12Hawkblood  Members

Posted 17 January 2014 - 04:31 PM

Kind of. Non-uniform scaling (i.e. scale x, y, z by different amounts) can be a pain.

I saw, somewhere, the breakdown of DX matrix. I don't remember every part.

m(3,0),m(3,1),m(3,2) is the location.

m(3,3) is uniform scaling.

I don't remember how to pull out the rest....

EDIT:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb206269(v=vs.85).aspx

This breaks it down..... I just need to figure out how to extract the xyz scaling from (0,0),(1,1),and (2,2)...........

Edited by Hawkblood, 17 January 2014 - 04:37 PM.

Posted 17 January 2014 - 04:36 PM

Google matrix to quaternion and quaternion to matrix and you will be good.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #14Álvaro  Members

Posted 17 January 2014 - 04:40 PM

You should use quaternions for interpolation, as others have mentioned. Then you have a couple of options about how exactly to do the interpolation: slerp and nlerp. If consecutive key frames are not too different, you can get away with nlerp, which is way simpler.

Quaternion nlerp(Quaternion q1, Quaternion q2, float lambda) {
if (dot_product(q1,q2) < 0)
q2 = -q2;
Quaternion lerp = lambda * q1 + (1.0f - lambda) * q2;
return lerp * (1.0f / abs(lerp));
}


Posted 17 January 2014 - 04:49 PM

Premature optimisation is the root of all evil though ;) Should try and get it working with slerps first.

Extracting scale isn't as straightforward as reading (0,0) (1,1) and (2,2) though, it's the length of the row/column vectors in the rows/columns I think? (Not thinking too well I have just come back from Belgium after travelling all day).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #16Álvaro  Members

Posted 17 January 2014 - 04:52 PM

When using homogeneous coordinates, the matrix of a projective transformation is only determined up to scaling. Typically one picks m(3,3)=1, but I guess you could also effectively scale everything else in the matrix by setting m(3,3)=1/scale. You would have to be careful to divide the translation vector by scale' as well. I haven't seen anyone doing this, but it would probably work.

### #17Hawkblood  Members

Posted 17 January 2014 - 05:01 PM

I don't scale the translation part for any reason. This keeps things simple for me.

### #18Hawkblood  Members

Posted 17 January 2014 - 05:06 PM

Think I got something:

D3DXMATRIX MatrixLerp(D3DXMATRIX &M1,D3DXMATRIX &M2,float s){
D3DXMATRIX m1,m2;//**** this is so that the original matrices are unafected....
m1=M1;
m2=M2;
//strip out the locations lerp them
//************ NOTE: SCALING IS NOT FACTORED INTO THE LOCATION OF THE MATRICES.
D3DXVECTOR3 locFinal,loc1,loc2;
loc1=D3DXVECTOR3(m1(3,0),m1(3,1),m1(3,2));
loc2=D3DXVECTOR3(m2(3,0),m2(3,1),m2(3,2));
D3DXVec3Lerp(&locFinal,&loc1,&loc2,s);
//remove the location from the matrices
m1(3,0)=m1(3,1)=m1(3,2)=
m2(3,0)=m2(3,1)=m2(3,2)=0;

//do the scale (only uniform scaling is accepted because I can't figure out how to do non-uniform
float scale=m1(3,3)*(1.0f-s)+m2(3,3)*s;
D3DXMATRIX tmp;
float is=1.0f/m1(3,3);
D3DXMatrixScaling(&tmp,is,is,is);
m1*=tmp;
is=1.0f/m2(3,3);
D3DXMatrixScaling(&tmp,is,is,is);
m2*=tmp;

D3DXQUATERNION qf,q1,q2;
D3DXQuaternionRotationMatrix(&q1,&m1);
D3DXQuaternionRotationMatrix(&q2,&m2);

D3DXQuaternionSlerp(&qf,&q1,&q2,s);

D3DXMATRIX out;
D3DXMatrixRotationQuaternion(&out,&qf);
D3DXMatrixScaling(&tmp,scale,scale,scale);
out*=tmp;
out(3,0)=locFinal.x;
out(3,1)=locFinal.y;
out(3,2)=locFinal.z;

return out;
}

`

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.