# Correct order to multiply matrices in 2D space

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

## 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;

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.

Regards.

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

Perfect!

Regards.

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 9
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632940
• Total Posts
3009329
• ### Who's Online (See full list)

There are no registered users currently online

×