Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

neurokaotix

Rotate mesh to face camera..

This topic is 5612 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

How do you rotate a specific mesh and have it face the camera? (In this case its the mesh of a skeleton and its supposed to face the player)

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
What you need is billboarding. Why dont you check the SDK? It has a pretty good example of it.

Share this post


Link to post
Share on other sites
mmm I don''t think billboarding is the technique I''m looking for. Think of the meshes as enemies in a first-person shooter; when you are spotted the meshes face you and persue you. My problem is that I''m not sure as to how I can make the meshes face the camera.

Share this post


Link to post
Share on other sites
Here's what you need to do:

Get a vector from the mesh to the camera's current position. Dot-product that with the vector representing the mesh's current "look-at" position. If the dot product is less than 1.0, you know that the mesh must rotate. The question is: which direction to rotate?

To get the proper direction, cross-product the two vectors mentioned above. This will give you a new vector, let's call it crossVect. crossVect will point either up or down, depending on whether your look-at vector is to the left or to the right of the vector pointing at the camera (also depending on what order you set up the input vectors in the cross-product operation).

So, all you need to do is check the sign (+/-) of the y-value of crossVect, and that will tell you whether you need to rotate left or rotate right. Create an appropriate rotation matrix with some arbitrary rotation amount, and update your world matrix.

Run this algorithm every round, and eventually, your mesh will be facing the camera.

Two cautions: this method assumes your game is operating in a land-based game (i.e. this will not work in a 3D space game-type environment). Secondly, this method may produce a slight amount of "jitter" as the mesh adjusts and re-adjusts its heading to get that dot-product down to a perfect 1.0, which it will never quite do.

If anyone else can throw in their two cents regarding cleaner ways of solving this problem, I'd love to hear your suggestions, too, because I'm using this technique in my own game demo.

Good luck!
--Hoozit.

(edit: made my text a little clearer...

[edited by - HoozitWhatzit on July 16, 2002 6:51:48 PM]

Share this post


Link to post
Share on other sites
I dont'' think it''s nearly that hard. All you have to do is create a world matrix and rotate it the negative rotation of the camera. It''s as simple as that. You could do it the long way that HoozitWhatzit said but if you know the rotation values for the camera you don''t need to.

---
My Site
Come join us on IRC in #directxdev @ irc.afternet.org

Share this post


Link to post
Share on other sites
A little code to help you out...

    
// Assumes D3DXVECTOR3 position, direction; that are initialized


D3DXMATRIX matInv, matWorld;
D3DXVECTOR3 look ( position.x + direction.x,
position.y + direction.y,
position.z + direction.z);

D3DXMatrixLookAtLH(&matInv, &position, &look, &D3DXVECTOR3(0,1,0));
D3DXMatrixInverse(&matWorld, Null, &matInv);
d3ddevice->SetTransform(D3DTS_WORLD, &matWorld);

... And then draw the model...


This will let the model face whatever direction you want. (Given that the vertices are relative to the model center.) If you want to make a model face the camera, replace the look-initialization with the camera position.

Edit: Missed a line...

[edited by - nystagmus on July 17, 2002 6:34:26 AM]

Share this post


Link to post
Share on other sites
I''d like to dig a bit deeper into this topic if I could--this problem has vexed me, too.

It sounds like what neurokaotix is trying to do is have monsters in a FPS determine the player''s current position and run toward it (in order to kill the player, I''d guess!)

nyStagmus''s approach would allow you to make the monster _immediately_ face the player/camera, but what if you wanted the monster instead to make a smooth rotation that lasted until he was facing the player? Is there a better way of doing this than the approach I''ve outlined above?

As to RapidStunna''s approach, it would produce an immediate direction for the monster as well, but (and please correct me if I''m wrong) wouldn''t this only work if the monster was in the center of the camera view? I mean, the approach makes the monster face in the inverse direction that the camera is facing, but this would only make the monster actually point toward the camera if the monster was in the camera''s center-of-view. Otherwise the angle would be off by a little bit.

Or maybe I''m full of shit. Again, correct me if I''m wrong.
--Hoozit.

Share this post


Link to post
Share on other sites
hoozit: The neat part with my code is that the model can face any way you would like it to. What neurokaotix needs to do is calculate the angle between the direction it is currently facing and the line to the player. If this is greater than the max rotation value of the model/creature you rotate the max value, else you just rotate directly. Or am I mistaken?

Share this post


Link to post
Share on other sites
nyStagmus: Hmmm...I''m not sure I follow you. I understood the part where you said that you can make the monster face any direction you''d like to (just substitute any look-at point for direction.[x|y|z] in your sample code, right?) So far, you and I have similar approaches: we have a vector to the player, and a vector representing the monster''s current direction.

The next thing you said was to calculate the angle between these two vectors. Which, I gather, would be done using dot-product (well, giving you the cosine of the angle, at least).

After this point you lost me. What did you mean by the max rotation value? Also, what does it mean to ''rotate the max value'' versus ''rotating directly?''

I''m probably coming off pretty dense, apologies...

--Hoozit.

Share this post


Link to post
Share on other sites
Hoozit: I am sorry, I formulated that a bit awkwardly. What I mean is, a creature is only supposed to rotate a maximum angle per unit time (for example 1 degree), right? If the angle it wants to rotate is greater than 1 degree we rotate it by 1 degree, wait for the next frame and begin anew. But if the angle it wants to rotate is less than 1 degree, just let the creature face the player immediately.

Am I making more sense?

Share this post


Link to post
Share on other sites

  • Advertisement
×

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!