# [Resolved]Spherical billboarding using matrices

## Recommended Posts

From the spherical billboarding tutorials I have seen, they involve complex math functions. I'm trying to make my particle system as fast as possible, so I was wondering, what is the fastest way to do spherical billboarding, preferrably with matrices, and still keeping the Z value? Using a point sprite style matrix is really fast, but it always renders with z=1. Thanks [Edited by - CadeF on April 4, 2006 8:49:53 AM]

##### Share on other sites
I'm not sure I entirely understand what you mean, but for simple proper billboarding, just use the vector from the particle to the camera as the "front" vector and build a local frame matrix for the particle from that. It certainly isn't the fastest of methods, but it does produce good results.

##### Share on other sites
Spherical billboarding is when the sprite rotates on the x,y and z axis to face the camera (normal billboarding does not rotate on the y axis). I'm not really sure what you mean by local matrix, could you please elaborate?

##### Share on other sites
I understand, since there is no "normal" billboarding, I assume spherical (rather than cylindrical).

The "local frame matrix" is just the transformation matrix that describes the frame of the particle (frame being a local coordinate system). They take the form:

[Sx Ux Fx Px][Sy Uy Fy Py][Sz Uz Fz Pz][0  0  0  1]

Where S is the side vector, U is the up vector, F is the front vector (the three orthogonal basis vectors that describe the frame) and P is the position of the frame.

This would be the transformation matrix to use for the particle.

##### Share on other sites
Thanks for the matrix. So, given a quad in object space, and P is object space(-32,-32 32,32 32,-32 -32,32), can I apply the matrix to the 4 points in object space and then add the particle position to them for world space?

##### Share on other sites
Well, P in that matrix is relative of course. It defines the position of the frame relative to another frame. You can concatentate the transformations, however you need to *eventually* get the vertices into world space.

When you say "P is in object space", I presume you mean that P is not relative to the world frame - but instead some other frame (of course, if P is relative to the space that the vertices are in, then it'd just be zero). In that case, you need another transformation to transform the vertices in "P-Space" to be in world space.

An example:

You have a quad, as shown. The vertices are defined in object space.
    A       B    +-------+    |   P   |    |   +   |    |       |    +-------+    C       D

A, B, C and D are the vertices of the quad (so, [-1, 1], [1, 1] ... ), P is the position vector of the origin of the frame, relative to another frame.

To make this simple, lets say that P is relative to the world frame, it would literally be the position of the particle in world-space.

To transform the particle, you'd build the matrix I posted further up, and load it into the transformation pipeline.

If P is not relative to the world-frame, you'll need more matrices to load in sequence, until the vertices have been transformed into world space (from there you can do your camera transformations, projections and so forth).

One thing to note, that matrix is for OpenGL - you'd have to transpose it to use with D3D ( different handedness to do with row-major/column-major difference ).

[Edited by - python_regious on April 3, 2006 8:28:35 AM]

##### Share on other sites
P, is the center of the particle,
P-space, its 0,0
World space, its the particle's position

One last question (I hope), do you know how to construct this matrix as left-handed, in D3D?

Sorry, but my brain is sort of slow right now. :)

##### Share on other sites
[Sx Ux Fx Px]T[Sy Uy Fy Py][Sz Uz Fz Pz][0  0  0  1]=[Sx Sy Sz 0][Ux Uy Uz 0][Fx Fy Fz 0][Px Py Pz 1]

[Edited by - python_regious on April 3, 2006 9:55:52 AM]

##### Share on other sites
Oh God, I just turned my brain on and realised I spread a bit of mis-information. The transpose of the matrix isn't to do with the handedness of the system, but rather the fact that D3D transforms are represented using row-major matrices, rather than column major. Hence the transpose.

##### Share on other sites
So in DirectX, which matrix do I use?

##### Share on other sites
The transpose of the original matrix I showed you, which I calculated in a further post. I just made it bold to make it stand out.

##### Share on other sites
P = WorldCameraPosition - WorldParticlePosition

Matrix =
1 0 0 0
0 1 0 0
0 0 1 0
P.x P.y P.z 1

Using this, everything is projected a distance behind the camera. If P = WorldParticlePosition - WorldCameraPosition then everything is projected a distance infront of the camera, further than their positions were before, and still no billboarding. I'm setting this matrix as my world matrix.

##### Share on other sites
Yeah, that isn't correct.

Ok, the particle as at position P (in world space), and the camera is at position C (again in world space).

Firstly, you'll need to calculate the "front" vector of the particle, which would be:

F = C-P

Using the particles up vector as (0,1,0) helps to calculate the side vector like so:

S = U x F

(where here U = [0,1,0])

We then recalculate U to it's actual value using the cross product of the front and side vector. Note that you will get a little artifact when F and U get very close (the particle will spin as F passes over U - however this isn't very noticable).

You then normalise all the basis vectors (U, F, and S), and plonk them in the matrix along with P, and add to the transformation pipeline.

P is not the vector from the particle to the camera - as you have calculated it to be. It is simply the particles position in world space.

##### Share on other sites
Thanks, worked like a charm. I tried to rate you up a while back to extremely helpful, but for some reason, I get the error message "you can only rate one user at a time"

Final code used
        Dim F As Vector3 = Camera.Position - Position        Dim S As Vector3 = Vector3.Cross(New Vector3(0, 1, 0), F)        Dim U As Vector3 = Vector3.Cross(S, F)        F.Normalize()        S.Normalize()        U.Normalize()        Dim matBillboard As Matrix = Matrix.Identity        With matBillboard            .M11 = S.X            .M12 = S.Y            .M13 = S.Z            .M14 = 0            .M21 = U.X            .M22 = U.Y            .M23 = U.Z            .M24 = 0            .M31 = F.X            .M32 = F.Y            .M33 = F.Z            .M34 = 0            .M41 = Pos.X            .M42 = Pos.Y            .M43 = Pos.Z            .M44 = 1        End With

##### Share on other sites
Watch out so that your lookat vector never becomes exactly (0,1,0) :) you could also test for this and modify your cross products in this case.

##### Share on other sites
Thanks, I already have that check. I'm working on a particle system editor, with a spherical camera, like a 3rd person camera. I've set it in such a way, it cant reach a right angle to the ground (view dir = 0,1,0), it stops at 89 degrees.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627686
• Total Posts
2978636

• 11
• 14
• 12
• 10
• 12