# Should I try to optimise the World matrix build like this?

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

## Recommended Posts

Hi all,

It seems a bit wastful to do this for every object in the application:

m_xmWorld = m_xmScale * m_xmSpin * m_xmTranslate * m_xmOrbit;


Especially if it isn't even scaling that particular frame. Or even spinning that particular frame. So my question is... is the following an optimisation? I'm trying to avoid unnecessary matrix multiplication:

// All booleans set to false each frame
// Then certain booleans set to true dependant on characteristics of movement during ::Update()

if (m_bScaling)
m_xmWorld *= m_xmScale;

if (m_bSpinning)
m_xmWorld *= m_xmSpin;

if (m_bTranslating)
m_xmWorld *= m_xmTranslate;

if (m_bOrbiting)
m_xmWorld *= m_xmOrbit;


Or are the first example's multiplications so over-optimisidly-fast that it would make no difference? Are there any problems with this idea?

Thanks

Edited by Dissipate

##### Share on other sites

It's likely that a few boolean tests is faster than an unnecessary matrix mult. Be careful that you don't spend the savings on the determination of each boolean. That is, if you perform some math to determine whether you need scale, spin, translation, etc., you may not save much.

##### Share on other sites

I don't even use scaling (i don't need it at runtime for my project), as for translation and rotation i just compose into one matrix from vector and quaternion, and this is done only for "dynamic" objects since i have lot of static objects (where world is identity) so i can skip lots of unnecessary world*view*proj (so i have just one view*proj for all of those).

Edited by belfegor

##### Share on other sites

What Mike Acton would say.

##### Share on other sites

@Kalle_h

Thanks for your video was interesting and some good philosophical quandaries to ponder.

@Buckeye & @Belfegor: Thanks

btw... I tried my idea in the OP but my object zooms across the screen. Probably because of over accumulation each update. I changed it to m_xmWorld = m_xmTranslate;  and it works fine.

So my question is therefore: What should I be initially setting the m_xmWorld matrix to in ::Update() while using the above model in OP? I can't know in advance which is going to be chosen so how do I initialise the matrix?

*EDIT

Ok... Ok... You let me find out for myself... I can't do what I suggsted in breaking up the matrices because the world matrix would not have the needed components set within it when it comes to render. Oh well... I learned the hard way!

##### Share on other sites

Ok I take it all back again... (I hang my eyes in shame and lift my head in victory)

This works (if for only a measly optimisation)

    if (m_bScaling)
{
m_xmWorld = m_xmScale;
}

if (m_bSpinning)
{
if (m_bScaling)
m_xmWorld *= m_xmSpin;
else
m_xmWorld = m_xmSpin;
}

if (m_bTranslating)
{
if (m_bScaling || m_bSpinning)
m_xmWorld *= m_xmTranslate;
else
m_xmWorld = m_xmTranslate;

if (m_bOrbiting)
m_xmWorld *= m_xmOrbit;
}
Edited by Dissipate

##### Share on other sites

Why not have different methods for each case - if you know an object is only going to be using translation and rotation, then don't include the other calculations.  The other thing you can do is to decompose those matrix multiplies into a direct calculation for each element of the end result matrix.  That lets you factor out common calculations and reduce the overall number of multiplies to perform.

1. 1
2. 2
3. 3
Rutin
14
4. 4
frob
12
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633670
• Total Posts
3013262
×