Jump to content
  • Advertisement
Sign in to follow this  
kosmon_x

Efficient object world matrix calculations?

This topic is 4859 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

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 this post


Link to post
Share on other sites
Advertisement
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 member
D3DXMATRIX world; //also global or member

//Recalc Matrix function
RecalcMatrix()
{
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 this post


Link to post
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 this post


Link to post
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 axis
s is the scale
dX dY dZ is the translation vector (inverse position).

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

Share this post


Link to post
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 ^ U

RotMat =
[ 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 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!