I'm trying to have the camera always move up with its rotation so if you press w it will always move you up at the same speed as pressing s but in the oposite direction. The problem now is that the velocity added to the camera is unreliable and not what i'm trying to get.
void OrthographicCameraController::CameraAddRelativeVelocity(Timestep ts, glm::vec2 velocity)
{
glm::vec2 rotatedVelocity(
velocity.x * cos(glm::radians(m_CameraRotation)) + velocity.y *
sin(glm::radians(m_CameraRotation)),
velocity.x * sin(glm::radians(m_CameraRotation)) + velocity.y * cos(glm::radians(m_CameraRotation))
);
m_CameraVelocity += rotatedVelocity * (ts.GetMilliseconds() * 0.1f);
//LOG_INFO("x, {0} y: {1}", velocity.x, velocity.y);
The camera should always go up relative to itself but at the same speed in all directions. For some reason this code gives a seamingly random pattern of velocity to the camera even tho the math checks out. The input to this function is the function below that basicly checks for inputs and first increase velocity with an acceleration speed and then decrease with a retardation speed. Ive checked the numbers and the code below works. It's the CameraAddVecolity function that's disfunctional.
bool xNegative = 0 > m_CameraVelocity.x;
bool xPositive = 0 < m_CameraVelocity.x;
bool yNegative = 0 > m_CameraVelocity.y;
bool yPositive = 0 < m_CameraVelocity.y;
glm::vec2 velToAdd = {0.0f, 0.0f};
if ((Input::IsKeyPressed(HZ_KEY_D) - Input::IsKeyPressed(HZ_KEY_A)) == 0) {
if (m_CameraMovementRetardation * m_MaxCameraTranslationSpeed * ts > m_CameraVelocity.x && m_CameraMovementRetardation * m_MaxCameraTranslationSpeed * ts > -m_CameraVelocity.x) {
m_CameraVelocity.x = 0;
}
else {
m_CameraVelocity.x += m_CameraMovementRetardation * (xNegative - xPositive) * m_MaxCameraTranslationSpeed * ts;
}
} else {
velToAdd.x = (Input::IsKeyPressed(HZ_KEY_D) - Input::IsKeyPressed(HZ_KEY_A)) * m_CameraMovementAcceleration * m_MaxCameraTranslationSpeed * ts;
}
if ((Input::IsKeyPressed(HZ_KEY_W) - Input::IsKeyPressed(HZ_KEY_S)) == 0) {
if (m_CameraMovementRetardation * m_MaxCameraTranslationSpeed * ts > m_CameraVelocity.y && m_CameraMovementRetardation * m_MaxCameraTranslationSpeed * ts > -m_CameraVelocity.y) {
m_CameraVelocity.y = 0;
}
else {
m_CameraVelocity.y += m_CameraMovementRetardation * (yNegative - yPositive) * m_MaxCameraTranslationSpeed * ts;
}
}
else {
velToAdd.y = (Input::IsKeyPressed(HZ_KEY_W) - Input::IsKeyPressed(HZ_KEY_S)) * m_CameraMovementAcceleration * m_MaxCameraTranslationSpeed * ts;
}
// Camera rotation
if (m_Rotation)
{
if (Input::IsKeyPressed(HZ_KEY_E))
m_CameraRotation += m_MaxCameraRotationSpeed * ts;
if (Input::IsKeyPressed(HZ_KEY_Q))
m_CameraRotation -= m_MaxCameraRotationSpeed * ts;
if (m_CameraRotation > 180.0f)
m_CameraRotation -= 360.0f;
else if (m_CameraRotation <= -180.0f)
m_CameraRotation += 360.0f;
m_Camera.SetRotation(m_CameraRotation);
}
m_WorldSpaceMovement ? CameraAddWorldSpaceVelocity(ts, velToAdd) : CameraAddRelativeVelocity(ts, velToAdd);
m_CameraVelocity = glm::clamp(m_CameraVelocity, -m_MaxCameraTranslationSpeed, m_MaxCameraTranslationSpeed);
m_CameraPosition += m_CameraVelocity;
m_Camera.SetPosition(m_CameraPosition);
m_MaxCameraTranslationSpeed = m_ZoomLevel;