**1**

# Scaling along an arbitrary axis (Dealing with non-uniform scale)

###
#1
Members - Reputation: **104**

Posted 15 December 2012 - 05:20 PM

I have a transform class that on each frame it creates a Scaling Matrix (S), a Rotation Matrix from a Quaternion ® and concatenates them together (S*R). Once i have SR, I insert the translation values into the bottom of the three columns. So i end up with a transformation matrix that looks like:

[source lang="jscript"][SR SR SR 0][SR SR SR 0][SR SR SR 0][tx ty tz 1][/source]

This works perfectly in all cases except when rotating an object that has a non-uniform scale.

For example a unit cube with ScaleX = 4, ScaleY = 2, ScaleZ = 1 will give me a rectangular box that is 4 times as wide as the depth and twice as high as the depth.

If i then translate this around, the box stays the same and looks normal.

The problem happens whenever I try to rotate this scaled box. The shape itself becomes distorted and it appears as though the Scale factors are affecting the object on the World X,Y,Z axis rather than the local X,Y,Z axis of the object.

I've dug through most of my textbooks and there isn't a ton there to go off of. Online, most of the answers say to avoid non-uniform scaling which I understand the desire to avoid it, but I'd still like to figure out how to support it.

The only thing I can think off is that when constructing a Scale Matrix:

[source lang="jscript"][sx 0 0 0][0 sy 0 0][0 0 sz 0][0 0 0 1][/source]

This is scaling along the World Axis instead of the object's local Direction, Up and Right vectors or it's local Z, Y, X axis.

Does anyone have any tips or ideas on how to handle construction a transformation matrix that allows for non-uniform scaling and rotation?

Thanks!

###
#2
Crossbones+ - Reputation: **14675**

Posted 15 December 2012 - 07:13 PM

For the specific trouble you seem to be having, try multiplying the scaling and the rotation in the reverse order of however you are doing it now.

The problem with non-uniform scaling is that the combination of two transformations of the type you are now implementing (non-uniform scaling + rotation) is not of the same type. You can either restrict yourself to movements (rotations + translations, no scaling), compositions of movements and homotheties (rotations + translations + uniform scalings), or affine transforms (arbitrary linear endomorfisms + translations). When you include the non-uniform scalings and rotations, the smallest class that will be closed under composition is arbitrary affine transformations. Well, technically you can stay within orientation-preserving affine transformations, as long as your scales are always positive.

###
#3
Crossbones+ - Reputation: **2724**

Posted 15 December 2012 - 11:53 PM

Personally, I don't think non-uniform scaling for objects is so bad. The main gotcha is that for correct lighting you need to use a different matrix for your normals than for your vertices - instead of just lopping off the translation part of the matrix, you need to calculate the inverse transpose. If you're using fixed function, then I believe this is handled automatically, but if you're using shaders then they may well be implicitly assuming uniform scaling.

###
#4
Crossbones+ - Reputation: **16528**

Posted 16 December 2012 - 02:55 AM

Wrong.[source lang="jscript"][sx 0 0 0][0 sy 0 0][0 0 sz 0][0 0 0 1][/source]

Do this:

[sx sx sx 0] [sy sy sy 0] [sz sz sz 0] [0 0 0 1]

Problem solved depending on how you combine it with the other matrices. If you are using matrix multiplication (very slow, to be avoided) then your scaling matrix is correct and your order of multiplication is wrong. Scale * Rotation * Translation, in that order.

But if you have each component separately it is better to just construct the final matrix directly by putting each part into the matrix where it belongs. In this case you have to multiply each rotation component as I have shown in my snippet. Much faster than matrix math.

L. Spiro

**Edited by L. Spiro, 16 December 2012 - 02:59 AM.**

My Art: http://l-spiro.deviantart.com/gallery/4844241/Realism My Music: https://soundcloud.com/l-spiro

L. Spiro Engine: http://lspiroengine.com

L. Spiro Engine Forums: http://lspiroengine.com/forums

###
#6
Members - Reputation: **104**

Posted 16 December 2012 - 10:24 AM

I've been digging into it a bit more and going through some lectures from MIT and UCSD, the good news is that I'm understand the rules of Matrix multiplication and transforming to different spaces but unfortunately I'm still having the same problems.

@L.Spiro

I tried your matrix but this resulted in the terms canceling each other out so that scale values had no effect. Perhaps if you could explain what that matrix should do in a bit more detail?

@Alvaro, @C0lumbo

I tried changing the order so that rotation was applied first and then scaling, this did appear to properly preserve the scaling along the local X,Y,Z axis but at the expense of shearing the cube as well.

I guess I'm trying to understand why W= S*R*T is the commonly chosen convention as opposed to W = R*S*T as you've recommended for this specific case.

For completeness I've tried checking with other engines with my same setup. (1x1x1 unit cube, scaled x 4 in the X axis, x2 in the Y Axis, translated to the right and down and then rotating) The engine most similar to mine in terms of language and platform is Away3D (http://away3d.com/) which is supported by Adobe and also open source so I can dive in and see how they are doing things. They have the exact same problem as I do with this test case which is comforting in that I'm not missing anything obvious but disappointing in that the problem still isn't solved.

I fired up Unity3D after that and set up the same test case. Unity does seem to handle it correctly but they have methods specific to rotationAroundLocal and scaleLocal.

So the next step will be to do the multiplications by hand and compare with the values spit out from my engine and use unity as an example and see if I can figure out where things are going wrong.

###
#7
Crossbones+ - Reputation: **14675**

Posted 16 December 2012 - 12:26 PM

###
#8
Crossbones+ - Reputation: **16528**

Posted 16 December 2012 - 06:54 PM

It isn’t supposed to be a matrix. I will explain below.

[sx sx sx 0] [sy sy sy 0] [sz sz sz 0] [0 0 0 1]

What!? What is that matrix supposed to do?

Let’s say you already have your original matrix in the form of:@L.Spiro

I tried your matrix but this resulted in the terms canceling each other out so that scale values had no effect. Perhaps if you could explain what that matrix should do in a bit more detail?

[R R R 0] [R R R 0] [R R R 0] [tx ty tz 1](Notice the scaling term S has been removed).

Without using matrix multiplies (do not construct a scaling matrix and multiply it with this one, just modify this one in-place) perform the following multiplies:

[R*sx R*sx R*sx 0] [R*sy R*sy R*sy 0] [R*sz R*sz R*sz 0] [tx ty tz 1]

It wasn’t supposed to be seen as a matrix. It was supposed to show you where the scaling values are applied to the final matrix at the end.

This should make it clearer:

/** * Build a matrix from our data. * * \param _mRet Holds the created matrix. */ LSVOID LSE_FCALL COrientation::BuildMatrix( CMatrix4x4 &_mRet ) const { _mRet._11 = m_vRight.x * m_vScale.x; _mRet._12 = m_vRight.y * m_vScale.x; _mRet._13 = m_vRight.z * m_vScale.x; _mRet._14 = 0.0f; _mRet._21 = m_vUp.x * m_vScale.y; _mRet._22 = m_vUp.y * m_vScale.y; _mRet._23 = m_vUp.z * m_vScale.y; _mRet._24 = 0.0f; _mRet._31 = m_vForward.x * m_vScale.z; _mRet._32 = m_vForward.y * m_vScale.z; _mRet._33 = m_vForward.z * m_vScale.z; _mRet._34 = 0.0f; _mRet._41 = m_vPos.x; _mRet._42 = m_vPos.y; _mRet._43 = m_vPos.z; _mRet._44 = 1.0f; }I construct the matrix directly instead of using any matrix math. This is much more efficient. The forward, up, right, scale, and position components are all 3D vectors. Notice that the scaling terms are multiplied all the way across each row rather than just along the diagonal.

L. Spiro

**Edited by L. Spiro, 16 December 2012 - 06:59 PM.**

My Art: http://l-spiro.deviantart.com/gallery/4844241/Realism My Music: https://soundcloud.com/l-spiro

L. Spiro Engine: http://lspiroengine.com

L. Spiro Engine Forums: http://lspiroengine.com/forums

###
#9
Crossbones+ - Reputation: **14675**

Posted 16 December 2012 - 07:13 PM