Archived

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

dacrisxp

3rd person camera movement

Recommended Posts

I''m looking for a code example of how to make my camera rotate & translate with the player. The camera is a certain distance away from the player''s center (say 0,-4,-8), and it''s looking at a certain point near the player (say 0,-0.6,-0.3) or the player''s neck. How do I make my camera rotate with the player? I have three variables (RX,RY,RZ) which control the rotation of the player (Yaw,Pitch,Roll), and three more variables (XOffset, YOffset, ZOffset) which control the player movement. The camera is located at (X+XOffset,Y+YOffset,Z+ZOffset) and looks at (LX+XOffset,LY+YOffset,LZ+ZOffset), so the camera translates correctly with the player, but how do I make it rotate correctly with the player as well? Thanks for your help, I''ve been trying to figure this out for a few days now.

Share this post


Link to post
Share on other sites
Here''s the code I''m using right now.
XPos and ZPos are 0.
RotateWithCar is a flag indicating whether I want the camera to rotate w/ the player (car). Number is the camera ID, 9 is when the camera is transitioning (say from 3rd person view to 1st person view).

"Matrix" is set as the view matrix in the end.

Enuf talk, here''s the code:
  
D3DXMatrixLookAtLH(&Matrix,&D3DXVECTOR3( XPos+X+XOffset, Y+YOffset, ZPos+Z+ZOffset),
&D3DXVECTOR3( LX+LXOffset, LY+LYOffset, LZ+LZOffset),
&D3DXVECTOR3( 0, 1, 0 ) );
if(RotateWithCar || Number == 9)
{
D3DXMATRIX m3;
D3DXMATRIX m2;
D3DXMATRIX m1;
D3DXMATRIX mr1;
D3DXMatrixIdentity(&m3);
D3DXMatrixIdentity(&m2);
D3DXMatrixIdentity(&m1);
D3DXMatrixIdentity(&mr1);
D3DXMatrixTranslation(&m3, ((X+XOffset)-(LX+LXOffset)), ((Y+YOffset)-(LY+LYOffset)), ((Z+ZOffset)-(LZ+LZOffset)));
D3DXMatrixTranslation(&m1, -((X+XOffset)-(LX+LXOffset)), -((Y+YOffset)-(LY+LYOffset)), -((Z+ZOffset)-(LZ+LZOffset)));
D3DXMatrixRotationYawPitchRoll(&m2, -RX, 0, 0);
D3DXMatrixMultiply(&mr1, &m3, &m2);
D3DXMATRIX rMatrix;
D3DXMatrixIdentity(&rMatrix);
D3DXMatrixMultiply(&rMatrix, &mr1, &m1);
D3DXMATRIX xMatrix = Matrix;
D3DXMatrixMultiply(&Matrix, &xMatrix, &rMatrix);

Share this post


Link to post
Share on other sites
I''ll reply

Since I''m not sure where the camera will be first person or third person, the matrix operations are basicly the same

You should store the position, velocity, rotation, scaling, etc of the "car" in a directx matrix. And so you can rotate and translate the "car" very easily with the approaite functions.
Also when moving the car "forward" or "backwards" this is easily done by calling D3dxmatrixtranslate(....) since the matrix will move in the direction it''s facing. This is important since order of operations is important. Most of the time you do translate to the position, rotate, move forward in direction (or backwards), then scale.

Any ways I said all that as a precursor to this:
You can then set the camera equal to the car matrix and this is cause a first person view.

To get a third person view that rotates and moves you would still copy the matrix, but then you would relocate it to where you want the camera to be.


I hope that made some sense...I do realize I typed that fast off the top of my head

Pactuul

Share this post


Link to post
Share on other sites