Matrix math help

Started by
16 comments, last by Hawkblood 10 years, 3 months ago

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
Advertisement

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

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

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

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

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.

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

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

This topic is closed to new replies.

Advertisement