Incorrect getScale() from matrix4 when rotation of 0,180,0

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
luthyr
Posts: 69
Joined: Wed Dec 30, 2009 5:47 pm

Incorrect getScale() from matrix4 when rotation of 0,180,0

Post by luthyr »

I've noticed that a rotation of 0,180,0 degrees will return an incorrect scale. The scale value should be positive, but it will return with some axes negative since it assumes it has no rotation and simply returns individual matrix elements.

I've added "&&  M[0] > 0 && M[5] > 0 && M[10] > 0)" to only return the scale if they all happen to be positive and to do the calculation otherwise. This appears to fix the issue, but I'm not sure if this is the best way to solve this issue. Thoughts? I've pasted the changed function below (from matrix4.h).

Code: Select all

//! Returns the absolute values of the scales of the matrix.
    /**
    Note that this returns the absolute (positive) values unless only scale is set.
    Unfortunately it does not appear to be possible to extract any original negative
    values. The best that we could do would be to arbitrarily make one scale
    negative if one or three of them were negative.
    FIXME - return the original values.
    */
    template <class T>
    inline vector3d<T> CMatrix4<T>::getScale() const
    {
        // See http://www.robertblum.com/articles/2005/02/14/decomposing-matrices
 
        // Deal with the 0 rotation case first
        // Prior to Irrlicht 1.6, we always returned this value.
        if(core::iszero(M[1]) && core::iszero(M[2]) &&
            core::iszero(M[4]) && core::iszero(M[6]) &&
            core::iszero(M[8]) && core::iszero(M[9]) &&
            M[0] > 0 && M[5] > 0 && M[10] > 0)
            return vector3d<T>(M[0], M[5], M[10]);
 
        // We have to do the full calculation.
        return vector3d<T>(sqrtf(M[0] * M[0] + M[1] * M[1] + M[2] * M[2]),
                            sqrtf(M[4] * M[4] + M[5] * M[5] + M[6] * M[6]),
                            sqrtf(M[8] * M[8] + M[9] * M[9] + M[10] * M[10]));
    }
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Incorrect getScale() from matrix4 when rotation of 0,180

Post by CuteAlien »

I guess we would need another getScale where you can pass the rotation to get the real value in this case.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply