Sign in to follow this  

Correct order to multiply matrices in 2D space

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

Hi everybody,

What is the correct order to multiply matrices in 2D space??

When I multiply:
m = projection * translate * scale;

And finally in the vertex shader
gl_Position = m * pos;

Works perfectly.

But when I multiply:
m = projection * scale * translate;

And in the vertex shader:
gl_Position = m * pos;

Nothing is draw in the screen.

In this case scale is a matrix to invert the y axis and translate is a matrix to move a square on the screen.

Thanks advance.
Regards.

Share this post


Link to post
Share on other sites

The order in which translate and scale is done is swapped when you swap the multiplication order.

 

So the first one first inverts Y, then moves the square, whereas the second first moves the square, and then flips the already moved square around Y.

 

I guess in your case, Y = 0 is at the edge of the screen, and if you first flip the square and then move it into the screen it will be visible.

If you instead move the square into the screen, and then afterwards flip it around Y, then it will be flipped with its translation so the final position will be in the opposite direction outside the screen.

Edited by Erik Rufelt

Share this post


Link to post
Share on other sites

It's always better to scale the model first (i.e. at the end), if you do it after a translation or a rotation you would have some unexpected results. The order, in 2D or 3D, should be projection * translate * rotate * scale always if you wan't to keep everything in proportion.

Share this post


Link to post
Share on other sites

If you are using the convention that vectors are columns, you can think of what (translate * rotate * scale) does like this:

 

(translate * rotate * scale) * point = translate * (rotate * (scale * point))

 

Because matrix multiplication is associative those things are equal, but it's easier to think about the latter (it's also faster to execute). So you start with a point in model coordinates, scale it, rotate the result and finally translate it.

 

Notice that matrix multiplication is not commutative in general, but rotation and [uniform] scaling do commute.

Share this post


Link to post
Share on other sites

This topic is 1120 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.

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