Jump to content
• Advertisement

# Billboarding (Rotating Sprites to Face Camera) Question

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I use point sprites in my game for my particle effect engine, and they work great. I am using quads now, though, to represent the damage that an enemy takes-- I have a really cool system setup that gets the damage the enemy has taken, then uses texture coordinates to read through a texture to get the number representation and then tile it however many digits the damage makes up. That all works fine. Then I have the quad go up the -z axis and eventually fade away. The problem is that the math I am using to try and rotate the quad to face the camera is NOT working right at all! Essentially, I was trying to take the 3D position of my camera and the 3D position of the quad sprite and find the angle between two vectors: 1. Vector from the sprite directly up the -z axis (0, 0, -16). 2. Vector from the sprite to the camera. And then I was rotating the quadSprite using D3DXMatrixRotation methods along that angle. I know this is a very naive approach... I must be really forgetting my math here somehow :( Could someone give me a better understanding of what I should be doing? I've been trying to rethink this, but this is the only idea I have been able to come up with so far, and it is failing miserably-- as I'm sure you can imagine! -dbh

#### Share this post

##### Share on other sites
Advertisement
There are standard solutions to the billboarding problem, so it's just a matter of knowing which one to refer you to.

You say the quads go 'up the -z axis'. What does that mean exactly? More specifically, how is your world oriented, i.e. what is the ground plane and which axis is up? Or is there no fixed up axis (like in a space sim)?

The next question is whether the billboard should face the camera directly at all times, or whether it should be constrained to a specific axis (such as the z axis in your example). Based on that info it should be easy to recommend an appropriate method.

(It may be that it's perfectly clear what you're trying to do, and that I'm just not getting it...)

#### Share this post

##### Share on other sites
Thanks for the expedient response, jyk; your questions are all pertinent... I should have described the world better!

First off, the camera can only "zoom" along the Z-Axis, which is the "up" axis. The world is spread across the X and Y axes, with (from camera's perspective) left being -X, right being +X, backwards being -Y and forwards being +Y as the camera "pans". -Z is up, and +Z is down.

I believe that is a LH system, but I may be mistaken.

Your second question is interesting... I'm still deciding whether it should be spherical or cylindrical. I have taken two screenshots to give you an idea of the world-system I am using that I described above, and to point out the problem and how I would like it to behave.

Image 1
Image 2

Image 1 has the coordinate system marked and shows the damage status that I was referring to. That is the quad I am trying to rotate.

Image 2 shows what happens if you "zoom" out (all it does is pull the camera "up" in the direction of the -z axis, and it keeps the camera pointing at its current location). You can see I am not rotating it properly around the x-axis so that the number would face the camera.

I guess, correct me if I'm wrong, all I technically need to do to get the desired effect is rotate the quad around the x-axis and I should be fine? I think I like the damage status NOT being rotated around the y and z axes towards the camera... so I guess I just need ONE rotation.

Does this help?

-dbh

#### Share this post

##### Share on other sites
Yup, that's a LH system, and your example is much clearer - or at least clear enough that I'm willing to take a guess.

Let's say you want to rotate the billboard on the x axis only, so that it will be as aligned as possible with the camera. You'll want to rotate first and then translate, which you probably already know. So, you just need the angle to rotate by:

Vector3 diff = cam.pos - billboard.pos;
float angle = atan2(-diff.z, -diff.y);

Anyway, that's my guess based on your diagram. If that doesn't work, I'd be happy to offer some other suggestions...

One last thing: there's a failure case when the camera lies along the billboard's local x axis. But if you're sure that will never happen, you needn't worry about it.

#### Share this post

##### Share on other sites
Ah, that makes sense. I had to think about why you were pulling out arctan, but after drawing out my trig on some paper that makes sense.

I'm plugging it in now, and we'll see what happens!

-dbh

#### Share this post

##### Share on other sites
Fantastic! Did the trick perfectly.

At first, everything was facing the camera just fine-- but it was always off by PI/2. I redid the vertices for my starting quad so it faced the camera so it faced down the -y axis, and that did the trick!

Thanks for taking the time to help, jyk! I really appreciate it! +rep to your already high rep count ;)

-dbh

#### Share this post

##### Share on other sites
Quote:
 Thanks for taking the time to help, jyk! I really appreciate it!
No problem - I'm glad you got it to work :)

#### Share this post

##### Share on other sites
Quote:
 I redid the vertices for my starting quad so it faced the camera so it faced down the -y axis, and that did the trick!
Oh, and yeah, the solution I posted assumed the billboard was initially facing along -y. If for some reason you wanted a different default orientation for the billboard, you could just swap and/or negate the arguments to atan2() to compensate.

#### Share this post

##### Share on other sites
Right, I thought about that, just figured it made more sense to reorient the quad so it was facing where it should to begin with :D I should have had it doing that to start with!

-dbh

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
• Advertisement

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633294
• Total Posts
3011242
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!