# How to calculate the inverse of a 4*4 opengl transform matrix

I am transforming my camera like any other object. I would like to set the view model matrix to the inverse of this so that I can draw the rest of the objects relative to the camera.

How do i calculate the inverse transformation?

I construct this matrix from a quaternion and a position vector, if it is easier to find the inverse of these then feel free to post.

In order to invert a rotation by unit quaternion q followed by a translation v, you apply a rotation by unit quaternion q' followed by a translation v' where:

• q' = ~q (conjugate of q, ie. inverse of rotation q)
• v' = -~q v q (transform of -v by the inverse of rotation q)

If you only do translations and rotations, it is simpler to not use transformation matrices until the final matrix is constructed.

• True, but I think he needs the transformation matrix, to use as a view matrix. May 11, 2012 at 7:31
• I was referring to OP's last paragraph. I will reword the last sentence. May 11, 2012 at 7:40

Inverting a matrix is a complex task so I'd recommend snatching an implementation from some library. For instance, here's the matrix code from OpenTK, search for the Invert method and see how it was implemented.

https://opentk.svn.sourceforge.net/svnroot/opentk/trunk/Source/OpenTK/Math/Matrix4.cs

But if you're creating your matrix like:

RotationMatrix(quaternion) * TranslationMatrix(position)


It might be worth trying to do:

TranslationMatrix(-position) * RotationMatrix(conjugate(quaternion))


Not sure if it will work, but that's how I usually create a view matrix for 2D games, using an angle instead of a quaternion.

• Shouldn't you use the conjugate of the quat in your second equation? -quaternion is a different thing (consider the quat to be (cos(t), sin(t)*n) where |n| =1 is a unit vector. So I reckon it should be RotationMatrix(inverse(quaternion)) = RotationMatrix(conjugate(quaternion)). Also, one typically applies the rotation first, then the translation (hope I'm not misunderstanding the idea).. May 10, 2012 at 11:20
• @teodron Thanks! I meant the conjugate but did not know the notation for it, nor what to call it, which is why I left a note explaining what I meant, despite the incorrect notation. I figured someone would fill it in in the comments ;-) As for the order of multiplication, see the related answer. The multiplication order is reversed when creating a view matrix, so it should be okay in this case. May 10, 2012 at 21:12
• @teodron To make it more clear what I meant, you can get the inverse of a SRT transformation (with scale, rotation, and position parameters) by performing an inverse transformation in reverse order, i.e. a TRS transformation (with -position, -rotation, and 1/scale as parameters). This is easier and cheaper than inverting the entire matrix. May 10, 2012 at 21:21
• The fact that $(ABC)^{-1} = C^{-1}B^{-1}A^{-1}$ was obvious, that is something that any programmer with the least of Algebra training should know.. I got your idea ;). +1 because you were the first, the second answer doesn't add any new insights! May 11, 2012 at 7:55
• @DavidGouveia a new matrix is still created in the end; but it is cheaper to build that matrix from a new vector v' and a new quaternion q' than to invert a matrix or even multiply two matrices. May 11, 2012 at 9:15

The inverse of a non-scaling rotation matrix is just its transpose.

If you construct a rotation matrix R from the quaternion, then the inverse of that rotation matrix (the "unrotation matrix" as it were) is just the transpose of R.

The reason this works is because rotation matrices are made up of orthogonal vectors.

How to handle the translation component depends on if you are translating before the rotation or after the rotation. In general to "untranslate" by an offset v, you just translate by -v.