• Create Account

## updating world matrix, identity needed?

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.

15 replies to this topic

### #1cozzie  Members

4633
Like
0Likes
Like

Posted 23 June 2013 - 01:03 PM

Hi,

I'm a bit confused when it comes to d3d9 matrix translation, rotation and scaling.

My assumption:

- when I calculate a new matrix for translation/rotation/ scaling, I always first have to set the identity matrix

- this is because I'm passing absolute values when recalculating the matrix, not 'incremental/ changes'.

The strange this is that the results are always the same, when I do and when I don't set the identity matrix.

The code snippet is below.

What would you say?

bool CD3dmeshInst::UpdateWorldMatrix()
{
if(!mDynamic) return false;
if(!mIsMoved && !mIsRotated && !mIsScaled) return false;

if(mIsMoved)
{
//		D3DXMatrixIdentity(&mMatTranslate);
D3DXMatrixTranslation(&mMatTranslate, mWorldPos.x, mWorldPos.y, mWorldPos.z);
mIsMoved = false;
}
if(mIsRotated)
{
//		D3DXMatrixIdentity(&mMatRotateX);
//		D3DXMatrixIdentity(&mMatRotateY);
//		D3DXMatrixIdentity(&mMatRotateZ);
mIsRotated = false;
}
if(mIsScaled)
{
//		D3DXMatrixIdentity(&mMatScale);
D3DXMatrixScaling(&mMatScale, mScale, mScale, mScale);
mIsScaled = false;
}
mMatWorld = (D3DXMATRIXA16)(mMatScale*mMatRotateX*mMatRotateY*mMatRotateZ*mMatTranslate);

D3DXMatrixInverse(&mMatWorldInv, NULL, &mMatWorld);
D3DXMatrixTranspose(&mMatWorldInvTransp, &mMatWorldInv);
return true;
}



Edited by cozzie, 23 June 2013 - 01:05 PM.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #2belfegor  Members

2833
Like
0Likes
Like

Posted 23 June 2013 - 02:20 PM

This function should compose matrix from scale, axis-angle rotation and translation vectors in one step instead of lots of matrix multiplies (and what not) you currently have.

#include <cmath>

D3DXMATRIX* D3DXMatrixCompose(D3DXMATRIX* pout, CONST D3DXVECTOR3* pscale, CONST D3DXVECTOR3* paxis, FLOAT angle, CONST D3DXVECTOR3* ptranslation)
{
D3DXVECTOR3 v;
D3DXVec3Normalize(&v, paxis);

pout->m[0][0] = ((1.0f - std::cos(angle)) * v.x * v.x + std::cos(angle)) * pscale->x;
pout->m[0][1] = ((1.0f - std::cos(angle)) * v.y * v.x + std::sin(angle) * v.z) * pscale->x;
pout->m[0][2] = ((1.0f - std::cos(angle)) * v.z * v.x - std::sin(angle) * v.y) * pscale->x;
pout->m[0][3] = 0.0f;

pout->m[1][0] = ((1.0f - std::cos(angle)) * v.x * v.y - std::sin(angle) * v.z) * pscale->y;
pout->m[1][1] = ((1.0f - std::cos(angle)) * v.y * v.y + std::cos(angle)) * pscale->y;
pout->m[1][2] = ((1.0f - std::cos(angle)) * v.z * v.y + std::sin(angle) * v.x) * pscale->y;
pout->m[1][3] = 0.0f;

pout->m[2][0] = ((1.0f - std::cos(angle)) * v.x * v.z + std::sin(angle) * v.y) * pscale->z;
pout->m[2][1] = ((1.0f - std::cos(angle)) * v.y * v.z - std::sin(angle) * v.x) * pscale->z;
pout->m[2][2] = ((1.0f - std::cos(angle)) * v.z * v.z + std::cos(angle)) * pscale->z;
pout->m[2][3] = 0.0f;

pout->m[3][0] = ptranslation->x;
pout->m[3][1] = ptranslation->y;
pout->m[3][2] = ptranslation->z;
pout->m[3][3] = 1.0f;

return pout;
}


Do you even need scale? If so, is it uniform? If yes, you can skip inverse-transpose part.

### #3phil_t  Members

7652
Like
1Likes
Like

Posted 23 June 2013 - 03:37 PM

The strange this is that the results are always the same, when I do and when I don't set the identity matrix

As long as those functions don't care about the matrix that's passed in, and always set every component of the matrix on output, then of course it won't matter. Basically, a function like D3DXMatrixTranslation is just going to stomp every value in the matrix that's passed in.

However, your code has conditionals in it. For instance, if mIsMoved is not true, you'll never set any value into mMatTranslate. So it will keep whatever value it had last time (I'm assuming based on the name that it's a member variable). So you have a bug if any of those conditionals is ever false. You should move all the D3DXMatrixIdentity calls outside of the conditionals so they are set every time.

### #4cozzie  Members

4633
Like
0Likes
Like

Posted 23 June 2013 - 05:01 PM

@Phil_t: thanks, I think you're right that the d3dxmatrix functions 'start from scratch'. What I dont understand in that case, is why one would need the d3dxmatrixidentity function. For now I'll just leave out the d3dxmatrixidentity calls (I execute them once when the world matrix is created initially). When a mesh instance is not moved etc, indeed the existing matrix is kept and used in calculating the end result world matrix.

@belfegor: thanks, I'll try it out, sounds good that it saves me a lot of matrix multiplications.
How can I pass in different rotation angles for x, y and z? (I now see one float for angle)
For now I need the inverse transpose just for my lighting shader, if I can change this I could skip it, because your right, I'm never scaling with different values for x/y/z.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #5cozzie  Members

4633
Like
0Likes
Like

Posted 23 June 2013 - 05:21 PM

Ps; I think I know why I might need the d3dxmatrixidentity matrix.
In my case I dont, because I'm always doing a translation, but say I wanted only rotation as component in a (world)matrix, I would need to set translation to identity or not use the translation matrix in the end multiplication.
Makes sense?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #6phil_t  Members

7652
Like
0Likes
Like

Posted 23 June 2013 - 05:22 PM

Yup, that's kind of what I tried to explain in the 2nd paragraph of my response.

### #7belfegor  Members

2833
Like
1Likes
Like

Posted 24 June 2013 - 01:49 AM

How can I pass in different rotation angles for x, y and z? (I now see one float for angle)

I think that this should work for euler angles:

D3DXMATRIX* D3DXMatrixCompose(D3DXMATRIX *pout, CONST D3DXVECTOR3* pscale, FLOAT pitch, FLOAT yaw, FLOAT roll, CONST D3DXVECTOR3* ptranslation)
{
pout->m[0][0] = std::cos(yaw) * std::cos(roll) * pscale->x;
pout->m[0][1] = std::cos(yaw) * std::sin(roll) * pscale->x;
pout->m[0][2] = -std::sin(yaw) * pscale->x;
pout->m[0][3] = 0.0f;

pout->m[1][0] = (((std::sin(pitch) * std::sin(yaw)) * std::cos(roll)) + (std::cos(pitch) * -std::sin(roll))) * pscale->y;
pout->m[1][1] = (((std::sin(pitch) * std::sin(yaw)) * std::sin(roll)) + (std::cos(pitch) * std::cos(roll))) * pscale->y;
pout->m[1][2] = std::sin(pitch) * std::cos(yaw) * pscale->y;
pout->m[1][3] = 0.0f;

pout->m[2][0] = (((std::cos(pitch) * std::sin(yaw)) * std::cos(roll)) + (-std::sin(pitch) * -std::sin(roll))) * pscale->z;
pout->m[2][1] = (((std::cos(pitch) * std::sin(yaw)) * std::sin(roll)) + (-std::sin(pitch) * std::cos(roll))) * pscale->z;
pout->m[2][2] = std::cos(pitch) * std::cos(yaw) * pscale->z;
pout->m[2][3] = 0.0f;

pout->m[3][0] = ptranslation->x;
pout->m[3][1] = ptranslation->y;
pout->m[3][2] = ptranslation->z;
pout->m[3][3] = 1.0f;

return pout;
}



And here is the version with quaternion rotation:

D3DXMATRIX* D3DXMatrixCompose(D3DXMATRIX* pout, CONST D3DXVECTOR3* pscale, CONST D3DXQUATERNION* prot, CONST D3DXVECTOR3* ptranslation)
{
pout->m[0][0] = (1.0f - 2.0f * (prot->y * prot->y + prot->z * prot->z)) * pscale->x;
pout->m[0][1] = (2.0f * (prot->x *prot->y + prot->z * prot->w)) * pscale->x;
pout->m[0][2] = (2.0f * (prot->x * prot->z - prot->y * prot->w)) * pscale->x;
pout->m[0][3] = 0.0f;

pout->m[1][0] = (2.0f * (prot->x * prot->y - prot->z * prot->w)) * pscale->y;
pout->m[1][1] = (1.0f - 2.0f * (prot->x * prot->x + prot->z * prot->z)) * pscale->y;
pout->m[1][2] = (2.0f * (prot->y *prot->z + prot->x * prot->w)) * pscale->y;
pout->m[1][3] = 0.0f;

pout->m[2][0] = (2.0f * (prot->x * prot->z + prot->y * prot->w)) * pscale->z;
pout->m[2][1] = (2.0f * (prot->y * prot->z - prot->x * prot->w)) * pscale->z;
pout->m[2][2] = (1.0f - 2.0f * (prot->x * prot->x + prot->y * prot->y)) * pscale->z;
pout->m[2][3] = 0.0f;

pout->m[3][0] = ptranslation->x;
pout->m[3][1] = ptranslation->y;
pout->m[3][2] = ptranslation->z;
pout->m[3][3] = 1.0f;

return pout;
}


Edited by belfegor, 24 June 2013 - 01:58 AM.

### #8cozzie  Members

4633
Like
0Likes
Like

Posted 24 June 2013 - 04:31 AM

Great, thanks.
I'll use the euler angle version and study it to understand

I didn't use quaternion rotations yet.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #9cozzie  Members

4633
Like
0Likes
Like

Posted 24 June 2013 - 02:29 PM

@belfegor: I got it working, thanks.

One strange thing though, rotation around Z and Y where switched, yaw and pitch (either in the compose matrix function or in my engine/ models )

It seems to work fine.

I assume I will now always have to create a new world matrix if either a mesh instance has moved, rotated or scaled?

(won't be better in performance to keep older matrices and check which of the three things has happened)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #10phil_t  Members

7652
Like
0Likes
Like

Posted 24 June 2013 - 02:33 PM

It will simplify your code if you just create a new world matrix from scratch each time. Don't optimize unless you have a reason to believe this is a performance bottleneck.

### #11cozzie  Members

4633
Like
0Likes
Like

Posted 24 June 2013 - 03:01 PM

Thanks, got it in right away.

Strangely there's something weird with the X/Y and Z rotations, when I adapt the function as is, I read:

- Y rotation, Z rotation, X rotation

But I have to pass my rotation values, in this order for:

- X rotation, Y rotation, Z rotation

I have to get into that, and compare the result with before (when I used the separate rotation matrices).

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #12belfegor  Members

2833
Like
0Likes
Like

Posted 24 June 2013 - 03:25 PM

Well, you had before in this order:

mMatRotateX*mMatRotateY*mMatRotateZ


so i assumed it is what you want?

One strange thing though, rotation around Z and Y where switched, yaw and pitch...

I assume pitch = X, yaw = Y and roll = Z

Edited by belfegor, 24 June 2013 - 03:30 PM.

### #13cozzie  Members

4633
Like
0Likes
Like

Posted 24 June 2013 - 05:24 PM

Got it, sorry. Probably messed it up, while changing pitch/yaw/roll to X/Y/Z rot.

Thanks again.

It all looks good now, I see a little difference for a specific rotation of a mesh instance compared to the earlier lot of multiplications with the d3dx functions. I'll get into that and see what causes the difference.

This is now the cleaned up function, to update the world matrix for dynamic mesh instances:

bool CD3dmeshInst::UpdateWorldMatrix()
{
if(!mDynamic) return false;
if(!mIsMoved && !mIsRotated && !mIsScaled) return false;
else
{
D3DXMatrixCompose(&mMatWorld, &D3DXVECTOR3(mScale, mScale, mScale), mRot.x, mRot.y, mRot.z, &mWorldPos);
}

D3DXMatrixInverse(&mMatWorldInv, NULL, &mMatWorld);
D3DXMatrixTranspose(&mMatWorldInvTransp, &mMatWorldInv);
return true;
}



Later on I might remove the inverse transpose part, after changing my pixelshader/ lighting shader.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #14cozzie  Members

4633
Like
0Likes
Like

Posted 24 June 2013 - 05:46 PM

Got it, stupid!!! I passed radians instead of degrees in the new improved function

Edited by cozzie, 24 June 2013 - 05:46 PM.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

### #15belfegor  Members

2833
Like
1Likes
Like

Posted 25 June 2013 - 02:34 AM

You should pass radians not degrees, like you did before with D3DXToRadian macro (if your mRot is in degrees):

D3DXMatrixCompose(&mMatWorld, &D3DXVECTOR3(mScale, mScale, mScale), D3DXToRadian(mRot.x), D3DXToRadian(mRot.y), D3DXToRadian(mRot.z), &mWorldPos);

Like Phil_t said, rebuild your matrix every time, there is no point to check if it is "moved".

Edited by belfegor, 25 June 2013 - 02:37 AM.

### #16cozzie  Members

4633
Like
0Likes
Like

Posted 25 June 2013 - 03:54 AM

Got it all up and running now, thanks.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

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.