# Need help understanding matrix transformation of vertices.

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

## 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
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 on other sites
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 on other sites
ok, I solved it, thanx for the help

[Edited by - Mercenarey on September 20, 2004 6:28:36 AM]

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632921
• Total Posts
3009214
• ### Who's Online (See full list)

There are no registered users currently online

×