# Is it possible to billboard a sprite using a transformation matrix?

None of the current topics on billboarding seemed to answer this question.

But, given

• a sprite (or quad) that has it's own 4x4 transformation matrix
• a camera with a view matrix (a standard 4x4 transformation matrix)

is it possible to compute a 4x4 transformation matrix such that when the quad's matrix is multiplied with this matrix it has an orientation of looking at the camera?

• You may want a LookAt transformation matrix, like you would use for a camera. This description has the equations used: msdn.microsoft.com/en-us/library/windows/desktop/…
– user13213
Jun 1, 2012 at 15:52
• My crude version: Create a vector from your sprite to the camera's location. Extract a vector representing your sprite's current facing based on it's matrix rotations. Create the matrix to rotate this vector to align with the camera's. Multiply and go forth. Jun 1, 2012 at 17:45

Yes. Start with the rotation part of the camera-to-world matrix (inverse of the world-to-camera matrix, if that's what you meant by "view matrix"). Set the translation part of the matrix to zero. This will make the quads rotate with the camera. Then, given that the quads have the correct initial orientation, they will face the camera. That is, if the camera setup is such that an identity view matrix results in the camera looking along the +X axis (for example), the quads should be set up to face -X before the camera-facing matrix is applied.

This will make the quads parallel to the image plane. In some cases you might prefer that quads turn to directly face the camera rather than facing the image plane; that approach gives a different look that can be better or worse depending on the situation. Making the quads directly face the camera will require a different matrix for each quad, though, computed like Patrick Hughes suggested in his comment.

• You're an amazing man, thank you so much for this. One further question, since inverting a matrix is usually an O(n^3) operation (I could be mistaken), would you recommend this operation being done on the CPU or should I just make a function in glsl to do it for me? I know I probably shouldn't be doing it every frame, but it seems like this is a job for a shader.
– Ross
Jun 1, 2012 at 19:14
• if you do it in the shader, you will potentially be re-inverting the matrix for every vertex
– user13213
Jun 1, 2012 at 19:20
• Oh you're right. And since every vertex is going to be facing the camera, it would be utterly stupid to do that pervertex >.>;; Thanks for that.
– Ross
Jun 1, 2012 at 19:30