I'm tinkering around with opengl and c++ and so far everything has gone pretty smoothly, using glm/glfw/glew however I've run into a bit of a problem when dealing with using the cameras rotation to rotate a movement vector in the direction of the camera for a fly-by camera effect. i.e.
The program receives W + A as inputs so W subtracts 1 to the z component of a direction vector and A adds 1 to the x component.
This produces a direction vector of {1, 0, -1}
example code might be
if (Keyboard::isKeyDown(Key::W)) {
dir += {0, 0, -1};
}
if (Keyboard::isKeyDown(Key::S)) {
dir += {0, 0, 1};
}
if (Keyboard::isKeyDown(Key::A)) {
dir += {1, 0, 0};
}
if (Keyboard::isKeyDown(Key::D)) {
dir += {-1. 0, 0};
}
Then the output is normalised to produce a constant movement speed no matter which direction, now this works fine if the camera is pointed perfectly in the "forward" direction, i can make this code work by calculating forward and right vectors and using this instead of the unit movement lengths, however i find this code not very ellegant and would like to use some sort of function to rotate the movement vector in the direction of the camera in 3d space so you can look up and fly up, I'm using the rotation vector of the camera to calculate the rotation matrix and using this, how this seems to calculate everything weirdly inverted and moving in the wrong direction but relative to the camera?
So calculated every frame is:
m_rotationMatrix = glm::identity<glm::mat4>();
Maths::rotateMatrix(m_rotationMatrix, m_rotation);
using the rotateMatrix method defined here:
void rotateMatrix(glm::mat4& matrix, const glm::vec3& degrees){
matrix = glm::rotate(matrix, glm::radians(degrees.x), { 1, 0, 0 });
matrix = glm::rotate(matrix, glm::radians(degrees.y), { 0, 1, 0 });
matrix = glm::rotate(matrix, glm::radians(degrees.z), { 0, 0, 1 });
}
then in the movement code it's used like so:
m_camera->translate(m_camera->transformDirection(dir) * deltaTime * movementSpeed);
using the transformDirection method defined in the camera as:
glm::vec3 Camera::transformDirection(glm::vec3 vec){
glm::vec4 inputAsVec4 = glm::vec4(vec.x, vec.y, vec.z, 1.0);
glm::vec4 result = m_rotationMatrix * inputAsVec4;
return { result.x, result.y, result.z };
}
I'm pretty new to OpenGL and relatively new to c++ also, so is there anything I'm missing/doing wrong to cause this behavior?