Efficient object world matrix calculations?

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

Recommended Posts

Hey all, Right now, this is what my object RecalcMatrix() method looks like:

D3DXMATRIX rotY, trans, scale;
D3DXMatrixIdentity( &m_World );

D3DXMatrixTranslation( &trans, m_Pos.x, m_Pos.y, m_Pos.z );
D3DXMatrixScaling( &scale, m_UniformScale, m_UniformScale, m_UniformScale );

D3DXMatrixRotationY( &rotY, m_AngleY );

m_World = scale * rotY * trans; // or should this be rotY * trans * scale?


It's pretty ugly and surely inefficient.. I'm creating 3 new matrices per object every frame! And, I have to store one angle per direction... Right now my object can only rotate around the Y axis. What I'd like to do is just store a Direction vector and compute the rotation based off of that. And, ideally, only have a single World matrix and then plug everything into it, instead of creating 3+ new matrices and then multiplying them all together. How does everyone do this efficiently? I'd love to see some example code! :)

Share on other sites
My two cents worth then:

One way would be to cache your matrices by having a global or member matrices for scaling, translation and rotation, so that you recalculate them only when required. For eg:

D3DXMATRIX rotY, trans, scale; //globals or memberD3DXMATRIX world;              //also global or member//Recalc Matrix functionRecalcMatrix(){  world = scale * rotY * trans;}And somewhere else in your code you will ensure that you update the rotY, trans and scale as and when required.

Share on other sites
Hm yeah good idea. I could make them static members of my object class. Rating++ :)

But, is there a way to do it without using them at all? I know that the ._14, ._24, and ._34 positions in the matrix represent the translation, so I could just plug in the position directly in there.. but what about the other stuff (like getting a rotation from a direction vector?)

Share on other sites
scale * rotation * translation =[ cos Y * s    0  -sin Y * s   0 ][     0        s       0       0 ][ sin Y * s    0   cos Y * s   0 ][    dX       dY      dZ       1 ]Y is the rotation around Y axiss is the scaledX dY dZ is the translation vector (inverse position).

!EDIT! sorry I made a mistake. corrected it...

Share on other sites
If I understood you correctly, if you have a direction vector N and a vector facing up V then you can get the rotation matrix like that:
U = V ^ N// V = N ^ URotMat =[  U.x  U.y  U.z   0 ][  V.x  V.y  V.z   0 ][  N.x  N.y  N.z   0 ][   0    0    0    1 ]// uncomment that V=N^U line if you are not sure that Up and Dir are perpendicular (you can do that and let V be (0,1,0)// then your full matrix will be:[ U.x*s  U.y*s  U.z*s   0 ][ V.x*s  V.y*s  V.z*s   0 ][ N.x*s  N.y*s  N.z*s   0 ][  dX     dY     dZ     1 ]

Share on other sites
Awesome, just what I was looking for.. thanks for the posts :)

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 34
• 16
• 11
• 12
• Forum Statistics

• Total Topics
634123
• Total Posts
3015649
×