1
\$\begingroup\$

I'm working on a 2D game engine project, and I want to implement matrices for my transformations. I'm going to use the GLM library.

Since my game is only 2D, I figured I only need a 3x3 matrix to combine the translation, rotation and scale operations. However, glm::translation is only overloaded to return a 4x4 matrix, and never a 3x3.

I thought a translation could be performed by using a 3x3 matrix; why does GLM only have a translate function that returns a 4x4 matrix, and not a 3x3 matrix?

\$\endgroup\$
5
  • 1
    \$\begingroup\$ You should use 4*4 matrices, just convert the vectors to 4d ones \$\endgroup\$
    – Bálint
    Commented Feb 18, 2017 at 20:50
  • \$\begingroup\$ So most of my vectors should have default W coordinate values of 1? In order to work with 4x4 matrices? \$\endgroup\$
    – Jason
    Commented Feb 18, 2017 at 20:53
  • 1
    \$\begingroup\$ If you ask for a vec4 in the shader, but you pass a vec3, then it'll automatically fill it up \$\endgroup\$
    – Bálint
    Commented Feb 18, 2017 at 21:03
  • 1
    \$\begingroup\$ I dont feel I know enough about this to properly answer, but the "w" axis (i.e. the fourth coordinate) has always been a point of confusion for me, and many sources fail to explain it without over explaining it. A recent tutorial I completed finally explained it clearly; we sometimes need a 4th coordinate to allow for matrix multiplication. The 4th coordinate is often 1 or 0 - because 0 cancels out the translation matrix, and 1 retains it. In other words, if the last coordinate is 0, we do not have an actual position applied to the final result, commonly used to convey a direction. \$\endgroup\$
    – Gnemlock
    Commented Mar 22, 2017 at 3:40
  • 1
    \$\begingroup\$ When a vector conveys a direction, actual position is ultimately irrelevant. \$\endgroup\$
    – Gnemlock
    Commented Mar 22, 2017 at 3:41

2 Answers 2

3
\$\begingroup\$

You still need 4x4 for the projection matrix, even in 2D.

Typically you will use a combined MVP matrix in your shaders rather than separate matrices, so the calculations are easier (and faster - 4x4 is SIMD-friendly whereas 3x3 is not) throughout the pipeline if you just use 4x4 matrices everywhere.

\$\endgroup\$
2
\$\begingroup\$

Because GLM (as well as OpenGL) is designed to work for 3D graphics in the first place, and you need mat4 to perform translations in 3D.

You could write a 2D translation function yourself. It's as simple as returning the following matrix:

1 0 x
0 1 y
0 0 1

Keep in mind that to use it you will need to have a 1 as the last component of your vectors (you don't need to actually store it, you may just add it temporarily before doing multiplication).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .