UPDATE: I have found that the delays happen when i press a key in the keyboard and move the mouse at the same time, how can i solve it?
I checked it setting a constant speed to the camera(going forward) and disabled the interactions with the keyboard. The lag didnt appear when i was moving forward while rotating the camera, but happened when i pressed keys(with no functions because i disabled them) while i was moving the mouse
I will let here my initial question, but the problem is not because of the code, it is about the keyboard + mouse use. IGNORE WHAT FOLLOWS
=======================================================================
I am running out of ideas of what could be the reason for this, but when i move and rotate the camera an insane lag appers(but it doesnt show of in the FPS, they are always around 120). It doesnt happen when i just rotate the camera or translate it.
Video to show the problem:
https://www.youtube.com/watch?v=3UuXZLzU-As&feature=youtu.be
What could be the reason for this? I tried:
- To use raw input, but because it didnt solve it i rolled back.
- Rewrite the camera view calculations.
I am using Vulkan, at the moment on Windows.
Ill include some code:
View update:
void Camera::UpdateView() { glm::mat4 rotate = glm::mat4_cast(GetRotation()); glm::mat4 translate = glm::mat4(1.0f); translate = glm::translate(translate, -GetPosition()); Matrices.view = rotate*translate; glm::mat4 matInverseView = glm::inverse(Matrices.view); forward = glm::normalize(glm::vec3(matInverseView[2])); right = glm::normalize(glm::vec3(matInverseView[0])); } Pitch and yaw update:
void Camera::ActiveMouseUpdate(Input * input, double deltaTime) { if (input->MouseMoved()) { yaw -= MOUSE_SENSIBILITY * input->GetMouseSpeed().x*deltaTime; pitch += MOUSE_SENSIBILITY * input->GetMouseSpeed().y*deltaTime; pitch = glm::clamp(pitch, glm::radians(-85.0f), glm::radians(85.0f)); CalculateComponents(); cameraChanged = true; } } Rotation update:
void Camera::CalculateComponents() { glm::quat qPitch = glm::angleAxis(pitch, glm::vec3(1, 0, 0)); glm::quat qYaw = glm::angleAxis(yaw, glm::vec3(0, 1, 0)); glm::quat qRoll = glm::angleAxis(roll, glm::vec3(0, 0, 1)); glm::quat orientation = glm::normalize(qPitch * qYaw*qRoll); SetRotation(orientation); } Movement:
void Camera::PassiveKeyboardUpdate(Input * input, double deltaTime) { cameraChanged = false; glm::vec3 pos = GetPosition(); if (input->isKeyDown(KEY_W)) { pos -= (float)deltaTime*forward*speed; cameraChanged = true; } if (input->isKeyDown(KEY_S)) { pos += (float)deltaTime*forward*speed; cameraChanged = true; } if (input->isKeyDown(KEY_D)) { pos += (float)deltaTime*right*speed; cameraChanged = true; } if (input->isKeyDown(KEY_A)) { pos -= (float)deltaTime*right*speed; cameraChanged = true; } } How my loop works:
void CoreEngine::Loop() { while (renderer->Run()) { UpdateFPS(); UpdateInput(); Update(); Draw(); } } Update input:
void CoreEngine::UpdateInput() { input->Update(); if(input->KeyboardChanged()) root->ActiveKeyboardUpdate(input, deltaTime); root->PassiveKeyboardUpdate(input, deltaTime); if (input->MouseChanged()) root->ActiveMouseUpdate(input, deltaTime); } Update:
void CoreEngine::Update() { root->Update(deltaTime,enlapse); } Draw:
void CoreEngine::Draw() { renderer->PrepareFrame(); renderer->Draw(); renderer->DrawTextOverlay(); renderer->PresentDraw(); renderer->UpdateUniformBuffers(false); } Root is the root node of my object tree which call all objects(nodes) attached updates, which nodes also call their own nodes updates.
UPDATE: I found that if i change this:
case WM_MOUSEMOVE: if (captureWindow) { mousePosition.x = LOWORD(lParam); mousePosition.y = HIWORD(lParam); mouseChangeState = true; if (forcedMouseMove) { lastUpdatePosition = mousePosition; forcedMouseMove = false; } else SetCursorCenter(); } break; For this:
case WM_MOUSEMOVE: if (captureWindow) { mousePosition.x = LOWORD(lParam); mousePosition.y = HIWORD(lParam); mouseChangeState = true; if (forcedMouseMove) { lastUpdatePosition = mousePosition; forcedMouseMove = false; } else if(glm::abs(mousePosition.x + screen.left - Middle.x ) > 400 || glm::abs(mousePosition.y + screen.top - Middle.y) > 300) SetCursorCenter(); } break; Where:
void Input::SetCursorCenter() { SetCursorPos(Middle.x, Middle.y); ForceMove(true); } - Middle: Is the center of the window
- screen: Is the rectangle that contains the window.
The effect is less appreciable, but still there(i still want to solve this less appreciation). I also interpolated the movement, rotation and forward and right vectors.