Hello! I'm currently working on a 3D camera, and I have it mostly working but I'm having a small issue with rotations.
Basically, when the I rotate the camera to the left, and move forwards, the camera moves towards the right instead of to the left, and vice versa for the right side.
I'm using glm as a maths library, and their quaternions for representing the rotation.
This is the code I use to move the camera.
const float movementSpeed = 200.0F;
const float mouseSensitivity = 10.0F;
m_Camera.Rotate(glm::vec3(0.0F, -Napalm::Input::GetDeltaX() * mouseSensitivity * (float)ts, 0.0F));
glm::vec3 rotation = m_Camera.GetRotationRadians();
glm::vec3 translation(0.0F);
if (Napalm::Input::IsKeyPressed(KEY_W))
{
translation.x = glm::sin(rotation.y) * 0.2F;
translation.z = -glm::cos(rotation.y) * 0.2F;
}
else if (Napalm::Input::IsKeyPressed(KEY_S))
{
translation.x = -glm::sin(rotation.y) * 0.2F;
translation.z = glm::cos(rotation.y) * 0.2F;
}
if (Napalm::Input::IsKeyPressed(KEY_D))
{
translation.x = glm::cos(rotation.y) * 0.2F;
translation.z = glm::sin(rotation.y) * 0.2F;
}
else if (Napalm::Input::IsKeyPressed(KEY_A))
{
translation.x = -glm::cos(rotation.y) * 0.2F;
translation.z = -glm::sin(rotation.y) * 0.2F;
}
m_Camera.Move(translation);
And here's the actual camera code:
void Camera::SetRotation(const glm::vec3& degrees)
{
m_DegreesRotation = degrees;
m_Rotation = glm::quat(glm::radians(degrees));
RecalculateViewMatrix();
}
void Camera::Rotate(const glm::vec3& degrees)
{
m_DegreesRotation += degrees;
m_Rotation = glm::quat(glm::radians(m_DegreesRotation));
RecalculateViewMatrix();
}
void Camera::RecreateProjectionMatrix()
{
const Window& window = Application::Get().GetWindow();
m_ProjectionMatrix = glm::perspectiveFov(glm::radians(m_FieldOfView), (float)window.GetWidth(), (float)window.GetHeight(), 0.1F, 1000.0F);
m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
}
void Camera::RecalculateViewMatrix()
{
glm::mat4 transform = glm::translate(glm::mat4(1.0F), m_Position) * glm::toMat4(m_Rotation) * glm::mat4(1.0F);
m_ViewMatrix = glm::inverse(transform);
m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix;
}
void SetPosition(const glm::vec3& position) { m_Position = position; RecalculateViewMatrix(); }
void Move(const glm::vec3& position) { m_Position += position; RecalculateViewMatrix(); }
inline const glm::vec3& GetRotationDegrees() const { return glm::degrees(glm::eulerAngles(m_Rotation)); }
inline const glm::vec3& GetRotationRadians() const { return glm::eulerAngles(m_Rotation); }
Any help is appreciated!