3rd Person camera
I''m trying to implement a camera system similar to that in Splinter Cell, where the avatar is always in the centre of the view and the camera revolves around it.
So now I''m trying to work out how the camera will be rotated by the mouse. The way I''m thinking is storing an angle of change each frame which will be use to calculate the x and z position of the camera (y will be static most of the time). So, is this right:
camera.x = cos(angle)
camera.z = sin(angle)
This gives me a vector of unit length. A distance of 1.0 from the avatar however is too little, so I multiply the vector x and z components by a scalar? to have it further away???
Then there is the issue of how the camera is put in the avatar''s coordinate system so that camera isn''t rotating around the world origin with the look at point on the avatar (which would be a weird effect). I''m using opengl so I suppose it would be some sort of push/pop matrix thing???
All help much appreciated!
Andy.
there is the gluLookAt() in opengl, which takes an eye posiiton, a target, and a up vector.
What you can do is compute the camera position each frame relative to the target, and set the target to the head of the avatar. Then you have the yaw angle and the pitch angle, and the distance of the camera to the avatar.
you use all that to compute the position of the camera relative to the avatar, and for the up vector.
also, you may want to consider the avatar''s orientation. Usually, only the orientation around the y axis is considered (like in Hitman. dunno about Splinter cell).
I would try something like
What you can do is compute the camera position each frame relative to the target, and set the target to the head of the avatar. Then you have the yaw angle and the pitch angle, and the distance of the camera to the avatar.
you use all that to compute the position of the camera relative to the avatar, and for the up vector.
also, you may want to consider the avatar''s orientation. Usually, only the orientation around the y axis is considered (like in Hitman. dunno about Splinter cell).
I would try something like
float fAvatarYaw;float fCamYaw;float fCamDist;float fCamHeight;Vector vAvatarPos;float fAvatarHeadHeight;Vector vCamPos;Vector vCamUp;Vector vTarget;vCamPos.x = cos(fCamYaw) * fCamDist + vAvatarPos.x;vCamPos.y = fCamHeight + vAvatarPos.y;vCamPos.z = sin(fCamYaw) * fCamDist + vAvatarPos.z;vCamUp.x = 0.0f;vCamUp.y = 1.0f;vCamUp.z = 0.0f;vTarget.x = vAvatarPos.x;vTarget.y = vAvatarPos.y + fAvatarHeadHeight;vTarget.z = vAvatarPos.z;glMatrixMode(GL_MODELVIEW);glLoadIdentity();glRotatef(fAvatarYaw, 0, 1, 0);gluLookAt(vCamPos.x, vCamPos.y, vCamPos.z, vCamUp.x, vCamUp.y, vCamUp.z, vTarget.x,vTarget.y,vTarget.z);
Everything is better with Metal.
also, I would probably compute the CamUp vector depending on the elevation of the camera.
one way to do it, is to do
vCamUp should be normalised. You do those calcualtions after the vTarget is computed.
It''s good to keep track of the full camera orientation (vCamDir, vCamUp, vCamSide), and in debug, to make sure the orientation creates a valid orientation matrix (all vectors are unit length, they are all perpendicular to each other, meaning their dot products are very close to 0.0f).
one way to do it, is to do
Vector vCamSide;Vector vCamDir;vCamDir = (vTarget - vCamPos).UnitVector();vCamSide = Vector(sin(fCamYaw), 0.0f, cos(fCamYaw));vCamUp = vCamSide.Cross(vCamDir);
vCamUp should be normalised. You do those calcualtions after the vTarget is computed.
It''s good to keep track of the full camera orientation (vCamDir, vCamUp, vCamSide), and in debug, to make sure the orientation creates a valid orientation matrix (all vectors are unit length, they are all perpendicular to each other, meaning their dot products are very close to 0.0f).
Everything is better with Metal.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement