# How can I reverse the effect of a transformation matrix?

I have recently been working on a game with using OpenGL and C++ through GLFW.

In the game I have an airship with a turret mounted on it. The airship moves around in world space coordinates and the turret 'follows' it.

The turret has it's own coordinate space for its look/aim direction. Basically, when the turret aims in direction (0,0,1), it aims parallel to the airships direction of movement.

To give the turret a world space target, I have a matrix that takes world space coordinates and transforms these to turret space coordinates.

As the turret fires, I want it to spawn projectiles in world space, so is there an elegant way of using my transformation matrix to convert turret space coordinates back to world space?

A more general way of asking the question might be: if I have a matrix M that takes coordinates from space A to space B. Is there an easy way to use M to get the coordinates from B to A?

So long as the matrix M is invertible (which it generally will be, unless you're doing something very unusual), then computing the matrix inverse of M will give you a matrix that does what you want.

That is, if M performs some transformation, inverse(M) performs the "opposite" transformation.

Most matrix/vector libraries provide a means for computing the inverse.

• Bonus question (as you seem like a person who would know the answer): Would any non-invertible transformation matrices produce visually interesting results? – user1306322 Dec 11 '14 at 0:12
• @user1306322 Maybe. Projecting into a lower dimension (e.g. zero all X values) would be a non-invertible transformation, since you're throwing away values. It's interesting in the sense that it's useful, but not that you'll get funky results. "Invertibility" and "visually interesting" are orthogonal. – congusbongus Dec 11 '14 at 1:12
• @congusbongus nope! Projection from 3D to 2D is a standard hack to get drop shadows on planar receivers. – imallett Dec 11 '14 at 4:45
• @user1306322 You might be interested in the Moore–Penrose pseudoinverse – Tobias Kienzler Dec 11 '14 at 9:00

If your transformation matrix is a rotation matrix then you can simplify the problem by taking advantage of the fact that the inverse of a rotation matrix is the transpose of that matrix.

If your transformation matrix represents a rotation followed by a translation, then treat the components separately. The inverse is equivalent to subtracting the translation and then applying the transpose of the rotation matrix.

In your matrix lib there is probably a function called inverse. That is probably what you are looking for.