0

I am working on making a FPS style camera for a class project. I am currently using 2 Vec3s to hold the cameras position and rotation. With my current code I am only able to translate with respect to the global axis, not in the direction of my current facing. How do I use the camera's rotation in order to translate in the direction I would like? EX: Pressing 'W' causes the camera to translate forward along its local Z axis.

camMat = Matrix4Identity();

//Input for camera
if (GetAsyncKeyState('Q'))
{
    camRot.y += 60.0f*timer.Delta();
}
if (GetAsyncKeyState('E'))
{
    camRot.y -= 60.0f*timer.Delta();
}
if (GetAsyncKeyState('W'))
{
    camPos.z += 4.0f*timer.Delta();
}
if (GetAsyncKeyState('S'))
{
    camPos.z -= 4.0f*timer.Delta();
}
if (GetAsyncKeyState('A'))
{
    camPos.x -= 4.0f*timer.Delta();
}
if (GetAsyncKeyState('D'))
{
    camPos.x += 4.0f*timer.Delta();
}

camMat = Matrix4Multiply(camMat, Matrix4RotationX(camRot.x));
camMat = Matrix4Multiply(camMat, Matrix4RotationY(camRot.y));
camMat = Matrix4Multiply(camMat, Matrix4RotationZ(camRot.z));
camMat = Matrix4Multiply(camMat, Matrix4Translate(camPos.x, camPos.y, camPos.z));

EDIT: I do see a lot of similar questions on SO but they all seem to use slightly different implementations that aren't making sense to me.

user3334986
  • 149
  • 1
  • 12
  • This is really a math question, not a C++ one. If my memory of LA doesn't fail me (it very well might), you should just be able to multiply your direction vector with your rotation matrix to get the rotated direction. – Cubic May 22 '15 at 14:41

1 Answers1

0

see:

For translation only extract direction vectors you want to move from your matrix and add it to the position of matrix. Beware that directx has row wise matrices instead of column wise like OpenGL

So for x-movement extract the X=(Xx,Xy,Xz) and add m*X to matrix origin (x0,y0,z0)

  • where m is the movement step size (+/-)4.0f*timer.Delta();
  • just remember that camera matrix is usually an inverse matrix
  • and the positions in matrix from OpenGL are transponed in DirectX matrices (see the second link)
Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380