A general question: When are matrices used for in 2D games? What would a person build with them in a 2D game? Are they only used in 3D games?
Matrices can be used in 2d games as well as 3d games, in fact I would definitely encourage their use when implementing a camera in any dimensions. Basically, a matrix can contain all the data you could ever want to transform the world from one position to another, which is exactly what you want a camera to do. You want the in game coordinates to be mapped to some on screen coordinates. Matrices can hold data on rotation, zoom and translation, which is pretty much everything you need. Then you simply pass this matrix in as a parameter to your spritebatch.begin, and you’re done!
Interestingly, to represent translations with matrices, you always need a matrix that is one dimension higher than your game. So in a 2D game, you need a 3x3 matrix to encapsulate all the necessary data. This is why the Matrix class in Monogame is 4x4, so that 3D games can also achieve translations.
So to sum up, yes matrices are incredibly useful for anything involving transformations in a game, with the best example being a camera. Do ask if anything doesn’t make sense or you wish for some more information
This was exactly what I needed to know. I was reading about game development math and wondering when I would find it necessary to use it.
I am a beginner and taking a class at my community college. So far, I only got multiple sprites to move randomly and collide. I am looking forward to eventually building worlds! Thank you!
Technically a Matrix is just a square array of vectors. In transforms it just holds the respective values for translation, rotation, scale. It has the same use in 2D as in 3D.
In 3D we only use a Vector3 in World-Space, but on the GPU it uses a Vector4 as the W component has additional meaning there (direction/“len” vs position/len=0), therefore a matrix in 3D is basically a 4x4 (4xVector4). It just makes things easier, like transposing, inverting, rotating when there is a uniform size on the array dimensions.
For transformation in 2D you would still need a 2x3 matrix at least (3xVector2) to hold rotation/translation/scale, so a 2x2 matrix is not enough and to keep it uniform it would still be a 3x3 matrix
So the usage is basically the same in 2D as in 3D. You can still translate/rotate/scale in 2D the same way you would do in 3D - so the math basically keeps totally the same, it just works with less dimensions.
(on the GPU there is no difference at all, as the GPU works in 3D anyway)
The maths can vary depending on the “handedness” of the graphics framework you are working in.