I have a camera that works with Pitch and Yaw (no roll) and compute the look at matrix as following :
glm::vec3 direction;
direction.x = cos(glm::radians(pitch)) * cos(glm::radians(yaw));
direction.y = sin(glm::radians(pitch));
direction.z = cos(glm::radians(pitch)) * sin(glm::radians(yaw));
direction = glm::normalize(direction);
this->view =
glm::lookAt(position, position + direction, glm::vec3(0, 1, 0));
Now I want to initialize these Pitch and Yaw from another View Matrix and used this method but without success:
/* previousCamera->view as a glm::mat4 */
position = glm::vec3(previousCamera->view[3]);
const glm::vec3 direction = glm::vec3(previousCamera->view[2]);
yaw = glm::degrees(glm::atan(direction.x, direction.z));
pitch = glm::degrees(glm::asin(direction.y));
Do you spot any mistake in my code?
Edit:
In addition to the answer from @DMGregory, my position and direction were not correctly extracted from the ViewMatrix:
const glm::mat4 inverted = glm::inverse(previousCamera->view);
position = glm::vec3(inverted[3]);
const glm::vec3 direction = - glm::vec3(inverted[2]);
yaw = glm::degrees(glm::atan(direction.z, direction.x));
pitch = glm::degrees(glm::asin(direction.y));