   Creating a 3rd person camera
  So far my attempt at this is kind've working. I can get a 3rd person camera around my game object, to rotate as if the cameras track was a sphere around my object based on mouse input, using some linear algebra and some distance equations and sphere equations etc... My question is, am I doing this completely the hard way? I know there are some functions in d3d that can make this easier i guess.Basically I am using my delta in x and y from mouse input, and using that to find the appropriate z value to set my camera position. The look position will always be the player.//To get a working algo, am using a const 20 radius to solve the mathbool usingInverse = false;//used to spin 360float inverseX = 0;float inverseY = 0;float distanceFromPlayer = 0;//This will be filled by querying the player drop to get its position in world space. 0,0,0 for now. Player position will be the center of my sphere to calculate where to place the camera    //based on mouse input.  TODO query real player coordinatesD3DXVECTOR3 playerPos(0.0f, 0.0f, 0.0f);float playerX = playerPos.x;float playerY = playerPos.y;float playerZ = playerPos.z;//Used to find the z valuefloat xyFind = pow(((_camera->GetCameraPos().x + xChange) - playerX), 2) + pow(((_camera->GetCameraPos().y + yChange) - playerY), 2);//Find distance from player in screen coordinatesdistanceFromPlayer = sqrt(pow((_camera->GetCameraPos().x + xChange - playerX), 2)+ pow((_camera->GetCameraPos().y + yChange - playerY), 2));//if the distance is greater than the radiusif(distanceFromPlayer > 20.0f){//Adjust the new x and y coordinate to be used to solve for zinverseX = _camera->GetCameraPos().x - xChange;inverseY = _camera->GetCameraPos().y - yChange;//Recalculate the xyFindxyFind = pow(((inverseX + xChange) - playerX), 2) + pow(((inverseY + yChange) - playerY), 2);usingInverse = true;}//Right side of the equation is going to equal my radius sqaured - xyFindfloat rightSide = (float)400.0f - xyFind;float zFind = 0;if(!usingInverse){zFind = playerZ + sqrt(rightSide);//Update camera position_camera->SetCameraPos((_camera->GetCameraPos().x + xChange), (_camera->GetCameraPos().y + yChange), zFind);}else{zFind = (playerZ + sqrt(rightSide)) * -1;//Update camera position_camera->SetCameraPos((_camera->GetCameraPos().x - xChange), (_camera->GetCameraPos().y - yChange), zFind);}
  If it works for you, then its fine for now. I have a 3rd-person camera too, and I need a little less calculations & branching, so if you want to know:void CCamera::Update(void){D3DXMATRIX ViewMatrix;D3DXVECTOR3 targetPosition = m_Owner->GetPosition();float NewRadius = 0.0f;D3DXVECTOR3 Point;NewRadius = m_Radius * sin(DEGTORAD(90 - m_AngleY));Point.x = NewRadius * sin(DEGTORAD(90 - m_AngleX));Point.z = NewRadius * sin(DEGTORAD(m_AngleX));Point.y = m_Radius * sin(DEGTORAD(m_AngleY));m_Position = targetPosition + Point;m_LookAt = targetPosition;D3DXMatrixLookAtLH(&ViewMatrix,&m_Position,&m_LookAt,&m_Up);m_lpDevice->SetTransform(D3DTS_VIEW, &ViewMatrix);}targetPosition would equal your players-position, I just designed my camera to being able to follow any of my objects I want. I'm using the sinus-function to find out the position of the camera based on a fixed distance to the object, and two angles determining the individual positions of the camera (think about two circles around the followed object, one horizontally and one vertically oriented). On mouse-move I simply add the delta to the angle. I just have to convert the angles to radiant, using these macros:#define DEGTORAD(degree) ((D3DX_PI / 180.0f) * (degree)) // converts from degrees to radians#define RADTODEG(radian) ((180.0f / D3DX_PI) * (radian)) // converts from radians to degreesI think my attempt is easier and basically it should be faster. But honestly it shouldn't make a big difference.   ;  Well like i said, mine kind've works. I can move along the front half of the sphere, but am running into problems making it go all the way around.  Could you comment your code so I can under stand it better and know what your variables mean? Thanks.   ;  Hey well if your code works for your camera then it works. Although i think I may have a 2 line solution for your problem. First i am assuming you have the look, up, and right vector classes for your player? If so you can just build the cameras position with those vectors from the player class.It seems like you are constantly updating your cameras position based on your players position. If you are making a 3rd person camera I'm assuming you always want this camera positioned a certain point behind your player. For example the following code is so the camera will always follow 2.5 units above, and 10.5 units behind the player.//Setting a position 10.5 units behind the player and 2.5 units above the playerD3DXVECTOR3 pos = playerPos - (10.5f * player.getLookVector() ) + (2.5 * player.getUpVector() );  //Setting your cameras current position to this new position_camera->SetCameraPos( pos.x, pos.y, pos.z );  [font="Arial"]Now just use the function D3DXMatrixLookAtLH( .............. ); to Look at your player.[/font]If you want a more dynamic camera, like a more realistic one where you can see the player turning etc... set up your look, up, and right vectors inside of your camera class, and turn them whenever the player turns, but maybe at a slower rate, and use the camera vectors for the position.D3DXVECTOR3 pos = playerPos - (10.5f * _camera->getLookVector() ) + (2.5 * _camera->getUpVector() ); and increment your camera's angles only once every while like 50 milliseconds.   ;void CCamera::Update(void){   D3DXMATRIX ViewMatrix;   //Get the LookAt-position of the camera from cameras target    D3DXVECTOR3 targetPosition = m_Owner->GetPosition();   //temporary variable for calculations   float NewRadius = 0.0f;   //distance-vector from LookAt-position   D3DXVECTOR3 Point;   //create inner circle for x/z-position based on cameras y-position angle    NewRadius = m_Radius * sin(DEGTORAD(90 - m_AngleY));   //get x/z-distance using the law of sine with the new radius and the xz-plane angle in radiant   Point.x = NewRadius * sin(DEGTORAD(90 - m_AngleX));   Point.z = NewRadius * sin(DEGTORAD(m_AngleX));   //now finally calculate y-distance using the xy-plane angle in radiant   Point.y = m_Radius * sin(DEGTORAD(m_AngleY));   //camera position = targets LookAt-Position + distances-vector   m_Position = targetPosition + Point;   //store LookAt-position   m_LookAt = targetPosition;   //build view matrix using position, LookAt, and up   D3DXMatrixLookAtLH(&ViewMatrix,&m_Position,&m_LookAt,&m_Up);   //set view matrix   m_lpDevice->SetTransform(D3DTS_VIEW, &ViewMatrix);}To make it even a little more clear I've drawn it out:As the camera is orbiting around the target on a sphere, you can take a circle out of this sphere. D is the fixed distance from the camera to the position, T[sub]xyz[/sub] is the targets position. A is the angle for the horizontal orientation (sry can't draw ? in photoshop). You can now use the angle and the distance to calculate the y-coordinate of the cameras distance to its target (C[sub]y[/sub]) using the law of sine:in any triangle, a/sin? equals b/sin? equals c/sin?. where each pair of these is a side with the opposite angle. As our traingle here is a right-angled one, you can now say D/sin90 = C[sub]y[/sub]/sinA. As sin90 equals 1 you can solve that statement easily to calculate C[sub]y[/sub] (look at my code). Now you only need to know the x/z-distance too. you can use the same principif you just flip the circle into the x/z-plane, using a different angle for the rotation in that plane. and, as the x/z-distance depends on the y-distances, you need to use a new radius:N_R in my graphic. You can calculate it with the law of sine, too, you just need to calculate it's opposite angle (hint: all angles in a triangle add up to 180 degrees). look into my code again. Puh.. explaining maths in english isn't that easy. I hope I didn't explain unneccesary thing, but I don't know how much maths you know. I hope you understand it nethertheless, because afaik this should be the easiest (if you understand it), fastest and most reliableway to do it. [quote=Belive82]If you are making a 3rd person camera I'm assuming you always want this camera positioned a certain point behind your player. [/quote]Well in todays games you would not want to do this, you would rather want the camera to being able orbit around the player with the mouse in a fixed distance on a sphere, like I'm doing it..;  Thanks for the responses. Im going to take a look at your post there and do a litte bit of research on simple trig. Thanks again   ;  the DX SDK has a working third person camera, check the multianimation sample   