turning model in 3rd person view
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
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.
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.
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?
Could I use glRotate or would I have to do some thing a bit more complex?
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
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
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?
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?
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?
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement