# What does the identity matrix really do?

I understand that multiplying by the identity matrix is like multiplying by 1. Why would you multiply a matrix that will only contain the same result?

Also, I'm experimenting with some OpenGL code and found some very interesting things. Like for example:

When you use setIdentity(), the image rotates slower:

Matrix.setIdentityM(mProjectionMatrix, 0);
Matrix.rotateM(mProjectionMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);


But if you try to remove the setIdentity(), the image will rotate so fast... first it will rotate clockwise so fast, and then it will become slower until it stops and then it will begin to rotate counter clockwise, and then will stop again, then rotate clockwise.

I really don't understand how the identity matrix affects them despite multiplying by 1.

Can someone explain to me what's going on?

Here is the tutorial I am following.

• I am confused of your code. I see no setIdentity method on the docs: developer.android.com/reference/android/graphics/Matrix.html Which api are you using? Nov 17, 2013 at 10:42
• @Lasse Here developer.android.com/reference/android/opengl/Matrix.html theres a setIdentity method Nov 17, 2013 at 13:57
• I think your impression of changing clockwiseness is just a stroboscopic effect (aliasing of framerate with angular speed) because if you dont reset to identity you increase the angle at each frame which is a bug. Jun 2, 2016 at 6:13

TL; DR: If you multiply stuff together, you need to start with a 1

Forget about matrices for a second, let's talk about numbers. Suppose to rotate by 90, you multiply by 90. So

P' = 90*P

Now you do other transforms - a rotation R, a translation T, a scale S and so on. So

P' = T*R*S*P

Since you will apply all these transforms to a lot of points, you want to precompute them into a single transform so you can apply it many times.

M = T*R*S

P1' = P1*M

P2' = P2*M

and so on.

Now OpenGL has the concept of the "current transform" M. The way you construct M is by multiplying stuff into it, something like this

M = S

M = R*M

M = T*M

So there are two operations you can do on the current transform: set it to something (M = S) or multiply it by something (M = R*M). So you'd need several sets of functions, like setScale and multiplyByScale, setRotation and multiplyByRotation, and so on.

OR you can have a single setIdentity that sets M = 1, and then you only need the functions that multiply the other transforms over it:

M = 1

M = S*M

M = R*M

M = T*M

So let's go back to your example.

Matrix.setIdentityM(mProjectionMatrix, 0);


This sets the current transform to identity (1)...

Matrix.rotateM(mProjectionMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);


...and this multiplies the current transform by a rotation matrix.

Why do things rotate "faster" if you don't reset the matrix to identity? Because you're adding a rotation on top of the rotation you already had!

First frame:

M = 1

M *= 10 <--- M is a 10 degree rotation

Second frame:

M *= 10 <--- M is now a 20 degree rotation, because you started from 10, not from 1!

• I see.. Thanks for your great explanation. BTW can you tell how the rotation slows down until it stopn then rotate counter clockwise after rotating if clockwise. that is so weird. Nov 17, 2013 at 14:03
• You're welcome! As for this question, taking a stab in the dark... could it be similar to why car wheels look like they slow down and start rotating backwards? See en.wikipedia.org/wiki/Wagon-wheel_effect, en.wikipedia.org/wiki/Stroboscopic_effect and youtube.com/watch?v=rVSh-au_9aM Nov 17, 2013 at 15:43
• @ggambett youtube video got removed Aug 12, 2015 at 2:00
• this is the best explanation of all that i've read. i didn't know the concept of current transform, until i read this Sep 11, 2016 at 22:24
• @gamdevNoobie what you see here: "how the rotation slows down until it stopn then rotate counter clockwise" is sampling aliasing. When you render, that frame is a sample of the ongoing rotation. When rotation is slow this sample captures many points in time before it spins all the way around. When rotation gets close to spinning once around at every frame captured, you see the frame at almost the same place but rotated all the way around. When rotation goes even faster then the frame captures after the rotation has spun around past once. Search Nyquist sampling theorem! Mar 6, 2019 at 21:04

Identity matrix does nothing. It looks like this:

1,0,0,0
0,1,0,0
0,0,1,0
0,0,0,1


And that just multiplies everything by 1 if applied to other matrices or vectors.