# Billboarding + aligning with velocity direction

I'm working on a particle system where I'm orientating the billboard using the inverted orientation matrix of my camera. This works quite well and my quad are rotated correctly towards the camera.

But, now I want to to rotate the quads in such a way that they point towards the direction they are going to.

In 2D this can be done by normalizing the velocity vector and using that vector for a rotation around the Z-axis (where vel.x = cos(a) and vel.y = sin(a)). But how does this work in 3D?

Thanks roxlu

It works the same in 3D. Use your inverted camera matrix to transform the particle velocity as well as their position, and this will put the velocity into screen space. You will then have, effectively, a 2D vel.x and vel.y which you can use to construct a rotation to apply to your quads.

In 3d it is often easier to use linear algebra rather than trig to create the matrix. Assuming your 3d world is a Y up world, it goes like this:

1.) You start with the normalized vector representing quadPosition - camPosition. these x, y, z components become your m31, m32, m33 values.

2.) Cross the normalized velocity with the result of #1 and normalize the result which becomes your m11, m12, m13 values.

3.) Cross those two vectors (result of #1 & #2) and this result becomes your m21, m22, m23 values.

4.) the location of your particle in world space becomes the m41, m42, m43 values.

5.) set m44 to 1. Set m14, m24, m34 to 0.

You now have a matrix that would orient the quad to face the camera but rotated in a way that the upper edge of the quad is 90 degrees to the velocity.

• Keep in mind that this depends if you are using column or row major matrices. You never know :P Oct 11, 2012 at 2:00
• @Gustave That's why I used mxx nomenclature. m21, for instance is the X component of the Matrix's up vector whether it's row or column matrix. Oct 11, 2012 at 2:06
• @SteveH thanks, so once I have created this orientation matrix for my quad, I assume my next step is to align it so that if faces the camera? Oct 11, 2012 at 7:45
• @roxlu um, no. you said you already had that and didn't want that. I think I mis understood what you wanted. My description makes the quad face the direction it is going, not face the camera. Sorry, Ill edit my answer, I think I understand now. Oct 11, 2012 at 13:10
• The answer is edited to be what I think you are asking for. It is important that the cross multiplication gets done correctly. I don't use OpenGL so I'm not familiar if the is a built in function that will do that for you but if so, the order of the params for that function could be important. If it's done incorrectly, some of the basis vectors for the matrix will be pointing 180 out. Oct 11, 2012 at 13:26