3
\$\begingroup\$

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;
D3DXVECTOR3 RotAxis;
// 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
                         lookat,//LookAt
                         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
D3DXVec3TransformCoord(&Direction,&Direction,&matRotAxis);
//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]);

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

D3DXMatrixIdentity(&in_viewMatrix);

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);
\$\endgroup\$

Browse other questions tagged or ask your own question.