• Advertisement
Sign in to follow this  

Matrix math help

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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:

Gotta ask the right question in Google.

 

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

Share this post


Link to post
Share on other sites

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));
}

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement