From 8138c506d9eed62f3df46482447e023a064f60aa Mon Sep 17 00:00:00 2001 From: karl Date: Fri, 2 Oct 2020 22:47:27 +0200 Subject: [PATCH] Improve mouse rotation code This had some quirks previously, now it works cleanly with quaternions. --- ECS/Systems/MouseLookSystem.h | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/ECS/Systems/MouseLookSystem.h b/ECS/Systems/MouseLookSystem.h index 5f9df2e..330fe0d 100644 --- a/ECS/Systems/MouseLookSystem.h +++ b/ECS/Systems/MouseLookSystem.h @@ -41,19 +41,15 @@ public: void tick(World *pWorld, float deltaTime) override { pWorld->each([&](Entity *ent, ComponentHandle transform, ComponentHandle mouse, ComponentHandle camera) { - if(mouse->pitch > 50.0f) - mouse->pitch = 50.0f; - if(mouse->pitch < -50.0f) - mouse->pitch = -50.0f; + if(mouse->pitch > 80.0f) + mouse->pitch = 80.0f; + if(mouse->pitch < -80.0f) + mouse->pitch = -80.0f; - glm::mat4x4 newTransform = glm::mat4x4(1.0); + glm::quat rotation = glm::angleAxis(glm::radians((float)mouse->yaw), glm::vec3(0.f, 1.f, 0.f)); + rotation *= glm::angleAxis(glm::radians((float)mouse->pitch), glm::vec3(1.f, 0.f, 0.f)); - newTransform = glm::rotate(newTransform, glm::radians((float)mouse->pitch), transform->right()); - newTransform = glm::rotate(newTransform, glm::radians((float)mouse->yaw), glm::vec3(0.0, 1.0, 0.0)); - - newTransform[3] = transform->matrix[3]; - - transform->matrix = newTransform; + transform->set_rotation_from_quat(rotation); }); }