# turning model in 3rd person view

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

## Recommended Posts

I have my camera set up in 3d person view around a model. However I have been having trouble getting the model to turn with the camera, so as to always appear to be facing away from the camera. I have the position of the camera and model, and the view vector of the camera.How would I do this? Tera_Dragon

##### Share on other sites
turn the actor, not the camera. then place the camera so it's always behind the actor :)

##### Share on other sites
Assuming your character is always upright, and you use a right-handed coord system with x=left, y=up, z=fwd:

1. The characters "up vector" is just the world up direction, (0, 1, 0)

2. Calculate the cross product of the up direction with the camera view direction and normalise the result - this gives the characters "left vector".

3. the character's look vector is the cross product of its "left" and "up" vectors.

Then the characters 3x3 orientation matrix is given by the three vectors just calculated.

##### Share on other sites
How would I be able to rotate the model depening upon it's view vector?
Could I use glRotate or would I have to do some thing a bit more complex?

##### Share on other sites
If you have the objects transformation matrix use glMultMatrix

If your model only rotates around the "up" axis, then calculate the direction (using something like atan2(camera_look.x(), camera_look.z()) and use glRotate around the world "up" axis

##### Share on other sites
I have no idea how to do that, even after looking at msdn...

##### Share on other sites
I've had another look at atan2, and hav come up with this line of code to rotate the model:
float rot = atan2(g_Camera.View.x, g_Camera.View.z);
glRotatef( rot, 0.0f, 1.0f, 0.0f);

However it looks as though this does nothing. What am I doing wrong?

##### Share on other sites
glRotatef() takes degrees, the C++ trig functions use radians. Multiply by 180 / pi.

##### Share on other sites
I now have this:
Vector3 temp = g_Camera.View;
float rot = atan2(temp.x, temp.z) * 180 / 3.14;
glRotatef( rot, 0.0f, 0.0f, 1.0f);//have to spin around z (for some reason..)

However the model only faces the same way as the origin when I it is running counter clockwise around the origin. What am I doing wrong?

• 21
• 13
• 9
• 17
• 13