Jump to content
  • Advertisement
Sign in to follow this  

Transformation matrices and linear algebra in 2d development

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

*This question is a duplicate from Stack Exchange site. Their rules don't allow questions like this so I will copy it to this forum*


I am trying to get into game development and I have started with a simple 2D platformer. I've also had some experience with XNA. At the moment I'm studying OpenGL ES and this is the second time when I face matrices in gamedev. I've decided to read about matrices and linear algebra in general (as I have attended a 6 month course 4 years ago). Doing so I have faced a question/issue. I'm not sure how to use it in 2D development and if it's as necessary as it is in 3D. Correct me if I'm wrong but transformation matrices are needed in 3D to show perspective. And in general (both 2D and 3D) it's needed for rotation, moving and so on.

My main question is: What is the need in matrices and linear algebra if I'm making a 2D game? What do I need to know about linear algebra or advanced (I mean something besides basic arithmetics and physics) math for the correct understanding of gamedev? Are there any good books for learning algebra for gamedev specifically? Are there any specifics in OpenGL about matrices and transforming or should I write all of these by myself?

Share this post

Link to post
Share on other sites

You could try glm and avoid all the trouble of doing it yourself. It's good to understand how/why things work but you certainly don't need to be able to implement things yourself. You don't necessarily need matrices for a 2D game, you could define everything in screen space and rotate easy enough with a simple formula. Matrices just neatly wrap things up.


When you know the specifics about what you want to achieve you can also do it a fair bit more efficiently than a general solution. For example it is quite trivial to turn a 2D position and an angle (all you really need for a 2D game) into a matrix pretty much in one go but to create a general rotation matrix and a general translation matrix and to combine them without knowing anything about them (a general solution) is quite involved so from that respect it is good to know about them. Matrices as they are used in computer graphics are really just a set of equations, some x/y/z goes in and a new x/y/z comes out, the equations might be doing rotation, translation or even projection.


in 3D you will usually have a perspective projection, in 2D you will usually have an orthogonal projection. They are both projection matrices. You can possibly get away without using an orthogonal projection depending on how you define your positions. In 3D you are usually concerned with 3 degrees of freedom for your rotations, in 2D you usually only care for one degree of freedom, both can be neatly contained in a matrix. The 2D version can be done without a matrix easily enough.


I don't really have any suggestions on where you can learn more but I would try out glm, use matrices and get on making what you actually want to make rather than worrying too much about the specifics (unless it is a particular interest to you). you can learn little bits as you go and I feel it's better to see the purpose of something and then learn how to do it than learn something without a purpose. Try not to reinvent the wheel.


This is doing rotation with a matrix:

Matrix m = |cos(angle)   -Sin(angle)|
           |sin(angle)   cos(angle) |
Vector position(1, 2);
Vector newPosition = m*position;

This is doing it without:

Vector position(1, 2);
Vector newPosition;
newPosition.x = position.x*cos(angle) + position.y*(-sin(angle));
newPosition.y = position.x*sin(angle) + position.y*cos(angle);

They are actually identical (you may notice similarity with the terms) , the first version just hides it in the matrix*vector multiplication (The vector is technically a matrix too). Matrices wrap up those equations.

Edited by Nanoha

Share this post

Link to post
Share on other sites

@Nanoha Thank you very much for the reply. I found it more than complete.

What I'm basically trying to do is both learn deep stuff and create the first real game. Of course I understand that creating a full project requires a lot of time and effort thus I'm trying to understand as much as I can for now. 

For now I have one question left. How efficient is using matrices in comparison with the simpler method you've shown. Somewhere in the net I've read that matrix operations are way more efficient in terms of resources. But I am not sure it is that important in a relatively simple game.

Once again thank you for reply as it was very informative.

Share this post

Link to post
Share on other sites

Aside possibly for setting up an orthographic projection in OpenGL without using GLU, matrices will serve no purpose for most 2D games. Even rotation can be done without using matrices, it's actually just an application of circle geometry.

Share this post

Link to post
Share on other sites

How efficient is using matrices in comparison with the simpler method you've shown. Somewhere in the net I've read that matrix operations are way more efficient in terms of resources. But I am not sure it is that important in a relatively simple game.



Graphics cards are optimised to perform matrix multiplication but general matrix multiplication is still quite costly and gets very costly the larger the matrix you use. If you want rotation and position you will need a 3x3 matrix. Multiplying that by a vector would take 9 multiplications and 9 additions, that's not so bad but the equivalent of doing it as an equation just uses 4 multiplications and 4 additions; less than half the work. 


Your vertex shader would look something like this:

attribute vec2 position; // vertex position
uniform mat4 projection;
uniform float angle; 
uniform vec2 objectPosition; // where the object is in the world
void main()
    float cosA = cos(angle);
    float sinA = sin(angle);
    vec2 worldPos;
    worldPos.x = position.x*cosA - position.y*sinA + objectPosition.x;
    worldPos.y = position.x*sinA + position.y*cosA + objectPosition.y;
    gl_Position = projection*vec4(worldPos, 0.0, 1.0);

You'll notice at the end you are doing a matrix*vector multiplication anyway so you could calculate a matrix before hand that does all your transforming (often called a model-view-projection matrix) and not have to do the translation and rotation in there, that will become more efficient the more vertices you have per object.


As nfries says you really don't need to use them beyond that projection. I certainly wouldn't worry too much about efficiency for a 2D game.


My personal feelings are that they are good to know, you will need them when you start to do 3D but for now they are not essential. There's also no reason why you can't use just a position and angle for now while you get your game up and running and then switch to doing it with matrices later if you want to, switching over would likely be a very trivial task. There's also no reason why you can't use both in the same program, pick the right tool for the job.

Edited by Nanoha

Share this post

Link to post
Share on other sites

The matrix is used in OpenGL for clipping, projection, distance checking, etc.


Technically, even when you are using a vector, you are uses a 1x2 or 1x3 matrix. Knowing how they work is pretty much key to survival anyways.

The reasons you'd want to use a matrix in 2D is nearly the same as it was in 3D.


Though, it's probably better if I give you a much better example.


You have an entity in 2D walking on a planet that is circling around another planet. This planet is round in shape, therfore the entity can walk around this planet in a circle, and be percieved to be upside down by other entitites.

Remember that at the same time, THIS SAME planet the entity is walking around on, is moving through space and rotating as well.


If you try doing this with basic vector math, you are going to have a bad time. Matrix math makes this easier because it does most of the work for you with basic matrix multiplication. Very little trig knowledge is needed to be known at that time.

The other bit is when you are making complex platforms. Just doing raw vector math when your character is standing on a moving platform can cause some speed differences in your character's motion. The matrix math will keep everything perceptively correct.


Lastly, it's computationally easier to multiply a series of matrices (especially with SSE) while passing down the previous one... than doing vector math per object.


And now a-days, it really doesn't even matter if the matrix math is "longer". Processors and GPUs will zip through it like it's nothing.

Edited by Tangletail

Share this post

Link to post
Share on other sites

Here's where I learned everything I know about matrices and junk.




It's not... the best? explanation? Maybe? But it worked for me, and maybe it can help you.

The silliest thing is that it doesn't explain how, once you have a "rotated" matrix, you use to to calculate a rotated point/vector/whatever.

(for future reference, a 2D matrix has an x-axis vector and a y-axis vector. To rotate a point/vector, you take the matrix's x-axis vector, multiply both values by the point's x value, then similarly multiply the matrix's y-axis vector by the point's y value, then add the two results together. I'm sure that explanation makes any sense.)


(Btw, for just 2D, you don't need to read the bits at the end about cross product and... 3D.)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!