Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My camera moves and rotates, but relative to the worlds origin, instead of the players. I am having difficulty rotating the camera and then translating the camera in the direction relative to the camera facing angle.

I have been able to translate the camera and rotate relative to the players origin, but not then rotate and translate in the direction relative to the cameras facing direction.

My goal is to have a standard FPS-style camera.

    float yaw, pitch, roll;
D3DXMATRIX rotationMatrix;
D3DXVECTOR3 Direction;
D3DXMATRIX matRotAxis,matRotZ;
// Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians.
pitch = m_rotationX * 0.0174532925f;
yaw   = m_rotationY * 0.0174532925f;
roll  = m_rotationZ * 0.0174532925f;

up          = D3DXVECTOR3(0.0f, 1.0f, 0.0f);//Create the up vector
//Build eye ,lookat and rotation vectors from player input data
eye        = D3DXVECTOR3(m_fCameraX, m_fCameraY, m_fCameraZ);
lookat    = D3DXVECTOR3(m_fLookatX, m_fLookatY, m_fLookatZ);
rotation = D3DXVECTOR3(m_rotationX, m_rotationY, m_rotationZ);

D3DXVECTOR3 camera[3] = {eye,//Eye
                         up };//Up
RotAxis.x = pitch;
RotAxis.y = yaw;
RotAxis.z = roll;

D3DXVec3Normalize(&Direction, &(camera[1] - camera[0]));//Direction vector
D3DXVec3Cross(&RotAxis, &Direction, &camera[2]);//Strafe vector
D3DXVec3Normalize(&RotAxis, &RotAxis);

// Create the rotation matrix from the yaw, pitch, and roll values.
D3DXMatrixRotationYawPitchRoll(&matRotAxis, pitch,yaw, roll);

//rotate direction
//Translate up vector
D3DXVec3TransformCoord(&camera[2], &camera[2], &matRotAxis);

 //Translate in the direction of player rotation
D3DXVec3TransformCoord(&camera[0], &camera[0], &matRotAxis);

camera[1] = Direction + camera[0];//Avoid gimble locking

D3DXMatrixLookAtLH(&in_viewMatrix, &camera[0], &camera[1], &camera[2]);
share|improve this question

closed as too localized by Sean Middleditch, Byte56, bummzack, Trevor Powell, Ali.S Mar 15 '13 at 23:24

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Well it seems that I was not converting from world to local space. I am still having difficulties with translating in the direction. Here's the new code for anyone interested?

float yaw, pitch, roll;
//D3DXMATRIX rotationMatrix;

D3DXMATRIX yawMatrix;
D3DXMATRIX pitchMatrix;
D3DXMATRIX rollMatrix;

D3DXVECTOR3 Direction;

// Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians.
pitch = m_rotationX * 0.0174532925f;
yaw   = m_rotationY * 0.0174532925f;
roll  = m_rotationZ * 0.0174532925f;

position.x = m_fCameraX;
position.y = m_fCameraY;
position.z = m_fCameraZ;

up          = D3DXVECTOR3(0.0f, 1.0f, 0.0f);//Create the up vector
right        = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
lookat    = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
rotation = D3DXVECTOR3(m_rotationX, m_rotationY, m_rotationZ);

D3DXVec3Normalize(&Direction, &(lookat - right));//Direction vector

// Setup matrices to rotate around axes
D3DXMatrixRotationAxis(&yawMatrix, &up, yaw);
D3DXMatrixRotationAxis(&pitchMatrix, &right, pitch);
D3DXMatrixRotationAxis(&rollMatrix, &lookat, roll);

//move lookat vector
    D3DXVec3TransformCoord(&lookat, &lookat, &pitchMatrix);
D3DXVec3TransformCoord(&lookat, &lookat, &yawMatrix);

D3DXVec3Normalize(&lookat, &lookat);

//use lookat vector to define right vector
D3DXVec3Cross(&right, &up, &lookat);
D3DXVec3Normalize(&right, &right);

//use vectors to define up vector
D3DXVec3Cross(&up, &lookat, &right);

//lookat = Direction + right;//Avoid gimble locking


in_viewMatrix._11 = right.x;
in_viewMatrix._21 = right.y;
in_viewMatrix._31 = right.z;

in_viewMatrix._12 = up.x;
in_viewMatrix._22 = up.y;
in_viewMatrix._32 = up.z;

in_viewMatrix._13 = lookat.x;
in_viewMatrix._23 = lookat.y;
in_viewMatrix._33 = lookat.z;

in_viewMatrix._41 = -D3DXVec3Dot(&position, &right);
in_viewMatrix._42 = -D3DXVec3Dot(&position, &up);
in_viewMatrix._43 = -D3DXVec3Dot(&position, &lookat);
share|improve this answer
So is this an answer to your question? Or are you just updating code? If it's the latter, you should remove this answer and just update the code in your question. – Byte56 May 18 '12 at 17:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.