5
\$\begingroup\$

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.

\$\endgroup\$
12
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – gamdevNoobie Nov 17 '13 at 14:03
  • 3
    \$\begingroup\$ 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 \$\endgroup\$ – ggambett Nov 17 '13 at 15:43
  • \$\begingroup\$ @ggambett youtube video got removed \$\endgroup\$ – Semper Ambroscus Aug 12 '15 at 2:00
  • \$\begingroup\$ this is the best explanation of all that i've read. i didn't know the concept of current transform, until i read this \$\endgroup\$ – vipin8169 Sep 11 '16 at 22:24
  • \$\begingroup\$ @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! \$\endgroup\$ – Patrick Hughes Mar 6 at 21:04
5
\$\begingroup\$

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.

Read more about matrices

An identity matrix could be used as a starting point for some calculations, or if your object is at the origo of the world with no rotations, then you would apply identity world matrix to it.

The setIdentity method might be resetting your matrix back to identity matrix, and if your matrix is not declared inside the method you use it in, it is needed to be reset back to identity. Otherwise more and more rotations are applied on the matrix which results in the fast spinning.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.