5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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.

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

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.

Related answer: https://gamedev.stackexchange.com/a/28833/11686

\$\endgroup\$
11
  • 2
    \$\begingroup\$ 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).. \$\endgroup\$
    – teodron
    May 10 '12 at 11:20
  • \$\begingroup\$ @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. \$\endgroup\$ May 10 '12 at 21:12
  • \$\begingroup\$ @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. \$\endgroup\$ May 10 '12 at 21:21
  • \$\begingroup\$ 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! \$\endgroup\$
    – teodron
    May 11 '12 at 7:55
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ May 11 '12 at 9:15
0
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .