# Moving camera in 3D - directX12

I begin to learn graphic programming on directx12. I try to make a camera wich can move dans rotate around the world.

I take this code as a basis: https://www.braynzarsoft.net/viewtutorial/q16390-transformations-and-world-view-projection-space-matrices

I try lot of method, but every time I finish on the same problem, my camera move and can rotate (on Y only for the moment). But when I move after the control still relative to my basis orientation than my new one.

I dunno if it come from how I handle movement (During waiting answer I will try to make my movement base on a target vector) Or how I multiply martix.

This is the actual point of my code. I use this source to not use LookAtLH(): https://www.3dgep.com/understanding-the-view-matrix/

void UpdateCamera() {

XMVECTOR cUp = XMLoadFloat4(&cameraUp);

if (GetAsyncKeyState('Z')) {

cameraPosition.y += 0.001f;

}
else if (GetAsyncKeyState('S')) {
cameraPosition.y -= 0.001f;
}

if (GetAsyncKeyState('Q')) {
cameraPosition.x -= 0.001f;
}
else if (GetAsyncKeyState('D')) {
cameraPosition.x += 0.001f;
}
if (GetAsyncKeyState('A')) {
rotation += 0.0001f;

}
else if (GetAsyncKeyState('E')) {
rotation -= 0.0001f;
}

if (GetAsyncKeyState('P')) {
cameraPosition.z += 0.001f;

}
else if (GetAsyncKeyState('M')) {
cameraPosition.z -= 0.001f;

}
XMMATRIX translationMat =     XMMatrixTranslationFromVector(XMLoadFloat4(&cameraPosition));
/* XMMATRIX rotYMat = XMMatrixRotationAxis(cUp, rotation);*/

float cosPitch = cos(0.0f);
float sinPitch = sin(0.0f);
float cosYaw = cos(rotation);
float sinYaw = sin(rotation);

XMFLOAT3 xaxis = { cosYaw, 0, -sinYaw };
XMFLOAT3 yaxis = { sinYaw * sinPitch, cosPitch, cosYaw * sinPitch };
XMFLOAT3 zaxis = { sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw };

XMVECTOR eye = XMLoadFloat4(&cameraPosition);

// Create a 4x4 view matrix from the right, up, forward and eye position vectors
XMMATRIX viewMatrix = {
xaxis.x,yaxis.x,zaxis.x,0.0f,
xaxis.y,yaxis.y,zaxis.y,0.0f,
xaxis.z,yaxis.z,zaxis.z,00.0f,
-XMVectorGetX(XMVector3Dot(XMLoadFloat3(&xaxis), eye)), -XMVectorGetX(XMVector3Dot(XMLoadFloat3(&yaxis), eye)), -XMVectorGetX(XMVector3Dot(XMLoadFloat3(&zaxis), eye)), 1.0f
};

XMStoreFloat4x4(&cameraViewMat, viewMatrix);// XMMatrixInverse(nullptr, translationMat * rotXMat));

}

void Update()
{

UpdateCamera();
///....

}


## 1 Answer

My hint was good, it's come from how I do the position update. There is the correct code. By this way I can keep the local coordonate with cUp, cTarg and cPos

void UpdateCamera() {

// Get Datas
XMMATRIX viewMatrix = XMLoadFloat4x4(&cameraViewMat);

//Move Up and bottom
if (GetAsyncKeyState('Z')) {
viewMatrix *= XMMatrixTranslation(0.0f,-0.0001f,0.0f);
}
else if (GetAsyncKeyState('S')) {
viewMatrix *= XMMatrixTranslation(0.0f, 0.0001f, 0.0f);
}

//Move Right and Left
if (GetAsyncKeyState('Q')) {
viewMatrix *= XMMatrixTranslation(0.0001f, 0.0f, 0.0f);
}
else if (GetAsyncKeyState('D')) {
viewMatrix *= XMMatrixTranslation(-0.0001f, 0.0f, 0.0f);
}

//Move forward/Backward
if (GetAsyncKeyState('P')) {
viewMatrix *= XMMatrixTranslation(0.0f, 0.0f, -0.0001f);
}
else if (GetAsyncKeyState('M')) {
viewMatrix *= XMMatrixTranslation(0.0f, 0.0f, 0.0001f);
}

//Rotate Y axis
if (GetAsyncKeyState('A')) {
viewMatrix *= XMMatrixRotationY(0.0001f);
}
else if (GetAsyncKeyState('E')) {
viewMatrix *= XMMatrixRotationY(-0.0001f);
}

//Rotate X axis
if (GetAsyncKeyState('R')) {
viewMatrix *= XMMatrixRotationX(0.0001f);
}
else if (GetAsyncKeyState('T')) {
viewMatrix *= XMMatrixRotationX(-0.0001f);
}

//Rotate Z axis
if (GetAsyncKeyState('F')) {
viewMatrix *= XMMatrixRotationZ(0.0001f);
}
else if (GetAsyncKeyState('G')) {
viewMatrix *= XMMatrixRotationZ(-0.0001f);
}

//Update View Matrix of Camera
XMStoreFloat4x4(&cameraViewMat, viewMatrix);
}


Link to the project : https://github.com/Zeldarck/TrainingDx12