• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
jkeon

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

8 posts in this topic

I'm trying to build my own little engine to get more familiar with the concepts of 3D programming.

I have a transform class that on each frame it creates a Scaling Matrix (S), a Rotation Matrix from a Quaternion (R) 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!
0

Share this post


Link to post
Share on other sites
I second the advice to avoid non-uniform scaling.

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.
1

Share this post


Link to post
Share on other sites
I agree that you probably just need to multiply S and R the other way around.

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.
1

Share this post


Link to post
Share on other sites
[quote name='jkeon' timestamp='1355613640' post='5011085']
[source lang="jscript"][sx 0 0 0]
[0 sy 0 0]
[0 0 sz 0]
[0 0 0 1][/source]
[/quote]
Wrong.
Do this:

[CODE][sx sx sx 0]
[sy sy sy 0]
[sz sz sz 0]
[0 0 0 1][/CODE]

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
0

Share this post


Link to post
Share on other sites
[quote name='L. Spiro' timestamp='1355648147' post='5011204']
[quote name='jkeon' timestamp='1355613640' post='5011085']
[source lang="jscript"][sx 0 0 0]
[0 sy 0 0]
[0 0 sz 0]
[0 0 0 1][/source]
[/quote]
Wrong.
Do this:

[CODE][sx sx sx 0]
[sy sy sy 0]
[sz sz sz 0]
[0 0 0 1][/CODE]
[/quote]

What!? What is that matrix supposed to do?
0

Share this post


Link to post
Share on other sites
Hey, I appreciate the responses everyone thanks!

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.
0

Share this post


Link to post
Share on other sites
Shearing can appear if you apply the rotation first and then scale. You want to apply the matrices in the following order: Scale, Rotation, Translation. This means that, if you are using the convention that vectors are rows, you want to combine them as S*R*T; if you are using column vectors, T*R*S.
2

Share this post


Link to post
Share on other sites
[quote name='Álvaro' timestamp='1355661306' post='5011243']
[quote name='L. Spiro' timestamp='1355648147' post='5011204']
[CODE][sx sx sx 0]
[sy sy sy 0]
[sz sz sz 0]
[0 0 0 1][/CODE]
[/quote]

What!? What is that matrix supposed to do?
[/quote]
It isn’t supposed to be a matrix. I will explain below.


[quote name='jkeon' timestamp='1355675042' post='5011293']
@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?
[/quote]
Let’s say you already have your original matrix in the form of:
[CODE][R R R 0]
[R R R 0]
[R R R 0]
[tx ty tz 1][/CODE]
(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:

[CODE][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][/CODE]


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:
[CODE] /**
* 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;
}[/CODE]
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
0

Share this post


Link to post
Share on other sites
Oh, you are implementing a special case of matrix multiply where one of the matrices is symmetric, which is faster than using a general dense-matrix-times-dense-matrix multiplication. Yes, that makes sense. I was confused by the way you expressed it the first time around.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0