Jump to content
  • Advertisement
Sign in to follow this  
Mercenarey

Need help understanding matrix transformation of vertices.

This topic is 5052 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 thought I had it all figured out about matrices, concatenating them and transforming vertices with them. I assumed, that successively transforming a vertex by different matrices would be the same as concatenating the matrices, and then transforming with the resulting matrix. However, when I tested different ways, I got surprising results. I tested, trying to simulate a bone system consisting of three bones, each 10.0f long and turning 45 degrees around the X-axis. The result should be looking something like this: | / Here is my first test:
//###########################
Vector3 vec(0.0f, 0.0f, 0.0f);
D3DXMATRIX mat;
D3DXMATRIX mat2;
D3DXMATRIX mat3;

D3DXMatrixIdentity(&mat);
Matrix4x4RotationZAxis(&mat, 45.0f);
mat._41 = 10.0f;

D3DXMatrixIdentity(&mat2);
Matrix4x4RotationZAxis(&mat2, 45.0f);
mat2._41 = 10.0f;

D3DXMatrixIdentity(&mat3);
Matrix4x4RotationZAxis(&mat3, 45.0f);
mat3._41 = 10.0f;

Vector3DTranslate(&vec, &mat);
Vector3DTransform(&vec, &mat);

Vector3DTranslate(&vec, &mat2);
Vector3DTransform(&vec, &mat2);

Vector3DTranslate(&vec, &mat3);
Vector3DTransform(&vec, &mat3);









Each of the translate/transform sets result in: 7.071/7.071/0.0 7.071/17.071/0.0 0.0/24.14/0.0 The end result is just what I expected. However, when I tested with concatenating the three matrices, the result became rather different (the three matrices are the same as above):
//###########################
vec.x = vec.y = vec.z = 0.0f;

Concatenate4x4Matrices(&mat, &mat2);
Concatenate4x4Matrices(&mat, &mat3);

//Vector3DTotalTransform(&vec, &mat);
Vector3DTranslate(&vec, &mat);
Vector3DTransform(&vec, &mat);
//###########################








This resulted in: -24.14/0.0/0.0 The basic number is right, but its negative and on the wrong axis :/ What am I doing wrong here? Shouldn't I just be able to concatenate them? -------------------------- PS: I tested the TotalTransform as well, but it gave an unexpected result (17.071/17.071/0.0) Just for reference:
void Vector3DTotalTransform(Vector3* a_vec, const D3DXMATRIX* a_mat)
{
	float x = a_vec->x;
	float y = a_vec->y;
	float z = a_vec->z;

	a_vec->x = x*a_mat->_11 + y*a_mat->_21 + z*a_mat->_31 + a_mat->_41;
	a_vec->y = x*a_mat->_12 + y*a_mat->_22 + z*a_mat->_32 + a_mat->_42;
	a_vec->z = x*a_mat->_13 + y*a_mat->_23 + z*a_mat->_33 + a_mat->_43;
}

void Vector3DTransform(Vector3* a_vec, const D3DXMATRIX* a_mat)
{
	float x = a_vec->x;
	float y = a_vec->y;
	float z = a_vec->z;

	a_vec->x = x*a_mat->_11 + y*a_mat->_21 + z*a_mat->_31;
	a_vec->y = x*a_mat->_12 + y*a_mat->_22 + z*a_mat->_32;
	a_vec->z = x*a_mat->_13 + y*a_mat->_23 + z*a_mat->_33;
}

void Vector3DTranslate(Vector3* a_vec, const D3DXMATRIX* a_mat)
{
	a_vec->x += a_mat->_41;
	a_vec->y += a_mat->_42;
	a_vec->z += a_mat->_43;
}

void Matrix4x4RotationZAxis(D3DXMATRIX* a_rotateematrix, float a_theta)
{
	// we are getting theta in degrees, so we need to convert it to radians first, so our functions can swallow it
	a_theta *= g_degree_to_radian;
	D3DXMATRIX l_matrix;
    D3DXMatrixIdentity(&l_matrix);

	l_matrix._11 = (float)cos(a_theta);	l_matrix._12 = (float)sin(a_theta);	l_matrix._13 = 0.0f;	l_matrix._14 = 0.0f;
	l_matrix._21 = (float)-sin(a_theta);l_matrix._22 = (float)cos(a_theta);	l_matrix._23 = 0.0f;	l_matrix._24 = 0.0f;
	l_matrix._31 = 0.0f;				l_matrix._32 = 0.0f;				l_matrix._33 = 1.0f;	l_matrix._34 = 0.0f;
	l_matrix._41 = 0.0f;				l_matrix._42 = 0.0f;				l_matrix._43 = 0.0f;	l_matrix._44 = 1.0f;

	Concatenate4x4Matrices(a_rotateematrix, &l_matrix);
}




[Edited by - Mercenarey on September 20, 2004 5:24:16 AM]

Share this post


Link to post
Share on other sites
Advertisement
Remember, matrix multiplication is not commutative; have you tried changing the direction of the concatenation?

ie. you have probably tried concatenating as follows T = A.B.C.D but you should try T = D.C.B.A

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!